In the last of this two-part project, Darren Yates looks at setting up the servo motor leg positioning and modifying the source code for our four-legged walking robot.
Stompy is one of the simpler and more compact four-legged walking robots you can build, which makes him a great, low-cost learning tool for understanding a little of the vast world of robotics.
In part 1 of this project, we covered the major pieces — how to put together the chassis and how to assemble the electronics — but now we're looking at some of the more complex parts, including how to get the leg assembly right, wiring up the special DC converter board and how to modify the code to change Stompy's walking speed. Oh, and show off Stompy's new trick.
Positioning the leg servo motors
While some of Stompy's electronics might have seemed a little tricky, the first step before you get there is to make sure you get those servo motors mounted correctly. You have to position the motor assemblies that form each leg so that they have their arc of movement in the right area.
Looking at the front legs from above, the left-front hip — the servo motor attached to the main body — has to swing through an arc from about 1 o'clock counter-clockwise through to 6 o'clock, using a clock face analogy.
The right-front hip needs the same arc in reverse — from about 11o'clock clockwise through to 6 o'clock. If you've built Stompy correctly, the front-right hip segment should run into the battery holder at about 4:30; that's OK because the front leg never swings down that far. The point is to just make sure the hip arc is roughly where it should be.
Not all legs will swing down this far, but they need to come close.
As for the rear legs, the rear-left hip will also run into the battery holder on its front-going swing, but it should still arc from about 10:30 counter-clockwise to about 5:30. The rear right hip should arc through from about 1:30 clockwise to 6 o'clock.
The feet are relatively simple to position : each foot segment should arc so that the standing position is halfway through its travel.
The hip movement arc should start with the hip motor fully forward.
So how do you tell this? You should be able to grab the hip segment and slowly (and gently) move it around while the power is turned off. If the segment motor doesn't want to move, don't force it — the plastic nylon gears in these servo motors will strip easily if you yank them.
If the movement arc is wrong, unscrew the motors, rotate the header arm until the motor moves the right way and screw it back into place. And don't worry if the legs don't look symmetrical — since they're made up of eight identical pieces, they'll look a bit odd, but they'll work perfectly well.
Loading the sketch
The sketch for Stompy is available from the APC website at apcmag.com/arduino.htm. It contains all of the libraries and code you need. The contents of the 'libraries' folder needs to be copied across to the 'libraries' folder of your Arduino IDE installation. And you must do this before you start the IDE, otherwise you'll need to restart it before the libraries will be loaded.
As for programming the Arduino board, all you need to do is load the sketch, compile it and upload it onto the Arduino through the Arduino IDE. This will get you going at a basic level, but you may find the leg segments still need some alignment.
This block diagram shows how Stompy works.
Provided you've followed the basic movement arc guidelines above, the legs might not be perfectly aligned, but they should be reasonably close. What you'll likely find is that depending on the positioning of those motors, Stompy's standing position might be out of alignment.
Setting the standing position alignment
After you've loaded up the sketch into the Arduino Uno board, turn on Stompy's power switch and the first thing that should happen is Stompy stands to attention, with all hips at right angles to his body, feet pointing straight ahead, ready to begin walking.
As soon as he's in the 'attention' position, turn the power off. Remember how we said that it didn't matter if your motor positioning wasn't millimetre perfect? Here's how you fix it.
In the Stompy sketch, there's a header file called 'constants.h'. It should be the second tab on the IDE when the Stompy file is loaded. There are eight constants here, which are the centre positions for each leg segment.
The letter code is: 'F' for front, 'R' for rear, then 'L' for left, 'R' for right, and finally 'H' for hip and 'F' for foot. For example, 'FRH' stands for 'Front Right Hip'. By changing the value of each constant, you can adjust the standing position of each leg segment.
Make the constant number larger and it should move the servo motor further up; make it small and it should go down. The simple trick is to adjust them until you get the motors shifting so that Stompy is in perfect alignment — feet pointing straight ahead and the hips at right angles to his body direction.
Another tip is to carefully hold Stompy as the legs move and watch how his feet move. You want them to just fractionally curl under his body, so when he stands, the legs will just bow slightly under the weight and he'll stand straight. Again, you do this by adjusting the appropriate numbers in the 'constants.h' file.
The 'constants.h' code provides adjustment for leg alignment.
Stompy's new trick
Since I finished the first part of this story, I've been playing around with the sketch. These servo motors come with plastic gears and they get warm, but more importantly, they won't take much rough treatment to wreck.
If you do happen to break one, it's no major drama — they're basic 9g servos you can buy on eBay for around $3 including shipping.
However, I wanted a way to preserve the life of these motors a bit, so after every 20 steps, Stompy will demonstrate his new trick: he'll squat down and sit on his hind legs like a well-trained pooch.
While seated, he'll spin his head and have a bit of a look around. After about 10 seconds, he'll spring back up, count to three and continue on his way. The idea is to just provide some downtime for the servo motors, so they're not full-tilt all the time.
Setting up the DC voltage converter
As we mentioned last month, a quadbot like this would normally use two batteries: one for the Arduino and another for the servo motors. The reason is the latter work over a limited voltage range that's too low to reliably power up the Arduino board.
To fix it, you have separate batteries. We've come up with an unusual solution that allows us to drop the second battery and have just one battery power the whole thing. That's what the little postage-stamp sized DC-DC converter board solves — it provides a constant, regulated 5VDC power supply for the Arduino board from the battery that's (mostly) independent.
The only chore is you need to solder a wire from the 'OUT+' output of the DC converter to the Arduino's '5V' pin. It shouldn't be too hard to do and only needs cheap hookup-grade wire — just keep your soldering iron tip clean, tin the wire (twist the wire strands and give them a lick of solder beforehand) and it should solder quickly and easily.
Solder a wire from the DC converter's '+' pin to the bottom of the Arduino Uno board's '5V' pin.
If you're nervous about heat damaging the Arduino chip, use your needle-nose pliers and lift the DIP chip out of the IC socket, do the solder joint and put the chip back the way you found it.
Before you power things up, make sure you remove the little jumper header next to the DC input terminal block on the Sensor Shield. This electrically separates the Arduino from the Sensor Shield so that the Arduino can be powered by the DC converter and the Shield by the batteries directly without conflict.
Unfortunately, there are two types of this Sensor Shield board available: one without the jumper and one with. You need the one with the jumper, otherwise there's no way to electrically separate the voltage rails and without that, it won't work. Thankfully, most are now the jumpered type, so make sure you get one of these.
Changing Stompy's walk speed
Stompy's gait is designed for speed. Basically, the left-front and right-rear legs take a step as a pair, just as the right-front/left-rear pair do. For more stability, there's a more complex gait you can try that moves one leg at a time, but it involves you leaning Stompy to the opposite corner of the leg you want to move.
That's to shift the weight and allow Stompy to balance on three legs while he moves the other. This gait is more suited to rocky terrain and as you'd expect, it's quite a bit slower. We haven't coded it here and we'll leave it as an exercise for you to try if you're interested.
The simpler option for changing how fast Stompy walks is to simply change the speed or more precisely, increase or reduce the delay time between each leg segment move.
Every 20 steps, Stompy will now sit on his hind legs to rest his motors for 10 seconds.
This is the part of Quadbot designer Russell Cameron's original code we've kept intact because it was brilliantly simple. It works by using a series of small delays between each leg movement that's controlled by a single integer variable called 'time', which you'll find right up at the front of the Stompy sketch.
By default, the variable is set to 90 (as in 90 milliseconds). To speed up Stompy's walk rate, you just reduce the number in that variable down to 80 or 70. To slow him down, increase it to 100 or 110. However, don't reduce it too much — try to make Stompy walk too fast and he'll probably blow a hip joint trying to keep up with what his 'brain' is telling him.
The servo motors can only move so fast and you need to keep the delay time long enough to allow for the full extension of the foot or hip movement to be reached to ensure he takes proper steps. That takes a minimum amount of time — try changing the 'time' variable yourself and see what effect it has. Of course, the other thing is the faster you go, the quicker you'll drain the battery.
Set the walk speed by adjusting the 'time' integer variable in the sketch.
And that brings us onto something we didn't talk about last time: battery life. With a set of four 2,000mAh NiMH cells, Stompy should get around for at least an hour, possibly more, depending on how much walking into walls you make him do.
Basically, the more physical resistance the motors are subjected to, the more power they pull and the more likely they are to be damaged if you do it long enough.
The DC converter and the low power consumption of the Arduino Uno board itself ensures the servo motors will slow down long before the Arduino board shuts down through lack of supply voltage.
The DC converter should continue to work until its input voltage from the battery drops down to around 2VDC — by that time, the four NiMH cells will be as dead as a doorknob anyway and the motors long stopped.
Battery cell voltage also tends to fall off a cliff as the battery is exhausted, so you'll probably find Stompy just stops dead somewhere in the middle of his travels.
And remember, don't use standard 1.5V alkaline cells in Stompy — the higher combined 6.4V (most cells are 1.6V initially) voltage from these cells will swamp the DC converter and potentially apply too much voltage to the Arduino's ATMEGA328P microcontroller chip and damage it.
Building your own
Of course, there's nothing stopping you from using our sketch as the basis for your own walking robot made out of whatever parts you wish. Keep in mind that the code is designed for what are called two degrees of freedom (2DOF) designs like ours, where each leg is made from hip and foot leg segments.
Remember, the Arduino Uno has a limited number of digital outputs — if you want a six-legged 3DOF spiderbot, for example (which requires 18 outputs minimum), you'll need to consider another microcontroller, such as the larger Arduino Mega.
This $5 Sensor Shield interacts between the Arduino and the servo motors.
One fairly simple way you could improve the existing design is to swap out the plastic-geared servo motors for metal-geared ones. This would give Stompy considerably more strength as he stands and walks and even give him better endurance.
However, to use the existing plastic chassis and leg mounts, you'd have to make sure those metal-gear servos fit the leg screw mounts. We'll leave that up to you.
The good thing is that using Arduino puts you in the designer's chair. You can bend, mould and shape the project to fit your own imagination, since you have the source code to muck around with yourself.
Simplifying the design
You could also simplify the design by just getting rid of the neck servo motor and mounting the ultrasonic sensor directly to the front of the body plate. This will affect Stompy's ability to see obstacles just off his eyeline — if they're not right in front of him, he won't see them, so chair legs and the like will be a problem.
You could also use two batteries if you wish: a 9V battery to power the Arduino (plug it in via the DC socket — you can by battery snap-to-plug adapters on eBay for $2) and the four AA cells to handle the servo motors.
The benefit of this is you could then use standard alkaline cells if you wish and you can ditch the DC converter board. Personally, I prefer the single-battery solution, but the choice is yours. None of these changes require any modification to the sketch code.
I'm continually amazed at just what you can do with an Arduino Uno board and its tiny 32KB of programming space. And we're not done yet.
Over the next few series, we'll start taking a closer look at how to network an Arduino Uno board. We'll look at how you can control it wirelessly using a smartphone and we'll even get your Arduino talking to the world via Twitter.
Yep, we'll show you how to get your Arduino its own Twitter account, use the new Twitter REST 1.1 API and automatically send tweets. If you're struggling for useful ideas on what you could do with a self-tweeting device, take a look at my Arduino account (@IAmArduino)
SHIELD JUMPER SET