lakerz690
Posts: 13
Joined: Mon Nov 26, 2018 7:13 am

Radar Project Using Ultra Sonic Sensor

Wed Jan 30, 2019 3:54 am

Hello, so I have been trying to figure out the working code to create a radar using the Raspberry Pi 3+ and a HC-SR04 sensor. I have not found much luck, specifically for the coding that goes behind this. Most references are for the Arduino, so that doesn't help much. I have everything wired and I am using a remote desktop connection, if that makes any difference. So far this is this only source code that I can find for this project: https://github.com/AmrMohamed-95/radar- ... master/src. And it is not working for me, I keep running into errors in my IDE as, as well as if I try running the program through terminal. Any help, references, etc. would be much appreciative my friends. Thank you!

tempest766
Posts: 105
Joined: Sun Jan 07, 2018 3:44 pm

Re: Radar Project Using Ultra Sonic Sensor

Wed Jan 30, 2019 4:57 am

sonar, not radar.

User avatar
Joel_Mckay
Posts: 289
Joined: Mon Nov 12, 2012 10:22 pm
Contact: Website

Re: Radar Project Using Ultra Sonic Sensor

Wed Jan 30, 2019 8:14 am

Sonars make great wind-speed indicators, and unreliable distance sensors.
;-)

Low cost Radars are a real option now, and worth looking at given the example code is for a PI if I recall:
https://www.acconeer.com/products
https://www.digikey.com/product-detail/ ... ND/8040771

Best of luck,
J

scotty101
Posts: 3712
Joined: Fri Jun 08, 2012 6:03 pm

Re: Radar Project Using Ultra Sonic Sensor

Wed Jan 30, 2019 9:24 am

The HR-04 sensor is supported by the gpiozero libary

https://gpiozero.readthedocs.io/en/stab ... or-hc-sr04
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

lakerz690
Posts: 13
Joined: Mon Nov 26, 2018 7:13 am

Re: Radar Project Using Ultra Sonic Sensor

Wed Jan 30, 2019 5:09 pm

I have already created several projects using the HC-SR04 to measure distance, and to be used as a indicator. None of these responses answered my main question. I'm not going to go out and buy another sensor when I know I can utilize this as a radar. I simply need help with the coding to create this: https://www.youtube.com/watch?v=JvmIutmQd9U. All the sources I can find are for the Arduino only though. I'm repeating my initial post...

lakerz690
Posts: 13
Joined: Mon Nov 26, 2018 7:13 am

Re: Radar Project Using Ultra Sonic Sensor

Wed Jan 30, 2019 5:12 pm

tempest766 wrote:
Wed Jan 30, 2019 4:57 am
sonar, not radar.
The project implements a radar application, if you're here to respond to posts with a smart ass response and nothing else then you should go to Reddit. This forum is utilized for educational and research purposes.

lakerz690
Posts: 13
Joined: Mon Nov 26, 2018 7:13 am

Re: Radar Project Using Ultra Sonic Sensor

Wed Jan 30, 2019 5:17 pm

scotty101 wrote:
Wed Jan 30, 2019 9:24 am
The HR-04 sensor is supported by the gpiozero libary

https://gpiozero.readthedocs.io/en/stab ... or-hc-sr04
Thank you, I am aware of the code that is used to run the sensor for its intended purpose but the project I am referring to uses a radar application. Just like this picture Image
Attachments
hqdefault.jpg
hqdefault.jpg (18.73 KiB) Viewed 1938 times

tempest766
Posts: 105
Joined: Sun Jan 07, 2018 3:44 pm

Re: Radar Project Using Ultra Sonic Sensor

Wed Jan 30, 2019 5:55 pm

lakerz690 wrote:
Wed Jan 30, 2019 5:12 pm
tempest766 wrote:
Wed Jan 30, 2019 4:57 am
sonar, not radar.
The project implements a radar application, if you're here to respond to posts with a smart ass response and nothing else then you should go to Reddit. This forum is utilized for educational and research purposes.
Well, then continue to be ignorant of correct terminology. Radar is RF based NOT SOUND BASED! I thought I was educating you, but apparently you don't want to learn. Have fun.

https://en.wikipedia.org/wiki/Radar - RF
https://en.wikipedia.org/wiki/Sonar - Sound

hippy
Posts: 5935
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Radar Project Using Ultra Sonic Sensor

Wed Jan 30, 2019 6:35 pm

lakerz690 wrote:
Wed Jan 30, 2019 3:54 am
So far this is this only source code that I can find for this project: https://github.com/AmrMohamed-95/radar- ... master/src. And it is not working for me, I keep running into errors in my IDE as, as well as if I try running the program through terminal.
What errors are you getting and what parts of the project do you need help with; reading the sensor, creating your 'radar-like display', or something else ?

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12212
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Radar Project Using Ultra Sonic Sensor

Wed Jan 30, 2019 10:32 pm

Please keep being kind to each other (we demand that here) and stop making such a fuss over using the wrong terminology, yeah we all know that radar means that radio waves are used, and that sonar is using sound waves. Once the data is captured, you can use a very similar program to visualise the data, what I suppose the OP calls the "radar app".

Still I fear you are trying to use the wrong tools for the job, a RPI is okay for the frontend (the "radar" application you are misusing for sonar, to create the radar image) so for visualising the radar data, but NOT for timing sensitive radar/sonar data capturing, for that you will need something like an Arduino, something that doesn't run a full multitasking system like Linux does, which interrupts your code in the background, making timing sensitive tasks almost impossible..
Interfacing an Arduino to an RPI is a common thing, and easy to do using the PI's built in serial port (or through USB-Serial, although you will get more latency problems that way).

lakerz690
Posts: 13
Joined: Mon Nov 26, 2018 7:13 am

Re: Radar Project Using Ultra Sonic Sensor

Thu Jan 31, 2019 2:13 am

mahjongg wrote:
Wed Jan 30, 2019 10:32 pm
Please keep being kind to each other (we demand that here) and stop making such a fuss over using the wrong terminology, yeah we all know that radar means that radio waves are used, and that sonar is using sound waves. Once the data is captured, you can use a very similar program to visualise the data, what I suppose the OP calls the "radar app".

Still I fear you are trying to use the wrong tools for the job, a RPI is okay for the frontend (the "radar" application you are misusing for sonar, to create the radar image) so for visualising the radar data, but NOT for timing sensitive radar/sonar data capturing, for that you will need something like an Arduino, something that doesn't run a full multitasking system like Linux does, which interrupts your code in the background, making timing sensitive tasks almost impossible..
Interfacing an Arduino to an RPI is a common thing, and easy to do using the PI's built in serial port (or through USB-Serial, although you will get more latency problems that way).
Awesome, this is helpful information. I am relatively new to coding, but I've tried to implement another source code to try and get this to work. Unfortunately I kept running into compiling errors. I also noticed that all the videos and forums that are running this application on top of using the sonar device were using an Arduino. So just to confirm this with you, you're saying that I'm better off recreating this project using an Arduino because it will run smoother compared to the RPI, and it will be easier to implement the "radar application" since it will be two separate programs running in sync? I have no issue ordering an Arduino, I just wanted to challenge myself and try to get this project to work on the RPI.

User avatar
Gavinmc42
Posts: 3715
Joined: Wed Aug 28, 2013 3:31 am

Re: Radar Project Using Ultra Sonic Sensor

Thu Jan 31, 2019 4:39 am

Split it into two parts.

A timing part and a display part.
The timer sends a trigger and records the Pi's internal 1MHz clock count.
A hardware interupt on the GPIO echo reads the new count and subtract the first one and stores it.
The Display reads that stored time and calculats the distance.

It is a 5V part so feeding the echo back into a 3V3 pin is an issue, 10K resistor?
GPIO interrupts I am not expert in, I normally use an external micro controller for timer stuff.
A micro could do the servo PCM and the timing and be read from the Pi via i2c.
i2c is open collector so that helps with the 5 to 3V3 interfacing.

But for me I would use Ultibo so there is no Linux to get in the way ;)
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

hippy
Posts: 5935
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Radar Project Using Ultra Sonic Sensor

Thu Jan 31, 2019 3:43 pm

lakerz690 wrote:
Thu Jan 31, 2019 2:13 am
I've tried to implement another source code to try and get this to work. Unfortunately I kept running into compiling errors
If you don't or won't tell us what actual "compiling errors" you get, what code you are using, what program you have, then no one can really help you, even if they wanted to. The more you help people to help you the more they will be able to.
lakerz690 wrote:
Thu Jan 31, 2019 2:13 am
I just wanted to challenge myself and try to get this project to work on the RPI.
It should work. It's just a matter of rotating a servo, pinging a sensor and measuring the pulse coming back, then putting that on a 'radar-like' display. How well it works would seem to only depend on how accurately you can measure the pulse length.

lakerz690
Posts: 13
Joined: Mon Nov 26, 2018 7:13 am

Re: Radar Project Using Ultra Sonic Sensor

Thu Jan 31, 2019 4:36 pm

hippy wrote:
Thu Jan 31, 2019 3:43 pm
lakerz690 wrote:
Thu Jan 31, 2019 2:13 am
I've tried to implement another source code to try and get this to work. Unfortunately I kept running into compiling errors
If you don't or won't tell us what actual "compiling errors" you get, what code you are using, what program you have, then no one can really help you, even if they wanted to. The more you help people to help you the more they will be able to.
lakerz690 wrote:
Thu Jan 31, 2019 2:13 am
I just wanted to challenge myself and try to get this project to work on the RPI.
It should work. It's just a matter of rotating a servo, pinging a sensor and measuring the pulse coming back, then putting that on a 'radar-like' display. How well it works would seem to only depend on how accurately you can measure the pulse length.
So when I try running it in Thonny IDE I get:

Traceback (most recent call last):
File "/home/pi/Documents/radar.py", line 7, in <module>
import cv2
File "/usr/local/lib/python3.5/dist-packages/cv2/ init .py' , line 3, in <module>
from . cv2 import *
ImportError: libQtTest.so.4: cannot open shared object file: No such file or directory


It seems that once the code runs into cv2 I keep getting a bunch of errors. I've gone to multiple forums and read what others are saying and they kept downloading different software packages and for some the error goes away and other it persists. I've installed multiple software packages and it seems like one error gets fixed but another just replaces it. I also tried running the code through terminal and this is what I get:

pi@raspberrypi:~ $ sudo python /home/pi/Doucments/radar.py
Radar Start
No protocol specified
No protocol specified
No protocol specified
xcb_connection_has_error() returned true
Segmentation fault

hippy
Posts: 5935
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Radar Project Using Ultra Sonic Sensor

Thu Jan 31, 2019 5:41 pm

lakerz690 wrote:
Thu Jan 31, 2019 4:36 pm
So when I try running it in Thonny IDE I get:

Traceback (most recent call last):
File "/home/pi/Documents/radar.py", line 7, in <module>
import cv2
File "/usr/local/lib/python3.5/dist-packages/cv2/ init .py' , line 3, in <module>
from . cv2 import *
ImportError: libQtTest.so.4: cannot open shared object file: No such file or directory
It does all seem to be a bit of a mess. Not sure why it's looking in a Python 3.5 directory when the code from your original link is using Python 2 ( maybe that's a Thonny issue ), why that code is importing cv2 when that doesn't seem to be needed, why it's trying to load something which seems to be for Qt when using pygame, or why you are seeing the errors you get when run from the command line.

I downloaded the code from your original link, commented out the "import cv2" in radar.py and it worked just fine launched using a terminal from the Pi desktop. It draws the radar display as expected, scans the line back and forth, but does little else as I don't have a servo nor a sensor attached.

Maybe throw in the towel and start again, perhaps with a fresh install of Raspbian if you have somehow managed to completely mangle the Python installation trying to get things to work.

lakerz690
Posts: 13
Joined: Mon Nov 26, 2018 7:13 am

Re: Radar Project Using Ultra Sonic Sensor

Thu Jan 31, 2019 6:39 pm

hippy wrote:
Thu Jan 31, 2019 5:41 pm
lakerz690 wrote:
Thu Jan 31, 2019 4:36 pm
So when I try running it in Thonny IDE I get:

Traceback (most recent call last):
File "/home/pi/Documents/radar.py", line 7, in <module>
import cv2
File "/usr/local/lib/python3.5/dist-packages/cv2/ init .py' , line 3, in <module>
from . cv2 import *
ImportError: libQtTest.so.4: cannot open shared object file: No such file or directory
It does all seem to be a bit of a mess. Not sure why it's looking in a Python 3.5 directory when the code from your original link is using Python 2 ( maybe that's a Thonny issue ), why that code is importing cv2 when that doesn't seem to be needed, why it's trying to load something which seems to be for Qt when using pygame, or why you are seeing the errors you get when run from the command line.

I downloaded the code from your original link, commented out the "import cv2" in radar.py and it worked just fine launched using a terminal from the Pi desktop. It draws the radar display as expected, scans the line back and forth, but does little else as I don't have a servo nor a sensor attached.

Maybe throw in the towel and start again, perhaps with a fresh install of Raspbian if you have somehow managed to completely mangle the Python installation trying to get things to work.
Thanks again for your help btw. So, I took out "import cv2" as well and everything started to run, not properly though. The "radar screen" will pop up, the servo motor will begin going back and forth like it's having a seizure then the task will stop running and the "radar screen" closes. The IDE says this:

Radar Start
e
Radar Exit


Also I'll run the program several times and it will take different amounts of time for the program to shut off on its own, always within seconds. And the servo motor is still acting crazy.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12212
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Radar Project Using Ultra Sonic Sensor

Sat Feb 02, 2019 11:49 pm

Do all timing sensitive stuff with an arduino, that is running only your software, so isn't disturbed by other codes interrupting what it is doing.
do all display and calculation stuff with a raspberry PI, that "talks" to the arduino (serially).

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Radar Project Using Ultra Sonic Sensor

