In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Raspberry pie how to carry out ultrasonic distance measurement automatic obstacle avoidance, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
The principle of ultrasonic ranging is very simple: it emits a sound wave, bounces back, and then receives the sound wave that bounces back. Using this time difference, the distance can be calculated. First of all, connect the ultrasonic module, my connection is on GPIO20 and GPIO21, so the corresponding code is as follows
# Ultrasonic pin TRIG = 20ECHO = 21
The name TRIG can also be changed, but the name used on the module is better remembered with this one. TRIG is responsible for emitting ultrasonic waves and ECHO is responsible for receiving them.
1. Initialize the ultrasonic module, that is, prevent it from being emitted, and place it at a low level GPIO.output (TRIG, 0).
Many of them append the following code:
Time.sleep (0.000002)
It should be to prevent errors, because then the transmitter needs to be set to a high level.
2. Emit ultrasonic wave GPIO.output (TRIG, 1) time.sleep (0.00001) GPIO.output (TRIG, 0)
Be sure to use GPIO.output (TRIG, 0) to turn off the ultrasonic emission, otherwise it will be emitted all the time, and there will be no way to measure the distance. Now the launch is over.
3. Receive ultrasonic while GPIO.input (ECHO) = = 0: passemitTime = time.time () while GPIO.input (ECHO) = = 1: pass acceptTime = time.time ()
Through this code, the status of the ECHO is obtained. At the beginning of the transmission, the ECHO input is low, and a time emitTime is obtained at this time. After the transmission is over, the reception starts, the signal is received, and a time acceptTime is obtained.
4. Calculate the distance totalTime = acceptTime-emitTimedistanceForReturn = totalTime * 340 / 2 * 100
The reason for multiplying 100 here is because the distance obtained is microns, multiplied by 100 is centimeters.
5. Integrate the code
Now integrate the above code, put it in a distance function, and return the value of the distance. The complete code is as follows:
# Ultrasonic ranging function def distance (): GPIO.output (TRIG, 0) time.sleep (0.000002) GPIO.output (TRIG, 1) time.sleep (0.00001) GPIO.output (TRIG 0) while GPIO.input (ECHO) = = 0: pass emitTime = time.time () while GPIO.input (ECHO) = = 1: pass acceptTime = time.time () totalTime = acceptTime-emitTime distanceForReturn = totalTime * 340 / 2 * 100 return distanceForReturn6, cycle to emit ultrasound and detect distance
Get the distance, need to let the ultrasonic wave in accordance with the requirements of continuous emission and reception, the car also needs to make a corresponding response, so it also needs a circular function. The code is difficult to explain in words, but easy to understand. As follows:
Def loop (): while True: dis= distance () if dis
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.