K'nexapod


K'nexapod is a hexapod robot built with K'nex building toys and controlled by a PICAXE microcontroller. Propelled by small hobby servos it can move forward, backward, and make turns. It is equipped with an ultrasonic sensor and follows a simple algorithm to wander about while avoiding obstacles in its path.

K'nexapod is by no means an engineering marvel. It is, quite literally, tossed together from spare parts I had lying around that were mostly salvaged from earlier projects. It is presented here to demonstrate how easy (and fun) it can be to design and build an autonomous walking hobby robot, and to explore some of the associated thought process.

The Three-Servo Hexapod

Inspired by the insect world, hexapods (six legged robots) provide an inherently stable platform that can be both easy to implement and efficient in operation. The overall stability of the hexapod comes from its ability to establish a gait (walking pattern) in which at least three legs are on the ground at any time. The three legs will form a tripod with properties similar to a camera tripod or a three-legged stool - the robot will be stable as long as its center of gravity falls within the support zone defined by the feet that are currently planted.

The most basic hexapod walking pattern is called the alternating tripod gait. In this gait, the six legs are treated in two groups of three. Either group of three is a tripod formed by the front and rear legs of one side, and the middle leg of the opposite side. The three component legs of each tripod are moved as a unit. As one tripod is lifted, the other tripod pushes forward. In this gait it can be helpful to think of each tripod as a foot and compare it to your own bipedal walking where as one foot is lifted the other foot pushes forward.

K'nexapod employs a common variation of the alternating tripod gait which uses only three servo motors to control the movement of the six legs. With a three-servo implementation, the front and back legs on either side are controlled as a pair by a servo which moves them forward and backward. A third servo controls both middle legs which move up and down. As a middle leg moves to the down position it pushes the chassis upward lifting the front and rear legs of that side and forming a tripod with the front and rear legs on the opposite side. You can see this gait in action in the video embedded below.

This "modified" alternating tripod lacks some of the agility and grace of more complex hexapdods which might have 18 servos or more controlling the movements of each leg individually. Nonetheless, the solution described here is very powerful and has its own advantages. Servos can be expensive so a design which only uses a few will save money. Servos also add weight, consume power, and use microcontroller resources.

The machine is quite stable, and by adjusting the pattern in which the tripods are moved, the robot can move in any direction including a full reverse. Much like the operation of a differential drive in wheeled robots, by having tripods push and pull in opposing directions the robot can rotate left or right. More subtle variations can be achieved simply by alternating the length of stride.

PICAXE, Servos and Ultrasound

K'nexapod is controlled by a PICAXE 20X2, my current favorite PICAXE microcontroller for general purpose applications. These days hobbyists have more good microcontroller choices than ever, but PICAXE remains a very solid contender. The chips are available in a range of sizes from 8-pin to 40-pin making it easy to find a good match for a project. The BASIC programming language is extremely easy to use but is also extremely powerful. The language has features to make interfacing with motors, sensors and other common components a snap. Often just one or two lines of code are all it takes to configure and use a device. The 20X2 is a 20-pin chip with memory to execute about 2,000 instructions. It is based on the Microchip Technology PIC18F14K22 processor giving direct access to many of that chip's more advanced features. I've actually been using both the 20X2 and the raw PIC18F14K22 in my designs, using the 20X2 for basic tasks and the PIC when I want to use Assembly language to optimize for speed or handle one of the occasional routines that I can't tease out of PICAXE BASIC. A nice plus to this approach has been that since they are in fact the same chip, I can swap them in and out of designs with little or no modification to the associated circuitry!

We have a nice offering of PICAXE tutorials here at Hobbizine. If you are brand new to microcontrollers, you might want to start with Introduction to the PICAXE Microcontroller

K'nexapod uses three Tower Pro SG-90 servos, which at the time of this writing are probably the least expensive servos on the market. I really like these servos. They're not as accurate as some of the more expensive options, but for a lot of basic applications they don't need to be and the price really opens up possibilities. The SG-90 is a 9-gram microservo and as such doesn't have a lot of torque. Although they were fun to use in this project, they're better suited for lighter-duty work. I frequently use them to build pan and tilt mechanisms for small sensors. The limited torque became a major design constraint here. K'nexapod is about as heavy as it can get. Also, you'll notice in the pictures that K'nexapod's center legs are very close to the center; if the crossbar was much wider the robot would not be able to lift itself.

One of the great things about using servos in a simple project like this one is that because they have a built-in motor driver they can be connected directly to a microcontroller with virtually no additional components. A current limiting resistor on the input, and a capacitor for noise suppression and you should be good-to-go. For more information about servos and an introduction to using them in projects see Basic PICAXE Servo Interfacing

Perhaps K'nexapod's coolest feature is its SRF05 ultrasonic range finder. The SRF05 uses sonar technology to find obstacles in its path and accurately determine the distance to those obstacles. This is the same basic technology used in submarine navigation and similar applications. The SRF05 takes care of most of the "hard work" of ultrasonic range finding making it very easy to interface with a microcontroller. Driven by a simple pulse from the microcontroller, the sensor both generates the necessary 40khz ultrasonic tone and then detects the echo. It has a range of about 3 centimeters to almost 4 meters. The SRF05 detects objects across a fairly wide arc known as the beam width. The wide beam is sometimes a liability. However the SRF05 is well suited to this application where it will be mounted in a fixed position but will be able to detect objects on a collision path across the full width of the robot. For more details of ultrasonic range finding and the SRF05 see Robot Obstacle Detection and Avoidance with the Devantech SRF05 Ultrasonic Range Finder

A basic schematic diagram for K'nexapod in pictured above. As promised, it really couldn't get much simpler. Each of the three servos gets a dedicated output from the PICAXE. The SRF05 gets an input and an output, and I also tossed in an LED as an indicator light to flash when obstacles are detected. The circuit is powered by 4 AA rechargeable NiMH batteries. NiMH rechargeables will provide about 4.8 volts over most of their discharge cycle which is ideal for this project. It is enough to comfortably drive the servos and sensor but not too much for the PICAXE, meaning we can get away with a single power supply and no regulator.

Working With K'nex

As a poor man's robotic construction system K'nex is not-too-terrible. In addition to its hallmark rods and connectors, the product line includes gears, wheels, motors and a number of other specialized parts. K'nex enables projects that are relatively light weight, yet surprisingly strong. More importantly K'nex is fun. The system invites a playful, trial and error approach to design, and the proportions of the various parts cause geometric patterns to somewhat magically emerge as you experiment. As you can see, I wasn't extremely creative in interfacing the non-K'nex parts, relying more on my trusty hot glue gun than my ingenuity. The SRF05 was already mounted to a piece of scratch modeler's styrene so I left that alone and glued the whole setup to a half-circle K'nex connector. Unfortunately I had filed off the mounting flanges of the servos for another project which made it more difficult to rig up a mount than it might have been. Otherwise, servos and K'nex seem to be made for each other. It's pretty easy to make an attachment to the servo horns without cutting or drilling into the K'nex. I did have to enlarge the holes in the servo horn slightly using a hand drill, but with that done a couple of home-made styrene washers and some 2-56 machine screws were all I needed to bolt down another half-circle K'nex connector to each servo. When working with standard size servos there is room for the larger, 4-40 machine screws and the fit to K'nex is even better.

The Algorithm


;----------------------------------------------------
; Source Code for K'nexapod
;----------------------------------------------------


#picaxe 20x2
#no_data

;----------------------------------------------------
; Assign intuitive names to the inputs and outputs
; and variables that will be used
;----------------------------------------------------

  symbol left_legs = b.2          
  symbol center_legs = b.3
  symbol right_legs = b.4
  symbol trig = b.1              ; SRF05 Trigger output
  symbol echo  = c.7             ; SRF05 Echo input

  symbol obstacle_led = c.5      
  
  
  symbol range = w0              ; variable holds SRF05 reading
  symbol loop_counter = b2
  
  