Sun Feb 03, 2019 3:58 am

An item like the SR04 has a 15 degree cone of emission. A SR04 does not give accurate distance readings whiles the SR04 is moving. Thus, the SR04 will have to ping, move, stop, ping... The SR04 is a noisy device but with about 30.00USD bucks of modifications you can get 2 to give stable and accurate readings. I used 2 SR04's to verify the results of the 1st SR04.

One of the issues with the SR04 is it has a noisy step up power supply. So you'll want to add filtering right on the module. The other issues is the SR04 transmits at 40Khz. The receiver operates at 38Khz. To get the receiver to be tuned to the transmitted frequency you'll need to buy some components and have some soldering skills. The SR04 transmission cone of 15 degrees will need to be trimmed to increase object detection resolution; have fun with that.

I worked for about 8 months to get a good scanning device from an SR04. I went and used the Arduino Uno does the SR04 and sends the data to the RPi for processing route, route.

I finally, switched to a TFMini, LIDAR. Whiles the LIDAR does not work with close in distances (0 to 4 cm), I get accurate returns out to 500cm. The TFMini sends out a 2 degree cone. I do a forward sweep of 5 degrees and take 62 readings, which displays nicely on a 128X64 OLED screen.

Higher quality Ultrasonic sensors can be had. When price compared to a LIDAR the LIDAR's features wins hands down.

Next, the RPi with its OS has a high overhead. You'll move into multithreading and multi processing, with the LIDAR, to be able to achieve decent scan to processing. The RPi can handle that but the Arduino Uno will start to fall behind and become a bottleneck. The Arduino Mega will not be much better. A DUE running a RTOS ( Real Time Operating System) will be able to feed the RPi scan info at a decent rate to produce near real time imaging displays and post signal processing. You'll need to beef up the RPI cooling.

If you want your project to move around the DUE is just too big to fit nicely in a mobile environment. Which will bring you to the STM32 or the ESP32. The STM332, Blue Pill, running at 72Mhz will supply the RPi at a decent data rate but will be limited with what can be done with it by the low operational RAM.

The ESP32, with 4 or 8 MB of RAM running at 240mHz with a dual core processor and with native RTOS (freeRTOS), built in blue tooth, WiFI, and is small in size; just to name a few features, does LIDAR imaging and post signal processing really well.

I really, really enjoyed my explorations of the various devices and the route I took. I learned a lot whiles I used the RPI for this project and am looking forward to the next RPI upgrade.
Without knowing why you are deleting my postings, I will not know how...

hippy
Posts: 5935
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Radar Project Using Ultra Sonic Sensor

Sun Feb 03, 2019 3:51 pm

lakerz690 wrote:
Thu Jan 31, 2019 6:39 pm
Thanks again for your help btw. So, I took out "import cv2" as well and everything started to run, not properly though. The "radar screen" will pop up, the servo motor will begin going back and forth like it's having a seizure then the task will stop running and the "radar screen" closes. The IDE says this:

Radar Start
e
Radar Exit
No idea on that one; I'm not familiar with the code only took a brief look at it. Jerky servo movement might be that the servo power supply doesn't have enough oomph to it, or the PWM is okay for some earlier Pi but not what you are using, or maybe not for your servo.
lakerz690 wrote:
Thu Jan 31, 2019 6:39 pm
Also I'll run the program several times and it will take different amounts of time for the program to shut off on its own, always within seconds.
I noticed that. I'm guessing it completes a full sweep of the servo before checking if it should shut down, hence variable times for closing.

But if you mean just ending when it shouldn't I don't know. I'm guessing there's some bug in the code.

danjperron
Posts: 3402
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Radar Project Using Ultra Sonic Sensor

Mon Feb 04, 2019 2:27 am

https://github.com/AmrMohamed-95/radar- ... r/src[url][/url]
The code works fine. there is a small error and it is easy to fix.

in the ultrasonicsensor.py module just create the variable start_time. This way if the condition doesn't create it python won't report an error.

Code: Select all

def ultrasonicRead(GPIO, TRIG, ECHO):
    start_time = time.time()

Also you need to add some modules for opencv to work

https://www.raspberrypi.org/forums/view ... 00#p945218


This is a small video of the code running using vncviewer with the SR-4 mount on a servo.
I also change the display range from 50cm to 200cm
https://www.dropbox.com/s/q2efoub4iimzc ... r.mov?dl=1

danjperron
Posts: 3402
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Radar Project Using Ultra Sonic Sensor

Mon Feb 04, 2019 12:16 pm

I forget to mention that the GPIO is set to GPIO.BOARD.

Be sure that you are using the pin number and not the GPIO number!

Return to “Troubleshooting”