Hello,
I have played with some pololu controllers before, serial ones, I bought a USB one a bit ago (the little meastro 6 channel one). I also got some inexpensive servos to play with a bit (tower-pro MG90s).
I am trying to work with some c example code that I found on the pololu site The servos are supposed to be able the have a 180 dgr ‘throw’/‘sweep’ with a duty cycle between 1 and 2 ms, with 1500us being center.
However, it only seems to do a 90 degree throw/sweep, 45 degr to each side from ‘center’
any ideas why that is??
I don’t know if I am supposed to add code here, but it’s short:
// Uses POSIX functions to send and receive data from a Maestro.
// NOTE: The Maestro's serial mode must be set to "USB Dual Port".
// NOTE: You must change the 'const char * device' line below.
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
// Gets the position of a Maestro channel.
// See the "Serial Servo Commands" section of the user's guide.
int maestroGetPosition(int fd, unsigned char channel)
{
unsigned char command[] = {0x90, channel};
if (write(fd, command, sizeof(command)) == -1) {
perror("error writing");
return -1;
}
unsigned char response[2];
if (read(fd,response,2) != 2) {
perror("error reading");
return -1;
}
return response[0] + 256*response[1];
}
// Sets the target of a Maestro channel.
// See the "Serial Servo Commands" section of the user's guide.
// The units of 'target' are quarter-microseconds.
int maestroSetTarget(int fd, unsigned char channel, unsigned short target)
{
unsigned char command[] = {0x84, channel, target & 0x7F, target >> 7 & 0x7F};
if (write(fd, command, sizeof(command)) == -1)
{
perror("error writing");
return -1;
}
return 0;
}
int maestroGoHome(int fd)
{
unsigned char command[] = {0xA2};
if (write(fd, command, sizeof(command)) == -1)
{
perror("error writing");
return -1;
}
return 0;
}
int main()
{
// Open the Maestro's virtual COM port.
const char * device = "/dev/ttyACM0"; // Linux
int fd = open(device, O_RDWR | O_NOCTTY);
if (fd == -1) {
perror(device);
return 1;
}
struct termios options;
tcgetattr(fd, &options);
options.c_iflag &= ~(INLCR | IGNCR | ICRNL | IXON | IXOFF);
options.c_oflag &= ~(ONLCR | OCRNL);
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tcsetattr(fd, TCSANOW, &options);
int position, target;
position = maestroGetPosition(fd, 0);
printf("Current position is %d.\n", position);
printf("Going home\n");
maestroGoHome(fd);
sleep(1);
target = 4000; // 4 * 1000 -90 degr
printf("Setting target to %d (%d us -- -90 degr.).\n", target, target/4);
maestroSetTarget(fd, 0, target);
sleep(1);
target = 6000; // 4 * center 0 degr
printf("Setting target to %d (%d us -- 0 degr.).\n", target, target/4);
maestroSetTarget(fd, 0, target);
sleep(1);
target = 8000; // 4 * 2000 90 degr
printf("Setting target to %d (%d us -- 90 degr.).\n", target, target/4);
maestroSetTarget(fd, 0, target);
close(fd);
return 0;</code>
}