11. avoidance.py

Share for us

Procedures of the obstacle-avoidance function: Use speech prompt and speech command to control. When the car starts running, there will be a speech prompt asking whether any calibration is needed or not. Then, the LED ring on the top of the PiSmart lights up, the car starts listening to commands. You can answer yes or no and the PiSmart will take actions according to your reply.

In calibrating, the PiSmart car will ask whether to implement the calibration. Answer yes or no to accept or refuse the calibration.

In obstacle avoidance, the car will speak “Turn left” or “Turn right” when it’s running. If it comes to a dead end or is stuck by stuff on the surface and the detecting distance doesn’t change for a certain time, then the car will stop and speak to seek help. You can move the car a bit to change the value of detected distance to make the car move again. 

Wiring:

The ultrasonic avoidance module connects the PiSmart through the 3-pin digital ports.

Principle: The signal wire transfers a start signal to the module and then switch to receiving mode. After receiving the start signal, the module transmits an ultrasonic wave which will be reflected when encountering an obstacle. If the module receives the reflected wave, it will output a message through the signal wire. Use a timer to record the time, and then multiply it by sound velocity to calculate the distance.

Connect SIG, VCC, and GND in the module respectively to the digital port 0, 5V, and GND in Digital ports group on the PiSmart (Here use the channel 0).

UltrasonicPiSmart
SIG0(DIGITAL)
VCC5V
GNDGND

Run the example code for line following:

pi@raspberrypi:~/SunFounder_PiSmart_Car/examples $ python avoidance.py

Phenomenon: The car moves forward from side to side to detect the obstacles in sector ahead). When having detected an obstacle, the car will keep turning around till totally be away from the obstacle and continue to move forward. If the car is too close to the obstacle, it will move backward before turning directions.

You can open avoidance.py in the nano editor to check the code. Some comments are provided for better understanding.

When the distance between the car and the obstacle is greater than safe_line, the car goes straight. When it is between safe_line anddead_line, the car turns. When it is less than dead_line, the car moves backward.

In this example, we add a seeking for help function when the car comes to a dead end.  

When the fluctuation of the detecting distance is less than or_so, the distance is regarded as a steady value. Add those fluctuated distance values of dis to the check_stop list continuously. When the element quantity in this list is more than check_stop_max, it indicates that the car is stuck and then it will stop to seek for help.

Below is the programming flow diagram: