5.e. OrangutanMotors - Motor Control Library

<h2>Overview</h2> <p>This library gives you the ability to control the motor drivers on the <strong><a href="/product/975">3pi robot</a></strong>, <strong><a href="/product/1227">Orangutan SV-xx8</a></strong>, <strong><a href="/product/775">Orangutan LV-168</a></strong>, and <strong><a href="/product/1220">Baby Orangutan B</a></strong>. It accomplishes this by using the four hardware PWM outputs from timers Timer0 and Timer2, so <strong><font color="red">this library will conflict with any other libraries that rely on or reconfigure Timer0 or Timer2</font></strong>.</p> <p class="note">Because the Arduino environment relies on Timer0 for timing functions like millis() and delay(), this library cannot reconfigure the timers; consequently, <strong>the PWM outputs are limited to a frequency of 1.25&nbsp;kHz in the Arduino environment</strong>. 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 <a href="/docs/0J20">Pololu AVR Library</a>, which allows you to use the motor drivers at higher PWM frequencies.</p> <p>You do not need to initialize your OrangutanMotors object before use. All initialization is performed automatically when needed.</p> <p>All of the methods in this class are static; you should never have more than one instance of an OrangutanMotors object in your sketch.</p> <h2>OrangutanMotors Methods</h2> <p>Complete documentation of this library&#8217;s methods can be found in <strong><a href="/docs/0J18/7">Section 7</a></strong> of the <a href="/docs/0J18">Pololu AVR Library Command Reference</a>.</p> <h2>Usage Examples</h2> <p>This library comes with two example sketches that you can load by going to <strong>File &gt; Examples &gt; OrangutanMotors</strong>.</p> <h3>1. OrangutanMotorExample</h3> <p>Demonstrates controlling the motors using the trimmer potentiometer and uses the red LED for feedback.</p> <pre name="code" class="c">&#x000A;#include &lt;OrangutanLEDs.h&gt;&#x000A;#include &lt;OrangutanAnalog.h&gt;&#x000A;#include &lt;OrangutanMotors.h&gt;&#x000A;&#x000A;&#x000A;/*&#x000A; * OrangutanMotorExample for the 3pi robot, Orangutan LV-168, Orangutan SV-xx8,&#x000A; * and Baby Orangutan B&#x000A; *&#x000A; * This example uses the OrangutanMotors library to drive&#x000A; * motors in response to the position of user trimmer potentiometer&#x000A; * and blinks the red user LED at a rate determined by the trimmer&#x000A; * potentiometer position. It uses the OrangutanAnalog library to measure&#x000A; * the trimpot position, and it uses the OrangutanLEDs library to provide&#x000A; * limited feedback with the red user LED.&#x000A; *&#x000A; * http://www.pololu.com/docs/0J17/5.e&#x000A; * http://www.pololu.com&#x000A; * http://forum.pololu.com&#x000A; */&#x000A;&#x000A;OrangutanAnalog analog;&#x000A;OrangutanLEDs leds;&#x000A;OrangutanMotors motors;&#x000A;&#x000A;void setup() // run once, when the sketch starts&#x000A;{&#x000A;&#x000A;}&#x000A;&#x000A;void loop() // run over and over again&#x000A;{&#x000A; // note that the following line could also be accomplished with:&#x000A; // int pot = analog.read(7);&#x000A; int pot = analog.readTrimpot(); // determine the trimpot position&#x000A; int motorSpeed = pot/2-256; // turn pot reading into number between -256 and 255&#x000A; if(motorSpeed == -256)&#x000A; motorSpeed = -255; // 256 is out of range&#x000A; motors.setSpeeds(motorSpeed, motorSpeed);&#x000A; &#x000A; int ledDelay = motorSpeed;&#x000A; if(ledDelay &lt; 0)&#x000A; ledDelay = -ledDelay; // make the delay a non-negative number&#x000A; ledDelay = 256-ledDelay; // the delay should be short when the speed is high&#x000A;&#x000A; leds.red(HIGH); // turn red LED on&#x000A; delay(ledDelay);&#x000A;&#x000A; leds.red(LOW); // turn red LED off&#x000A; delay(ledDelay);&#x000A;}</pre> <h3>2. OrangutanMotorExample2</h3> <p>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.</p> <pre name="code" class="c">&#x000A;#include &lt;OrangutanLEDs.h&gt;&#x000A;#include &lt;OrangutanAnalog.h&gt;&#x000A;#include &lt;OrangutanMotors.h&gt;&#x000A;#include &lt;OrangutanLCD.h&gt;&#x000A;&#x000A;/*&#x000A; * OrangutanMotorExample2 for the 3pi robot, Orangutan LV-168,&#x000A; * and Orangutan SV-xx8.&#x000A; *&#x000A; * This example uses the OrangutanMotors and OrangutanLCD libraries to drive&#x000A; * motors in response to the position of user trimmer potentiometer&#x000A; * and to display the potentiometer position and desired motor speed&#x000A; * on the LCD. It uses the OrangutanAnalog library to measure the&#x000A; * trimpot position, and it uses the OrangutanLEDs library to provide&#x000A; * limited feedback with the red and green user LEDs.&#x000A; */&#x000A;&#x000A;OrangutanLCD lcd;&#x000A;OrangutanMotors motors;&#x000A;OrangutanAnalog analog;&#x000A;OrangutanLEDs leds;&#x000A;&#x000A;void setup() // run once, when the sketch starts&#x000A;{&#x000A;&#x000A;}&#x000A;&#x000A;void loop() // run over and over again&#x000A;{&#x000A; // note that the following line could also be accomplished with:&#x000A; // int pot = analogRead(7);&#x000A; int pot = analog.readTrimpot(); // determine the trimpot position&#x000A; &#x000A; // avoid clearing the LCD to reduce flicker&#x000A; lcd.gotoXY(0, 0);&#x000A; lcd.print("pot=");&#x000A; lcd.print(pot); // print the trim pot position (0 - 1023)&#x000A; lcd.print(" "); // overwrite any left over digits&#x000A; &#x000A; int motorSpeed = (512 - pot) / 2;&#x000A; lcd.gotoXY(0, 1);&#x000A; lcd.print("spd=");&#x000A; lcd.print(motorSpeed); // print the resulting motor speed (-255 - 255)&#x000A; lcd.print(" ");&#x000A; motors.setSpeeds(motorSpeed, motorSpeed); // set speeds of motors 1 and 2&#x000A; &#x000A;&#x000A; // all LEDs off&#x000A; leds.red(LOW);&#x000A; leds.green(LOW);&#x000A; // turn green LED on when motors are spinning forward&#x000A; if (motorSpeed &gt; 0)&#x000A; leds.green(HIGH);&#x000A; // turn red LED on when motors are spinning in reverse&#x000A; if (motorSpeed &lt; 0)&#x000A; leds.red(HIGH);&#x000A; delay(100);&#x000A;}</pre>

Related Products

Baby Orangutan B-328 + USB Programmer Combo
Baby Orangutan B-48 + USB Programmer Combo
Baby Orangutan B-168 + USB Programmer Combo
3pi Robot + USB Programmer + Cable Combo
Orangutan SV-168 Robot Controller
Orangutan SV-168 + USB Programmer Combo
Orangutan LV-168 + USB Programmer Combo
Orangutan SV-328 + USB Programmer Combo
Baby Orangutan B-328 + USB AVR Programmer Combo
Baby Orangutan B-48 + USB Programmer Combo
Orangutan LV-168 + USB Programmer Combo
Orangutan SV-328 + USB Programmer Combo
Orangutan SV-328 Robot Controller
Orangutan LV-168 Robot Controller
Baby Orangutan B-48 Robot Controller
Baby Orangutan B-328 Robot Controller
Pololu 3pi Robot
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors