Support » Programming Orangutans and the 3pi Robot from the Arduino Environment » 5. Arduino Libraries for the Orangutan and 3pi Robot »
5.e. OrangutanMotors - Motor Control Library
<h2>Overview</h2>
<p>This library gives you the ability to control the motor drivers on the <strong><a href="/product/975">3pi robot</a></strong>, <strong><a href="/product/1227">Orangutan SV-xx8</a></strong>, <strong><a href="/product/775">Orangutan LV-168</a></strong>, and <strong><a href="/product/1220">Baby Orangutan B</a></strong>. It accomplishes this by using the four hardware PWM outputs from timers Timer0 and Timer2, so <strong><font color="red">this library will conflict with any other libraries that rely on or reconfigure Timer0 or Timer2</font></strong>.</p>
<p class="note">Because the Arduino environment relies on Timer0 for timing functions like millis() and delay(), this library cannot reconfigure the timers; consequently, <strong>the PWM outputs are limited to a frequency of 1.25 kHz in the Arduino environment</strong>. In many cases, this will result in an audible motor whine. To avoid this problem, you can instead program in C or C++ with the <a href="/docs/0J20">Pololu AVR Library</a>, which allows you to use the motor drivers at higher PWM frequencies.</p>
<p>You do not need to initialize your OrangutanMotors object before use. All initialization is performed automatically when needed.</p>
<p>All of the methods in this class are static; you should never have more than one instance of an OrangutanMotors object in your sketch.</p>
<h2>OrangutanMotors Methods</h2>
<p>Complete documentation of this library’s methods can be found in <strong><a href="/docs/0J18/7">Section 7</a></strong> of the <a href="/docs/0J18">Pololu AVR Library Command Reference</a>.</p>
<h2>Usage Examples</h2>
<p>This library comes with two example sketches that you can load by going to <strong>File > Examples > OrangutanMotors</strong>.</p>
<h3>1. OrangutanMotorExample</h3>
<p>Demonstrates controlling the motors using the trimmer potentiometer and uses the red LED for feedback.</p>
<pre name="code" class="c">
#include <OrangutanLEDs.h>
#include <OrangutanAnalog.h>
#include <OrangutanMotors.h>


/*
 * OrangutanMotorExample for the 3pi robot, Orangutan LV-168, Orangutan SV-xx8,
 * and Baby Orangutan B
 *
 * This example uses the OrangutanMotors library to drive
 * motors in response to the position of user trimmer potentiometer
 * and blinks the red user LED at a rate determined by the trimmer
 * potentiometer position. It uses the OrangutanAnalog library to measure
 * the trimpot position, and it uses the OrangutanLEDs library to provide
 * limited feedback with the red user LED.
 *
 * http://www.pololu.com/docs/0J17/5.e
 * http://www.pololu.com
 * http://forum.pololu.com
 */

OrangutanAnalog analog;
OrangutanLEDs leds;
OrangutanMotors motors;

void setup() // run once, when the sketch starts
{

}

void loop() // run over and over again
{
 // note that the following line could also be accomplished with:
 // int pot = analog.read(7);
 int pot = analog.readTrimpot(); // determine the trimpot position
 int motorSpeed = pot/2-256; // turn pot reading into number between -256 and 255
 if(motorSpeed == -256)
 motorSpeed = -255; // 256 is out of range
 motors.setSpeeds(motorSpeed, motorSpeed);
 
 int ledDelay = motorSpeed;
 if(ledDelay < 0)
 ledDelay = -ledDelay; // make the delay a non-negative number
 ledDelay = 256-ledDelay; // the delay should be short when the speed is high

 leds.red(HIGH); // turn red LED on
 delay(ledDelay);

 leds.red(LOW); // turn red LED off
 delay(ledDelay);
}</pre>
<h3>2. OrangutanMotorExample2</h3>
<p>Demonstrates controlling the motors using the trimmer potentiometer, but it uses the LCD for most of the feedback, so it will not fully work on the Baby Orangutan.</p>
<pre name="code" class="c">
#include <OrangutanLEDs.h>
#include <OrangutanAnalog.h>
#include <OrangutanMotors.h>
#include <OrangutanLCD.h>

/*
 * OrangutanMotorExample2 for the 3pi robot, Orangutan LV-168,
 * and Orangutan SV-xx8.
 *
 * This example uses the OrangutanMotors and OrangutanLCD libraries to drive
 * motors in response to the position of user trimmer potentiometer
 * and to display the potentiometer position and desired motor speed
 * on the LCD. It uses the OrangutanAnalog library to measure the
 * trimpot position, and it uses the OrangutanLEDs library to provide
 * limited feedback with the red and green user LEDs.
 */

OrangutanLCD lcd;
OrangutanMotors motors;
OrangutanAnalog analog;
OrangutanLEDs leds;

void setup() // run once, when the sketch starts
{

}

void loop() // run over and over again
{
 // note that the following line could also be accomplished with:
 // int pot = analogRead(7);
 int pot = analog.readTrimpot(); // determine the trimpot position
 
 // avoid clearing the LCD to reduce flicker
 lcd.gotoXY(0, 0);
 lcd.print("pot=");
 lcd.print(pot); // print the trim pot position (0 - 1023)
 lcd.print(" "); // overwrite any left over digits
 
 int motorSpeed = (512 - pot) / 2;
 lcd.gotoXY(0, 1);
 lcd.print("spd=");
 lcd.print(motorSpeed); // print the resulting motor speed (-255 - 255)
 lcd.print(" ");
 motors.setSpeeds(motorSpeed, motorSpeed); // set speeds of motors 1 and 2
 

 // all LEDs off
 leds.red(LOW);
 leds.green(LOW);
 // turn green LED on when motors are spinning forward
 if (motorSpeed > 0)
 leds.green(HIGH);
 // turn red LED on when motors are spinning in reverse
 if (motorSpeed < 0)
 leds.red(HIGH);
 delay(100);
}</pre>