Download printable PDF
Transcript
Pololu Simple Motor Controller User’s Guide
© 2001–2015 Pololu Corporation
#endif
}
char receiveBlocking(char * buffer, unsigned char size, unsigned int timeout_ms)
{
#if _SERIAL_PORTS > 1 // Orangutan X2 and SVP users
return serial_receive_blocking(UART0, buffer, size, timeout_ms);
#else
return serial_receive_blocking(buffer, size, timeout_ms);
#endif
}
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
command[0] = 0x83;
sendBlocking(command, 1);
}
// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
command[0] = 0x86; // motor reverse command
speed = -speed; // make speed positive
}
else
{
command[0] = 0x85; // motor forward command
}
command[1] = speed & 0x1F;
command[2] = speed >> 5;
sendBlocking(command, 3);
}
char setMotorLimit(unsigned char limitID, unsigned int limitValue)
{
command[0] = 0xA2;
command[1] = limitID;
command[2] = limitValue & 0x7F;
command[3] = limitValue >> 7;
sendBlocking(command, 4);
}
char response = -1;
receiveBlocking(&response, 1, 500);
return response;
// returns the specified variable as an unsigned integer.
// if the requested variable is signed, the value returned by this function
// should be typecast as an int.
unsigned int getVariable(unsigned char variableID)
{
command[0] = 0xA1;
command[1] = variableID;
sendBlocking(command, 2);
}
unsigned int response;
if (receiveBlocking((char *)&response, 2, 500))
return 0; // if we don't get a response in 500 ms, return 0
return response;
// initialization code called once when the program starts running
void setup()
{
setBaudRate(115200);
// briefly reset SMC when Arduino starts up (optional)
set_digital_output(resetPin, LOW);
delay_ms(1); // wait 1 ms
set_digital_input(resetPin, HIGH_IMPEDANCE); // let SMC run again
6. Using the Serial Interface
Page 101 of 108