Using a servo to move a single sensor

You now have your sensors in place. If we want to sense more than just one direction, we could use several sensors, each mounted to a different side of the robot. However, there is a way to use servos to move your sensor, which allows you to use a single sensor to sense in several directions.

The simplest way to avoid having to purchase and configure several sensors is to mount the sensor on a single servo and then use a servo bracket to connect this assembly to the platform. Using the sonar sensor, the assembly will look something as shown in the following image:

Using a servo to move a single sensor

Make sure you connect your servo to the servo controller; it can fit into any open connection. I am connecting mine to my quadruped robot that has eight servos to control, so I have connected mine to the eighth connection on the servo controller board, as follows:

Using a servo to move a single sensor

I'll assume that you already have your sensor up and working and know how to read data. In this section, you will add the ability to move the sensor by communicating with the servo through the servo controller we configured in the previous chapter.

For the program, you will begin with the robot.py program you created in Chapter 6, Making the Unit Very Mobile – Controlling the Movement of a Robot with Legs, as you are going to need to access the servo controller. However, you may want to keep a copy of this program, just in case you want to use it later. First, go to the directory that contains the robot.py program; in my case, I placed it in the maestro_linux directory, so I would type cd ./maestro_linux from my login or home directory. Now, let's create a copy of this program by typing cp robot.py sense.py.

You'll want to edit this program. So if you are using the Emacs editor, type emacs sense.py. The program you want to create will look as shown in the following screenshot:

Using a servo to move a single sensor

Let's walk through the code to see what it does. I will begin with the section that begins with if __name__=="__main__": as everything above this comes to us from the robot.py code and is covered in the previous chapter.

  • The robot=PololuMicroMaestro() line initializes the servo motor controller and connects it to the proper USB port.
  • The next line opens a serial port, which we will call sensor, that connects you to the USB sonar sensor at the /dev/ttyUSB0 port and sets its parameters.
  • You can now ask the servo to go to a specific position and take a reading. In this case, I am doing this for the servo positions at 65 degrees, 90 degrees, and 115 degrees. At each of these locations, you can ask for a range reading. Note that you need to wait 2.5 seconds for the sensor to respond, based on the specifications of the manufacturer for the device to deliver a stable reading.

That's it! Now, you can sense objects in front of you and on either side. The following screenshot is an example of what might be displayed as a result of running the program:

Using a servo to move a single sensor

If you are adding the sensor-servo combination to your wheeled vehicle, you'll need to add the servo motor controller as well. The motor controllers, servo controllers, and USB sonar or IR sensor can all coexist on the same Raspberry Pi. You'll need to merge the dcmotor.py and sense.py programs so that you can access each individual capability.

You can also use sensors to find an object by having two sensors that determine the distance of each sensor from the object and then triangulate its position. This can help your robot actually find the position of specific obstacles. How this is accomplished is detailed on the MaxBotix website at http://www.maxbotix.com/documents/MaxBotix_Ultrasonic_Sensors_Find_Direction_and_Distance.pdf. You now have all the knowledge you need to add this type of capability to your robot.

For more details on using IR and sonar sensors for obstacle avoidance, you can visit several good places on the web. Try http://www.intorobotics.com/interfacing-programming-ultrasonic-sensors-tutorials-resources/ and http://www.geology.smu.edu/~dpa-www/robo/challenge/obstacles.html as well as http://www.societyofrobots.com/member_tutorials/book/export/html/71.

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

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