Chapter 5 Ultrasonic Obstacle Avoidance Car
If you have any concerns, please feel free to contact us via support@freenove.com
Description
The obstacle avoidance function of the car mainly uses the HC-SR04 ultrasonic module. The ultrasonic module
is controlled by the servo. The servo rotates to the left, middle and right repeatedly, so that the ultrasonic
module measures the distance of obstacles on the left, middle and right directions. And then it controls the
car to move according to different distances.
Run program
If the terminal displays the directory as below, you can directly run the Ultrasonic.py.
1. If not, execute the cd command:
cd ~/Freenove_4WD_Smart_Car_Kit_for_Raspberry_Pi/Code/Server
2. Run Ultrasonic.py:
sudo python Ultrasonic.py
You can press "Ctrl + C" to end the program.
Part of code is as below:
def run(self):
self.PWM=Motor()
self.pwm_S=Servo()
for i in range(30,151,60):
self.pwm_S.setServoPwm('0',i)
time.sleep(0.2)
if i==30:
L = self.get_distance()
elif i==90:
M = self.get_distance()
else:
R = self.get_distance()
while True: