I am building for the fun a robot that I want to move autonomously inside a house.
It is a long work and I am doing step by step.
This instructable focus on the obstacles detection with Arduino Mega.
Ultrasonic sensors HC-SR04 vs HY-SRF05 are cheap and simple to use but can become difficult to integrate in the microcontroller loop in a complex robot. I wanted to run obstacles detection in an asynchronous way.
I already published 3 instructables about features of this robot:
Step 1: What Is Precisely the Issue With Ultrasonic Sensors and Microcontrollers ?
Synchronous wait and Arduino limitations
Microcontrollers code runs in a loop and does not support multi-thread. Ultrasonic sensors are based on signal duration. This duration last up to 30 m s that is very long to wait for inside the loop when the microcontrollers has to deal with multiple motors and sensors (for instance servo and DC motors with wheel encoders).
So I wanted to develop an object that runs asynchronously.
Step 2: How Does It Work ?
It has been designed for Atmega for obstacles detection. It supports up to 4 ultrasonic sensors.
Thanks to periodic time interruption, the system can monitor up to 4 ultrasonic sensors. The main code only has to define which sensor to activate with condition and threshold.
The main wil be interrupted only in case (condition , threshold) appears.
The main functions are:
- Alert is the basic obstacle detection and provides interrupt if at least 1 of the 4 sensors detects a distance under it threshold
- Monitor is an extended function that provides interrupt on a combination of distance condition of up to 4 sensors. The possible conditions are over, under, equal or not equal to thresholds.
Step 3: Technical Details
Use timer4 so pin 6 7 8 can not be used as PWM.
For each sensor the object needs a trigger PIN and an interrupt PIN.
On top of the sensors interrupt PINs the object needs an other interrupt PIN for software usage.
Step 4: How to Implement ?
Connect the sensors as above
Create EchoObstacleDetection directory inside your IDE library and move the .cpp and .h
This is a simple obstacles detection example runnng with 2 ultrasonic sensors.
Output is directed on serial monitor. At first it will print distances detected by the 2 sensors and then print alerts depending on distances under thresholds.