Ever wonder that we can make a device just like RADAR and LiDAR using these sensors! Yes it is possible; but with some limitation. RADAR are the most precise but are much expensive. So let's make our own distance mapper device which acts like RADAR using UltraSonic Distance Sensor (HC-SR04) and a Servo.
UltraSonic and IrfraRed are basic fundamental sensor for measurement of distance. These sensor measures the distance in the visible sight of them with the range of 80cm to 500cm. Both sensor have their pros and cons. IR are susceptible to sun-lite and are not good in open field.
Mapping distance like RADAR requires a rotating sensor that picks up distance at different level and maps into 2D view. Just like RADAR, to rotate UltraSonic Distance sensor, we need a servo that accurately rotates it. Once RPi2 rotates servo at specific angle, RPi2 then measures distance and plot it into 2D view as shown in title image of the article. But we can't directly operate servo from RPi2 because, Windows IoT buil 10531 provides access to ADC and PWM via external chips. It means we can't directly control servo from RPi2 and requires extra components. Thus I found a way to operate servo via Arduino. Arduino will communicate with RPi2 via I2C bus and operates servo:
It is clear that we need to develop two firmware. One for Arduino and another for RPi2 (UWP). You don't have to bother with the complexity of the communication as I have already provided library that will provide easy way for communication between Arduino and RPi2.
I am developing gateway firmware for Arduino. With the gateway firmware, you will be able to operate Arduino directly from Windows IoT code just like you wrote in Arduino sketch. It means that you will be able to manipulate Arduino's digital, analog, PWM and Serial pins directly from RPi2 with minimal coding. I have already developed gateway firmware for Arduino but right now there are lots of stability issues with it. Thus I'm improving the firmware and going to write PowerTip article for that. Will update here when it will be ready.
Here, gateway is an I2C slave device. Which will serves the task requested by RPi2. There is no special requirement to understand for gateway sketch as it is already provided at the end of the article. It is simple and well commented.
Universal Windows App will communicate with gateway via I2C and requests gateway to move servo at specified position. After successful move of servo, UWApp will measures distance and plot it into 2D view.
Do not take immediate distance reading after moving servo because it will take some time (in milliseconds) to set servo at an angle. Take multiple sample of a specific direction to get accurate distance reading. Higher the sampling, more accuracy but with less resolution (it means it will take some time to process samples). So we need to balance sampling and performance.
An issue has been found with Arduino Nano. RPi2 won't be able to communicate via I2C with Arduino Nano if you have implemented voltage divider circuit (Green rectangle in above image) for I2C. Try direct connection for I2C between Arduino Nano and RPi2. It not good for RPi2 (because RPi2 works at 3.3v while Arduino works on 5v) but it worked for me and there is no any side effect has been seen on RPi2.
'WinRT Xaml Toolkit' provides various pre-developed components like PieSlice, charts, etc. You can find complete list of components on its website. This toolkit provide a key component for this project that is PieSlice. You can see PieSlice in the title image of the article. RADAR like 2D map screen is developed using multiple PieSlice component which is explained later in the article.
After successful installation of assembly, you need to specify alias of the namespace into XAML code to use components of the assembly.
<Shape:PieSlice ... />
Most of have seen RADAR screen on television. RADAR is object detection system that will determine range and angle of an object. By considering RADAR idea, we are gonna make such view that will be able to map object distance related to an angle.
I have provided 2D map for up-to 300cm ranging between -70 to +70 degrees ahead of the sensor. You can implement 2D map of your own choice of angle and distances. Even full circular just like RADAR. Just a little bit of coding will be necessary.
The overall concept is really easy. Let's understand by an algorithm:
If you want to startup your universal windows app just after Pi boots, read this ProTip.
Arduino Nano Random Crash
Servo seems to be stop suddenly and did't move at all. I don't know what was happens with Arduino Nano. I wrote a watchdog reset but some Arduino Nano comes with older bootloader and they don't supports watchdog.
Solution: Press Arduino Nano's reset. RPi2 can also reset Arduino Nano for you. You just have to make appropriate logic so that RPi2 will know about the Nano's state and resets it whenever it is necessary.
UltraSonic Distance Sensor Issues
It is not possible to get more accurate reading from ultrasonic sensor. Sometimes, they might give false reading. There is range limitation with ultrasonic which ranges between 3 - 5 meter (may differ from sensor to sensor). Different surfaces have different property. Thus measurement might vary for surface to surface.