Now that you can control your servos using basic commands, let's control them using a program.
So, you know that you can talk to your servo motor controller and set your servos. In this section, you'll create a Python SW 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, lean right, or even take a step forward.
Let's start with a simple program that will make your legged mobile robot's servos go to 90 degrees. This should be somewhere close to the middle of the 180 degrees you can set. However, the center, maximum, and minimum values can vary from servo to servo, so you may need to calibrate these values. To keep things simple, we will not cover that here. The following is the code:
The explanation of the code is as follows:
#!/user/bin/python
: This first line allows you to make this Python file execute from the command line. This will allow you to call this program from your voice command program. We'll talk about that in the next section.import serial
, import time
: These next two lines include the serial
and time
libraries. You need the serial
library to talk to your unit via USB, and the time
library you will use later to wait between servo commands.PololuMicroMaestro
class 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 setting of servo and angle into the serial command that the servo motor controller needs. The values, such as minTarget
, maxTarget
, and the structure of the communications, channelByte
, commandByte lowTargetByte
, and highTargetByte
, come from the manufacturer.close
, closes the serial port.__main__
part of the program instantiates an instance of your servo motor controller class so you can call it.Once you have the basic home position set, you can now ask your robot to do some things. Here are 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, then to the left, then you can use a combination of commands to make your robot step forward with the left foot, and then with the right foot.
18.188.218.157