User Manual
33
Code Explanation
ua = Ultra_Sonic.UltraSonic_Avoidance(17)
Create an object ua of a UltraSonic_Avoidance class in the Ultra_Sonic module. The number
in the round bracket is the initial parameter, which represents the pin number the SIG of the
module is connected to. Since the BCM naming method is applied, the corresponding pin on
the Raspberry Pi is #17.
back_distance and turn_distance, two constants are to set the thresholds of the ranging
distance.
while() loop
When the detected distance is less than the back_distance, the car will move backwards;
when it is between back_distance and turn_distance, the car will turn a direction (you can
set the turning angle in the aforementioned parameter turning_angle and the angle can be
a positive or negative number, for turning left or turning right respectively; NOTE that the
number of the turning angle should be -90 to 90 considering the servo’s max rotation degrees,
or the servo may be burnt.); when the detected distance is greater than the turn_distance,
the car will keep moving forward.
bw.backward(), making the rear wheels rotate backwards; bw.forward(), making the rear
wheels spin forward. These two functions in the rear wheel driving module back_wheels are to
set the wheel’s rotating direction.
bw.set_speed(speed), function in the back_wheels, to set the wheel's rotating speed. The
Turn
Backward
Forward