3.f. Orangutan Motor Control Functions

Overview

This set of functions gives you the ability to control the motor drivers on the 3pi Robot, Orangutan SV, Orangutan SVP, Orangutan LV-168, Baby Orangutan B, and Orangutan X2. For all controllers except the SVP and the X2, it accomplishes this by using the four hardware PWM outputs from timers Timer0 and Timer2, so when used with all boards other than the SVP and the X2, this library will conflict with any other libraries that rely on or reconfigure Timer0 or Timer2. On the SVP, these motor control functions only use Timer2 (they do not affect Timer0). No timers are used for motor control on the X2; for that board, these functions just send the appropriate SPI commands to the X2’s auxiliary processor, which takes care of generating the appropriate control signals for the X2’s motor drivers.

C++ users: See Section 5.e of Programming Orangutans and the 3pi Robot 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.

#include <pololu/orangutan.h>

/*
 * motors1: for the Orangutan LV-168, Orangutan SV-xx8, Orangutan SVP,
 *    and Baby Orangutan B
 *
 * This example 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 user LED.
 *
 * http://www.pololu.com/docs/0J20
 * 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 not fully work on the Baby Orangutan.

#include <pololu/orangutan.h>

/*
 * motors2: for the 3pi robot, Orangutan LV-168,
 *    Orangutan SVP, 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.
 *
 *
 * http://www.pololu.com/docs/0J20
 * 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);
  }
}