I have always seen things differently with my electronics and programming background. Servo Math came from me seeing servos as having three parts. The angles 0-1000, 90-1500 and 180-2000 microseconds(us). 0 is the Low Range(1000) for Servo Math. The third part was 1000 / 180(high angle) = 5.556. This is the Microseconds(us) Per Degree(UPD) of the servo. The first formula is 78 * 5.556 = 433.368 + 1000 = 1434us. This gives you your angle in microseconds. This is fine for very old servos. I do not have any servo with a 1000us low range or 5.556 for the UPD. Something else was needed.

YouTube is having trouble with the thumbnails.

Angle * UPD + Low Range = Your angle in microseconds
78 * 5.556 = 433.368 + 1000 = 1434 is 78 degrees on an old servo
78 * 8 = 624 + 450 = 1074 is 78 on a Tower Pro Micro SG90 servo

The second stage of Servo Math was changing the Low Range from 1000 to 400 to 1000. This gave me zero on a servo. Zero is 0 degrees with the servo horn resting against the low stop of the servo. Usually square left of the servo. Then I changed the UPD form 5.556 to 8 – 10. Most of my servos are 8 for the UPD. My programs let you find your servos settings. The new formula is 78 * 8 = 624 + 450 = 1074us. With your angle in microseconds you can use functions like Pulsout and delay microseconds. This is where I needed Servo Math to be. An easy way to control a servo. I wish I had friends to share this with.

For large groups of servos test each one and put the same type(Servo Math) into a sub-group of the larger group. Then you can use a new Servo Math equation for each sub-group and control all of the servos by angle. That is what Servo Math does for your servos. One equation for each type of servo. If the servo is in this group then use this equation. A logic tree to control each type of servo by angle.

I started playing with servos when I got my starter robot from letsmakerobots.com. You get the starter kit from Solarbotics.com. The kit has a Picaxe 28 project board, Motor driver L293, a standard servo, a Sharp infrared range finder, 4 AA battery holder and two right angle motors with wheels. The servo moves the range finder side to side for object detecting. The Picaxe only supported approximate numbers for angles on a servo. I looked for a better way to set the angle on a servo. Servo Math just happened.

I hope this gives you some new ideas on ways to use servos for art, automation and robots. Servos are accurate to a point. They work for big moves. Little moves they have trouble. Move a servo 6 degrees and you get nothing. Why? Servos use a potentiometer (pot) connected to the main shaft to tell it where the servo horn is. The pot sensitivity is too low for small changes. Add 20 to your angle and move to it and then move back to your angle.

Step 1: Software

With my Arduino program you first set the servo to 0 degrees and set the low range until the servo horn is resting on the low stop. Then run set UPD that runs the servo test. You press the button and type in a new UPD(floating point). Do this until 90 is square front on the servo. On the Tower Pro micro servo SG90(DFRobots.com) the UPD is 8, the Low Range of 450 and a high angle of 220.  Even an older servo like the hitec HS-300 has the UPD at 8, the low range of 800 and a high angle of 200. Another micro I have is UPD of 8, low range of 800 and a high angle of 180. You can play with the settings until your servo are as accurate as you need it to be. Watch the video. You can do most of this with my Basic Stamp 2 program.

My programs demonstrate my Servo Math on Picaxe, Basic Stamp and Arduino processors. With little modifications to things like ports and pulsout units you should be able to run my programs. My Arduino program is the most fun and it supports floating point math for more accuracy. I use a port button because little processors like these are prone to crashing waiting for keyboard input. A port button is B1 to VDD, B2 to 10K to ground to 220 to the port. You should protect your microprocessor ports with at least an inline 220 resistor. All of my programs are active loops that do not wait for anything. You can easily add my Servo Math to your own programs and move your servos accurately to their full range. If you make money with my ideas then contact me, please.

Because of lack of keyboard input and string handling the Picaxe and Basic Stamp 1 are very simple programs. You push the button to start the test and push the button to stop the test. You change the settings in the program code and re-load the program.  The Basic Stamp 2 and Arduino have full keyboard input and great string handling. You run my program and push the button for commands. Enter a command and press enter. 10-Angle, 20-Set Low Range, 30-UPD, 35= run Set UPD, 40-Set High Angle, 50-Run Test, 70-Info and 100-Turn Off servo. The Arduino has 55 auto Run, 60- True90, 75- show angle. For very slow servos adjust the test timing for a slower test.

