5.e. OrangutanMotors - Motor Control Library

Overview

This library gives you the ability to control the motor drivers on the 3pi robot, Orangutan LV-168, or 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. Unfortunately the Arduino environment relies on Timer0 for its millis() and delay() functions, but this library enables a Timer2 interrupt that restores the functionality of millis() and delay() to normal.

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 > Sketchbook > Examples > Library-OrangutanMotors.

1. OrangutanMotorExample

Demonstrates controlling the motors using the trimmer potentiometer and uses the red LED for feedback. This example is designed to work on the 3pi robot, Orangutan LV-168, and Baby Orangutan B.

#include <OrangutanLEDs.h>
#include <OrangutanAnalog.h>
#include <OrangutanMotors.h>

/*
 * OrangutanMotorExample for the 3pi robot, Orangutan LV-168, or Baby Orangutan B
 *
 * This example program is indended for use on the 3pi, Orangutan LV-168, or
 * Baby Orangutan B.  It 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 and green user LEDs.
 */

OrangutanAnalog analog;
OrangutanLEDs leds;
OrangutanMotors motors;
unsigned long prevMillis = 0;

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

  // turn pot reading into number between -256 and 256
  int motorSpeed = (512 - pot) / 2;
  motors.setSpeeds(motorSpeed, motorSpeed);

  // the following code blinks the red user LED at a rate that's approximately
  // inversely proportional to the position of the user trim pot
  if (millis() - prevMillis >= pot)
  {
    prevMillis = millis() + 100;
    leds.red(HIGH);       // turn red LED on
  }
  delay(100);
  leds.red(LOW);          // turn red LED off
}

2. OrangutanMotorExample2

Demonstrates controlling the motors using the trimmer potentiometer, but it uses the LCD for most of the feedback, so it will only fully work on the Orangutan LV-168 or 3pi robot.

#include <OrangutanLEDs.h>
#include <OrangutanAnalog.h>
#include <OrangutanMotors.h>
#include <OrangutanLCD.h>

/*
 * OrangutanMotorExample2 for the 3pi robot or Orangutan LV-168
 *
 * This example program is indended for use on the Orangutan LV-168 or 3pi.
 * It 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 - 256)
  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);
}