OK Jan, I followed your advice, and here’s the new code, I hope someone will fond what’s wrong with it.
It only consists in:
1 - main(),
2 - connectSsc()
3 - endSession(),
functions 2 and 3 being copied out from the Linux Tutorial.
The main() function procedes as follows:
- Connect to the usb16sc
- Set speed to 1
- Set position (absolute) to pos1
- Set position (absolute) to pos2
- Wait 2 seconds
- Set speed to 127
- Set position (absolute) to pos1
- Set position (absolute) to pos2
I found that the commands were much more “understood” if I add at least 100us between two orders, that’s why I added usleep() calls between each sending.
Anyway, and again with this new code, I see no difference regarding the speed of execution of the 2 moves pos1->pos2, executed respectively at speed 1 and 127. I tried this code on all four servos, with different sets of speed (1,2, 10, 16, and 0), of positions, of baud rates (9600-19200),… I’m guessing that I don’t send an understandable “speed” command?
Could it be due the options chosen at the connection through the serial port?
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/select.h>
#include "ssc.h"
#define PI 3.14159265
int main(int argc, char **argv){
ssc s;
char portName[100];
char buffer5[5];
char buffer6[6];
int pos1=1400;
int pos2=4700;
int myDelay=1000000;
sprintf(portName, "/dev/ttyUSB0");
//first, connect the SSC (see below the connectSsc() function
connectSsc(&s,portName);
//set params for servo #0 (not really used here)
snprintf(buffer5,5,"%c%c%c%c%c",0x80,0x01,0x00,0x00,0x40);
write((&s)->fd,buffer5,5);
printf("Params set.\n");
usleep(myDelay);
//set servo #0 speed to 1 for instance
snprintf(buffer5,5,"%c%c%c%c%c",0x80,0x01,0x01,0x00,0x01);
write((&s)->fd,buffer5,5);
printf("Speed set to 1.\n");
usleep(myDelay);
//step to position <pos1> (absolute mode, 6 bytes)
snprintf(buffer6,6,"%c%c%c%c%c%c",0x80,0x01,0x04,0x00,(char)(pos1/128),(char)(pos1%128));
write((&s)->fd,buffer6,6);
printf("Order 1 sent.\n");
usleep(myDelay);
//then step to position <pos2> (absolute mode, 6 bytes)
snprintf(buffer6,6,"%c%c%c%c%c%c",0x80,0x01,0x04,0x00,(char)(pos2/128),(char)(pos2%128));
write((&s)->fd,buffer6,6);
printf("Order 2 sent.\n");
//wait 2 sec before the rest
sleep(2);
//set servo #0 speed to 127
snprintf(buffer5,5,"%c%c%c%c%c",0x80,0x01,0x01,0x00,0x7f);
write((&s)->fd,buffer5,5);
printf("Speed set to 127.\n");
usleep(myDelay);
//step to position <pos1> (absolute mode, 6 bytes)
snprintf(buffer6,6,"%c%c%c%c%c%c",0x80,0x01,0x04,0x00,(char)(pos1/128),(char)(pos1%128));
write((&s)->fd,buffer6,6);
printf("Order 3 sent.\n");
usleep(myDelay);
//then step to position <pos2> (absolute mode, 6 bytes)
snprintf(buffer6,6,"%c%c%c%c%c%c",0x80,0x01,0x04,0x00,(char)(pos2/128),(char)(pos2%128));
write((&s)->fd,buffer6,6);
printf("Order 4 sent.\n");
usleep(myDelay);
endSession(&s);
}
int connectSsc(ssc *s, char *port) {
struct termios options;
int count,ret;
s->connected=0; /* reset in case we fail to connect */
printf("ok\n");
s->fd = open(port, O_RDWR | O_NOCTTY);
if (s->fd == -1) {
printf("Could not open connexion to Pololu USB16sc.\n");
return 0;
}
else fcntl(s->fd, F_SETFL, 0);
tcgetattr(s->fd, &options);
/* go to 9600 baud */
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD); /* enable */
options.c_cflag &= ~PARENB; /* 8N1 */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* set all of the options */
tcsetattr(s->fd, TCSANOW, &options);
s->connected=1;
printf("Connection to Pololu USB16sc open.\n");
return 1;
}
void endSession(ssc *s) {
if(!s->connected) return;
close(s->fd);
printf("Connection to Pololu USB16sc closed.\n");
}
Thanks for the help.
- Den