7. Orangutan Motor Control

The OrangutanMotors class and the C functions in this section provide PWM-based speed (and direction) control of the two motor channels on the Orangutan controllers and 3pi. The motor control functions rely on the hardware PWM outputs from Timer 0 and Timer 2 for all Orangutans (and the 3pi) except for the Orangutan SVP, which just uses Timer 2 PWM outputs and leaves Timer 0 free, and the Orangutan X2, which uses its auxiliary microcontroller to interface with the motor drivers and leaves both Timer 0 and Timer 2 free.

General Note about Timers: The functions in Pololu AVR library will conflict with code that tries to reconfigure the hardware timers it is relying upon, so if you want to use a timer for some custom task, you cannot use the portion of the library that relies on that timer. The Pololu AVR library only uses Timer 0 for motor PWM generation, and it is only used for this purpose on the Orangutan LV, SV, Baby Orangutan, and 3pi robot, so the Orangutan SVP and X2 can safely use Timer 0 for any custom task. Timer 1 is used by OrangutanBuzzer for sound generation on all devices, and it is used by OrangutanServos for servo pulse generation on all devices. Timer 2 is used for motor PWM generation on all devices except Orangutan X2, and it is used by OrangutanTime to run the system timer on all devices. Additionally, the Orangutan SVP-1284 has a second 16-bit timer, Timer 3, that can safely be used for any custom task (the Pololu AVR library does not use Timer 3 for anything).

Unfortunately, the Arduino environment relies on Timer 0 for its millis() and delay() functions, and it uses Timer 0 in a way that would conflict with this library. To fix the problem, this library disables the Arduino environment’s Timer 0 interrupt and enables a special Timer 2 interrupt when used in the Arduino environment that restores the functionality of millis() and delay() to normal. This interrupt is not included in the C and C++ versions of the library. When not used in the Arduino environment, the millis() and delay() functions come from the OrangutanTime class, which is driven by its own Timer 2 overflow interrupt.

Since the Orangutan X2 uses its auxiliary MCU to control the motor drivers, the OrangutanMotors functions in the X2 version of this library just call the appropriate motor commands from the OrangutanX2 class. The OrangutanX2 class has a number of additional functions for higher-level motor control (e.g. variable braking, setting the PWM frequency, acceleration, current-limiting, paralleling the outputs for control of a single, more powerful motor, and more). Please see Section 15 for more information.

For a higher level overview of this library and example programs that show how this library can be used, please see Section 3.f of the Pololu AVR C/C++ Library User’s Guide.


C++ and Arduino methods are shown in red.

C functions are shown in green.

static void OrangutanMotors::setM1Speed(int speed)

void set_m1_speed(int speed)

This method will set the speed and direction of motor 1. Speed is a value between -255 and +255. The sign of speed determines the direction of the motor and the magnitude determines the speed. A speed of 0 results in full brake (full coast on the Orangutan X2) while a speed of 255 or -255 results in maximum speed forward or backward. If a speed greater than 255 is supplied, the motor speed will be set to 255. If a speed less than -255 is supplied, the motor speed will be set to -255.

static void OrangutanMotors::setM2Speed(int speed)

void set_m2_speed(int speed)

This method will set the speed and direction of motor 2.

static void OrangutanMotors::setSpeeds(int m1Speed, int m2Speed)

void set_motors(int m1Speed, int m2Speed)

This method will set the speeds and directions of motors 1 and 2.