Download printable PDF

Transcript
Pololu Simple Motor Controller User’s Guide
}
© 2001–2015 Pololu Corporation
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;
}
#ifdef _WIN32
_setmode(fd, _O_BINARY);
#else
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;
printf("Setting Target Speed to %d.\n", newSpeed);
smcSetTargetSpeed(fd, newSpeed);
}
close(fd);
return 0;
6. Using the Serial Interface
Page 104 of 108