5.e. OrangutanMotors - Motor Control Library

Overview

This library gives you the ability to control the motor drivers on the 3pi robot, Orangutan SV-xx8, Orangutan LV-168, and Baby Orangutan B. It accomplishes this by using the four hardware PWM outputs from timers Timer0 and Timer2, so this library will conflict with any other libraries that rely on or reconfigure Timer0 or Timer2.

Because the Arduino environment relies on Timer0 for timing functions like millis() and delay(), this library cannot reconfigure the timers; consequently, the PWM outputs are limited to a frequency of 1.25 kHz in the Arduino environment. 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 Pololu AVR Library, which allows you to use the motor drivers at higher PWM frequencies.

You do not need to initialize your OrangutanMotors object before use. All initialization is performed automatically when needed.

All of the methods in this class are static; you should never have more than one instance of an OrangutanMotors object in your sketch.

OrangutanMotors Methods

Complete documentation of this library’s methods can be found in Section 7 of the Pololu AVR Library Command Reference.

Usage Examples

This library comes with two example sketches that you can load by going to File > Examples > OrangutanMotors.

1. OrangutanMotorExample

Demonstrates controlling the motors using the trimmer potentiometer and uses the red LED for feedback.

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

2. OrangutanMotorExample2

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.

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