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:
The following is an explanation of the code:
#!/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.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.PololuMicroMaestro
holds the methods that will allow you to communicate with your motor controller.__init__
method, opens the USB port associated with your servo motor controller.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.close
, closes the serial port.__main__
statement of the program instantiates an instance of your servo motor controller class so that you can call it.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:
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:
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.
3.16.207.226