Pegleg the six-legged (hexapod) robot – part 4 programming and servo control

We’ve gone over how to build a hexapod and how to analyze it’s motion; in this section we review how to actually program this robot.  Although there are probably better options out there (Arduino seems to be a good value although I haven’t tried it – Update: tried it recently, see my review), I used a Basic Stamp II processor for my robot.  I’d already done a project for school with a BSII, so I was pretty confident in my programming abilities with it.  It uses a version of BASIC, while Arduino uses C, so that may be something you want to consider when selecting your processor.  Before I give you the code, here is the final version of Pegleg, which is much faster than the original.  I’ve put both versions here for comparison.

[ad#Google Adsense-text leaderboard wide not home]

mini ssc2 in use

Serial SSCII

If you watch the two versions side by side, it should be evident which one is more refined from a speed point of view.  The final version also looks like it might stay together for more than a couple minutes (hopefully you agree).  Servo optimization helped of course, but using the mini SSCII servo controller pictured in use to the right had the greatest effect (one of the servos was taken off to allow you to see it).  The servo controller allowed simultaneous movement of all legs since the BSII didn’t have to send them a signal continuously while moving.

Here is the program originally used to control Pegleg, followed by a more refined version using subroutines and, of course, the serial SSC II servo controller.

Simple forward walk, no SSC II in use:

‘whole servo controller (makes the robot walk forward)
startval var bit

bs2 circuit

Wiring for Pegleg on the breadboard. The black circle is a speaker.

endval var byte
starv var nib
ending var nib
a var byte
b var nib
ending = 5
startval = 1
endval = 40
start
input 4
if in4 = 0 then loop
goto start
loop
for A = startval to endval
pulsout 12,400 ‘puts the left middle leg down
pause 25
next
pause 50
for A = startval to endval
pulsout 13,1012 ‘rotates the left back and the right forward
pulsout 14,1012
pause 25
next
pause 50
for A = startval to endval
pulsout 12, 1150 ‘puts the right middle leg down
pause 25
next
for A = startval to endval
pulsout 13, 581 ‘rotates the left leg back and the right leg forward
pulsout 14, 581
pause 25
next
pause 100
goto start
end

Walking demonstration routine.  Subroutines and serial servo controller used.

‘Subroutines:
rfwd:
serout 0,n96n,[sync,2,65]
return

rmid:
serout 0,n96n,[sync,2,155]
return

rback:
serout 0,n96n,[sync,2,255]
return

lfwd:
serout 0,n96n,[sync,1,65]
return

lmid:
serout 0,n96n,[sync,1,155]
return

lback:
serout 0,n96n,[sync,1,255]
return

rtmidup:
serout 0,n96n,[sync,0,255]
return

ltmidup:
serout 0,n96n,[sync,0,0]
return:

midmiddle:
serout 0,n96n,[sync,0,127]
return

lfwdrback:

return

rfwdlback:

return

rtturn:
gosub rtmidup
gosub lfwd
for b = startval to ending
gosub ltmidup
gosub rback
gosub lback
gosub rtmidup
gosub rfwd
gosub lfwd
next
return

forwalk:
gosub rtmidup
gosub lfwdrback
gosub ltmidup
gosub rfwdlback
return

backwalk:
freqout 11, 500, 2500, 3000
gosub ltmidup
gosub lfwdrback
gosub rtmidup
gosub rfwdlback
return

ltturn:
gosub ltmidup
gosub rfwd
for b = startval to ending
gosub rtmidup
gosub lback
gosub rback
gosub ltmidup
gosub lfwd
gosub rfwd
next
return

It’s been a long time since I programmed this, but those are the files that I had (minus some sort of freqout command – maybe I was trying out a speaker) on my computer.  If you’re somewhat familiar with programming it shouldn’t be too hard to modify my code for your use.  I don’t have any sort of buttons or sensors in these routines, but that would be pretty easy to add with if-then statements.

One very important command if you’re not using a separate servo controller is “pulsout” which sends a PWM (pulse width modulation) signal to the servos to do your bidding.  If you are using one, the command to do the same action is “serout” which signals the controller to send a PWM signal to the servos to then do what it is supposed to.  The second program also makes use of subroutines, which is highly recommended.

robot side view final assembly

So that is it, I’ve gone over everything I can think of needed to build your own hexapod robot.  There’s probably more, so leave a comment if you want me to fill you in on anything else or if I missed anything.  I may do a couple more posts on this ‘bot if I try to get it working again, or maybe I’ll do a post on some of the ideas I tried but never pursued fully.

-JC

  1. Improving a hexapod design - Hack a Day - pingback on January 15, 2011 at 4:42 pm
  2. Pegleg the six-legged (hexapod) robot – part 1, introduction | - pingback on February 17, 2011 at 9:43 pm
  3. Be sure to check out the other steps to making this robot, listed here: http://www.jcopro.net/category/robotics/page/2/

  4. Old School Update: December 2010 | JCOPRO.NET - pingback on July 11, 2012 at 5:03 pm

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Trackbacks and Pingbacks: