i’m trying to work with a serial 16 port servo controller in pololu mode.
I’m using absolute positioning, the first command i send to the servo controller works, the servo is reacting and moving. When i send the second command (after 5 seconds) it doesn’t work nothing is happening.
Here is the code i’m using:
/*
* Small program to test servo functonality
*/
#define SERIALPORT "/dev/ttyUSB0"
#define BAUDRATE B9600
#include <unistd.h> // sleep
#include <termios.h> // serial
#include <fcntl.h> // serial
#include <sys/signal.h> // events
#include <sys/types.h> // events
#include <stdlib.h> // atof
#include <stdio.h>
int main(){
struct termios tty; // will be used for new port settings
struct termios oldtty;
int fd_serial;
fd_serial = open(SERIALPORT, O_RDWR | O_NOCTTY );
if (fd_serial < 0) {
printf("port open failed\n");
return 1;;
}
tcgetattr(fd_serial, &oldtty); // save current port settings
// then set the baud rate, handshaking and a few other settings
tty.c_iflag = 0;
tty.c_lflag = 0; // set input mode (non-canonical, no echo,.)
tty.c_oflag = 0;
tty.c_cflag = BAUDRATE | CS8 | CLOCAL;
tty.c_cc[VTIME] = 0; // inter-character timer unused
tty.c_cc[VMIN] = 0; // blocking read until first char received
tcflush(fd_serial, TCIFLUSH);
tcsetattr(fd_serial, TCSANOW, &tty);
// min 03 73
// max 4a 7c
unsigned char out[6];
out[0] = 0x80; // start byte
out[1] = 0x01; // deviceid (1=servo)
out[2] = 0x04; // 2=7bit position
out[3] = 0x00;
out[4] = 0x03; // max 0x03-0x4a
out[5] = 0x73;
write(fd_serial, out, 6);
tcflush(fd_serial, TCIFLUSH);
sleep(5);
out[0] = 0x80; // start byte
out[1] = 0x01; // deviceid (1=servo)
out[2] = 0x04; // 2=7bit position
out[3] = 0x00;
out[4] = 0x4a; // max 0x03-0x4a
out[5] = 0x73;
write(fd_serial, out, 6);
tcflush(fd_serial, TCIFLUSH);
tcsetattr(fd_serial, TCSANOW, &oldtty);
return 0;
}
I don’t know what your message “setting to 49” is supposed to mean, but it looks like you tried to set it to 0x4a*128+0x73 which is 9587, outside of the 500-5500 range of the servo controller.
I just noticed that your initial value evaluates to 499, which is also outside of the allowed range - that probably just caused the servo to turn on.
By the way, you could easily break a servo by trying to set it to a location outside of its mechanical limit! I recommend you test over a safe range until you understand what you are doing!
Those numbers match the hex values, but 1000 is still not a safe range for an initial servo test. The safe range for servos is general 1-2ms, which would correspond to 2000-4000 in the units of your servo controller.
Anyway, if it still does not work, please simplify your code as much as possible - it wastes everyone’s time if you tell me that there are parts that I am supposed to ignore, and there is always a chance that those parts could be causing the problem. Verify that you are still having the problem with your simplified code and tell me what the problem is exactly - does it always accept the first command? Does it move to different positions depending on what you put in the first command? Do you have to reset the controller or just re-run the program to send it to a new position?
does it always accept the first command?
=> the servo always responds to the first command
Does it move to different positions depending on what you put in the first command?
=> it moves to the center position when i send the first 2 bytes, it seems its just turning the servo on, and is not doing a thing
Do you have to reset the controller or just re-run the program to send it to a new position?
=> the second time i run the program, the servo is already at the center position so its not doing anything