;----------------------------------------------------
; Power up and initialize the servos
;----------------------------------------------------

  pause 200     
    
  servo b.2, 150
  servo b.3, 160
  servo b.4, 150
  pause 2000


;----------------------------------------------------
; The main program loop continuously executes the "ping"
; subroutine which takes an ultrasound reading and stores
; the result in the variable named "range"
; Depending on the result of the reading one of three
; locomotion subroutines will execute
;----------------------------------------------------

 main:

   gosub ping
    
   if range > 250 then       
    low obstacle_led
    gosub goforward

   elseif range > 175
    high obstacle_led
    gosub goright

   else
    high obstacle_led
    gosub gobackward

   endif

 goto main 

;----------------------------------------------------
; Each of the locomotion subroutines cycles through a 
; sequence of servo positions which together make a
; complete step  
;----------------------------------------------------


;--------------  
;forward
;-------------- 
 goforward:  
  
   servopos center_legs, 140
    
   pause 250
    
   servopos left_legs, 150
   servopos right_legs, 120

   pause 250

   servopos center_legs, 180
    
   pause 250
    
   servopos left_legs, 180
   servopos right_legs, 150    

   pause 250
    
  
 return

;--------------  
;right
;--------------
   
 goright:

   servopos center_legs, 140
    
   pause 250
    
   servopos left_legs, 150
   servopos right_legs, 150

   pause 250

   servopos center_legs, 180
    
   pause 250
    
   servopos left_legs, 180
   servopos right_legs, 120    

   pause 250
    
 
 return
      
;--------------  
;backward
;--------------

 gobackward:   

   servopos center_legs, 180
    
   pause 250
    
   servopos left_legs, 150
   servopos right_legs, 120

   pause 250

   servopos center_legs, 140
    
   pause 250
    
   servopos left_legs, 180
   servopos right_legs, 150    

   pause 250
    
 
 return
     
;------------------------------  
; take an ultrasound reading 
; from the SRF05
;------------------------------

 ping:
   pulsout trig,1         ; send a 10uS trigger pulse on output 7
   pulsin  echo,1,range   ; read the pulse on input 1 and store the result in w0
   pause   10             ; SRF05 required 10mS recharge period after ranging completes
 return

K'nexapod runs in an endless looping taking readings with the SRF05 and then executing one of three behaviors based on the result. If the SRF05 returns a 250 or greater, K'nexapod determines that the path ahead is clear and moves forward. If the SRF05 returns a value between 176 and 249, then it determines an object is in its path and turns toward the right. If the SRF05 returns a value of 175 or less, then K'nexapod determines that a collision in imminent and goes in reverse. Of course this extremely basic strategy can easily fail resulting in a collision, but as was seen in the accompanying video it is frequently successful.

Going Forward

If you are just starting out, K'nexapod has hopefully given you some inspiration and some raw material to work with in designing your own robot. One of the great things about the robotics hobby is that no robot is ever really finished, and a big advantage to designing your own platform is that you can always be building with your next steps in mind. Virtually every aspect of K'nexapod is calling out to be upgraded or optimized. The project only uses a fraction of the processing power of the 20X2 and could easily accommodate more sensors as well as a more sophisticated approach to navigation to make better use of the data coming in from the SRF05. Upgrading to more powerful servos would allow you to design a bigger K'nex chassis and you could smooth out the gait and carry more electronics. If you haven't already done so, I'd encourage you now to read some of the related Hobbizine resources for PICAXE and robotics - then order up some supplies and start building robots today!



Other Articles You Might Find Enjoyable

Design and Build Your Own Robot

K'nexabeast - A Theo Jansen Style Octopod Robot

Robot Obstacle Detection and Avoidance with the Devantech SRF05 Ultrasonic Range Finder

Introduction to the PICAXE Microcontroller

Setting Up A Differential Drive For Your PICAXE Project

Basic PICAXE Servo Interfacing

Flexinol Control Circuit Using PIC 16F690 and ULN2003A

Precision Flexinol Position Control Using Arduino

Analog Sensors and the MSP430 Launchpad

An Arduino Neural Network


Things to Do Here

Home
Links