37 device=(
char*)malloc((strlen(pdevice)+10)*
sizeof(char));
49 struct termios params;
52 memset( ¶ms,0,
sizeof(params));
59 perror(
"Failed to open port");
65 params.c_cflag = B19200 | CS8 | CLOCAL | CREAD | CSTOPB | IGNPAR;
66 params.c_iflag = IGNPAR;
69 ret=tcsetattr(
port, TCSANOW, ¶ms );
72 perror(
"Set serial communication parameters failed");
86 msg[4]=256 - (0x80 + msg[1] + msg[2] + msg[3])%256;
94 ret = write(
port,&msg,
sizeof(msg));
97 perror(
"Serial communication setup failed...");
115 perror(
"Error closing COMM port.");
117 perror(
"Retrying...");
125 perror(
"FAILED TO CLOSE COMM.");
134 short unsigned int responce;
150 msg[3]=(char)position;
151 msg[4]=256 - (0x80 + msg[1] + msg[2] + msg[3])%256;
159 ret = write(
port,&msg,
sizeof(msg));
182 if(speed>255) msg[3]=255;
else if (speed<0) msg[3]=0;
else msg[3]=speed;
183 msg[4]=256 - (0x80 + msg[1] + msg[2] + msg[3])%256;
191 ret = write(
port,&msg,
sizeof(msg));
198 if(ret<0 || ret==0xFFFF)
208 short unsigned int responce;
224 msg[3]=(char)position;
225 msg[4]=256 - (0x80 + msg[1] + msg[2] + msg[3])%256;
233 ret = write(
port,&msg,
sizeof(msg));
248 int nbytes_to_read=0;
255 msg[4]=256 - (0x80 + msg[1] + msg[2] + msg[3])%256;
263 ret = write(
port,&msg,
sizeof(msg));
271 while(nbytes_to_read!=7)
273 ret=ioctl(
port,FIONREAD,&nbytes_to_read);
277 ret=read(
port,&data,7);
281 return (0x00FF & data[6]);
299 msg[4]=256 - (0x80 + msg[1] + msg[2] + msg[3])%256;
307 ret = write(
port,&msg,
sizeof(msg));
312 int nbytes_to_read=0;
315 while(nbytes_to_read!=7)
317 ret=ioctl(
port,FIONREAD,&nbytes_to_read);
321 ret=read(
port,&data,7);
325 responce=(0x00FF & data[5]);
341 int nbytes_to_read=0;
348 msg[4]=256 - (0x80 + msg[1] + msg[2] + msg[3])%256;
356 ret = write(
port,&msg,
sizeof(msg));
366 while(nbytes_to_read!=7)
368 ret=ioctl(
port,FIONREAD,&nbytes_to_read);
372 ret=read(
port,&data,7);
376 return (0x00FF & data[6]);
383 short unsigned int responce;
400 msg[4]=256 - (0x80 + msg[1] + msg[2] + msg[3])%256;
408 ret = write(
port,&msg,
sizeof(msg));
415 int nbytes_to_read=0;
418 while(nbytes_to_read!=7)
420 ret=ioctl(
port,FIONREAD,&nbytes_to_read);
424 ret=read(
port,&data,7);
428 responce=(0x00FF & data[5]);
445 int nbytes_to_read=0;
453 while(nbytes_to_read!=7)
455 ret=ioctl(
port,FIONREAD,&nbytes_to_read);
458 diff_time=difftime ( end, start);
463 ret=read(
port,&data,7);
467 if(response_1 && response_2)
473 return (0xFF00 & (data[5]<<8)) | (0x00FF & data[6]);
486 ret=read(
port,&data,1);
496 short unsigned int return_value;
509 if(fabs(angular_speed)>=max_speed){
512 return_value = round((255./max_speed) * fabs(angular_speed));}
short unsigned int maximum
Maximum position of the servo.
double HSR_5498SG_MAX_ANGULAR_SPEED
Maximum angular speed of the servo HSR-5498SG.
short unsigned int SetSpeedPosition(int id, int speed)
Set velocity and read position of the servo.
short unsigned int minimum
Minimum position of the servo.
double HSR_5980SG_MAX_ANGULAR_SPEED
Maximum angular speed of the servo HSR-5980SG.
void CleanBuffer(void)
Cleans the buffer.
short unsigned int SetServoID(int id)
Sets servo ID.
~hitec_5980SG()
De-constructor.
short unsigned int SetPositionAllServos(short unsigned int position)
Sets the target position of all servos in the bus.
short unsigned int ConvertAngularSpeedToServoSpeed(double angular_speed, unsigned int Servo_type)
Converts angular speed to servo speed range [1...255].
short unsigned int ReadResponse(char *response_1=NULL, char *response_2=NULL)
Reads the response to a command.
bool IsActive(void)
Checks por activation.
Class hitec_5980SG declararion.
char * device
Communication device to use.
short unsigned int ReleaseServos(void)
Releases all the servos in the bus.
short unsigned int center
Center position of the servo.
short unsigned int SetGoStop(short unsigned int value)
Sends command go/stop to all servos in the bus.
int port
Communication port to use.
short int GetVersionAndID(void)
Gets servos version and id's.
hitec_5980SG(const char *pdevice)
Constructor.
bool active
This variable indicates that the communication is active.
short unsigned int SetPosition(int id, int position)
Sets the target position of a servo.