Hello,
at first I would like to say “Hello!!” to every user of this forum, to site admins and to every Pololu’s stuff user:).
I use Micro Maestro 6-Channel USB Servo Controller and Ubuntu Linux. My problem is that I would like to set a channel (e.g. 0 or 5) to a High state. I’m not sure if this is clear, but I would like to set a current on the signal pin of this channel (I would like to achive the same result as when You use the “Pololu Maestro Control center” and ‘enable’ each channel). I have a LED connected to that pin and I would like to light that LED with that pin. I know that this device wasn’t made for LED blinking, but I it’s curious for me. I found some code to connect to Maestro which works very nice, but I have no idea what should I send to ‘turn this pin on’. In example below I use code to control servo, but it doesn’t work for me.
Here is the code:
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <time.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 */
void put(int servo, int angle); // Moves servo (0-5) to angle (500-5500)
void waitMS (int ms); // This allows for a set system instruction pause. Note: 1000ms = 1 sec.
void neutral(); // This sets up all the servos neutral positions before use.
typedef unsigned int DWORD;
int mainfd = 0; /* File descriptor for the port */
void waitMS (int ms) {
clock_t endwait;
endwait = clock() + ms * CLOCKS_PER_SEC/1000;
while (clock() < endwait);
}
/*
* 'open_port()' - Open the serial port.
* Returns the file descriptor on success or -1 on error.
*/
int open_port (char portName[]) {
int fd; /* File descriptor for the port */
fd = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) { /* Could not open the port */
fprintf(stderr, "\nopen port: Unable to open %s - %s\n\n", portName, strerror(errno));
exit(EXIT_FAILURE); // EXIT
}
else {
printf("\nopen port: port %s has been opened correctly.\n\n", portName);
printf("fd = %i\n\n", fd);
}
return (fd);
}
void config_port(int mainfd) {
struct termios options;
// Get the current settings of the serial port.
tcgetattr (mainfd, &options);
// Set the read and write speed to 19200 BAUD.
// All speeds can be prefixed with B as a settings.
cfsetispeed (&options, B9600);
cfsetospeed (&options, B9600);
// Now to set the other settings. Here we use the no parity example. Both will assumme 8-bit words.
// PARENB is enabled parity bit. This disables the parity bit.
options.c_cflag &= ~PARENB;
// CSTOPB means 2 stop bits, otherwise (in this case) only one stop bit.
options.c_cflag &= ~CSTOPB;
// CSIZE is a mask for all the data size bits, so anding with the negation clears out the current data size setting.
options.c_cflag &= ~CSIZE;
// CS8 means 8-bits per work
options.c_cflag |= CS8;
}
void close_port(int mainfd, char portName[]) {
/* Close the serial port */
if (close(mainfd) == -1) {
fprintf(stderr, "\n\nclose port: Unable to close %s - %s\n\n", portName, strerror(errno));
exit(EXIT_FAILURE); // EXIT
}
else {
printf("\n\nclose port: The port %s has been closed correctly.\n\n", portName);
exit(EXIT_SUCCESS);
}
}
int main(int argc, char* argv[]){
if (argc != 2) {
printf("USAGE: %s PORT. i.e. %s /dev/ttyACM1.\n", argv[0], argv[0]);
exit(EXIT_FAILURE); // EXIT
}
mainfd = open_port(argv[1]);
config_port(mainfd);
waitMS(2000); // Wait one second
unsigned char serialBytes[4];
int channel = 5;
int target = 6000;
serialBytes[0] = 0x84; // Command byte: Set Target.
serialBytes[1] = channel; // First data byte holds channel number.
serialBytes[2] = 0xFF; // Second byte holds the lower 7 bits of target.
serialBytes[3] = 0xFF; // Third data byte holds the bits 7-13 of target.
int a;
while(1){
a = write(mainfd,serialBytes,4);
printf(":::: %i: \n",a);
waitMS(100);
}
close_port(mainfd, argv[1]);
}
OK, that’s all :). Hope I found some help here :).