Links to stuff on this blog

Use the Site Index of Projects page link above for an easier way to find stuff on my blog that you might be looking for!

Saturday, October 31, 2009

A Cryptographic Component called Sawtooth

I have been interested in cryptography and the tricky math and such that is used in cryptography for a long time. The subject is interesting to me but I am not a cryptographer and I don't have the time and computing power to be one. Having said that I have decided to put some of my ideas and screwball calculations on this blog. I have looked around the web and have seen other peoples blog's where they have put up their own ideas and I have read all the nasty comments people leave on their sites. To try and mitigate some of that let me say that I am not offering anything that I post here as an actual algorithm and I'm not trying to necessarily create an actual working cryptographic function. I do this as a hobby, pastime and just for me to play around with and learn about programming, math and fun!

OK having said all that let me get to the point of this post. In reading about different cryptographic functions one thing that seems to come up is 'mixing functions' and/or 'avalanche effect'. Similar concepts were brought up in the 'Wide Trail Design Strategy' with branch numbers and all that. While thinking about this I thought it might be fun to mess around with something that is mathematically simple and see what the results turn out ot be. I'm not a math wizard and I don't pretend to understand all the concepts so this is just a guess on a Saturday afternoon.
Again this is not an actual algorithm but might be useful as a component to something more complicated. The idea is to start with a block of numbers and mess with them to see what happens. I picked 8 bytes as the block size to use because that is a common block size (64 bits) that is used in various 'real' crypto-systems. So start with 8 Input bytes, do something to them, get 8 new Output bytes! (yes this is a boring Saturday). The 'mess with them' part is the interesting thing and I decided to set some Design Goals for what the 'mess with them' part should do:
a) If you change any one of the Input bytes by even 1 bit, all the Output bytes should change
b) In addition to all the Output bytes changing about half of the 64 Output bits should change
c) In addition to that ideally about half of the bits in each Output byte should change (4 bits of the 8 bits in each byte)
d) If all the Input bytes are 0's then the Output bytes should not all be 0's
e) Keep it really simple because I want to do all the tricky stuff in Microsoft Excel
The reasoning behind this is that if the input to a cipher changes just a little bit (like by 1 bit) the Output should change a lot and the change should be spread out as evenly as possibly throughout the entire output. As I have mentioned a few times what I am describing here is not a cipher by itself and isn't supposed to be. So here is my first go around with the above stated goals.

Now it's time for a picture! This is a screen shot from Excel. As I mentioned I wanted to use Excel because it's easy to change stuff and get statistical info without having to write a bunch of code and compile it.

So what is going on in the picture you ask? The blue row at the top is the bytes of input data in decimal and the purple row at the bottom is the output data in decimal. The blue arrows show the data being added together with a 1 added as well. The addition is being done mod 257 because 257 is a prime number and prime numbers are cool! So what is happening is the numbers are being added together from left to right as the blue arrows indicate. When I got to the right side (where the number 14 is) I copied the number 14 down  (green arrow) and then added mod 275 back from right to left. When I got to the left side where the number 73 is I copied the 73 down and then continued adding left to right. The arrows look like a Sawtooth waveform so I called this 'Sawtooth' just to have a cool sounding name for it. The last step is to take the last row of numbers and convert them to values that are 8 bits long by taking the number mod 256. More on that below.

So a quick summary and explanation. I added the numbers from left to right then back from right to left so that if any one number changes is changes all the numbers that come after it. See 'a' above in the design goals. I decided to go back and forth from left to right 3 times because by experimentation any less than that doesn't cause a change to 'trickle down' to the output numbers and any more times doesn't increase the 'trickle down'. 3 times back and forth seems to be enough for a change in one number to change all of the later numbers nicely. I decided to have each addition include a +1 in it just in case all the input numbers are 0's. With the +1 in there the all 0's case doesn't result in all 0's on the output. See 'd' above in the design goals. Lastly I used a modulus of 257 not only because it's a cool prime but because if i used 256 (the obvious choice) then an input of all 128's causes the output to be all 128's. Even with the +1 in there an input of 128, 127, 126 etc.. causes the output to be all 128's. And now the last of the lastly in the explanation is the conversion with the mod 256. I had to do this because if I didn't the numbers would be in the range from 0 to 256 and 256 is bigger than 8 bits. The conversion with the mod 256 makes all the numbers 0 to 255.

OK now it's time once again for another picture!!! Once I got the Excel thing figured out and working I copied it so that I have two identical copies. This is important so that I can see how changes in the inputs differ or in other words how differences in the inputs compare to each other.

In the picture you can see both copies of the identical regions of the spreadsheet. Above those there are two boxes that are labeled with I' * I" and O' * O" where the * is the Exclusive Or symbol circle with a + in it. The  I' * I" is the binary Exclusive Or of the two blue input parts of the spreadsheet and the O' * O" is the binary Exclusive Or of the two purple outputs. The numbers under the 1's and 0's is the number of bits that are different which is the number of 1's after the Exclusive Or. To get the Exclusive Or to work  in Excel I had to create a custom function in Microsoft Visual Basic for Excel like this:
Function EXCLUSIVEOR(Byte1 As Integer, Byte2 As Integer)
' EXCLUSIVEOR (Byte1 , Byte2)
' XOR Exclusive Or of Two 8 bit Byte's
    EXCLUSIVEOR = Byte1 Xor Byte2

End Function
Once I had that EXCLUSIVEOR function created I could type in a cell in the spreadsheet:
and I would get in the example above the binary Exclusive Or of cells E16 and E25 in an 8 bit binary value.
To figure out how many 1's that there are in a cell I used the command string:
and that will count the number of 1's and return how many it finds. I don't know exactly why it does that but it does and that is all I care about. Boy this is fun!!!! This, by the way I believe is called the Hamming Weight or Hamming Distance. This is named after a guy named Richard Hamming - too bad Francis Bacon didn't invent it.

So you can see in the above picture that in the top region the 4th byte from the left is a 6 and in the 4th byte from the left on the bottom region it's 0. The difference in binary between the two is shown above in the I'*I" row as 00000110 which is two 1's. In the bottom O'*O" row you can see the Exclusive Or difference between the outputs and the number of 1's in each. So in other words with an Input Difference of 00000110 in the fourth byte from the left the Output difference is shown in the O'*O" row.

At this point I can type any value I want into either of the blue input rows and see what the Input Difference is and see what the corresponding Output differences are. I can also see how many bits in each Input and Output byte are different. This is all getting to the answers to 'a through c' in the Design Goals above.

OK now that I can change the Inputs, look at the outputs and see the differences it's time to change all the input bits one at a time and see how the outputs change. One way to do this would be to change each bit by hand in the correct Input cell and copy and paste the I*I and O*O parts to someplace else on the spreadsheet for later review. But that wouldn't be too much fun so what I did was write another function in Visual Basic for Excel to do it for me. Here is the function:

Sub Walk1Key2()
' Set up a loop then enter values in Input range
' After setting values in Input range, paste values to Output range from Formula range
' Go back and change Input range and paste new Output values into NEXT row of Output range
' Input to Change       C9, D9, E9, F9, G9, H9, I9, J9
' Input Diff to Copy   C2, D2, E2, F2, G2, H2, I2, J2
' Output Diff to Copy  C5, D5, E5, F5, G5, H5, I5, J5
    Dim Multi As Long     ' This is the walking 1 var
    Dim Row As Long      ' This keeps track of what row the output goes to
    Dim Col As Long        ' This keeps track of the column that Multi will change
    Col = 10                     ' Start in Column "J" 10th letter
    Multi = 1
    Row = 9
      For ColCount = 1 To 8
          For Counter = 1 To 8
          'Copy and Paste Input Values Only to Output range
                Cells(9, Col) = Multi      ' Set Input cell to multi Cells(ROW=9, COL)
                Range("C2:J2").Select    ' Select Input Diff range to copy
                Range(Cells(Row, 12), Cells(Row, 19)).Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
          'Copy and Paste Output Values Only to Output range
                    Range("C5:J5").Select     ' Select Input Diff range to copy
                    Range(Cells(Row, 21), Cells(Row, 28)).Select
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                    :=False, Transpose:=False
                    Row = Row + 1
                    Multi = Multi * 2
             Next Counter
      Multi = 1               ' Reset Multi for next input byte
      Cells(9, Col) = 0   ' Set current input cell to 0
      Col = Col - 1        ' Move back one column
   Next ColCount
End Sub

Boy oh boy talk about cryptic cryptography!?!? Figuring out that piece of code and getting it to work right took most of the afternoon!! Anyway what it does is a 'walking 1' in binary across all the blue input bytes of Row 9 in the pictures. 'Walking a 1' is nothing more that setting values of 1, 2, 4, 8, 16, 32, 64 and 128 as the Input byte in each byte of the blue input row. After changing the value in the Input byte the code copies and pastes the I*I and the O*O values to the right in the spreadsheet.

After all that it's time once again for yet another picture!!!!! Click on it for a bigger view - it's worth it!!!!

In the picture above you can see the familiar copied spreadsheet regions with the infamous I*I and O*O above them. To the right and going down the sheet there is a big region of binary numbers under the heading I*I and you can see the "walking 1" pattern in it. To the right of that and under the O*O heading is the output differences that correspond to the input differences.  That is just a portion of the data, it went on for a total of 64 rows to get all the input differences.
Now that I had this interesting spreadsheet with all these numbers on it I could go address the Design Goals set forth above. Here is what I found:
1) Did every output byte change in every case? Every output byte did in fact change with every input difference change of 1 bit. So at least in this test a bit difference of 1 in every possible input bit location caused every byte of the output to change. No byte was left unchanged!!!

2) Did approx half of the 64 output bits change in each case? Ideally exactly half of the 64 total output bits would change with a input difference of just 1 bit. The minimum number of bits that changed was 25 bits or 39.063%. The maximum number of bits that changed was 38 or 59.375% . The mathematical average was 48.438%. with 11 instances being exactly 32 bits or 50%. Not too bad but not exactly half of them in every case. As mentioned above the bit changes were spread out over all 8 output bytes in every case.

3) Did on average 4 bits in each byte of the output change with each 1 bit input difference? This is trickier because of the statistics above not every byte had exactly 4 bits change. To simplify this I did a histogram of each number so:
# bits      # times
   8            1
   7            15
   6            54
   5            92
   4            138
   3            138
   2            47
   1            27
   0            0

So from the above chart there was only 1 time that a byte had all 8 bits change. There were 15 times a byte had 7 bits change and so on. As can be seen the majority of bytes that changed had 2, 3, 4 and 5 bits change.

What does all this mean??? This means without a doubt that I have spent an entire afternoon messing around with numbers and Excel!!! Yeah! Big Blog High 5!!!! As far as 'Sawtooth' being useful as an element in a cryptographic algorithm I think I'm leaning toward a qualified yes. As a small part of something bigger it might be a useful method of creating an avalanche effect in input data and diffuse small  input changes over larger chucks of data. I will mess around with it some more to get a better idea of it's characteristics and see how it works in different input ranges. Things I am considering investigating:
A) Does every possible output state exist for every possible input state? Or are there some output values that it won't reach no matter what the input is making some different inputs have the same output? In other words is it a mathematical group?
B) Figure out exactly how much avalanche this created over a broad range of input differences. I'm not sure I need to do this one as the 'walking 1' should be enough but I need to prove that to myself.

Saturday, October 24, 2009

The Robot Walks On Carpet

Here is another video of the robot walking! Wow exciting! Well not really but there are a few things that are different between this video and the last couple that I have posted. The first thing that is new is I broke down and bought a new camera... I really needed a new camera. The digital camera that I have been using was originally purchased from an Egyptian Pharaoh, second hand right outside his pyramid so it was old but in good condition! The new camera was on sale, has sound and is much better resolution. So no more fuzzy dark silent movies that you can't see anything.

The other thing that is different is the robot is walking on carpet. This may not seem like a big deal but it is really (to me). Up until now the contraption has been stumbling along on a rubber mat to add some friction to the feet. Trying to walk in carpet didn't work very well because the feet tended to get hung up in the threads of the carpet. Well messing with the speed and the leg positions overcame that problem so now it can walk on the carpet. There are still some issues to be worked out to get it to walk straight but I know what is going on with that so it's just a matter of fixing it.

The last video is the initialization of the servo motors and the legs running through the walk cycle. Looking closely I think I see a problem in the speed changes not being sent to the board and some timing in the commands being executed. More stuff to fix in the code.

This is an ongoing project that is being done in a 'make it up as I go along' style. Now that I have it walking I want to make it turn, change speeds etc... but adding sensors and a stand alone controller board so I'm not tethered to my laptop are the next big things. Stay tuned....

Sunday, October 18, 2009

Robot Experimental Walk Cycle First Attempt

This weekend I finally hat the time to mess around with the robot some more and I managed to get it to walk. It still isn't as smooth as I would like but it does walk! I'll start this blog entry with a couple videos of it doing it's thing.

From the video you can see it stumble along the floor on a piece of rubber mat that I set on the carpet. The rubber gives it's 'feet' some traction because it was getting hung up in the carpet, and /or slipping and sliding on the hardwood floor. I suppose the obvious solution would be to put rubber tips on the feet but the rubber mat was easier for now. The legs and feet are made from 0.17" diameter carbon fiber tube and there is no traction on wood and the ends of the tube get caught in the carpet. A long term solution will be to put some rubber or other type of material on the ends of the tube.

The walk cycle that the legs are running through is just a pattern that is repeating and a rather rapid rate. I found that running it slow is actually more unstable than at a faster rate. A couple of things that I am going to play with are the speed that the servos are running and the center of gravity. Changing the speed will be a software change and lowering the center of gravity will mean cutting the legs shorter. I could lower it by adding some weight to the robot base underneath the robot but the increase in overall weight would strain the servos too much.

What follows is a detail of the leg motions that I am using right now to make it move and a picture to help explain it. The rectangle at the top is showing the top view of the robot with each leg location 1 through 4. The bottom rectangle shows a side view with some colored arrow boxes. The letters in the boxes indicate locations of the feet relative to the base of the robot.

So if you look at the colored box on the right it's showing the feet positions of legs 1 and 4, and the colored box on the left is showing the feet positions of legs 2 and 3. As you can see from the box moving a foot from position 'a' to position 'b' is raising the foot up. Moving from 'b' to 'c' is moving the foot away from the base  while it's up off the floor, and moving from 'c' to 'd' is setting the foot down at a distance from the base.

Additionally there are some colored arrows in the boxes that indicate the order in which the feet are moving through these positions. The blue arrows indicate motion that a foot is doing all by itself and the orange arrows show the motion that all the feet do together at the same time. Here is what is happening to make the robot walk with all the legs (feet) starting at the 'a' positions:

Move Leg 1 from 'a' to 'b' to 'c' to 'd'
Move Leg 4 from 'a' to 'b' to 'c' to 'd'
Move all the legs together:
     Legs 1 and 4 move to their 'a' positions while legs 2 and 3 move to their 'd' positions
Move Leg 2 from 'd' to 'c' to 'b' to 'a'
Move Leg 3 from 'd' to 'c' to 'b' to 'a'

At the end of the above motions all the legs are back to their 'a' positions and the robot has moved about 1.5" forward. Right now that is all it can do but that is enough for now. Each of the legs also has a Turn servo that isn't being used yet. I want to get the walking forward part completely figured out and smooth, then I'll work on walking backwards and hopefully turning  after that.

Anyone interested in the Python code I wrote to make this work let me know and I'll post it.

Friday, October 16, 2009

Naked Lady Mannequin Coat Rack (I call it Frankie)

If you are like me you probably have a few plastic female torsos laying around the house and quite possibly a coat or two looking for a place to reside. OK... I don't have more than one naked female mannequin torso's  - I only have one - because having one is 'normal' and having a bunch of them would be 'weird'. I picked this thing up from a sporting goods store in San Francisco that was going out of business and selling off all their stuff. I  have to say that I didn't buy it because that too would be weird - paying money for something like this - it was given to me - sorta. I was at the store after they had gone out of business and closed down with a bunch of other guys that were taking down all the store display racks. We were taking all these shelves and racks to the place that I worked because at the time we were setting up a retail store near Fisherman's Wharf - sorta.

Anyway my employer bought all the shelving from this store and we were all there taking the shelves and displays down as were other 'crews' of people getting other things that they had bought. Near the door were these two swimsuit mannequins and I asked the guy in charge if I could take one. He looked at me like I was weird and said get "it out of here!" So off I went with it back to my office... from there I had to take it home on the BART Train (public transportation) and I thought I would get some really strange looks and people laughing at me for carrying a naked female torso under my arm - but I was in San Francisco and that isn't weird - in fact few things are really weird in that city compared to what is an adjusted 'normal'

So after getting it home I had to figure out what to do with it. Putting clothes on it seemed to be the right thing to do but before I could do that I had to make it stand up by itself. Because it didn't have any legs I cut up some black PVC pipe and drilled some holes in the bottoms of the truncated leg stubs. The two pieces of pipe are a lot longer that they look in the picture because this thing is hollow. So I jammed the pipes up inside the body until the wouldn't go in any farther. I cut the pipe parts that were sticking out so they were the same length and glued on the U pipe joint. This essentially made the two pipes into one and gave me a place to glue in a short piece of pipe. Out in the backyard I had a couple of those old direct TV dishes that someone gave me and I laid it flat on the floor and attached the single PVC pipe to it. Check out the picture if this description isn't clear.

The top pipe sticking out of where the neck/head would be is actually part of the mannequin, it was designed to hang from that. These things were hanging by their skinny pipe necks from the ceiling in the store. I didn't mention that at first because it sounds weird. I figured if you have read to this point of the blog it wouldn't sound so weird. Am I right?

So to cover up the strange pipe sticking out the top I put my hat on it. I did consider building a transparent head bowl like thing with lights in it - and using that for a head - and I still may do that. I think it would look good if it had something that lit up when someone walked by or made noise... kinda like the lamp that I made. or maybe it could have a scrolling message but I'd have to think up something funny for it to say. For now it's just a coat rack and hat holder.
I bet that you want one !

Sunday, October 11, 2009

More on the home made robot... starting to walk

After a long weekend I finally got the Python code to work the way I wanted it to and I am able to more easily control the motors. Most of my time over the last several days has been spent figuring out how Python works and not really messing with the robot motion directly. Because of that I'm tired and don't have the time to get a really smooth walking motion today. What I currently have does work but it jerks along the floor and doesn't make the 'forward motion' that it should be doing based on how much the legs are actually moving. I do count this as some success because it should be easier from where I am at now to get it to at least walk.

What you can see from the video is that all 4 of the legs are moving in cyclical 'walking' motions but they are not coordinated enough to really get the robot to move forward. This is because at a few points in the cycle some of the legs are moving forward and others are not. So when it's trying to walk on the floor the legs that are not moving are being dragged along!!! Oh well like I mentioned I have spent all my energy getting the code to work and not on actually the motion.
The progress that I did make this weekend was I managed to figure out how to use the import statement in Python to bring in a chunk of code that defines some custom commands that I wrote. What that means is now I can write a script like this:
So what is going on in the above code is I can just directly address the particular motor I want and send it to a particular position. For example the first command PS.ServoPos(6,2200) is sending servo number 6 to position 2200 !!! WOW! I'm sure all you Python gurus out there are laughing at me but considering I'm not a programmer and I have only been using Python now for 12 days or so it's an accomplishment.
Here are the steps that I listed in my Last Post that I wanted to get done with updates to each:
1) figuring out a clean way to mount the Pololu board to the robot base with it's one mounting hole
    I mounted the board and made a strain relief for the cables (see picture below)
2) cable management to make sure the servo wires don't get all tangled up while it's running
    I can't say that they are under control but the cables now don't get tangled!!! (see picture below)
3) using the jog program to get the remaining leg hard stop values
    I got this done and wrote a little converter program to help (see source code below)
4) hours of coding and testing to make this thing walk!!!
    When I wrote hours of coding and testing I was underestimating the task... lets say DAYS of coding!

A picture of the Pololu board mounted to the Epson Printer Base that I cut up and turned into the robot base. The black plastic on top of the board with the wires coming out of it is a piece of 0.04" thick ABS plastic that I cut and screwed to the base. There is a second piece of ABS as well that is sandwiching the wires in there with some rubber bands to hold it together. It's not elegant but it works to keep the Futaba servo connectors from being pulled or worked off the Pololu board while the robot is running.
The next thing to do at this point is refine the walk cycle to get all the legs to move together so it can walk instead of dragging its feet along the floor. As soon as I get that done I'll post another video of it walking.
Below is the Python code that I wrote to help with the programming of the Pololu board. The Pololu requires position commands that range from 500 to 5500 to position the motors. When sending the position to the Pololu board you have to take a position like 3800 and convert it into 2 bytes of data with the Most Significant Bit of each of those two bytes set to 0, shifting bits around as needed.
So 3800 decimal  = 
00001110 11011000 binary 
which then has to be converted to 
00011101 01011000
which is equal to two bytes of
0x1D 0x58
Anyway doing that by hand is a pain so I wrote the program below to do it for me. This was handy when figuring out positions that I wanted to have defined in the function calls that I wrote.
#  Pololu Range Converter
#  Written By Otto Belden Oct 2009
#  This will take a input from the console and print out the
#  high and low byte that the Pololu needs to position the servo
#  with the MSB of each of the two bytes 0 as required by Pololu
import sys
# Set a default position
pos = 2500
while pos != 0:
    pos = int(raw_input("Give me a number or 0 to exit "))
    if pos == 0:
        print "Exiting..."
    # get the data 1 byte (high byte)
    data1 = (pos-(pos&127))/128
    # get the data 2 byte (low byte)
    data2 = pos&127
    print " Position = ", pos, "High Byte = Ox%x"%data1,"Low Data = 0x%x"%data2

Wednesday, October 7, 2009

DIY - Homemade (free) Shop Dust Collector (Disperser)

It's getting a little dusty and dirty in here... well it was until I made my very own shop dust collector! With all the work that I do dust is a big problem especially when wood turning on my Homemade Lathe. Of course and sanding, cutting and drilling also generates a lot of dust. Aside from being unhealthy it is also a pain as dust gets everywhere. No matter what I do it ends up in the house, upstairs, in my nose and pretty much every other place that  you can imagine. UPDATE: I wrote a new post with more details HERE with a video have a look!
To deal with this I decided to build a dust collector system that uses a radial blower. I chose a radial blower for this because they tend to be better at building up (static) pressure compared to a fan type but also I happened to have a blower that I wasn't using for anything (free). The static pressure part is good because to really work as a dust collector I have to not only suck up the dust but blow it through a filter of some kind. Filters especially when they get dirty and clogged don't pass air too well and the back pressure builds up. A radial blower can keep the pressure up and keep the air flowing.
So step one was to get a suitable container to collect the dust. I decided a paint bucket was just about right because of it's convenient size but also I happened to have one laying around that I wasn't using (free). Here is a picture of the finished system.
Finished Homemade Dust Collector

Saturday, October 3, 2009

Robotic Leg Testing with Python, Pololu and some good luck...

A day or so ago I got my Pololu USB 16 RC Servo Controller board in the mail. This is a USB or serial controlled RC Servo controller that can handle up to 16 RC Servo motors simultaneously. The board takes serial data from either the standard mini USB connection or a RS-232 connector and turns it into the pulse train necessary to position the RC servos. The handy thing about this board (and other like it) is that the board takes care of the pulse generating and all you have to do is sent it a serial string of data to tell it which motor goes to what position - the board takes care of the pulses.
For my initial mechanism testing I built a little test box that had an oscillator and three 'one shot' pulse stretchers that I could control with potentiometers to get the desired pulse train. That was good enough to test the mechanism one part at a time but for the coordinated motion required for exciting robotic action I need something like the Pololu USB 16. Because my robot has 3 servos per leg and a total of 4 legs I need to be able to control 12 servos and the Pololu handles 16... so far so good with 4 to spare....

My initial impression of the Pololu USB 16 is that it's small and only has one mounting hole to mount the board. The small size is great but the single mounting hole will make it an interesting challenge to mount it to the robot base. The other thing that I noticed about it is the servo power connector wasn't soldered to the board itself when I got it. I decided to solder it to the backside of the board since on the component side it will cover up the silkscreen on the board and make it hard to connect the servos to the header. Not a big deal I guess. I didn't bother to figure out a way to mount it just yet, I thought I'd better test it first. So I just set it on the robot base and hooked it all up.
Sorry about the fuzzy picture but you get the idea.

Another nice thing about the Pololu is it comes with a driver that once installed lets the USB connection to the Pololu board look like a standard serial COM port on your computer. That way controlling the Pololu is done by sending serial data out the COM port (really the USB). This is handy because most computers, especially laptops these days don't have an actual serial COM port.

The first thing that I did to make sure that this was all working was write a simple Jog test program in Python. I'll post the code for this at the end of this blog post. The code is mostly mine with a bit of the data conversion stuff from an example on the Pololu website. I used this jogger program to incrementally move the servos to their mechanical stops. I had to do this because the travel on each servo is 180 degrees but my robot leg mechanism doesn't use all 180, mechanically the linkages wont go that far. Each time I moved the servo with the jogger program it prints out the actual servo position and once I was at the mechanical stops I wrote down that position for each servo. As long as I don't tell the servo to go past that position I won't 'crash' the robot mechanism into it's hard mechanical stops.

