Krabos - walking robot

In this tutorial I will describe construction of a six-leg walking robot in enough detail so that everyone with basic experience with electronics and computer programming can build his own robot for cost less than $70.

 Krabos - walking robot

The aim was to create walking robot which is more attractive than wheeled machines and its programming offers interesting problems to solve not only on the higher level of autonomous operation but also on the low level of microcontroller programming - which is useful for people who want to learn how to program microcontrollers and have some fun along the way. The building cost should be very low, and that’s why design with only three servos is used. There are designs which use two or even three servos per leg, which gives the robot more maneuverability, but makes it quite expensive. The 3-servo walker described here is a good compromise between price and performance. It can not only walk forward but also turn and walk backward.

The robot is able of autonomous operation controlled by its microcontroller or it may be directly commanded from a personal computer which makes it possible to experiment with even very complicated movement algorithms.

Described robot is built from a scratch, from “raw” materials and electronic parts.

The mechanical construction is easy; it does not require any electric tools except a small drill. The electronic is also simple using single printed circuit board which can be drawn by hand. It would be even possible to make it on a bread board. All that is needed is a soldering iron and little experience with this kind of work.

 

The communication program for PC is written in C# using Microsoft Visual C# Express development environment. If you are not familiar with C# you will still be able to play with the robot and write simple algorithms. 

 

Both the MCU programming and PC programming are interesting areas which one may sooner or later find himself studying in a less interesting way, so why not start now with this project? Building your own robot is not as difficult as it may seem and it will bring you great satisfaction when you see it’s really walking. Just try it!

 

Here is overview of Krabos' features:

The tutorial is divided into folowing parts:

- Control board design

As mentioned earlier Krabos uses its own controller board. The heart of it is Atmel Tiny 2313 microcontroller (further referred to as MCU).

This MCU can be programmed via In-system programming (ISP) interface using very simple programming cable instead of relatively expensive programmer - as will be described later.

The MCU itself actually requires no other parts to run. However, the controller board provides some additional functionality which makes it little more complex. Feel free to omit any parts in the design you do not consider important. You can choose not to use the parts for RS 232 connection to PC if you want to experiment just with the autonomous robot without direct control from PC.

 

The board can be divided into the following parts:

 

Power supply (5V)

 

This uses standard 5V, 1A integrated voltage regulator 7805. 5V is the optimal voltage for most of the components, including the MCU. Also the servos are powered from the output of this regulator. It would be possible to power the servos directly from 4 NiMH or NiCd cells and use the regulator only for the electronics, but connecting the servos to stabilized 5V makes it easy to power the robot also from commonly available DC adapters (9 or 12 V), which would not be possible otherwise, as the maximum allowed voltage for servos is typically only 6V.

On the prototype no problems with noise from the servos in the power supply were noticed in this configuration.

Power from DC adapter is connected to the standard DC connector. Power from battery is connected to the pin header with 2 pins (2.5 mm distance) next to the DC jack. My LiPol battery has just the right connector to attach to this.

 

Microcontroller

 

The MCU uses virtually no external parts. Its ISP pins are connected to a pin header on the board and programmer cable can be connected to this pinhead to update the program in the control board without removing the MCU or switching any jumpers.

 

 

Serial communication interface

 

To allow control from PC, serial interface (RS232) support was included using MAX 232 circuit to convert the voltage levels between MCU and the PC. The board uses standard 9-pin female connector so that is can be connected with PC using standard male-female serial cable. There is also pinhead output intended for communication with another MCU board which could contain another (more powerful) microcontroller to serve as the brain for autonomous operation. This is not used at this stage.

 

Sensors

 

There are 2 touch sensors on front of the robot which are connected directly to the MCU pins INT0 and INT1. The pins have internal pull up resistors enabled and when the sensor it activated it pulls the MCU pin to the ground. The sensor itself is composed of a short conductive pipe and steel wire which is placed in the center of the pipe. The pipe is connected with the ground of the control board. The wire is connected with the input pin of the MCU. As the wire bends and moves when it touches an obstacle, it will come into contact with the pipe thus pulling the MCU pin low. Interrupt will be generated by the MCU and the program can respond to this situation (obstacle in front of the robot). Hints for design of such sensor can be found here [8].

 

 

Low voltage detector

 

The board is also equipped with detection of low supply voltage using analog comparator available in the MCU. This is useful if the robot is powered by LiPol or LiOn battery (as the prototype is). Those batteries are sensitive to deep discharge which may damage them. When the system detects that voltage of the power supply is below certain level, the robot stops and flashes LED to indicate this state.

Voltage from the power supply (before voltage regulator of course) is divided by resistor divider so that when it is low (e.g. 7V), the voltage on the AIN1 pin of the microcontroller is about 1.1 V. The other input of the comparer is connected to MCU’s internal reference which has nominal voltage 1.1V with tolerance +/- 0.1V. The exact voltage which will trigger the low power state of the robot is adjusted with trim R2. For battery of 2 LiPol cells I use 7V as the trigger level.

 

 

LEDs and switch

 

There is one push switch and two LEDs on the board to allow some degree of direct control without PC. In the included firmware the robot can be started and stopped by the switch and LEDs indicate obstacle detection, low voltage state etc.

 

Schematic for the board in Eagle [7] can be downloaded from the Downloads section as well as prototype PCB layout. Please note that the PCB on prototype was drawn by hand and the PCD design is adapted for this purpose. The connections are wide and lot of wire links is used. If you will be making the PCB using more sophisticated method or have it made somewhere, you can design the board much smaller and neat.

 Controller board schematics

Fig. 3.1 - Schematics of the controller board

To download the schematics and PCB design in Eagle format plus the image of the schematics in full size click here.

 Plan of the control board

Fig. 3.2 – Plan of the control board

 Photo of the control board

Fig. 3.3 - Control board attached to the robot

 

Connections

In the figure above you can see the sensors connected with blue, green and white cable to pinhead connector on the board. The signal wires for servos are also connected using pinhead connector on the board. Power cables of servos are connected with screw clamp (it’s the bunch of wires to the left of the DC power jack at the rear of the robot).

As mentioned above the board is really messy. It was just a prototype, you know...

 

Putting the board together is not too hard. The MCU should be in a socket rather than soldered to the PCB. You may want to put the MAX 232 into socket also.

 

- Controlling the robot from PC

For controlling the robot from PC a Windows program was developed in C# including class which encapsulates the control of the robot. With this class it should be fairly easy to implement your own interface for controlling the robot if you know C#. For those not familiar with this programming language there is an option to write their own algorithms in easier way as will be described later. All that is needed is Microsoft Visual C# Express, which is free. [9].

PC program window

 

 

Fig. 5.1 – Application for controlling the robot from PC

 

This program allows executing all the supported commands. Source code is provided in the Downloads section.

 

 

 

Communication protocol

 

Sending a command to the robot means writing single byte to serial port. The value of the byte to send can be calculated from the values of commands as defined in ServoDriver.H but in C# program you can use the RobotCommander class provided in the sample application. This class provides methods such as WalkStraight, TurnLeft, etc.

Some commands require input parameter, such as speed for the walk command. Value of the parameter is also contained in the command byte. Each command including its parameter is only one byte wide. The lower 3 bits of the byte are reserved for parameter value. Higher 5 bits encode the command.

 

The robot sends data over serial line only as an answer to commands “Get sensor state” and “Get robot state”. The data consist of 1 byte. The returned value is unique. For example, value 1 could mean left sensor is active, value 2 - left sensor is not active, value 3 - right sensor is active, and so on. This allows the PC program to send command to the robot asking for data and receive the answer asynchronously in a separate routine.

 

 

Description of the PC control application

 

The included application Robot Control serves 2 purposes:

You can use it to experiment with the robot from PC including command at very low level such as setting position of servos. But you can also write your own algorithm for the robot in and run it from this application (with the Run Program button).

 

You can use the application as an inspiration for your own program written in C# which can control the robot in any way you want. Using the RobotCommander class this is quite an easy task if you have some experience with C#.

 

How to use the application

 

 

 

Run program button

When you click this button an autonomous algorithm will be started which controls the robot from PC. The default algorithm provided here is the same as the one which is “hard coded” into the MCU program. But you can change it easily if you open the project in Visual C# and edit RobotProgram.CS file. More details on this follow.

 

 

 

Writing your own movement algorithms

 

There are two options for writing your own program to control the robot from PC:

  1. Write your own program in C# using the RobotCommander class

  2. Write just the algorithm and let the sample application execute it using the Run Program button.

 

Actually, there is a third option – to write your own program in any language. You just need to be able to handle serial communication in this language and look up the codes which you need to send to the serial line in ServoDriver.H.

 

For option 1 you need to know C#. You may want to have a look at the description of the classes available for controlling the robot below.

 

For option 2 you don’t need to know C# even though you will be actually writing a C# program. The sample application includes Run Program button which executes a method (function) into which you can write your own code. Obviously you need to have some understanding of programming (basic knowledge of C is a big plus), but you do not need to know what is a class, nor anything about object oriented programming (which scares some people). All you will be doing is writing simple sequence of commands.

 

Here is how to do it. I assume you have Microsoft Visual C# Express edition [9] installed and that you played a little with it so that you know how to open a project and build it.

 

This is the function which will be executed when you click Run Program button in the main window. Into this function you can write your program to control the robot. See the sample program which is already there¨. It is in fact PC version of the simple autonomous algorithm programmed in the MCU of the robot.

The names of the functions are understandable I hope, so just a couple of notes:

 

When execution reaches the end of the Move function (the closing brace “}”), your program ends. To start it again you need to press the Run Program button again and it will start over from the first line of Move function.

 

When you give the robot some command, e.g. GoStraight(); it will start walking and it will walk until you tell it to do something else (for example to stop). The same applies to turning and walking back)

 

You can use loops (such as while or for) to prevent your program from ending. For example, most of the code of the sample program is in the following loop:

 

while ( !ShouldStop())

{

CODE

}

The function ShouldStop() returns true if user requested your program to stop – clicked the Stop Program button which appears once the program is started. So, the sample code makes the robot walk (and try to avoid obstacles) until you click the Stop button.

 

To find out if there is any obstacle in front of the robot, use GetLeftSensor() and GetRightSensor() methods. Both return true if the sensor was signaled (touched something). Note that once the sensor touches something and becomes signaled, it will remain signaled until you reset it by calling ResetLeftSensor() or ResetRightSensor() method. This means that you will learn about an obstacle even if your program was busy doing something else in the very moment when the sensor touched it. But don’t forget to reset the sensor otherwise it will all the time report obstacle even if there isn’t any no longer.

 

The sample program in fact does nothing more than commanding the robot to walk straight and then checking the sensors in a loop until one any of them is signaled. Then it responds using this simple logic: if left sensor was signaled, turn right a little, and then continue walking straight. If right sensor was signaled do the same but with left turn. If both sensors are signaled at the same time, walk back a little, and then turn left and then walk straight again.

It is not very smart but it’s up to you to give your robot more IQ!

 

 

 

Description of the C# classes for robot control from PC

 

Note that you do not need to read this unless you know C# and intend to write your own programs in C# for controlling Krabos.

The C# interface for the robot is composed of 1 “high level” classs and some other helper classes which you do not need to use directly:

RobotCommander – this is the main class which provides high-level functions to control the robot (e.g. GoStraight) and to check for its state (e.g. GetLeftSensor).

 

RoboCommandInfo – This helper defines various symbolic names for robot speed, servo position etc. It also stores information about one command, such as whether the command requires parameter and of what type this parameter is. You do not need to use this class except when using the symbolic names for speed etc.

 

RobotCommands – This class allows obtaining instance of RoboCommandInfo class for each available command and also sending data to serial port and translating received data to symbolic values.

 

In the following text the RobotCommander class will be described in details:

 

Public properties:

 

RobotCommandInfo.RobotSpeed Speed

Defines the current speed of the robot. With this speed the robot moves when you call GoStraigh() or any other of the movement functions.

 

bool LeftSensorSignalled

bool RightSensorSignalled

Gets the state of the sensor. True means sensor is signalled. It will send the appropriate command to the robot to get fresh value and return the result. If timeout occurs during the communication (500 ms), it will return last value or false if no value was ever read.

Setting the property to false resets the sensor, making it ready to signal another obstacle. Setting it to true throws exception!

 

RobotCommandInfo.RobotState RobotState

Gets the state in which the robot is now. State is read from the robot using the appropriate command.

 

 

RobotCommandInfo.ServoPosition Servo1Position

RobotCommandInfo.ServoPosition Servo2Position

RobotCommandInfo.ServoPosition Servo3Position

Sets position of the specified servo. The position is one of the values defined in RobotCommandInfo.ServoPosition enumeration. Possible values are A, B and Center.

 

 

 

Public methods

 

RobotCommander(int serialPortNumber)

Creates instance of the RobotCommander class. Input argument is number of the seriál port (COM) to use for communication with the robot, e.g. 1 for COM1, 2 for COM2, etc.

Note that it will attempt to open the specified port and throw ArgumentException if it fails.

 

RobotCommander(System.IO.Ports.SerialPort port)

Creates instance of the RobotCommander class using given SerialPort object. The port must be already opened before it is passed to this constructor!

 

 

bool GoStraight()

bool GoBack()

bool TurnLeft()

bool TurnRight()

These methods set the robot moving in the required way. They return true is the command has been sent to robot successfully, false if not.

The robot moves with the speed specified in the Speed property, Medium speed by defuult.

 

bool Stop()

Stops the robot. The robot will stop with all servos in Center position.

 

 

bool GoAuto()

Puts the robot into autonomous mode walking forward. It will respond to sensor inputs by turning etc.

 

 

bool GetLeftSensor()

bool GetReftSensor()

Same as using the xSensorSignaled properties. Methods return false if there is error in communication as well as if the sensor is not signalled

 

 

bool QuerySensor(RobotCommandInfo.SensorCommandData sensor)

Call this method to ask the robot to report state of its sensors without waiting for the answer. You must handle the SerialPort DataReceived event to receive the answer asynchronously and then use TranslateReceivedByteToSensor method to decode received data.

RobotCommandInfo.SensorCommandData sensor – the sensor which state robot should report.

 

bool QueryState()

Call this method to ask the robot to report its state without waiting for the answer. You must handle the SerialPort DataReceived event to receive the answer asynchronously and then use TranslateReceivedByteToState method to decode received data.

 

 

bool TranslateReceivedByteToSensor(byte data,

out RobotCommandInfo.SensorCommandData sensor,

out bool active)

This method takes one byte of data received from serial line and translates it into information about sensor state. It can be used to receive answers from the robot asynchronously (after sending command using QuerySensor method).

byte data – data which the robot sent.

RobotCommandInfo.SensorCommandData sensor – which sensor state is in the data (if any).

bool active – state of the sensor, true means signalled, false nonsignalled.

Returns: true if the byte of data contained information about sensor state, false if it did not.

 

 

bool TranslateReceivedByteToState(byte data,

out RobotCommandInfo.RobotState state)

This method takes one byte of data received from serial line and translates it into information about robot state. It can be used to receive answers from the robot asynchronously (after sending command using QueryState method).

byte data – data which the robot sent.

RobotCommandInfo.RobotState state – receives state of the robot which was decoded from the data (if available)

Returns: true if the byte of data contained information about robot state, false if it did not.

 

- Design of the robot

My primary inspiration for the design was [1]. The source does not provide many details of the mechanical construction and dimensions so I had to experiment but it wasn’t hard. The drawing of my final design is below. The dimensions depend on the size of the servos used. I used standard size servos which yields robot about 12 cm long and 9 cm wide. It would be interesting to use smaller servos and make a tiny “spider” robot.

 

I choose to develop my own controller board instead of using a commercial one, such as BasicX. This was mainly because of my desire to get some experience with Atmel microcontrollers and also to cut down the price. Typical controller costs about 100 USD while the Atmel Tiny 2313 MCU costs about 4 dollars and with all the other parts the price of the board is still below 15 USD.

 

 

Basic theory

Don’t worry, I don’t like theory any more than you do, so this will be just brief description of the things you could find helpful.

 

The servos

 

Servo is used primarily in radio controlled models (airplanes, cars, ships etc.) but they are also very popular in robotics. The advantage is wide choice of sizes and strengths, low price and easy control with microcontrollers. There are tiny servos weighting some 5 grams but also huge ones with metal gears able to lift several kilograms on its lever.

 

Cheapest servo can be obtained for about $10 and for our purpose it will suffice. The prototype robot uses Hitec HS300 servos which are about 10 years old, have served their duty in various radio controlled airplanes and survived several crashes, yet they still work very well. From today’s market Hitec HS311 can be a good choice, but you can also use any of the less-known brands without problem.

Important servo parameters are:

Standard size servo is 40x20x36 mm and weights about 50 grams. There is cable with 3 strands, usually red (+), black (ground) and yellow or orange (signal for the servo).

Position of the servo lever is controlled by input signal which should have frequency of 50 Hz and pulses with length between 1 and 2 ms. change of the pulse length changes the position of the servo lever. See the picture:

 

 Servo movement - theory

Fig. 1.1 - Control of servo with pulse-width modulated signal

 

 

The mechanical calculations for the servos in this kind of robot can be found in [1]. For me it was sufficient to find out that the robot with 10 years old servos walks. How long the servos will survive is a different question, but it doesn’t seem to me that they would be overloaded when the robot is walking.

 

 

- Download links and references

Download Links for Krabos

 

Drawings – larger size.

Schematics of the control board (Eagle 4.16 format).

Program for the microcontroller.

Program for PC – setup.

Program for PC – source.

  

 

References

  

[1] Pivot, walking robot; at http://home.wanadoo.nl/m.hotze/robotproject/

[2] In System Programming (ISP) for ATMEL chips, By Ibrahim Kamal, Available at http://www.ikalogic.com/isp.php

[3] ISP Programmer by Adam Dybkowski (software), available at http://www.ikalogic.com/phpBB3/download/file.php?id=4

[4] Pony prog – serial device programmer. at http://www.lancos.com/prog.html.

[5] Atmel products – AVR studio development IDE. Available at http://www.atmel.com/products/AVR/

[6] WinAVR - C development tools for Atmel AVR microcontrollers. http://winavr.sourceforge.net/

[7] Eagle – software for PCB design. Available at http://www.cadsoft.de/

[8] Cricket – the robot, Available at http://home.earthlink.net/~henryarnold/

[9] Microsoft Visual C# 2008, express edition. Available at: http://www.microsoft.com/express/vcsharp/

 

YouTube Videos

 

Krabos walking slow

 

Walking fast

 

Obstacle

 

 

 

 

 

- Mechanical construction of the robot

The design is best described by the drawing and the pictures.

Main frame is composed of two wooden poles (1) connected by plywood panels (2). The side-legs servos are connected to two aluminum sheets (4), which are connected to the wooden poles by four M3 screws. The center servo is attached to aluminum block (3) which is held together with the sheet (4) by the same screws which attach the sheet to the poles (1).

It would be better to construct the body from aluminum profile rather then wood, but wood was just at hand when I needed it.

The legs are made of aluminum (dural) sheet 11 x 2 mm which I bought in Hobby market OBI. One meter of this sheet costs about 2 Euro.

drawing1

Fig. 2.1 - Drawing of the robot frame

drawing 2

Fig. 2.2 - Drawing of the robot legs 

 

You can download the drawings in full size here.

 

Instructions

First cut the wooden poles (1) to desired length. Then cut the plywood panels (2), shape them and finish with sand paper. Drill the holes for the wooden sticks.

Apply glue on the sticks and/or holes in the panels and put it all together. Make sure the poles are parallel and the panels are perpendicular to them.

 

Measure and drill the holes in the wooden poles for the traverse sheets (4). Then measure (or mark through the holes in the poles) the holes onto the sheets (4) and drill them. Make sure the servos will fit on the sheets without much free play! The dimension 42 mm is just approximate.

Fix sheets (4) to the poles with M3 screws.

Put the two side servos on the sheets and mark the holes for screws which will attach the servos to those sheets.

Detach sheets (4) and drill the holes. If you have the equipment, you may drill holes 2.5 mm and then cut M3 threads instead of using nuts. If not, just drill holes 3.2 mm and use nuts.

 

The center servo is attached to aluminum part (3), about 6x10x45 mm which I found in my storage. Alternatively, you may use piece of aluminum U-profile which may be easier to get. The part (3) is attached to the front sheet (4). The dimensions are not critical, it is only important that it can hold the center servo in proper position in the center of the frame. See the drawing and photos. Drill the holes for the servo now.

 

Attach the sheets (4) (also with the part (3)) to the frame with M3 screws.

 

Cut the aluminum sheets (5) and (6) for the side legs. You need four pieces of legs. The numbers 5 and 6 are used just to distinguish the “top” and “bottom” leg. On each side of the robot (that is, attached to each side servo) is one “top” and one “bottom” leg. See the drawing - round servo “lever” is number (7) on this drawing. Before bending the legs I suggest you make a model legs from carton and try them out on the frame to be sure that you know which way to bend the real legs. When ready, bend them in vice according to the drawing.

 

Legs 

Prepare round “levers” for the side servos. These you should get with the servos - there are usually several levers included in the box with a servo. If you do not have such a round “lever” for your servos, you will need to improvise with attaching the legs. If you have a lever with four arms, this will work well too.

 

Drill holes at the ends of the legs (5) and (6) – but JUST THE HOLE AT THE END of each leg - and put the legs onto the servo lever (7) as shown on the drawing. Drill ONE hole through (7) for the screw at the ends of legs and attach the legs temporarily with M3 screw. Mark the positions for the other 2 holes on (7) and drill them. These holes should be located so that there is enough material between the holes and edge of (7) and also there is enough material on both sides of the legs. Mark the position of these holes on the legs (5) and (6) through the holes in (7) which you just drilled.

 

Detach the legs and drill holes in them. Put the legs with part (7) together. Note which leg is up and which down as can be seen on the drawing! Do not tighten the screws too much.

There could be pieces of small pipe or similar under the higher leg to keep the distance from part (7) but it will also work without it – as on my robot. Just don’t over-tighten the screws.

 

Prepare the middle leg (9). This is quite a complicated construction. Feel free to use your own imagination to simplify it. Here is how I build mine.

Bend the leg sheet (9) to shape using vice and some piece of wood or metal to help you maintain the center space. Cut small hole on side of the top of the leg for the servo lever (8) – see drawing.

Prepare part (10) from aluminum, drill hole into the leg (9) and then put the part (10) in place and mark the position of the hole onto it. Drill it taking care to keep it straight.

Attach the servo lever (8) and mark position of hole which will be used for screw attaching the lever to the leg. Be sure not to put it through the traverse screw (for which you just drilled a hole through (9) and (10)) but little above it!

Drill the hole and put it all together using M3 screws.

 

Connect the servos to the robot frame and then connect the leg assembly to each servo. If you have a servo tester or similar tool, you can set the servos to neutral position before doing so, but it is not necessary at this stage.

 

Put the robot on flat surface and bend the legs as needed so that they all touch the ground and the robot is straight. Be careful not to put force on the servos. For small adjustments you can use two pairs of pliers – one to hold the leg and second to bend it. If bigger adjustment is needed, don't be lazy to remove the lever from the servo and bend the leg in vice.

Check if the legs can move freely. They should be arranged so that the top leg (5) on one servo (e.g. right-side) is above the lower leg (6) of the second-side servo, see the drawing and photos. Don’t worry if the top and bottom legs are slightly touching each other at some place. As long as they are easy to move (which should be, as the aluminum sheet is smooth), it's fine.

Be careful when moving the servos by hand. For standard size servos this is usually not a problem but some miniature servos have the gears constructed so that it is not possible to move them from outside by pulling the lever. They can be moved only by the motor from inside and if you pull too much on the lever you can break the gears easily. So in case you feel strong resistance, do not push the servo lever. You can check the free movement later when we have the controller board ready.

The mechanical construction is finished for now.

 

Bottom view of Krabos

Fig. 2.3 – bottom view of the robot

 Krabos - center leg

Fig. 2.4 – Detail of the center leg and servo attachment

 Krabos - leg attachement

Fig. 2.5 – Detail of the side legs attached to the servo

 Krabos - front view of legs

Fig. 2.6 – Legs of the robot. Front view.

 

Part list for mechanical construction (see the drawings for Frame and Legs)

 

  1. Pole (wooden stick, diameter 10mm) - 2 pcs

  2. Panel (plywood 5 mm thick) - 2 pcs

  3. Center servo holder (aluminum block) – 1 pc

  4. Side servo holders (aluminum sheets 11x2 mm) – 2 pcs

  5. Side legs, bottom (aluminum sheet 11x2 mm) – 2 pcs

  6. Side legs, top (aluminum sheet 11x2 mm) – 2 pcs

  7. Servo lever, round, standard accessory for servo – 2 pcs

  8. Servo lever, straight, standard accessory for servo – 1 pc

  9. Center leg, (aluminum sheet 11x2 mm) – 1 pc

  10. Center leg block, (aluminum) - 1 pc

  11. M3 screws and nuts

 

- Programming the microcontroller

The brain of the robot is made of Atmel Tiny2313 microcontroller. This MCU has a cool feature called In-System Programming which can be used to program memories of the MCU with almost-no-cost programmer. I used simple programmer which connects to parallel port. It is described in [2] and its schematic is below. The 74HC08 integrated circuit, which is the only part of this programmer besides the connector, costs about 50 cents. Everything can be built directly into the 25-pin LPT connector without any PCB.

programmer schematics

 

Fig. 4.1 – Schematics of a simple Atmel AVR programmer for parallel port

 

On the microcontroller side of the cable, I use pinhead female connector with my own layout as shown in fig. 4.2. Note that the cable should not be longer than about 50 cm.

 

ISP connector 

Fig. 4.2 – ISP connector for the controller board

 

 

 

The programmer works with free software called “ISP Programmer”, which is available at [3].

If parallel port is not available on your computer, you can use PonyProg software [4] with interface for serial port. The programming device is also very simple.

Note that if you use laptop with USB to Serial converter PonyProg may run very slow (10 minutes for programming the memory) – so this is not a good choice. I did not find any solution for this yet, but luckily my laptop has a parallel port and my old PC has both serial and parallel port. The simplest solution would be to buy some commercial Atmel programmer for USB, such us ISP MK II, which can cost about $40.

For controlling the robot from PC, USB to Serial converter works fine so you can use laptop with USB port and a converter.

 

The program for the MCU is built in AVR Studio, which is free IDE provided by Atmel [5]. AVR studio allows writing programs in assembly language only, but there is free set of tools including C compiler called WinAVR [6] available. WinAVR integrates into AVR studio seamlessly, so it is possible to write programs for the MCU in C.

 

So to sum it all up, the program for microcontroller is written in C and built with WinAVR tools integrated into AVR studio. First, download and install AVR studio. Then install WinAVR and it will integrate into AVR studio automatically. Then download and open the project with source code for Krabos. 

 

 

Description of the source code

 

The source code for MCU consists of the following files

ServoDriver.C – main source file, includes the main() function

ServoDriver.H – definitions

RoboCom.C – functions for robot movement and serial communication

 

The source file can be downloaded here (including AVR Studio project).

The main function performs initialization and then falls into infinite loop.

This loop does different actions based on the robot state. Possible states include Walk, TurnLeft, Stop (do nothing), etc.

In every pass through the loop one step is performed. What the step is depends on the state. For example, in Walk state one step is one complete set of movements of the servos which makes the robot do one step forward.

The state of the robot can be changed either by pressing the switch on control board or by a command from serial line. The switch can only toggle state between stopped and autonomous movement. When controlled from PC, a command arriving through serial line generates interrupt and interrupt handler routine changes the state of the robot according to this command. The change is recognized by the main loop during the next pass, so it may take some time before the robot responds to the command.

 

 

The bump sensors generate interrupt when activated (when robot touches an obstacle). The interrupt handlers only change state of the sensor in global variable (gLeftSensor or gRightSensor) and disable further interrupts from the sensor. Main loop can respond in the next pass. If the robot is in autonomous mode it will stop and turn etc. If controlled from PC, it is responsibility of the PC software to check for sensors and respond accordingly. Software controlling the robot from PC can read the sensor state by sending Get Sensor State command to the robot. The robot will send back information about the sensor (whether it is activated or not).

 

PWM signal for servos can be generated using 2 timers available in the MCU (each timer has 2 PWM channels). This option was tried first and works fine. It is still available in the source code if you uncomment the PWM_VER1 directive in ServoDriver.C.

However, using the timer-based PWM has some disadvantages because of which the current version of the software generates PWM in a different way and the PWM_VER1 directive is commented out.

The problem with “native” PWM generated by the timer is the resolution of the timer counter, which is only 8 bits. The period of the PWM is thus divided into 256 parts. The period should be 20 ms for the servo. The active level of the signal has however length of only 1 to 2 ms, where 1.5 ms is neutral position of the servo lever. So for the full servo movement we have 1 ms length, which is 12 units of the counter. In other words, we may change the servo position in only 12 steps across the whole movement range of the servo lever (about 60 degrees). This does not leave much room for adjusting the center position or movements of the legs. In my experiments, I ended up changing the position only by 1 unit +/- to move the legs into their position during walk.

That’s why I now use more software-based PWM which works as follows:

In timer overflow interrupt handler, a “software” counter is incremented. When this counter reaches 400, the PWM active signal starts – outputs are set to 1. Each servo has its own variable which holds the desired length of the pulse (from 20 to 40). This variable is compared with the counter and when equal, corresponding output pin is set low.

For speed and size purpose the software counter is implemented as two 8-bit variables rather than one 16 bit. When first counter reaches 256, the second starts incrementing and when it reaches 144, current period is ended and both counters reset.

The prototype robot walks fine with change of +/- 3 units, which is actually not much better than the completely timer-based PWM, but the advantage of this method is also that it allows for easy tuning of the PWM frequency (50 Hz), while the first method depends on combination of MCU clock and timer prescaler value, which are rather coarse (1, 8, 64, 256,..). The prototype with timer-based PWM used CPU clock 0.5 MHz and the PWM frequency was 30 Hz. Higher frequency in general leads to faster servo movement but also to bigger current drawn from the source battery and more heat on the voltage regulator.

 

 

Serial communication uses UART circuit integrated in the MCU, which makes it very easy. The UART is set up during initialization in main() and Rx complete interrupt is enabled. This interrupt is then generated whenever byte of data is received from the serial line.

Note that the communication speed depends on the CPU clock. It can be changed in the BAUD_RATE constant in ServoDriver.h. Preset speed is 9600, with 2 stop bits and no hardware control. The routines for initializing serial port and sending character are taken from the MCU documentation samples.

 

Current version of the program occupies 100% of the flash memory so, unfortunately, there is not much room for experiments. However, the design supposes that the Tiny2313 MCU will serve only as a servo driver and sensor converter. The actual movement algorithms should be implemented either on PC or on a separate “high-level” brain board which will send commands to the “low-level brain” through serial line.

 

Possible improvements of the MCU program (implemented probably instead of the simple autonomous algorithm which occupies memory space but is of little practical use) could be:

Saving trim values for servo position to EEPROM and adjusting them via serial line.

 

How to load the program to the MCU

 

I assume you have the control board ready, checked as described earlier and your AT Tiny 2313 is installed in the socket of this board. Parallel port programmer with the related software is used. If you use other programmer, you will need to modify some steps but in general the procedure will be the same.

Do not connect the servos to the board yet.

Programming

 

 

 

 

If no error appeared, your program is now in the MCU but we still need to change so called “Fuse bits” to configure the MCU clock frequency.

 

Now you can make sure the program works.

 

If it works as described, we are ready to connect the servos.

BUT IF YOU HAVE NOT set the neutral position of the servos earlier with servo signal generator; detach the levers with legs from the servos before connecting the servo cables to the control board!

This is because the servos probably will not be in neutral position and the legs could bump into each other as they move when the signal from MCU sends the servos to neutrals.

So after you have connected the servos and turned the power back on, they may move at first, but then they should remain in the same place. Little buzz is not a problem. It may be sign of power supply which is not able to provide enough current. That is why the robot should be powered from accumulators or powerful enough DC adapter (1 A should be fine). If you are using power supply with limited current, don’t forget to turn off the limitation!

 

If you now press the push-switch, the servos should start to move. You will more hear it than see it if you have detached the legs. Stop the robot by pressing (and holding) the switch again. Wait for the servos to stop moving! Do not turn the robot off by disconnecting the power. This would not stop the servos in neutral positions!

Now disconnect the power. Servos are in neutrals and we need to put the legs on them so that they are also in neutral. It is possible to attach the servo lever in different positions but the adjustment is not as fine as you could expect. So you may not be able to find the correct neutral position for legs. This is fine. Just try to attach them as close to neutral as possible. We will fine-tune (trim) the position in the software. BUT be careful not to move the servo as you push the lever on it.

 

With the legs attached connect the power again. The legs should not move much if you managed to put the levers on servos without disturbing the neutral position.

Now press the push-switch. The legs should start to move. If there are no collisions, put the robot down and watch it walk! Enjoy!

You may stop it and start again with the push-switch. Always hold the switch down until the LED goes on or off. Each time the robot is started, it increases the speed. When maximal speed is reached, it will start over again from slow speed.

 

If the legs seem to bump into each other it may have two reasons:

1 – Servo travel is too long and the legs cannot move that far. This is easy to fix in the software by changing the A and B values in the ServoPosition enum.

2 – Servos may move in reverse direction. It is possible that servos of some brand respond in opposite way than servos on the prototype. In this case exchange the A and B values in ServoPosition enum in the program.

 

 

Troubles with programming the MCU

 

If the ISP Prog software does not recognize the MCU (it displays “bad signature” status), the

Parallel port of your computer may need to be configured in BIOS. See the readme.txt file which comes with ISP Prog for instructions.

If BIOS does not solve the problem, there may be some bug either in the programmer cable itself or in the control board.

Measure the voltage on Reset pin of the MCU (pin 1). It should be 5V when the reset signal is off in the program. When you click the Reset On button, there should be 0V on the Reset pin. If it is not, the problem is probably in the programmer cable.

If Reset signal is ok, check that the MOSI, MISO, CLK and Reset pins of the MCU are connected with the ISP connector of the controller board.

As a last option you may want to put the MCU into breadboard, connect it to 5V, connect your programmer to the ISP pins and make sure it works. If it does work this way, the problem is not in the programmer cable but in the control board.

For more information and tips on solving problems please see the website [2].

 

 

- Walking algorithm

To figure out the sequence of servo movements which make the robot move was a tough task for me. My imagination betrayed me, so I had to use the robot, moving its legs by hand and taking notes of the movements.

I use the following notation when describing the movements:

The position of servo is A, B or Center. Center is self explanatory. Position A is with the lever of the servo moved counter clockwise from the center, B is moved clockwise (looking at the servo from the side on which the lever is attached).

Servos are assigned numbers: 1 – left-hand side servo, 2 – right-hand side servo, 3 – center servo. See the pictures for details:

Position of servos

Fig. 6.1 – Positions of servos

Servos on robot

 

 

Fig. 6.2 – Servos on the robot – top view

 

To present the algorithms in a clear way I use table where the columns mark position of the servo in each step of the algorithm. There is about 0.2 s pause in the execution of the MCU program between steps, so that the servo has enough time to reach desired position.

Don't be suprised if you won't understand the tables just by reading this. You will probably need to try it on the real robot (just like I had to) if you decide to make some changes in the walking algorithms. On the other hand, if you will be satisfied with the way Krabos walks with the provided algorithms, you do not need to spend much time on this "low level" information.

 

 

 

Algorithm to walking forward

 

Step

S1

S2

S3

0

Center

Center

Center

1

Center

Center

A

2

Center

A

A

3

A

A

A

4

A

A

B

5

B

A

B

6

B

B

B

7

B

B

A

8

B

A

A

 

The robot starts from neutral position (all servos at center). Steps 1 and 2 are performed only once when it starts to move. Then the program cycles through steps 3 to 8 to keep the robot moving. Note that at each step only one servo changes its position.

 

To describe the algorithm in words:

  1. tilt right (left side of robots body is higher than right side) – S3 to A

  2. move left legs forward - S2 to A

  3. move body forward (in other word move right legs backward) - S1 to A

  4. tilt left – S3 to B

  5. move right legs forward – S1 to B

  6. move body forward - S2 to B

  7. tilt right – S3 to A

  8. move left legs forward – S2 to A; see step 2

  9. move body forward – S1 to A; see step 3

  10. and so on, step 4, 5,...

 

 

Algorithm for turning left

 

Step

S1

S2

S3

0

Center

Center

Center

1

Center

Center

B

2

B

Center

B

3

B

Center

A

4

A

Center

A

5

A

Center

B

Step 1 is performed only at the start of the rotation from neutral position, then steps 2 through 5 repeat to keep the robot rotating.

 

Algorithm for tunrning right

 

Step

S1

S2

S3

0

Center

Center

Center

1

Center

Center

A

2

Center

A

A

3

Center

A

B

4

Center

B

B

5

Center

B

A

Step 1 is performed only at the start of the rotation from neutral position, then steps 2 through 5 repeat to keep the robot rotating.

 

To rotate left or right the robot uses this simple algorithm (right rotation described):

 

  1. tilt right (left side of the body higher than the right side), left legs up – S3 to A

  2. move left legs forward (they are above ground) – S2 to A

  3. tilt left, left legs back to the ground, right legs up – S3 to B

  4. move body forward; rotates robot body on the left legs to the right – S2 to B

  5. tilt right – S3 to A

  6. move left legs forward and so on – repeats step 2

 

 

Algorithm for walking backwards

 

Step

S1

S2

S3

0

Center

Center

Center

1

Center

Center

B

2

A

Center

B

3

A

A

B

4

A

A

A

5

B

A

A

6

B

B

A

7

B

B

B

8

B

A

B

Steps 1 and 2 are performed only once when robot starts to move. Then the program cycles through steps 3 to 8 to keep the robot moving. Note that in each step only one servo changes its position.