April 2009 Archives

Arduino multiple serial disappointment

| No Comments
Tonight I took a look at the 'software serial' support in the Arduino. I can't say I'm that impressed...

The Arduino has a built in hardware UART (a hardware serial port) which works as expected. It allows you to talk to external serial devices but there's only one of it so if you want to have an external device talk to you via a serial port and then you want to talk to something else via another serial port then you're out of luck. Serial ports (at the microcontroller level) are, essentially, just about sending a set of signals of a pre-agreed duration out of an I/O pin. This is where the software serial library for the Arduino comes in (by the way, ignore the page you find if you google for "software serial arduino",you want this one, not that one. The software serial library allows you to send and recv serial data on any of the Arduino's I/O pins, or so they say... This would be handy if you're already using the built in UART for something else.

Unfortunately the devil is in the timing when you're doing serial communications. Whilst the Arduino can manage to send data reasonably well it's slightly more challenged to receive it. Especially if you want to do something with it once you get it. The problem boils down to timing. The signals on the serial line don't wait and so if you're waiting for data and then doing something with that data you're likely to miss any other data that comes in whilst you're processing the data you just got... Since I come from a PC background I'm used to my serial data being buffered for me and, well, it just working. 

In summary the software serial code for the Arduino will likely work nicely if you define an appropriately anal protocol between the sender and receiver. You, the Arduino on the software serial port need to tell the other end when you're ready to receive data. You then need to receive a known quantity of data and once you have it the sender can't send any more until you've processed the data you have and then give it the all clear to send some more. Ideally, you need a flow control line between you and the sender for it to work reliably or you need to drive the software serial recv code from an interrupt and buffer the inbound data (and even then you'd need the flow control line to be reliable given data that exceeds your buffer space). Now, this is all possible to do but takes up another I/O pin for the flow control line and I'd need to write code to do it as the software serial library doesn't support it out of the box....

Anyway....

What I thought I could do is switch to simply sending from the software serial port (which is less likely to be affected by me doing other things, as I can't do other things whilst I'm sending!) and send and recv on the hardware UART... Unfortunately this evenings tests seem to imply that this approach is also unreliable... 

So, right now I am relying on the fact that both serial connections that I require are uni-directional, I'm receiving from the PC and sending to the servo controller... Because of this I can use the hardware UART for both links and use it half-duplex on each. However, this is hardly ideal...

One good thing about tonight is that this evening's journey has exposed me to how cute the Arduino's hardware support libraries are. You simply drop some C/C++ code in a directory and it gets built automatically and made available to the Arduino development GUI. Creating new libraries looks stupidly easy and patching existing ones is just a case of editing the source files...  A very nice interface for people who know how to customise the environment and an equally nice interface for those who don't.
I've been testing the new leg with one power supply for the servos and one for the electronics and all is working well. I expect that all would be working well if I were using a single power supply for both, as long as I was using the new one I soldered up and not the old one... 

I decided that since I would be using a 5v power supply for most of the bits of this project that I'd grab and old mains transformer and wire up a 5v regulator and run from the mains rather than batteries. Since this took up a chunk of space on my breadboard I decided to make it a little more permanent on some strip board. 

PowerSupply1.png
I pretty much followed the Sparkfun power supply tutorial with a little artistic license for the 'front panel' and so had a PTC resettable fuse in there to limit the current in case of short circuits, etc. Anyway, it was the PTC that was causing the problem with the servos. It was doing what it should but the servos were trying to pull more current than it would allow and so it was cutting the power (and resetting the servo controller). 

PowerSupply2.png
Anyway, once I realised what I thought was going on I slapped the multimeter into the circuit and watched the PTC cut out as the servos pulled more current than it allowed. 

