Pololu Maestro 12 and C

Hi,

I am a student at a University and I am just trying to understand all of this. I am using a 12 pin maestro pololu and am trying to get my c code to run my Servos on boat I am building. I believe it is probably just my settings for my serial bytes. I read many codes on your site and have read the manual for the 12 pin maestro to really understand this. So I run my code and my servos do not respond. I plugged my pololu through the USB to the computer to make sure it was not a problem with the pololu. Can someone please read through my code and steer me in the right dirrection?
servorory.c (4.76 KB)

Here is the code if the attachment does not work

#include <stdio.h>   /* Standard input/output definitions */
#include <stdlib.h>
#include <time.h>	 /* Clock Functions */
#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 waitMS (int ms); // This allows for a set system instruction pause. Note: 1000ms = 1 sec.


typedef unsigned int DWORD;
int mainfd = 0;   /* File descriptor for the port */

// Pausing subroutine
void waitMS (int ms) // Creates waitMS() function for later use
{ 
   clock_t stop; // Type capable of representing clock tick counts.
   stop = clock() + ms * CLOCKS_PER_SEC/1000; // Define Stop to be current clock + inputed pause time
   while (clock() < stop); // Loop that pauses until clock reaches stop time
}
/*
* '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 */
    // errors are sent to stderr ..... Prints erros logged under errno
      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 9600 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[]) // Subroutine to close port
 {
   /* 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[]) // argurments entered into terminal
{
  if (argc != 2) {
      printf("USAGE: %s PORT. i.e. %s /dev/ttts7.\n", argv[0], argv[0]);
      exit(EXIT_FAILURE); // EXIT
   }
   
   mainfd = open_port(argv[1]); // port location given by user
   config_port(mainfd); // configuring port

    waitMS(2000);   // Wait one second
    
    unsigned char serialBytes[6];
	
    //Sets which motorcontroller port you are talking to ie. 0x2=channel 2
    int channel = 0x00;
		
	int a;
    if(1)
	
	// Target
    serialBytes[0] = 0xAA; // Start Byte  
    serialBytes[1] = 0x0C; // Device ID = 12 
    serialBytes[2] = 0x04; // Command Set Speed 
    serialBytes[3] = channel; // Channel = 1. 
	serialBytes[4] = 0x70; // Second byte holds the lower 7 bits of speed.  
    serialBytes[5] = 0x2E; // Third data byte holds the bits 7-13 of speed. 	
  

    a = write(mainfd,serialBytes,6);
    printf(":::: %i: \n",a);
	printf("sending target");
  
    
		// Speed
    serialBytes[0] = 0xAA; // Start Byte  
    serialBytes[1] = 0x0C; // Device ID = 12 
    serialBytes[2] = 0x07; // Command Set Target 
    serialBytes[3] = channel; // Channel = 1. 
	serialBytes[4] = 0x10; // Second byte holds the lower 7 bits of target.  
    serialBytes[5] = 0x35; // Third data byte holds the bits 7-13 of target. 
 
	
    a = write(mainfd,serialBytes,6);
    printf(":::: %i: \n",a);
	printf("Sending speed");
    waitMS(3000);
	
	
		// PWM
	serialBytes[0] = 0xAA; // Start Byte 
    serialBytes[1] = 0x0C; // Device ID = 12 
    serialBytes[2] = 0x0A; // Command = Set Target  
    serialBytes[3] = channel; // Channel = 1 
	serialBytes[4] = 0x20; // Second byte holds the lower 7 bits of target.  
    serialBytes[5] = 0x2F; // Third data byte holds the bits 7-13 of target. 	
	
	a = write(mainfd,serialBytes,6);
    printf(":::: %i: \n",a);
	waitMS(100);
	
		
    close_port(mainfd, argv[1]);

}

Hello.

What operating system are you using? Did you set the Maestro’s serial mode to “USB Dual Port” so it can accept commands from the USB virtual COM port? What port are you connecting to? Are you able to control servos using the Maestro Control Center software? What kind of power supply are you using for the servos?

If looking into those questions does not help you solve the problem, please try compiling and running the “Cross-platform C” example under “Serial Example Code” in the Maestro User’s Guide. Be sure to read and follow the instructions at the top of that example. I have not looked carefully at your code; I think you will succeed faster if you start with our example code that is known to be good and then make small changes to it.

In the future, please surround your code with [code ] and [/code ] tags (without the spaces) to make it more readable.

–David

Thanks for the Help. We figured it out. It seams as if something was wrong with the board because when the pololu is plugged into the TS 7800 through USB then our code works