17 min read

In this article by Richard Grimmett, author of the book Raspberry Pi Robotics Projects – Second Edition, we will add the ability to move the entire project using legs. In this article, you will be introduced to some of the basics of servo motors and to using Raspberry Pi to control the speed and direction of your legged platform.

(For more resources related to this topic, see here.)

Even though you’ve learned to make your robot mobile by adding wheels or tracks, these platforms will only work well on smooth, flat surfaces. Often, you’ll want your robot to work in environments where the path is not smooth or flat; perhaps, you’ll even want your robot to go upstairs or over other barriers. In this article, you’ll learn how to attach your board, both mechanically and electrically, to a platform with legs so that your projects can be mobile in many more environments. Robots that can walk! What could be more amazing than this?

In this article, we will cover the following topics:

  • Connecting Raspberry Pi to a two-legged mobile platform using a servo motor controller
  • Creating a program in Linux so that you can control the movement of the two-legged mobile platform
  • Making your robot truly mobile by adding voice control

Gathering the hardware

In this article, you’ll need to add a legged platform to make your project mobile.

For a legged robot, there are a lot of choices for hardware. Some robots are completely assembled and others require some assembly; you may even choose to buy the components and construct your own custom mobile platform. Also, I’m going to assume that you don’t want to do any soldering or mechanical machining yourself, so let’s look at several choices of hardware that are available completely assembled or can be assembled using simple tools (a screwdriver and/or pliers).

One of the simplest legged mobile platforms is one that has two legs and four servo motors. The following is an image of this type of platform:

Raspberry Pi Robotics Projects - Second Edition

You’ll use this legged mobile platform in this article because it is the simplest to program and the least expensive, requiring only four servos. To construct this platform, you must purchase the parts and then assemble them yourself. Find the instructions and parts list at http://www.lynxmotion.com/images/html/build112.htm. Another easy way to get all the mechanical parts (except servos) is by purchasing a biped robot kit with six degrees of freedom (DOFs). This will contain the parts needed to construct a six-servo biped, but you can use a subset of the parts for your four-servo biped. These six DOF bipeds can be purchased on eBay or at http://www.robotshop.com/2-wheeled-development-platforms-1.html.

You’ll also need to purchase the servo motors. Servo motors are similar to the DC motors, except that servo motors are designed to move at specific angles based on the control signals that you send. For this type of robot, you can use standard-sized servos. I like Hitec HS-311 for this robot. They are inexpensive but powerful enough for the operations you’ll use for this robot. You can get them on Amazon or eBay. The following is an image of an HS-311 servo:

Raspberry Pi Robotics Projects - Second Edition

I personally like the 5-V cell phone rechargeable batteries that are available at almost any place that supplies cell phones. Choose one that comes with two USB connectors; you can use the second port to power your servo controller. The mobile power supply shown in the following image mounts well on the biped hardware platform:

Raspberry Pi Robotics Projects - Second Edition

You’ll also need a USB cable to connect your battery to Raspberry Pi. You should already have one of these.

Now that you have the mechanical parts for your legged mobile platform, you’ll need some hardware that will turn the control signals from your Raspberry Pi into voltage levels that can control the servo motors. Servo motors are controlled using a signal called PWM. For a good overview of this type of control, see http://pcbheaven.com/wikipages/How_RC_Servos_Works/ or https://www.ghielectronics.com/docs/18/pwm.

Although the Raspberry Pi’s GPIO pins do support some limited square-wave pulse width modulation (SW PWM) signals, unfortunately these signals are not stable enough to accurately control servos. In order to control servos reliably, you should purchase a servo controller that can talk over a USB and control the servo motor. These controllers protect your board and make controlling many servos easy. My personal favorite for this application is a simple servo motor controller utilizing a USB from Pololu that can control six servo motors—Micro Maestro 6-Channel USB Servo Controller (assembled). This is available at www.pololu.com. The following is an image of the unit:

Raspberry Pi Robotics Projects - Second Edition

Make sure you order the assembled version. This piece of hardware will turn USB commands into voltage levels that control your servo motors. Pololu makes a number of different versions of this controller, each able to control a certain number of servos. Once you’ve chosen your legged platform, simply count the number of servos you need to control and choose a controller that can control that many servos. In this article, you will use a two-legged, four-servo robot, so you’ll build the robot by using the six-servo version. Since you are going to connect this controller to Raspberry Pi through USB, you’ll also need a USB A to mini-B cable.

You’ll also need a power cable running from the battery to your servo controller. You’ll want to purchase a USB to FTDI cable adapter that has female connectors, for example, the PL2303HX USB to TTL to UART RS232 COM cable available at www.amazon.com. The TTL to UART RS232 cable isn’t particularly important; other than that, the cable itself provides individual connectors to each of the four wires in a USB cable. The following is an image of the cable:

Raspberry Pi Robotics Projects - Second Edition

Now that you have all the hardware, let’s walk through a quick tutorial of how a two-legged system with servos works and then some step-by-step instructions to make your project walk.

Connecting Raspberry Pi to the mobile platform using a servo controller

Now that you have a legged platform and a servo motor controller, you are ready to make your project walk! Before you begin, you’ll need some background on servo motors. Servo motors are somewhat similar to DC motors. However, there is an important difference; while DC motors are generally designed to move in a continuous way, rotating 360 degrees at a given speed, servo motors are generally designed to move at angles within a limited set. In other words, in the DC motor world, you generally want your motors to spin at a continuous rotation speed that you control. In the servo world, you want to limit the movement of your motor to a specific position. For more information on how servos work, visit http://www.seattlerobotics.org/guide/servos.html or http://www.societyofrobots.com/actuators_servos.shtml.

Connecting the hardware

To make your project walk, you first need to connect the servo motor controller to the servos. There are two connections you need to make, the first is to the servo motors, and the second is to the battery. In this section, before connecting your controller to your Raspberry Pi, you’ll first connect your servo controller to your PC or Linux machine to check whether or not everything is working. The steps for doing so are as follows:

  1. Connect the servos to the controller. The following is an image of your two-legged robot and the four different servo connections:

    Raspberry Pi Robotics Projects - Second Edition

  2. In order to be consistent, let’s connect your four servos to the connections marked from 0 to 3 on the controller by using the following configurations:
    •      0: Left foot
    •      1: Left hip
    •      2: Right foot
    •      3: Right hip

    The following is an image of the back of the controller; it will show you where to connect your servos:

    Raspberry Pi Robotics Projects - Second Edition

  3. Connect these servos to the servo motor controller as follows:
    •      The left foot to 0 (the top connector) and the black cable to the outside (-)
    •      The left hip to connector 1 and the black cable out
    •      The right foot to connector 2 and the black cable out
    •      The right hip to connector 3 and the black cable out

    See the following image indicating how to connect servos to the controller:

    Raspberry Pi Robotics Projects - Second Edition

  4. Now, you need to connect the servo motor controller to your battery. You’ll use the USB to the FTDI UART cable; plug the red and black cables into the power connector on the servo controller, as shown in the following image:

    Raspberry Pi Robotics Projects - Second Edition

Now, plug the other end of the USB cable into one of the battery outputs.

Configuring the software

Now, you can connect the motor controller to your PC or Linux machine to see whether or not you can talk to it. Once the hardware is connected, you will use some of the software provided by Polulu to control the servos. The steps to do so are as follows:

  1. Download the Polulu software from http://www.pololu.com/docs/0J40/3.a and install it using the instructions on the website. Once it is installed, run the software; you should see the window shown in the following screenshot:

    Raspberry Pi Robotics Projects - Second Edition

  2. You will first need to change the Serial mode configuration in Serial Settings, so select the Serial Settings tab; you should see the window shown in the following screenshot:

    Raspberry Pi Robotics Projects - Second Edition

  3. Make sure that USB Chained is selected; this will allow you to connect to and control the motor controller over the USB. Now, go back to the main screen by selecting the Status tab; you can now turn on the four servos. The screen should look as shown in the following screenshot:

    Raspberry Pi Robotics Projects - Second Edition

  4. Now, you can use the sliders to control the servos. Enable the four servos and make sure that servo 0 moves the left foot; 1, the left hip; 2, the right foot; and 3, the right hip.
  5. You’ve checked the motor controllers and the servos and you’ll now connect the motor controller to Raspberry Pi to control the servos from there. Remove the USB cable from the PC and connect it to Raspberry Pi. The entire system will look as shown in the following image:

    Raspberry Pi Robotics Projects - Second Edition

Let’s now talk to the motor controller from your Raspberry Pi by downloading the Linux code from Pololu at http://www.pololu.com/docs/0J40/3.b. Perhaps the best way to do this is by logging on to Raspberry Pi using vncserver and opening a VNC Viewer window on your PC. To do this, log in to your Raspberry Pi by using PuTTY, and then, type vncserver at the prompt to make sure vncserver is running. Then, perform the following steps:

  1. On your PC, open the VNC Viewer application, enter your IP address, and then click on Connect. Then, enter the password that you created for the vncserver; you should see the Raspberry Pi viewer screen, which should look as shown in the following screenshot:

    Raspberry Pi Robotics Projects - Second Edition

  2. Open a browser window and go to http://www.pololu.com/docs/0J40/3.b. Click on the Maestro Servo Controller Linux Software link. You will need to download the maestro_linux_100507.tar.gz file to the Download folder. You can also use wget to get this software by typing wget http://www.pololu.com/file/download/maestro-linux-100507.tar.gz?file_id=0J315 in a terminal window.
  3. Go to your Download folder, move it to your home folder by typing mv maestro_linux_100507.tar.gz .., and then go back to your home folder.
  4. Unpack the file by typing tar –xzfv maestro_linux_011507.tar.gz. This will create a folder called maestro_linux. Go to this folder by typing cd maestro_linux and then, type ls. You should see the output as shown in the following screenshot:

    Raspberry Pi Robotics Projects - Second Edition

  5. The document README.txt will give you explicit instructions on how to install the software. Unfortunately, you can’t run Maestro Control Center on your Raspberry Pi. The standard version of Maestro Control Center doesn’t support the Raspberry Pi graphical system, but you can control your servos by using the UscCmd command-line application. First, type ./UscCmd –list; you should see the following screenshot:

    Raspberry Pi Robotics Projects - Second Edition

  6. The software now recognizes that you have a servo controller. If you just type ./UscCmd, you can see all the commands you could send to your controller. When you run this command, you can see the result as shown in the following screenshot:

    Raspberry Pi Robotics Projects - Second Edition

Notice that you can send a servo a specific target angle, although if the target angle is not within range, it makes it a bit difficult to know where you are sending your servo. Try typing ./UscCmd –servo 0, 10. The servo will most likely move to its full angle position. Type ./UscCmd – servo 0, 0 and it will prevent the servo from trying to move. In the next section, you’ll write some software that will translate your angles to the electronic signals that will move the servos.

If you haven’t run the Maestro Controller tool and set the Serial Settings setting to USB Chained, your motor controller may not respond.

Creating a program in Linux to control the mobile platform

Now that you can control your servos by using a basic command-line program, let’s control them by programming some movement in Python. In this section, you’ll create a Python program that will let you talk to your servos a bit more intuitively. You’ll issue commands that tell a servo to go to a specific angle and it will go to that angle. You can then add a set of such commands to allow your legged mobile robot to lean left or right and even take a step forward.

Let’s start with a simple program that will make your legged mobile robot’s servos turn at 90-degrees; this should be somewhere close to the middle of the 180-degree range you can work within. However, the center, maximum, and minimum values can vary from one servo to another, so you may need to calibrate them. To keep things simple, we will not cover that here. The following screenshot shows the code required for turning the servos:

Raspberry Pi Robotics Projects - Second Edition

The following is an explanation of the code:

  • The #!/user/bin/python line allows you to make this Python file available for execution from the command line. It will allow you to call this program from your voice command program. We’ll talk about this in the next section.
  • The import serial and import time lines include the serial and time libraries. You need the serial library to talk to your unit via USB. If you have not installed this library, type sudo apt-get install python-serial. You will use the time library later to wait between servo commands.
  • The PololuMicroMaestro class holds the methods that will allow you to communicate with your motor controller.
  • The __init__ method, opens the USB port associated with your servo motor controller.
  • The setAngle, method converts your desired settings for the servo and angle to the serial command that the servo motor controller needs. The values, such as minTarget and maxTarget, and the structure of the communications—channelByte, commandByte, lowTargetByte, and highTargetByte—comes from the manufacturer.
  • The close, method closes the serial port.
  • Now that you have the class, the __main__ statement of the program instantiates an instance of your servo motor controller class so that you can call it.
  • Now, you can set each servo to the desired position. The default would be to set each servo to 90-degrees. However, the servos weren’t exactly centered, so I found that I needed to set the angle of each servo so that my robot has both feet on the ground and both hips centered.

Once you have the basic home position set, you can ask your robot to do different things; the following screenshot shows some examples in simple Python code:

Raspberry Pi Robotics Projects - Second Edition

In this case, you are using your setAngle command to set your servos to manipulate your robot. This set of commands first sets your robot to the home position. Then, you can use the feet to lean to the right and then to the left and then you can use a combination of commands to make your robot step forward with the left and then the right foot. Once you have the program working, you’ll want to package all your hardware onto the mobile robot.

By following these principles, you can make your robot do many amazing things, such as walk forward and backward, dance, and turn around—any number of movements are possible. The best way to learn these movements is to try positioning the servos in new and different ways.

Making your mobile platform truly mobile by issuing voice commands

Now that your robot can move, wouldn’t it be neat to have it obey your commands?

You should now have a mobile platform that you can program to move in any number of ways. Unfortunately, you still have your LAN cable connected, so the platform isn’t completely mobile. Once you have started executing the program, you can’t alter its behavior. In this section, you will use the principles to issue voice commands to initiate movement.

You’ll need to modify your voice recognition program so that it will run your Python program when it gets a voice command. You are going to make a simple modification to the continuous.c program in /home/pi/pocketsphinx-0.8/src/. To do this, type cd /home/pi/pocketsphinx-0.8/src/programs and then type emacs continuous.c. The changes will appear in the same section as your other voice commands and will look as shown in the following screenshot:

Raspberry Pi Robotics Projects - Second Edition

The additions are pretty straightforward. Let’s walk through them:

  • else if (strcmp(hyp, “FORWARD”) == 0): This checks the input word as recognized by your voice command program. If it corresponds with the word FORWARD, you will execute everything within the if statement. You use { and } to tell the system which commands go with this else if clause.
  • system(“espeak “moving robot””): This executes Espeak, which should tell you that you are about to run your robot program.
  • system(“/home/pi/maestro_linux/robot.py”): This indicates the name of the program you will execute. In this case, your mobile platform will do whatever the robot.py program tells it to.

After doing this, you will need to recompile the program, so type make and the pocketsphinx_continuous executable will be created. Run the program by typing ./pocketsphinx_continuous. Disconnect the LAN cable and the mobile platform will now take the forward voice command and execute your program. You should now have a complete mobile platform! When you execute your program, the mobile platform can now move around based on what you have programmed it to do.

You can use the command-line arguments, to make your robot do many different actions. Perhaps one voice command can move your robot forward, a different one can move it backwards, and another can turn it right or left.

Congratulations! Your robot should now be able to move around in any way you program it to move. You can even have the robot dance. You have now built a two-legged robot and you can easily expand on this knowledge to create robots with even more legs. The following is an image of the mechanical structure of a four-legged robot that has eight DOFs and is fairly easy to create by using many of the parts that you have used to create your two-legged robot; this is my personal favorite because it doesn’t fall over and break the electronics:

Raspberry Pi Robotics Projects - Second Edition

You’ll need eight servos and lots of batteries. If you search eBay, you can often find kits for sale for four-legged robots with 12 DOFs, but remember that the battery will need to be much bigger. For this application, you can use an RC (which stands for remote control) battery. RC batteries are nice as they are rechargeable and can provide lots of power, but make sure you either purchase one that is 5 V to 6 V or include a way to regulate the voltage. The following is an image of such a battery, available at most hobby stores:

Raspberry Pi Robotics Projects - Second Edition

If you use this type of battery, don’t forget its charger. The hobby store can help with choosing an appropriate match.

Summary

Now, you have the ability to build not only wheeled robots but also robots with legs. It is also easy to expand this ability to robots with arms; controlling the servos for an arm is the same as controlling them for legs.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here