I'm a big fan of robots. I'm a even bigger fan of autonomous robots. One feature that most rover style autonomous robots need is obstacle avoidance. Obstacle avoidance is achieved by collecting data from our environment via sensors and programmatically making decisions based off our sensor data to avoid potential hazards.
PartsFor this project I am using a Particle Photon, Phobot, Runt Rover, and a hc-sr04 ultrasonic range finder. The Phobot is great for this small experiment as it provides simple motor control functions along with ports for our hc-sr04 ultrasonic sensor.
The Runt Rover is not required but I found it to be a nice and quick way to get started. So long as you have two DC motors, a chassis to hook them to, and a 4 double A battery compartment then you should be good to go.
ConstructionAfter construction of your Runt Rover, or robot chassis of choice, attach your Photon board to your Phobot and connect your DC motors to the m3 and m4 motor clamps. Attach the black wire from your battery compartment to GND clamp and the red wire to Vin.
After connecting your device to your network launch the Particle Online Ide and Log-in (Or create an account and sync your device). Then create a new Application and add the following libraries:
#include "HC_SR04/HC_SR04.h"
#include "PhoBot/PhoBot.h"
Make sure to add the libraries through the IDE. If you just copy and paste the include statements it will not add the source files to your project. Below your #include statements add the following lines:
#include "HC_SR04/HC_SR04.h"
#include "PhoBot/PhoBot.h"
PhoBot p = PhoBot(9.0, 6.0);
HC_SR04 rangefinder = HC_SR04(p.trigPin, p.echoPin);
double distance = 0.0;
void setup() {
}
void loop() {
distance = rangefinder.getDistanceCM();
if (distance < 20){
pause();
rotateRight();
delay(100);
} else {
forward();
}
delay(100);
}
void forward(){
p.setMotors("M3-F-50");
p.setMotors("M4-F-50");
}
void backward(){
p.setMotors("M3-B-50");
p.setMotors("M4-B-50");
}
void rotateRight(){
p.setMotors("M3-F-100");
p.setMotors("M4-B-100");
}
void rotateLeft(){
p.setMotors("M3-B-100");
p.setMotors("M4-F-100");
}
void pause() {
p.setMotors("M3-S");
p.setMotors("M4-S");
delay(100);
}
The code above is very basic. It grabs the distance from our range finder sensor. If that range is less than 20 cms then it will turn right. Otherwise it will move forward. As you can see from the cover image, an object was detected in front of the rover which caused it to turn right until the path was clear.
SummaryThis is a very basic robot that avoids by turning right. Though much is left to be desired! This however is a great starting point and can easily be expanded on. Please leave questions or comments below!
Comments