Step 2: SetUp

Most processors like the Picaxe and Basic Stamps have a function called ‘pulsout’ that pulses a port pin for so many microseconds. This is perfect for controlling servos. Perfect for my Servo Math. The problem with pulsout is that it uses units of microseconds to make up the pulse. This unit varies based on the processor and the processor’s speed. To use pulsout you need to know your processor type and speed. You divide your angle in microseconds(my Servo Math) by the pulsout unit to get the pulsout integer for the pulsout command. The integer is the number of microsecond units it takes to make up the pulse. For this example I’ll use the Picaxe 28x2 that has a 5 for its pulsout unit and the Tower Pro SG90 servo.

90 * 8 = 720 + 450 = 1170us / 5 = 234 is pulsout port, 234 is 90 degrees on this servo.

I found a fix for floating point multiplication and division in integer math in the Picaxe programming manual under mathematics. My formula 90 * 5.556 becomes 90 / 10 * 56 in integer math. It works but it is not that accurate. The 56 if a fix and adding 1 like 90 / 10 + 1 * 56 is a bit more accurate. I do not have any servos with a UPD of 5.556 or that have a Low Range of 1000. None of my code uses this anymore. It was a fun find and it does work.

90 / 10 * 56 = 504 + 450 = 954 is about 90 on a servo
90 / 10 + 1 * 56 = 560 + 450 = 1010 is closer. That is why I do not use it anymore.
90 * 8 = 720 + 450 = 1170 is 90 on the Tower Pro Micro SG90

Floating point processors like the Arduino make this easy. The Arduino doesn’t have a pulsout command, but it has delayMicroseconds() . The Arduino also has a servo library that can do pretty much anything with a servo.
digitalWrite(pPin, HIGH);
delayMicroseconds((unsigned int)pulsAng); ‘pulsAng = 1170
digitalWrite(pPin, LOW)

The zip file contains programs for Basic Stamps 1&2, Picaxe and Arduino. Portait pictures for easy printing of binary numbers and details.

My Arduino Servo Math function
void pulsePin( int pPin, int pAng ){
  if( bitRead(sFlags, svr1On) == 0 ){
     bitWrite( sFlags, svr1On, 1);
  if( lastAng != pAng ) {
     cntSetSrv = svrSetR;
     RfhSetSrv = 1;
     lastAng = pAng;
     if( pAng == 0 ){
        pAng = svr1Lrng;
     else {
       pAng = pAng * curUPD;//Angle * UPD(us per degree)
       pAng = pAng + svr1Lrng;//Add Low Range to make
       }                      //servo angle
    pulsAng = (unsigned int)pAng;
  digitalWrite(pPin, HIGH);//Set Servo Angle
  digitalWrite(pPin, LOW);

Basic Stamp 2 Servo Math function
  IF sLastAng <> sAngle THEN
   'Hit pause & hit counter
    sLastAng = sAngle
   svoHitT = srvo1Hits
   svoRfrsh = srvo1Hpaus
    wTmp1 = sAngle
   'Change Angle to a PulsOut Int
   IF wTmp1 = 0 THEN
    wTmp1 = sRangeLow / srvo1Pu
      wTmp1 = wTmp1 * curUPD
      wTmp1 = wTmp1 + sRangeLow / srvo1Pu
  PULSOUT srvo1, wTmp1

Step 3: Fun

Binary Math Fun
I updated my picture that has my programming circuit in it to better show where VDD goes.
<p>can it rotate in 1deg precision?</p>
I made a mistake in my binary math picture. I went over it so many times I never saw the mistake for the binary for the number 62. I had 01100010 that is 98. The Hex is OK. It should be 62 &ndash; 0111110 &ndash; h3E. You can use Windows calculator in scientific view to do all of your Binary, Hex, Octal and Decimal math. It has all of the logic operators and math function too. It is funny how your mind can play tricks with you. Your mind always thinks you are right.

About This Instructable




Bio: I have always been a geek and I see things differently than most people. I am healthy too and I love biology. Programming has always ... More »
More by Geek07Boy:Fun Led Checker Tester Flying a toy QuadCopter Servo Math 
Add instructable to: