Creating a program in Linux to control the mobile platform

Now that you can control your servos 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 or 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:

Creating a program in Linux to control the mobile platform

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 that in the next section.
  • The import serial and import time lines include a serial and time library. 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 class PololuMicroMaestro holds the methods that will allow you to communicate with your motor controller.
  • The first method, the __init__ method, opens the USB port associated with your servo motor controller.
  • The next method, setAngle, 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—come from the manufacturer.
  • The last method, close, 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 were exactly centered, so I found that I needed to set the angle of each servo such 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 some things; the following screenshot shows some examples in simple Python code:

Creating a program in Linux to control the mobile platform

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. There is no right or wrong way to do this, but I like to use a small piece of transparent plastic because it is easy to cut and drill. The following is an image of my robot:

Creating a program in Linux to control the mobile platform

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.207.226