6.g. Orangutan Motor Control Functions

Overview

This set of functions gives you the ability to control the motor drivers on the 3pi Robot, the Orangutan LV-168, and the 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.

C++ users: See Section 5.e of Programming Orangutans from the Arduino Environment for examples of this class in the Arduino environment, which is almost identical to C++.

Complete documentation of these functions can be found in Section 7 of the Pololu AVR Library Command Reference.

Usage Examples

This library comes with two examples in libpololu-avr\examples.

1. motors1

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

#include <pololu/orangutan.h>

/*
 * motors1: for the Orangutan LV-168 or Baby Orangutan B
 *
 * This example program is intended for use on the Orangutan LV-168 or
 * Baby Orangutan B.  It uses the OrangutanMotors functions 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.
 *
 * http://www.pololu.com/docs/0J20/6.g
 * http://www.pololu.com
 * http://forum.pololu.com
 */

unsigned long prevMillis = 0;

int main()
{
  while(1)
  {
    // note that the following line could also be accomplished with:
    // int pot = analogRead(7);
    int pot = read_trimpot();    // 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
    set_motors(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

    red_led(1);       // turn red LED on
    delay_ms(ledDelay);

    red_led(0);       // turn red LED off
    delay_ms(ledDelay);
  }
}

2. motors2

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.

#include <pololu/orangutan.h>

/*
 * motors2: for the Orangutan LV-168
 *
 * This example program is indended for use on the Orangutan LV-168.
 * It uses the OrangutanMotors and OrangutanLCD functions 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 functions to measure the
 * trimpot position, and it uses the OrangutanLEDs functions to provide
 * limited feedback with the red and green user LEDs.
 *
 * http://www.pololu.com/docs/0J20/6.g
 * http://www.pololu.com
 * http://forum.pololu.com
 */

int main()                     // run over and over again
{
  while(1) 
  {
    // note that the following line could also be accomplished with:
    // int pot = analogRead(7);
    int pot = read_trimpot();  // determine the trimpot position

    // avoid clearing the LCD to reduce flicker
    lcd_goto_xy(0, 0);
    print("pot=");
    print_long(pot);               // print the trim pot position (0 - 1023)
    print("  ");              // overwrite any left over digits

    int motorSpeed = (512 - pot) / 2;
    lcd_goto_xy(0, 1);
    print("spd=");
    print_long(motorSpeed);        // print the resulting motor speed (-255 - 255)
    print("   ");
    set_motors(motorSpeed, motorSpeed);  // set speeds of motors 1 and 2

    // all LEDs off
    red_led(0);
    green_led(0);
    // turn green LED on when motors are spinning forward
    if (motorSpeed > 0)
      green_led(1);
    // turn red LED on when motors are spinning in reverse
    if (motorSpeed < 0)
      red_led(1);
    delay_ms(100);
  }
}