USB Pololu 16 servo controler C++ programm

Hi,
i bought the item listed above, here is the link ā€”> (http://www.active-robots.com/products/motorcon/pololu/usc01a_guide.pdf). I would like to write a program in c++ language to control all servos.
My C++ skills are on the intermediate level. If someone could provide me with information how to start working on my project i would be appreciateā€¦
I had been seeking for information on the forum, before i wrote this topicā€¦
What kind of libraries do i need and where can i find them?

Sorry for mistakes, English isnā€™t my native languageā€¦

Hello.

The servo controller looks like a serial port, so you just need to write a few bytes to the serial port for each servo command; the commands are described in the document you link to. We do not have any libraries for the servo controller.

- Jan

Hi there!

Skolo, take a look on this topic, I think it can help you as a first step.

Good luck! :smiley:

Terribeli.

Hello :slight_smile:
at the beginning i would like to thank for your answers guys!
I red this ----> https://forum.pololu.com/t/problem-with-c-access-to-ssc8/1172/1 topic. It put some light on my project.
I found this program:

   HANDLE comPort = 0;
   DCB comSettings = {0};
   TCHAR *portName = TEXT("COM3");
   comPort = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
   
   if(comPort == INVALID_HANDLE_VALUE) {
      cerr << "Error opening COM port!" << endl;
      return 1;
   }

   if(GetCommState(comPort, &comSettings)) {
      comSettings.BaudRate = CBR_19200; //9600;
      if(!SetCommState(comPort, &comSettings)) {
         cerr << "ERROR SETCOMMSTATE!!!!" << endl;
      };
   }

   COMMTIMEOUTS timeouts={0};
   timeouts.ReadIntervalTimeout=1000;
   timeouts.ReadTotalTimeoutConstant=1000;
   timeouts.ReadTotalTimeoutMultiplier=1000;
   timeouts.WriteTotalTimeoutConstant=1000;
   timeouts.WriteTotalTimeoutMultiplier=1000;

   if(!SetCommTimeouts(comPort,&timeouts)){
      cerr << "Timeout Setting Error\n";
      return 1;
   }

   unsigned short int buffer[6];
   DWORD bytesWritten;

   /*buffer[0] = 255;
   buffer[1] = 0;
   buffer[2] = 25;*/

   unsigned short int temp;
   unsigned char pos_hi,pos_low;

   int angle = 3500;

   temp=angle&0x1f80;
   pos_hi=temp>>7;
   pos_low=angle & 0x7f;

   buffer[0]=0x80;//start byte
   buffer[1]=0x01;//device id
   buffer[2]=0x04;//command number
   buffer[3]=0x07;//servo number
   buffer[4]=pos_hi;//data1
   buffer[5]=pos_low;//data2

   BOOL teste = false;

   teste = WriteFile(comPort, &buffer, 6, &bytesWritten, NULL);
   FlushFileBuffers(comPort);

   if(teste)
      cout << "OK!!!" << endl;

   CloseHandle(comPort);[/code]

Correct me if i am wrong:
1) This part of program is responsible for communication with servo controler.
[code]HANDLE comPort = 0;
   DCB comSettings = {0};
   TCHAR *portName = TEXT("COM3");
   comPort = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
   
   if(comPort == INVALID_HANDLE_VALUE) {
      cerr << "Error opening COM port!" << endl;
      return 1;
   }

   if(GetCommState(comPort, &comSettings)) {
      comSettings.BaudRate = CBR_19200; //9600;
      if(!SetCommState(comPort, &comSettings)) {
         cerr << "ERROR SETCOMMSTATE!!!!" << endl;
      };
   }

   COMMTIMEOUTS timeouts={0};
   timeouts.ReadIntervalTimeout=1000;
   timeouts.ReadTotalTimeoutConstant=1000;
   timeouts.ReadTotalTimeoutMultiplier=1000;
   timeouts.WriteTotalTimeoutConstant=1000;
   timeouts.WriteTotalTimeoutMultiplier=1000;

   if(!SetCommTimeouts(comPort,&timeouts)){
      cerr << "Timeout Setting Error\n";
      return 1;
   }
  1. This part of program is responsible for sending bytes to the servo controller.
   unsigned short int buffer[6];
   DWORD bytesWritten;

   /*buffer[0] = 255;
   buffer[1] = 0;
   buffer[2] = 25;*/

   unsigned short int temp;
   unsigned char pos_hi,pos_low;

   int angle = 3500;

   temp=angle&0x1f80;
   pos_hi=temp>>7;
   pos_low=angle & 0x7f;

   buffer[0]=0x80;//start byte
   buffer[1]=0x01;//device id
   buffer[2]=0x04;//command number
   buffer[3]=0x07;//servo number
   buffer[4]=pos_hi;//data1
   buffer[5]=pos_low;//data2

2.1) I have problem with understanding this part:

   int angle = 3500;

   temp=angle&0x1f80;
   pos_hi=temp>>7;
   pos_low=angle & 0x7f;

3500 is assigned to the angle variable, then ā€¦ temp=angle&0x1f80 (do not understand it, what is the result of this operation? ), then we move temp seven bites to the right, and another operation i have problem with understanding (pos_low=angle & 0x7f).

  1. This part tests if the data was sent correctly.
   BOOL teste = false;

   teste = WriteFile(comPort, &buffer, 6, &bytesWritten, NULL);
   FlushFileBuffers(comPort);

   if(teste)
      cout << "OK!!!" << endl;

   CloseHandle(comPort);

Thank you for your helpā€¦ :slight_smile:

Iā€™m not sure what distinction you are trying to make between ā€œresponsible for communicationā€ and ā€œresponsible for sending bytesā€. Sending bytes is how you communicate. Regarding the bit masking and shifting, do you understand what the program needs to do? Have you considered what the variable values are after each step?

- Jan

I understand that sending bytes is how i communicate with the servo, but i am trying to understand how it works. This is why i divided the program for few partsā€¦ If you can explain me each part of the program, why it is necessary to mask and shift bytes, what the variable values are after each step, etcā€¦

Thank you, Marcin.

You should look at the userā€™s guide to see what the program is trying to achieve. Asking someone to go through the values for you is too much: just run the code and look at the values yourself. (If the values turned out to be something other than what you expect, that would be a more legitimate question.) You should also look up bitwise operators.

- Jan