1 #ifndef _SERVO_SERIAL_H_ 2 #define _SERVO_SERIAL_H_ 11 #include <sys/types.h> 14 #include <sys/select.h> 18 #define cfsetspeed(term, baudrate) \ 19 cfsetispeed(term, baudrate); \ 20 cfsetospeed(term, baudrate); 28 fd = open(devname, O_RDWR);
30 char *pmesg = strerror(errno);
31 fprintf (stderr,
"[ServoSerial] failed to open %s: %s\n", devname, pmesg);
35 int res = tcgetattr(fd, &term);
37 char *pmesg = strerror(errno);
38 fprintf (stderr,
"[ServoSerial] failed to tcgetattr(): %s\n", pmesg);
43 char *pmesg = strerror(errno);
44 fprintf (stderr,
"[ServoSerial] failed to cfsetspeed(): %s\n", pmesg);
46 term.c_iflag |= IGNPAR;
47 term.c_cflag |= (CLOCAL | CREAD);
48 term.c_cflag &= ~PARENB;
50 term.c_cflag &= ~CSTOPB;
51 term.c_lflag = IEXTEN;
52 term.c_lflag &= ~(ECHO | ECHOCTL | ECHONL);
58 term.c_cflag &= ~(IHFLOW | OHFLOW);
60 res = tcsetattr(fd, TCSANOW, &term);
62 char *pmesg = strerror(errno);
63 fprintf (stderr,
"[ServoSerial] failed to tcsetattr(): %s\n", pmesg);
75 sendPacket(0xFAAF,
id, 0x20, 0xFF, 0, 0, NULL);
79 signed short angle = (
signed short)(180/
M_PI*rad*10);
80 printf(
"[ServoSerial] setPosition %f, %04x\n", 180/
M_PI*rad, angle);
81 unsigned char data[2] = {0xff & angle, 0xff & (angle>>8)};
82 sendPacket(0xFAAF,
id, 0x00, 0x1E, 2, 1, data);
87 unsigned char data[3*len];
88 for (
int i = 0;
i < len;
i++) {
89 short angle = (
int)(180/
M_PI*rad[
i]*10);
90 printf(
"[ServoSerial] setPositions %d: %f, %04x\n",
id[i], 180/
M_PI*rad[i], angle);
91 data[i*3 + 0] =
id[i];
92 data[i*3 + 1] = 0xff & angle;
93 data[i*3 + 2] = 0xff & (angle>>8);
95 sendPacket(0xFAAF, 0x00, 0x00, 0x1E, 3, len, data);
100 short angle = (short)(180/
M_PI*rad*10);
101 short msec = (short)(sec * 100);
102 printf(
"[ServoSerial] setPosition %f %f, %04x, %04x\n", 180/
M_PI*rad, sec, angle, msec);
103 unsigned char data[4] = {0xff & angle,0xff & (angle>>8),
104 0xff & msec, 0xff & (msec>>8) };
105 sendPacket(0xFAAF,
id, 0x00, 0x1E, 4, 1, data);
110 unsigned char data[5*len];
111 for (
int i = 0;
i < len;
i++) {
112 short angle = (
int)(180/
M_PI*rad[
i]*10);
113 short msec = (short)(sec[i] * 100);
114 printf(
"[ServoSerial] setPositions %d: %f %f, %04x, %04x\n",
id[i], 180/
M_PI*rad[i], sec[i], angle, msec);
115 data[i*5 + 0] =
id[i];
116 data[i*5 + 1] = 0xff & angle;
117 data[i*5 + 2] = 0xff & (angle>>8);
118 data[i*5 + 3] = 0xff & msec;
119 data[i*5 + 4] = 0xff & (msec>>8);
121 sendPacket(0xFAAF, 0x00, 0x00, 0x1E, 5, len, data);
126 unsigned char data[1];
127 data[0] = percentage;
128 sendPacket(0xFAAF,
id, 0x00, 0x23, 1, 1, data);
133 printf(
"[ServoSerial] setTorqueOn(%d)\n",
id);
134 unsigned char data[1] = {0x01};
135 sendPacket(0xFAAF,
id, 0x00, 0x24, 1, 1, data);
139 printf(
"[ServoSerial] setTorqueOff(%d)\n",
id);
140 unsigned char data[1] = {0x00};
141 sendPacket(0xFAAF,
id, 0x00, 0x24, 1, 1, data);
145 unsigned char data[1] = {0x02};
146 sendPacket(0xFAAF,
id, 0x00, 0x24, 1, 1, data);
151 if (
sendPacket(0xFAAF,
id, 0x09, 0x00, 0, 1, NULL)<0) {
155 unsigned char data[0x12];
160 *angle = ((short)(data[1]<<8|data[0]))/10.0;
165 if (
sendPacket(0xFAAF,
id, 0x09, 0x00, 0, 1, NULL)<0) {
169 unsigned char data[0x12];
174 *duration = ((short)(data[3]<<8|data[2]))*10.0;
179 if (
sendPacket(0xFAAF,
id, 0x09, 0x00, 0, 1, NULL)<0) {
183 unsigned char data[0x12];
188 *duration = ((short)(data[5]<<8|data[4]));
193 if (
sendPacket(0xFAAF,
id, 0x0B, 0x00, 0, 1, NULL)<0) {
197 unsigned char data[0x0C];
202 *percentage = (short)(data[5]);
207 if (
sendPacket(0xFAAF,
id, 0x09, 0x00, 0, 1, NULL)<0) {
211 unsigned char data[0x12];
216 *torque = ((short)(data[7]<<8|data[6]));
221 if (
sendPacket(0xFAAF,
id, 0x09, 0x00, 0, 1, NULL)<0) {
225 unsigned char data[0x12];
230 *temperature = ((short)(data[9]<<8|data[8]));
235 if (
sendPacket(0xFAAF,
id, 0x09, 0x00, 0, 1, NULL)<0) {
239 unsigned char data[0x12];
244 *voltage = ((short)(data[11]<<8|data[10]))/100;
249 if (
sendPacket(0xFAAF,
id, 0x05, 0x00, 0, 1, NULL)<0) {
262 unsigned char ids,
flags, addr, len, count, sum;
266 fprintf(stderr,
"[ServoSerial] received: ");
267 read(fd, &header, 2);
268 printf(
"%02X ", header>>8);
269 printf(
"%02X ", 0xff&header); fflush(stdout);
270 read(fd, &ids, 1); s ^= ids;
271 printf(
"%02X " , ids); fflush(stdout);
272 read(fd, &flags, 1);s ^= flags;
273 printf(
"%02X ", flags); fflush(stdout);
274 read(fd, &addr, 1); s ^= addr;
275 printf(
"%02X ", addr); fflush(stdout);
276 read(fd, &len, 1); s ^= len;
277 printf(
"%02X ", len); fflush(stdout);
278 read(fd, &count, 1);s ^= count;
279 printf(
"%02X ", count); fflush(stdout);
280 read(fd, data, length);
281 for(
int i = 0;
i < length;
i++){
283 printf(
"%02X ", data[
i]); fflush(stdout);
285 ret = read(fd, &sum, 1);
286 printf(
"%02X - %02X\n", sum, s); fflush(stdout);
288 if ( address != addr || length != len || sum != s ) {
289 fprintf(stderr,
"[ServoSerial] Failed to receive packet from servo(id:%d)\n",
id);
293 if ( flags & 0x0002 ) {
294 fprintf(stderr,
"[ServoSerial] Failed to receive packet from servo(id:%d) Fail to process received packet\n",
id);
298 if ( flags & 0x0008 ) {
299 fprintf(stderr,
"[ServoSerial] Failed to receive packet from servo(id:%d) fail to write Flash ROM\n",
id);
303 if ( flags & 0x0020 ) {
304 fprintf(stderr,
"[ServoSerial] Failed to receive packet from servo(id:%d) temperature limit warning\n",
id);
308 if ( flags & 0x0080 ) {
309 fprintf(stderr,
"[ServoSerial] Failed to receive packet from servo(id:%d) Temperature limit error\n",
id);
317 int flag,
int address,
318 int length,
int count,
321 unsigned char c, sum = 0x00, packet[8+length*count];
322 c = 0xff & (header>>8); packet[0] =
c;
323 c = 0xff & header; packet[1] =
c;
324 c =
id; packet[2] =
c;
325 c = flag; packet[3] =
c;
326 c = address; packet[4] =
c;
327 c = length; packet[5] =
c;
328 c = count; packet[6] =
c;
329 if ( length * count > 0 ) {
330 memcpy((
void *)(&(packet[7])), (
void *)data, length*count);
332 for(
int i = 2;
i < 7 + length*count;
i++){
335 packet[7+length*count] = sum;
337 fprintf (stderr,
"[ServoSerial] sending : ");
338 for(
int i = 0;
i < 7 + length*count + 1;
i++){
339 fprintf(stderr,
"%02X ", packet[
i]);
341 fprintf(stderr,
" - ");
344 ret1 = write(fd, packet, 8+length*count);
346 fprintf(stderr,
"%d\n", ret1);
348 if (ret1 != 8+length*count) {
349 fprintf(stderr,
"[ServoSerial] Failed to send packet to servo(id:%d)\n",
id);
353 unsigned char echo[8 + length*count];
358 struct timeval timeout;
362 timeout.tv_usec = 200*1000;
363 select(fd + 1, &
set, NULL, NULL, &timeout);
364 ret2 = read(fd, &echo, 8+length*count);
367 fprintf(stderr,
"[ServoSerial] received: ");
368 for(
int i = 0;
i < ret2;
i++){
369 fprintf(stderr,
"%02X ", echo[
i]);
371 fprintf(stderr,
" - %d\n", ret2);
373 fprintf(stderr,
"[ServoSerial] Failed to receive packet from servo (id:%d)\n",
id);
378 for(
int i = 0;
i < 8 + length*count;
i++){
379 if (echo[
i] != packet[
i]) {
380 fprintf(stderr,
"[ServoSerial] Failed to confirm packet from servo(id:%d)\n",
id);
391 int oldf = fcntl(fd, F_GETFL, 0);
392 fcntl(fd, F_SETFL, oldf | O_NONBLOCK);
394 while ( read(fd, &c, 1) != EOF );
395 fcntl(fd, F_SETFL, oldf);
399 #endif //_SERVO_SERIAL_H_
int sendPacket(int header, int id, int flag, int address, int length, int count, void *data)
int getSpeed(int id, double *duration)
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
int getVoltage(int id, double *voltage)
int getPosition(int id, double *angle)
int getDuration(int id, double *duration)
std_msgs::Header * header(M &m)
int receivePacket(int id, int address, int length, unsigned char data[])
int getTemperature(int id, double *temperature)
int getState(int id, unsigned char *data)
int setMaxTorque(int id, short percentage)
int getTorque(int id, double *torque)
int getMaxTorque(int id, short *percentage)
int setTorqueBreak(int id)
int setPosition(int id, double rad)
int setPosition(int id, double rad, double sec)
int setPositions(int len, int *id, double *rad)
int setPositions(int len, int *id, double *rad, double *sec)
ServoSerial(const char *devname)
#define cfsetspeed(term, baudrate)