| Servo to OOPic Example |
Last Modified: 2006-10-25
In this example, an OOPic microcontroller sends positioning signals to two continuous rotation servo motors. Continuous rotation servos are effective drive motors for a small robot. The source code listed below includes some useful subroutines for "binary" motor control (the motors are either fully on or fully off). This example may provide a good starting point for a small robot project.
In the above schematic, the OOPic is powered by an external 9V battery. The servos have their own supply of four NiMH AA batteries. Ni-Cad or Alkaline batteries would also work well. Servos may draw up to 1 amp of current. Do not use the OOpic's onboard voltage regulator to power the servos. It is only rated for 100ma.
A typical servo receives a series of pulses and moves its arm to a position that is proportional to the width of the input pulse. The servo uses an internal potentiometer attached to the arm to determine its position. It continuously monitors the potentiometer and drives the motor forward or backward to maintain the desired arm position. A modified servo has fixed resistors instead of a potentiometer and no physical limit to arm travel. The fixed resistors prevent the servo from reaching a final position. Without physical limits, commanding the servo to move its arm all the way to one side will make the servo rotate continuously in one direction. This feature makes it easy to control the direction of the motor. Stopping the input pulses stops the motor. It is also possible to stop the motor by using a pulse width that corresponds exactly to the center position. However, finding this value can be rather time-consuming. It will probably be different for each modified servo and it may drift over time.
This code uses BASIC syntax. It runs with version 3.0.1 of the OOPic compiler and version A.1.7 of the OOPic chip. With later versions of the compiler, you will need to change the oByte object declarations in the functions to Byte type declarations.
' Filename: servo-1.osc ' motor control example for ' OOPic-controlled robot driven by ' servos modified for continous rotation dim rmotor as new oServo ' 70 fw, 51 stall, 30 bw dim lmotor as new oServo ' 30 fw, 50 stall, 70 bw sub main() call initmotors call testmotors end sub '------------------------------------------------------ ' MOTOR CONTROL SUBROUTINES ' sub initmotors() rmotor.IOLine=6 rmotor.Center=0 lmotor.IOLine=5 lmotor.Center=0 end sub ' ' motor params are -1,0,1 ' reverse, stop, forward ' sub scoot(rmparam as oByte, lmparam as oByte) lmotor=30+(1-lmparam)*20 rmotor=30+(1+rmparam)*20 lmotor.Operate=lmparam and 1 rmotor.Operate=rmparam and 1 end sub ' ' timed scoot ' sub tscoot(trparam as oByte, tlparam as oByte, tmparam as oByte) lmotor=30+(1-tlparam)*20 rmotor=30+(1+trparam)*20 lmotor.Operate=tlparam and 1 rmotor.Operate=trparam and 1 oopic.delay=tmparam lmotor.Operate=cvFalse rmotor.Operate=cvFalse end sub ' ' try all various scoots ' rotate right, rotate left ' straight backward, straight forward ' forward/backward right turn ' forward/backward left turn ' sub testmotors() oopic.delay=500 call scoot(-1,1) oopic.delay=100 call scoot(1,-1) oopic.delay=100 call scoot(-1,-1) oopic.delay=100 call scoot(1,1) oopic.delay=100 call scoot(1,0) oopic.delay=100 call scoot(-1,0) oopic.delay=100 call scoot(0,1) oopic.delay=100 call scoot(0,-1) oopic.delay=100 call scoot(0,0) ' ' timed scoots ' call tscoot(-1,1,100) call tscoot(1,-1,100) call tscoot(-1,-1,100) call tscoot(1,1,100) call tscoot(1,0,100) call tscoot(-1,0,100) call tscoot(0,1,100) call tscoot(0,-1,100) end sub
The OOPic oServo object provides 7-bit servo position control. Setting the Operate property of each oServo object to cvFalse effectively stops the motors in this example. Experimentation showed that values of 30 and 70 will make these motors go full forward and full reverse. The motors are mounted on a small robot in the Acroname Lab. Because of the way the motors are mounted, a value of 70 makes the right motor go forward while a value of 30 makes the left motor go forward. By turning motors on and off and reversing motor directions, the robot can rotate, turn, and move forward or backward.
For convenience, the main motor control subroutine, scoot , uses identical input parameters (-1,0,1) to control each motor. Forward is represented by 1, Backward is represented by -1, and Stop is represented by 0. The least significant bit of both -1 and 1 (in binary) is 1. This is used as a flag for turning the motors on and off with the Operate property. The control logic uses integer math, as opposed to a more straightforward if-then approach, to convert each parameter into an appropriate servo position value. The integer math approach is an interesting exercise and saves several lines of code. The following examples illustrate the calculations. The bold values are input parameters.
30+(1+( 1 ))*20=70 forward
30+(1+( -1 ))*20=30 reverse
30+(1-( 1 ))*20=30 forward
30+(1-( -1 ))*20=70 reverse
The program contains an additional test routine that demonstrates all the possible scoot operations. The actual motor control routines occupy less than five percent of a standard OOpic's memory (4K EEPROM).
| voice: 720-564-0373, email: firstname.lastname@example.org, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy |
© Copyright 1994-2012 Acroname, Inc., Boulder, Colorado. All rights reserved.