beautypg.com

Pololu Simple User Manual

Page 92

background image

• Orangutan GND to Simple Motor Controller GND

Pin PD0 is the Orangutan’s hardware serial receive line and must be connected to the Simple Motor Controller as
described above for this sample program to work. See

Section 4.2

for more information on connecting a serial device

to the Simple Motor Controller.

This program demonstrates how to initiate serial communication with the Simple Motor Controller and how to send
commands to set the motor speed. For information about the serial commands used by this sample code, refer to

Section 6.2.1

. Note that the Simple Motor Controller must be powered when this Orangutan program starts running.

#include

char command[3];

// These first two functions call the appropriate Pololu AVR library serial functions

// depending on which Orangutan you are using. The Orangutan SVP and X2 have multiple

// serial ports, so the serial functions for these devices require an extra argument

// specifying which port to use. You can simplify this program by just calling the

// library function appropriate for your Orangutan board.

void setBaudRate(unsigned long baud)

{

#if _SERIAL_PORTS > 1 // Orangutan X2 and SVP users

serial_set_baud_rate(UART0, baud);

#else

serial_set_baud_rate(baud);

#endif

}

void sendBlocking(char * buffer, unsigned char size)

{

#if _SERIAL_PORTS > 1 // Orangutan X2 and SVP users

serial_send_blocking(UART0, buffer, size);

#else

serial_send_blocking(buffer, size);

#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);

}

// initialization code called once when the program starts running

void setup()

{

// initialize hardware serial (UART0) with baud rate of 115.2 kbps

setBaudRate(115200);

// the Simple Motor Controller must be running for at least 1 ms

// before we try to send serial data, so we delay here for 5 ms

delay_ms(5);

Pololu Simple Motor Controller User's Guide

© 2001–2014 Pololu Corporation

6. Using the Serial Interface

Page 92 of 101