Creating a program in Linux to control the mobile platform

Now that you can control your servos using basic commands, let's control them using a program.

Prepare for lift off

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.

Engage thrusters

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:

Engage thrusters

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.
  • The PololuMicroMaestro class 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 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.
  • The last method, close, closes the serial port.
  • Now that you have the class, the __main__ part of the program instantiates an instance of your servo motor controller class so 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 not exactly centered, so I found on my robot that I needed to set each servo to the values shown in this program to be lined up so my robot had both feet on the ground and both hips centered.

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:

Engage thrusters

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.

Objective complete – mini debriefing

Once you have the program working, you'll want to package all your HW 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. Here is what my robot looks like:

Objective complete – mini debriefing

Classified intel

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

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

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