Once I had all the mechanical stop positions written down I wrote a simple 'exerciser' script that moves the servos through their ranges. This was not only to test the mechanism but also to help me figure out how Python programming works (I'm not a programmer). Also this allowed me to see how well the Pololu handles coordinated motion of multiple servos. I made a 28 second grainy and dark video of the leg running this script.

It looks like the Python code talking to the Pololu USB 16 board works pretty good at getting everything running at the same time. All three servo's ran as expected without crashing into anything and the motors run at the same time as opposed to stepping through each motion one at a time.

The next immediate steps in the robot development are going to be:
1) figuring out a clean way to mount the Pololu board to the robot base with it's one mounting hole
2) cable management to make sure the servo wires don't get all tangled up while it's running
3) using the jog program to get the remaining leg hard stop values
4) hours of coding and testing to make this thing walk!!!

Here is the Python code of the Jogger program that I wrote. I want to say again that I am not a programmer and I have only been writing Python code for 4 days now so it probably isn't the best example of how to do this - but it woks. If you are a Python Guru please let me know what I can do to improve my code especially if it will make the next programming task (walking robot) a bit simpler and easier for me. In other words HELP!!!!   ;-)

- Python Pololu USB 16 Servo Jogger Program - 

Python needs to have everything indented correctly with spaces to run and the copy/past from Python IDLE seems to have removed some of those. You may have to tinker a bit to get it to work and look right in the Python editor but all of the code below works on my Windows Laptop running Python with the pySerial module loaded. PySerial gives you the serial commands to write out the virtual COM port (really a USB connection) that the Pololu USB driver sets up when you plug in the Pololu board.
# Pololu Motor Range Finder or Keyboard Jogger
# Written By Otto Belden Oct 2009
import serial
import sys
#set com 3 up for communications Pololu defaults to com 3
# set the speed of Servo at something slow and reasonable with
# a varaible serstr = Serial String to write to com port
#           Start     DevID   Command  Servo     Data1
serstr = chr(0x80)+chr(0x01)+chr(0x01)+chr(0x02)+chr(0x10)
# set a default position at a mid range position for the servo 2750
# which breaks down in Pololu speak to 0x15 0x3E Data 1 and Data 2
# a varaible pos = position
# then build a new Serial String using that position and send it out
pos = 2750
#           Start     DevID   Command   Servo     Data1      Data2
serstr = chr(0x80)+chr(0x01)+chr(0x04)+chr(0x02)+chr(0x15)+chr(0x3e)
# define function newposition() that will calculate the 2 byte positions
# from the current position and assign those bytes to Data1 and Data 2
# then write out a new string
def newposition():
    if pos <=700 or pos >= 5300:
        print "******WARNING SLOW DOWN******"
    # get the data 1 byte (high byte)
    data1 = (pos-(pos&127))/128
    # get the data 2 byte (low byte)
    data2 = pos&127
    #           Start     DevID   Command    Servo     Data1      Data2
    serstr = chr(0x80)+chr(0x01)+chr(0x04)+chr(0x02)+chr(data1)+chr(data2)
# print out to the console the instructions and keys to use then get keypresses
print "The servo should be on right now and in the middle of it's travel"
print "Use the '1' and '2' keys to jog the motor Forward and Reverse 100 counts"
print "Use the '3' and '4' keys to jog the motor Forward and Reverse 50 Counts"
print "Use the '5' and '6' keys to jog the motor Forward and Reverse 10 Counts"
print "Use the '7' and '8' keys to jog the motor Forward and Reverse 5 Counts"
print "Hit the '0' key to quit\n"
Input = 1
while Input != 0:
    Input = raw_input()
    Input = int(Input)
    if Input == 0:
        print "Now exiting last position was ",pos
        ser.close() # close the com port befor exiting
        print "Com port is open =",ser.isOpen() # make sure the port is closed
    if Input == 1:
        pos = pos - 100
    if Input == 2:
        pos = pos + 100
    if Input == 3:
        pos = pos - 50
    if Input == 4:
        pos = pos + 50
    if Input == 5:
        pos = pos - 10
    if Input == 6:
        pos = pos + 10
    if Input == 7:
        pos = pos - 5
    if Input == 8:
        pos = pos + 5
    print "New Position  = ",pos