The new power supply is pretty much the same as the old one without the PTC (I'll rely on the protection in the voltage regulators for this one) and with a 3.3v output as well as a 5v output.

PowerSupply3.png
The servos run fine now and the current draw is within the spec of the regulator. One more small piece of knowledge acquired...

Books for programming an AVR micro

| No Comments
So far I've bought three AVR programming books. AVR: An Introductory CourseProgramming and Customizing the AVR Microcontroller (Programming and Customizing Microcontrollers) and Atmel AVR Microcontroller Primer: Programming and Interfacing (Synthesis Lectures on Digital Circuits and Systems).

They are three quite different books and each has a distinct focus. I read AVR: An Introductory Course first. It's a slim volume that teaches you how to program the AVR family of micros with assembly language. It's a good book and the focus is very much on learning how to program the various facilities available and how to use the various programming constructs to get work done. It has an interesting style whereby the author will explain some machine instructions in the course of solving a programming problem and he'll then ask you to provide the next 'x' lines of the program under construction. There are answers at the end of the book. I haven't yet worked through the book in 'answering the questions' mode, but I expect it has a very good effect on how you learn the assembly language under discussion. The book starts with the simpler microcontrollers and later shows you how to simplify the earlier code that you wrote by using the more advanced facilities available with the more advanced processors. It's a good book and it's a real hands on introduction to the subject. 

I read Atmel AVR Microcontroller Primer: Programming and Interfacing (Synthesis Lectures on Digital Circuits and Systems) next and found it a disappointment. Again it does what it says on the cover, but compared to the other two books it's too simplistic and 'hand wavey' to be a great deal of use. It also seems to be a bit of a cut'n'paste job from a more general 'microprocessor' text book. All in all I could have done without this one.

Programming and Customizing the AVR Microcontroller (Programming and Customizing Microcontrollers) is another good one. The initial sections explain the facilities available and the later sections contain code to help you interface the AVR family with various other devices. After that there are a series of projects with schematics, design rationale and code. The book comes with a CD which contains data sheets and a host of information that you could download, some programming tools and code from the book.  

Once more with feeling

| No Comments
Having decided that the graduated servo positioning method was the way to go to allow for leg sensors to work correctly I started thinking about leg sensors.

Here's a new lower leg with three switches, one on the foot, one to the inside of the leg and one to the outside of the leg.  ProtoLegv02-1.png
ProtoLegv02-2.png
Ideally the front and back switches would be extended with thin metal bars running the length of the leg and fixed just above the ankle. There should also be two more switches to spot obstructions to the left and right. This would give feedback on the full range of motion of the lower leg.

For now, three switches are enough. The foot switch can detect if we're touching the ground and the other two can be used to ensure that we cater for multiple switches per leg (I can see we'll need to multiplex these into whatever chip we have dealing with the sensors or we'll need  30 inputs just for a set of 5 switch legs).

The next thing to do is to move the 'fine control of movement' code out of the Windows Leg Controller app and into the Arduino. The Windows app will then send the required end servo positions to the Arduino which will deal with breaking this down into a sequence of moves and send those to the servo controller whilst monitoring the switches. If a switch triggers then we can stop straight away. If we get to the end and the foot switch hasn't triggered then we know that the leg isn't grounded...

Meanwhile the Leg Controller can be adjusted to deal with the inverse kinematics issues so that we can initially feed into the GUI to move the foot to X,Y,Z and have it calculate the appropriate servo angles required. 

More thoughts on the proto leg

| No Comments
I adjusted my quick and dirty servo controller program to calculate the number of moves required for the servo that needed to move furthest and then spread the movement of the other servos across the same number of moves. So, given that the servo controller works in terms on a position from 0 - 254 where 0 is full left and 254 is full right and we have three servos that make up the leg I look at the differences between where the three servos are now and where they will be at the end of the move and then build a sequence of moves to get from here to there.

For example, if we're currently at 10, 20, 30 and we need to move to 30, 30, 30 then the second servo would move once for every two moves of the first servo and the third servo wouldn't move at all. The sequence being (10, 20, 30), (11, 20, 30), (12, 21, 30), etc. rather than simply (10, 20, 30), (30, 30, 30)... This works well and makes the movement much smoother. 

Of course, once I had that working I realised that things MUST work that way to allow for sensors on the legs to have any affect at all. If we just said move to 30,30,30 and the leg hit an obstruction halfway through the movement then even if we had a sensor that triggered when we hit the obstruction we wouldn't be able to do much about it as we a) wouldn't know where the servos were at the point at which we touched the obstruction and b) couldn't stop the servos in time to avoid potentially damaging them. With the smoothed sequence of moves we would, hopefully, get feedback in time to stop the servos and move them back a couple of steps in the sequence to hopefully take the pressure off. We'd also know where we were and so could plan accordingly from this point on.

Unfortunately firing lots of servo commands into the servo Pololu controller caused me some problems. The controller would randomly misread the commands and/or crash. Disconnecting the power supply from the motors fixed the problem so I guess the rapid commands to the motors was causing noise on the power supply (which is currently shared between the electronics and the motors) which was causing something to malfunction. Looks like the servos will need their own power supply sooner than I was expecting... 
This week I managed to scrape together some time to play with some of the non prototype bits and pieces that I purchased for this project. First off was breadboarding a MAX232 chip so that I could talk via my PCs serial port to my ATtiny2313 microcontroller. The MAX232 does the required level shifting and talks RS232 signals on one side and TTL signals on the other. The result is that it makes it possible to connect a microcontroller's internal UART to a PC. I jumped into this particular sub-project early last week and didn't bother with breadboarding the circuit because it was so simple; I soldered up a nice little bit of strip board with the MAX232 socketed on it and some little TX and RX LED indicators. Unfortunately I made several "schoolboy errors" and the board didn't work. Due to lack of tools, frustration and impatience I eventually managed to break a couple of legs off of the MAX232 chip as I wrestled with repeated removal and re-insertion of the chip whilst debugging the board with a multi-meter.... Grrrr! 

The errors were:
  • Not reading the MAX232 spec sheet correctly and putting the wrong capacitors in because I was following a tutorial that used a slightly different chip which needed different caps (I don't think this was fatal as the smaller caps would simply have reduced the max baud rate of the chip that I had).
  • Not connecting the ground from the RS232 cable correctly - this caused all manner of weird behaviour that was probably the main cause of my problems.
  • Attempting to be clever and add the TX and RX LEDs in the 'obvious' way. Unfortunately I didn't realise that the TTL level serial lines were held high when inactive and so my RX LED was permanently lit. I'd put the TX LED in back to front (Doh!) and so that never lit. Both LEDs were wired in to expect to source current from the MAX232 and sink it to ground. All in all, completely wrong :(
I only had a single MAX232 so breaking a leg was pretty fatal, though I did manage to debug further and breadboard a new circuit using a bit of wire conveniently pressed into the place where the leg had sheered off from...

Once I got some new chips (and a chip leg straightener, and insertion and removal tools, and a respect for how fragile they are, and some patience), I breadboarded the circuit again. This time with the proper caps, a correctly grounded RS232 cable and no LEDs to distract me. This worked fine. Unfortunately I'm out of stripboard so I can't build the circuit in a more permanent manner. I also, eventually, worked out that the LEDs had to be the other way around and had to sink current to the MAX232, so they now work properly (this link has a schematic that clearly shows how the LEDs are set up, I'm pretty sure that several other schematics that I found either had the LEDs sourcing current from the TTL lines or I'm just an idiot who sees what he wants to see). To test this circuit I hooked it up to the servo controller (even though the controller has the option to run at TTL levels or RS232 levels; I used the TTL level connections and put my own level shifting circuit in between it and my PCs serial port).

Next on the list of things to do was to put together a programmer for the AVR chips I have. I purchased a serial PG1 cable and so just had to follow this tutorial and adjust for the fact that I have a different chip. This was more successful and pretty much straight after putting the circuit together I could download code to the chip. Unfortunately it wouldn't run. It turned out that I had put the reset switch in incorrectly and it was holding the chip in a permanent reset state. Once that was fixed I had a blinking LED and a working C Compiler and development tool chain. Wooo! Now all I need to do is get the assembler working as well and I'm in business.

Thoughts on the proto leg

| No Comments
The prototype leg has some fairly major failings from a mechanical point of view. The only part that may eventually find its way into a weight bearing leg is the lower leg where the cut out around the knee servo provides ample support for the servo itself and also a suitable mounting point for the servo. The hip servos need much more support before they could be used to form a weight bearing leg either in the form of metal brackets or more complexly cut board. I expect that, at some point, I'll need to fabricate some metal brackets that have a bearing on the back, opposite the servo horn so that the servo's shaft doesn't need to take the weight of the leg and/or body. Ideally the skeleton of the leg should be self supporting with the servos needed only to move and hold positions.

Lynxmotion produces a 'servo erector set' (details here) which are brackets for standard sized servos that allow you to put together limbs and other contraptions with relative ease. I can see that the main issue with wanting to build my hexapod from scratch is initially not going to be working out how to design the various pieces that I need but actually fabricating them. There's a nice set of custom machined aluminium brackets on show here. I wonder how hard it is to get that kind of thing made for you in the UK...  

Anyway, I'm a fair way off from needing such things. The next thing on my list of things to do is to build the custom servo controller electronics and firmware. What became immediately apparent from using my quick and dirty servo controller program is that the sequences of servo moves that need to be made to move a limb from one position to another need to be blended so that all of the servos that are taking part can be moved at their own speed. My controller can easily send the three servos a set of position changing commands to move the foot from point A in space to point B in space but the end position, whilst important, isn't all that there is to it. If each of the servos has to move a different distance and you just tell them all where you'd like them to end up then they'll all complete their movement at different times. This works, but is rather, well, robotic to watch. I immediately decided that the controller should allow me to input the 'key frames' on a movement sequence and then be able to work out the intermediate points to move the servos along the way. Rather than using a single command to move each server from A to B I need a sequence of timed commands that move from A to B via a1, a2, etc. where the sequence for servo 2 takes into consideration the number of 'time slots' that it will take servo 1 and servo 3 to compete their moves. Taking this one step further and scaling to multiple legs the blending could be continued between legs to convert a synchronised, robotic, tripod gait into a much more natural 'ripple'. If the servo controller were to be doing this blending in real time then it could add an element of randomness into it which would probably result in an even more realistic movement (or something horrible). Interestingly this seems to be similar to the approach taken by the guys at Lynxmotion with their SEQ-01 servo sequencer product; so at least I seem to be thinking along the right lines.

Then there's the whole question of where to move the foot to and how knowing that position in 3d space we can calculate the required positions of each of the servos that make up the leg. That's where inverse kinematics comes in; see here for the details and the maths.

Prototype leg v0.1

| No Comments
I spent a little time at the weekend putting together my three servos into a prototype leg. The idea is that this very quick and very dirty little experiment should give me some more avenues to explore; and something to show people.

I had originally planned to build the leg out of wood, perhaps thin plywood, but I didn't have any laying around so instead I used some plastic window board which is relatively light and strong. This was easy to machine (cut with either a hacksaw or a hobby knife) and easy to drill with a drill bit in a hand driver. The results are indeed just as quick and dirty as I was expecting. 

ProtoLegv01.png
As you can see I cut a slot in the lower leg to mount the knee servo, I also shaped the lower leg so that the foot was small. The knee servo's "horn" is then screwed on to the thigh (which I didn't bother to shape at all) and once that's done the servo horn and thigh can be connected to the knee servo. The other end of the thigh has another servo horn fitted, this time on the other side (this is less than ideal from a mechanical point of view but allows for more mistakes whilst controlling the servos as the lower leg can't impact on the hip servo). The top of the thigh is then attached to one of the hip servos. The two servos that form the hip are currently screwed on to a small piece of plastic board, it's not very clever but it just about holds the two motors in the right place (with the addition of an elastic band or two) to allow for the correct three degrees of movement that I wanted. The two hip servos are set at right angles to each other to give rotation in two planes. The result isn't especially sturdy and probably couldn't take a great deal of weight but is enough for now. It is, after all, just a quick and dirty prototype...

After connecting the servos to the servo controller I threw together a matching quick and dirty C++ application to allow me to send servo control messages to the controller. This allows me to experiment with the movement available by setting each of the servos to particular positions. The controller also allows me to record a series of positions and then replay them to the controller. This results in very simple movement as can be seen from the video below...

The servos are twitching

| No Comments
My package from Cool Components arrived this morning. My choice of prototyping equipment was quickly validated when I plugged together the Arduino and the Pololu servo controller board, plugged in the servos and servo power supply, connected the lot to the pc via a usb cable and had three servos twitching back and forth under the control of the Arduino is no time. The idea is that I can now begin fabricating a leg, connect up the servos and then experiment with leg movement without needing to get the soldering iron out. The prototype hardware also gives me a reference platform to compare to my own servo controller hardware once I get going on that side of the project.

Also in the package were my XBee modems and several AVR microprocessors to play with; more on those items later...
First some basics: servo motors are what I'll be using to provide movement for the robot. Each leg will consist of at least three servo motors (1 at the knee and two at the hip). As you'll see from the wikipedia link above, servo motors are generally controlled by pulse width modulation. In order to control multiple servos you need to generate a continuous PWM control signal for each servo. 

Many modern microcontrollers have built in PWM generation capabilities but the number of channels is limited. For example the Atmel AVR ATtiny2313 has 4 PWM channels available. For a hexapod robot with 'standard design' legs each utilising 3 servo motors we need 18 channels. If we want to add a movable head (for sensor targeting, or mandibles) then we could add another 3 servos at least, the list goes on. However, we don't have to use the built in PWM facilities of the microcontroller, we could generate our own PWM signals and use any available I/O pin to output the signal; this means we can generate as many channels as the microprocessor has suitable I/O pins... This method is demonstrated here on a, now obsolete, Atmel AT90S4414. An alternative is to multiplex the available PWM channels to create the required amount of channels, we can do this because hobby servos generally require at most a 2.1ms pulse length with a 50Hz (20ms) refresh. Depending on the clock speed of your microcontroller the PWM signals you can generate are probably much faster than are required for a single servo, so by adding a 74xx138 to the output of each PWM channel from the micro and explicitly switching their channels and the pulse we're generating during the micro's PWM cycle we can create a number of PWM channels running at around 50Hz from a single PWM pin on our micro that runs at a faster refresh... Or so the theory goes.

The current plan for servo control is to multiplex an ATtiny2313's 4 PWM channels using multiplexers into a number of channels. Each micro should be controllable via a TTL serial interface from another controller (or a PC) and, since, potentially we'd need multiple ATtiny's, the serial link should be transparently chainable (that is each micro should only act on requests directed to it and should ignore all others...).  

And then we add another micro that talks to these servo controllers and deals with walking and the required gait to move the legs...

Of course, to get things going, I'm quite happy to use someone else's servo controller to get things moving, so to speak...

Useful links and suppliers

| 2 Comments
So far most of my more interesting components have been purchased from Cool Components mainly because they're UK based and they stocked stuff I was interested in. I found them via SparkFun which is a treasure trove of fun; unfortunately I can't find UK distributors for much of the stuff they have available and at present I'm trying to avoid buying bits and pieces for this project from the US as the shipping is often more expensive than the order. Some of the less interesting electrical components have come from Maplin but Maplin doesn't really have the range that I need. I toyed with ordering from Farnell but found I was getting carried away with their bounteous selection (and, there was always at least one item that they needed to order in from the US and that incurs an additional shipping charge...). Digikey is useful for working around the limitations of Farnell's search system (I do an initial search on Digikey and then, once I have a selection of exact component numbers, I do a search on Farnell) and also for datasheets for stuff that you can't find on Farnell. Also worth a mention is Dannell.4you on ebay which was useful for picking up some hard to find items.

Other places of interest and sources of ideas include...

Servos with status feedback

| No Comments
I saw these Dynamixel DX servos mentioned on Trossen Robotics this morning, see here. They're interesting because they have a richer interface than the normal hobby servos that I've been looking at and they provide a multidrop RS485 interface so that you can chain multiple servos together and control them individually via a single line. You can get feedback on the angular position, velocity and torque and also alerts if temperature and voltage deviate from expected (user specified) ranges. I expect that the torque feedback would be useful to help with determining if the foot is in contact with the ground and also should enable you to detect if the leg is being obstructed during movement which should make moving over uneven surfaces easier to program. All of this wonderfulness is not without a price though, these babies are around $200 each...

Micromagic systems

| No Comments
Micromagic systems do robotics and animatronics for the film and tv industry and have quite a nice selection of hexapod robots. It's especially interesting to see the evolution of their robots here. Their latest stuff is very impressive and puts into perspective how much I have to learn...


Their robotics store (http://www.hexapodrobot.com) is also full of useful information.

A-pod, an ant inspired hexapod

| No Comments
This is the kind of thing that I mean when I say 'hexapod robot'. I particularly like the head and mandibles. 




Welcome

| No Comments
Hi, my name is Len Holgate and this is a blog about my new project to design and build a hexapod robot; a mechanical vehicle that walks on six legs...

By day I design and build high performance TCP and UDP servers for the Windows platform, my C++ technical blog is here: www.lenholgate.com. Up until a few weeks ago I'd never done any electronics or embedded programming; I decided that I needed a new hobby and so started tinkering with electronics, breadboarding and the like and, in what tends to be my typical approach to such things, I decided that the best way to push myself into this new hobby was to dive in to reasonably complex project. I started to put together a list of things that I wanted to include in the project and the list looked something like this:
  • the end result should be an autonomous mobile robot that isn't tethered to a PC and isn't controlled by an operator.
  • all electronics and control gear should be hand made from components rather than bought in (though I'm happy to use bought in things to prototype with as this will speed up the learning process, I hope...).
  • all mechanical components should be hand made from components rather than bought in.
  • communications between the robot and a PC and between the robot and other robots. 
  • some form of 'vision', most likely simple sonar or range finding.
  • microprocessor controlled; programmed in C or assembler, I'm a C++ guy at heart and, for  as long as I care to remember the platforms that I've targeted have been powerful and complex. I quite fancy learning about how to work on more limited platforms and perhaps even learning a decent amount of assembly language...
Initially the robot was to be wheeled but then I started seeing walking hexapod robots on the web and, well, they're just way cooler than something with wheels and, quite possibly, way more complex both mechanically and from a programming perspective. I realise that the project is likely to take some time and that I have a lot to learn, but that's the point. Like most projects I expect to break things down into manageable chunks and given the scope of the overall project the chunks should be quite varied. Ideally I'd like to run off and play with the communications side of things when I get bored fighting with servo motors and the mechanical side of things, etc.

So here we are. I'm currently gathering components for the project. So far I have decided to use an Arduino as my simple prototyping platform along with some AVR micro controllers for the main robot control gear. Of course my choice of AVRs may change once I have played around with programming them but they seem like a reasonable choice at this point. I've got three servo motors on order (HS-422's) so that I can start work on a simple leg. I have a couple of XBee radio modules on the way as these look to take the pain out of the communications side of things but I may drop back to using simple RF transmitters and receivers if the XBee's are too heavy or power hungry. 

So, off to build a robot then...

About this Archive

This page is an archive of entries from April 2009 listed from newest to oldest.

May 2009 is the next archive.

Find recent content on the main index or look in the archives to find all content.