Connecting the BeagleBone Black to a USB sonar sensor

Now that you have a mobile platform and your robot can move, you will want to check if your robot could run into something. One of my favorite ways to do this is using a sonar sensor. First: a little tutorial on sonar sensors. This type of sensor uses ultrasonic sound to calculate the distance to an object. The sound wave travels out from the sensor, as illustrated here:

Connecting the BeagleBone Black to a USB sonar sensor

The device sends out a sound wave ten times a second. If an object is in the path of these waves, then the waves reflect off the object, sending waves that return to the sensor, as shown here:

Connecting the BeagleBone Black to a USB sonar sensor

The sensor then measures any return. It uses the time difference between when the sound wave was sent out and when it returned, to measure the distance to the object.

Prepare for lift off

The first thing you'll want to do is connect the USB sonar sensor to your PC, just to make sure everything works well. Here are the steps:

  1. First, download the terminal emulator SW from http://www.maxbotix.com/articles/059.htm, and select the Windows download. The page will look like the following screenshot:
    Prepare for lift off
  2. Unzip this file. Then plug the sensor into a USB port on your PC. Then open the terminal emulator file by selecting this file from the directory, as shown in the following screenshot:
    Prepare for lift off
  3. The following application window should pop up:
    Prepare for lift off
  4. You'll need to change the setting to find the sensor, so select the Settings button, and you should see this screen:
    Prepare for lift off
  5. Select the Port menu and select the port that is connected to your sensor. Most often this will be the last one in the list. In my case, I selected COM3, clicked on OK, and this is what I saw on the main screen:
    Prepare for lift off
  6. Notice the sensor readings. Now place an object in front of the sensor. You should now see something like this:
    Prepare for lift off

The readings have changed, specifically the value after R, and the value P1, indicating an object in front of the sensor. The R values indicates a range in mm, and P1 indicates an object is in the range of the sensor. It will read P0 if it thinks there is no object. You'll need to read these values into your program, and then you can avoid the object.

Now that you know the unit works, you'll want to mount the USB sensor on! your mobile platform. In this case, I am going to mount the USB sonar sensor on my quadruped robot.

Make sure you plug one end of the USB cable into the sensor and the other end into the USB hub connected to the BeagleBone Black.

Engage thrusters

With the HW all constructed and the sensor working, you can start communicating with your USB sensor using the BeagleBone Black. You are going to create a simple Python program that will read the value from the sensor. To do this—using Emacs as an editor—type emacs sonar.py. A new file will be created called sonar.py. Then type the code shown in the following screenshot:

Engage thrusters

Let's go through the code to see what is happening.

  • #!/usr/bin/python: This line simply makes this file available for us to execute from the command line.
  • import serial: You also again import the serial library. This will allow us to interface with the USB sonar sensor.
  • if __name__=="__main__": The main part of the program is then defined.
  • Ser=serial.Serial('/dev/ttyUSB0', 57600, timeout = 1): This command sets up the serial port to use the /dev/ttyUSB0 device, which is the sonar sensor, using a baud rate of 57600 and a timeout of 1.
  • x = ser.read(100): This command then reads the next hundred values from the USB port.
  • print(x): This command then prints out the value.

Once you have this file created, you can run the program and communicate with the device. Do this by typing ./sonar.py, and the program will run. I have found that sometimes the device returns no data when run for the first time, so don't be surprised if you print out no values the first time you run your program. For the second time, you should receive a valid return string. Here is my result after running the program:

Engage thrusters

The sensor is returning 064, which indicates a relative distance to a barrier in millimeters. If I place a good reflector just a few inches in front of the sensor and run the program, I will get the following result:

Engage thrusters

Objective complete – mini debriefing

Now the robot can sense its environment, so you can avoid bumping into walls and other barriers!

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

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