In this lesson, we will learn how to use the RGB ultrasonic module of Classic Car, Racing Car and Alter Dog to realize automatic obstacle avoidance function.
26.1 Introduction of RGB ultrasonic ranging module
Since the camera used by our Raspberry Pi robot is a monocular camera and cannot collect depth information, many of our robot products use ultrasonic ranging modules to obtain depth information and detect whether there are obstacles in a certain direction to get the distance of the obstacle.
The model of the ultrasonic ranging module used in our robot products is HC-SR04. The HC-SR04 module has four pins, which are VCC, GND, Echo and Trig. HC-SR04 can provide 2cm-400cm non-contact distance sensing function, and its ranging accuracy can reach 3mm; the module includes ultrasonic transmitter, receiver and control circuit. The basic working principle is as follows:
Use IO port TRIG to trigger distance measurement, and give a high level signal of at least 10us.
The module automatically sends 8 40khz square waves, and automatically detects whether there is a signal return.
There is a signal return, and a high level is output with the IO port ECHO. The duration of the high level is the time from emission to return of the ultrasonic wave.
The principle of distance detection by ultrasonic ranging sensor: the method of detecting distance by ultrasonic is called echo detection method, that is, the ultrasonic transmitter emits ultrasonic waves in a certain direction, and the timer starts timing at the same time as the launch time. The ultrasonic waves propagate in the air and encounter obstacles on the way. When the object surface (object) is blocked, it will be reflected back immediately, and the ultrasonic receiver will immediately stop timing when the reflected ultrasonic wave is received. The propagation speed of ultrasonic waves in the air is 340m/s. According to the time t recorded by the timer, the distance s from the launch point to the obstacle surface can be calculated, namely: s=340t/2. Using this principle of ultrasound, the ultrasonic ranging module is widely used in practical applications, such as car reversing radar, unmanned aerial vehicle, and smart car.
When using Robot HAT driver board, you need to connect HC-SR04 to the Ultrasonic interface on the driver board, and you must not connect it to the IIC port to avoid burning the ultrasonic module. (IIC is an interface used to connect I2C devices, and the pin positions of VCC and GND are different from Ultrasonic).
26.2 Preparation
1. The assembled Classic Car, Racing Car and Alter Dog.
2. Connect the components.
Classic Car:
Racing Car:
Alter Dog:
26.3 Turn on automatic obstacle avoidance Function
26.3.1 Run the program of this course
1. Open the terminal software MobaXterm:
2. Log in to your Raspberry Pi (the way to log in to Raspberry Pi has been introduced in Lesson 1):
3. Enter the Course Code folder, this folder stores the sample code program to control the robot, enter the following command:
cd adeept_alter/server
4. Enter the command to display the contents of the current directory:
ls
5. auto.py is the sample code of the automatic obstacle avoidance function of this lesson, enter the command to run it:
sudo python3 auto.py
6. After successfully running the program, the robot will automatically avoid obstacles when it encounters obstacles.
7. When you want to terminate the running program, you can press the shortcut key Ctrl+C on the keyboard.
26.3.2 Learning the code program of this lesson
After the above hands-on practice, you already know how to use and run our course sample code program, then you must be curious about how our code program is programmed to achieve the automatic obstacle avoidance function on the Raspberry Pi. Let’s learn about our main code program together. Here we use Subline IDE to view and edit the code program of this lesson, please see the content of lesson 2 for specific methods.
Import and instantiate the object used to control Alter.
The pin numbers of the input and output terminals of the ultrasonic module.
The example in this tutorial is a normal blue ultrasonic module, initialize the GPIO of the ultrasonic module.
When the value of the ranging result is less than this value, it will turn.
Calling this function will get the return value of ranging.
Set the input terminal of the module to high level, and send out an initial sound wave.
The module no longer receives the initial sound wave.
Write down the time when the initial sound wave was emitted.
The module receives the return sound wave.
Note the time when the return sound wave was captured.
Calculate the distance.
Print out the distance measurement results.
If the measured distance ahead is less than the value specified by turnRang, turn left; otherwise, go forward.