Pololu Simple User Manual
Page 97

// Returns a number where each bit represents a different error, and the
// bit is 1 if the error is currently active.
// See the user's guide for definitions of the different error bits.
// Returns SERIAL_ERROR if there is an error.
int smcGetErrorStatus(int fd)
{
return smcGetVariable(fd,0);
}
// Sends the Exit Safe Start command, which is required to drive the motor.
// Returns 0 if successful, SERIAL_ERROR if there was an error sending.
int smcExitSafeStart(int fd)
{
const unsigned char command = 0x83;
if (write(fd, &command, 1) == -1)
{
perror("error writing");
return SERIAL_ERROR;
}
return 0;
}
// Sets the SMC's target speed (-3200 to 3200).
// Returns 0 if successful, SERIAL_ERROR if there was an error sending.
int smcSetTargetSpeed(int fd, int speed)
{
unsigned char command[3];
if (speed < 0)
{
command[0] = 0x86; // Motor Reverse
speed = -speed;
}
else
{
command[0] = 0x85; // Motor Forward
}
command[1] = speed & 0x1F;
command[2] = speed >> 5 & 0x7F;
if (write(fd, command, sizeof(command)) == -1)
{
perror("error writing");
return SERIAL_ERROR;
}
return 0;
}
int main()
{
// Open the Simple Motor Controller's virtual COM port.
const char * device = "/dev/ttyACM0"; // Linux
//const char * device = "\\\\.\\USBSER000"; // Windows, "\\\\.\\COM6" also works
//const char * device = "/dev/cu.usbmodemfa121"; // Mac OS X
int fd = open(device, O_RDWR | O_NOCTTY);
if (fd == -1)
{
perror(device);
return 1;
}
#ifndef _WIN32
struct termios options;
tcgetattr(fd, &options);
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
options.c_oflag &= ~(ONLCR | OCRNL);
tcsetattr(fd, TCSANOW, &options);
#endif
smcExitSafeStart(fd);
printf("Error status: 0x%04x\n", smcGetErrorStatus(fd));
int speed = smcGetTargetSpeed(fd);
printf("Current Target Speed is %d.\n", speed);
int newSpeed = (speed <= 0) ? 3200 : -3200;
Pololu Simple Motor Controller User's Guide
© 2001–2014 Pololu Corporation
6. Using the Serial Interface
Page 97 of 101