Raspberry Pi with Java: Programming the Internet of Things (IoT) by Chin Stephen & Weaver James

Raspberry Pi with Java: Programming the Internet of Things (IoT) by Chin Stephen & Weaver James

Author:Chin, Stephen & Weaver, James [Chin, Stephen]
Language: eng
Format: epub
Publisher: McGraw-Hill Education
Published: 2015-10-23T04:00:00+00:00


FIGURE 5-16. Line runner in action (photo credit Bruno Borges)

Obstacle Detection

The last trick for our line runner is to teach it how to stop and wait for obstacles that obstruct the track. To accomplish this, we are going to use the Makeblock Me Ultrasonic Sensor. This sensor uses high-pitched sounds to detect the presence of and distance to a solid object in front of the sensor. It is accurate from 3cm to 4m, although the effective range of the sensor is limited by the speed and precision with which we can read and write to the digital pins on the Raspberry Pi.

The protocol for the Makeblock Me Ultrasonic Sensor is as follows. First, initialize the sensor by performing these steps:

1. Write a low value to the GPIO pin.

2. Wait 2 microseconds.

3. Write a high value to the GPIO pin.

4. Wait 10 microseconds.

5. Write a low value to the GPIO pin.

Read the sensor as follows:

1. Wait for it to go high.

2. Start a timer.

3. Wait for it go low.

4. Capture the pulse width in microseconds.

Once we have the pulse width of the sensor, we can use this to calculate the distance of the object. The distance in centimeters is calculated by dividing the pulse width by 58 as shown in the following formula:

Distance (cm) = Pulse Width (uS) / 58

At the minimum distance of 3cm for the sensor, this would require timings as precise as 174μs or .174ms. Unfortunately, the Raspberry Pi GPIO libraries cannot handle this resolution, so at this distance you will miss the pulse entirely. Using the fastest GPIO interface available, Pi4J raw mode, we can measure distances starting at around 20cm and up, which gives us an effective range of 20cm to 4m for the sensor. This is reasonable for obstacle avoidance, and is the range we will focus on.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.