Creating a program to control your biped

Now you know that you can talk to your servo motor controller and move your servos. In this section, you'll create a Python program that will let you talk to your servos to move them at specific angles. You can use Python as it is very simple and easy to run.

Let's start with a simple program that will make your legged mobile robot's servos go to 90 degrees (which should be somewhere close to the middle between the 0 to 180 degrees you can set). Here is the code:

Creating a program to control your biped

Here is an explanation of the code:

  • #!/usr/bin/python: This first line allows you to make this Python file execute from the command line.
  • import serial: This line imports the serial library. You need the serial library to talk to your unit via the USB.
  • def setAngle(ser, channel, angle):: This function converts your desired setting of servo and angle into the serial command that the servo motor controller needs. To understand the specifics of the code used to control the servos, refer to https://www.pololu.com/docs/0J40.
  • ser = serial.Serial("/dev/ttyACM0", 9600): This opens the serial port connection to your servo controller.
  • for i in range(0, 9): For loop to access all nine servos.
  • setAngle(ser, i, 90): Now, you can set each servo to the middle (home) position. The default would be to set each servo to 90 degrees. If your legs aren't in their middle position, you can adjust them by adjusting the position of the servo horns on each servo.

To access the serial port, you'll need to make sure that you have the Python serial library. If you don't, then type apt-get install python-serial. After you have installed the serial library, you can run your program by typing python quad.py.

Once you have the basic home position set, you can ask your robot to do something. Let's start by having your biped move its foot. Here is the Python code:

Creating a program to control your biped

In this case, you are using your setAngle command to set your servos to manipulate your robot's right ankle.

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

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