Intelligent obstacle detection and avoidance with autonomous navigation
🚧 Smart navigation - avoiding obstacles autonomously
The miniAuto platform integrates all the sensors and actuators we've learned about into a cohesive robotic system:
miniAuto programming follows a modular approach with distinct layers:
The miniAuto platform demonstrates how individual components work together to create intelligent, autonomous behavior - the same principles used in full-scale autonomous vehicles!
miniAuto provides high-level functions for basic movements:
#include <miniAuto.h>
miniAuto robot;
void setup() {
robot.begin();
Serial.begin(9600);
Serial.println("miniAuto Ready!");
}
void loop() {
// Basic movement commands
robot.moveForward(100); // Move forward at speed 100
delay(1000); // Move for 1 second
robot.turnRight(90); // Turn right 90 degrees
delay(500); // Wait for turn to complete
robot.moveBackward(50); // Move backward at speed 50
delay(1000);
robot.turnLeft(180); // Turn left 180 degrees
delay(1000);
robot.stop(); // Stop all movement
delay(2000); // Wait 2 seconds before repeating
}
Fine-tune movement with precise speed and direction control:
// Differential drive control
void moveWithDifferentialSpeed(int leftSpeed, int rightSpeed) {
robot.setMotorSpeeds(leftSpeed, rightSpeed);
}
// Smooth acceleration and deceleration
void smoothMove(int targetSpeed, int duration) {
int currentSpeed = 0;
int steps = 20;
int speedIncrement = targetSpeed / steps;
// Accelerate
for (int i = 0; i < steps; i++) {
currentSpeed += speedIncrement;
robot.moveForward(currentSpeed);
delay(duration / (steps * 2));
}
// Decelerate
for (int i = steps; i > 0; i--) {
currentSpeed -= speedIncrement;
robot.moveForward(currentSpeed);
delay(duration / (steps * 2));
}
robot.stop();
}
Always test movement commands in a safe, open area. Start with low speeds and gradually increase as you become familiar with the robot's behavior.
Access sensor information to make intelligent decisions:
void readAllSensors() {
// Ultrasonic distance measurement
float distance = robot.getDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// IMU orientation data
float heading = robot.getHeading();
Serial.print("Heading: ");
Serial.print(heading);
Serial.println(" degrees");
// Battery voltage monitoring
float voltage = robot.getBatteryVoltage();
Serial.print("Battery: ");
Serial.print(voltage);
Serial.println(" V");
// Light sensor readings
int lightLevel = robot.getLightLevel();
Serial.print("Light Level: ");
Serial.println(lightLevel);
}
Use sensor data to control robot behavior:
void sensorBasedNavigation() {
float distance = robot.getDistance();
if (distance > 30) {
// Path is clear - move forward
robot.moveForward(80);
Serial.println("Moving forward - path clear");
}
else if (distance > 15) {
// Obstacle detected - slow down
robot.moveForward(30);
Serial.println("Slowing down - obstacle ahead");
}
else {
// Obstacle too close - stop and turn
robot.stop();
delay(500);
robot.turnRight(45);
delay(1000);
Serial.println("Turning to avoid obstacle");
}
}
Combine multiple sensors for more robust navigation. For example, use both ultrasonic and IMU data to maintain straight-line movement while avoiding obstacles.
Create your first fully autonomous program that navigates around obstacles:
#include <miniAuto.h>
miniAuto robot;
// Autonomous navigation parameters
const int SAFE_DISTANCE = 25;
const int SLOW_DISTANCE = 40;
const int NORMAL_SPEED = 70;
const int SLOW_SPEED = 30;
void setup() {
robot.begin();
Serial.begin(9600);
Serial.println("Autonomous Navigation Started!");
// Initial sensor calibration
robot.calibrateSensors();
delay(2000);
}
void loop() {
autonomousNavigation();
delay(100); // Small delay for stability
}
void autonomousNavigation() {
float frontDistance = robot.getDistance();
// Display current sensor reading
Serial.print("Distance: ");
Serial.print(frontDistance);
Serial.print(" cm - ");
if (frontDistance > SLOW_DISTANCE) {
// Clear path - move at normal speed
robot.moveForward(NORMAL_SPEED);
Serial.println("Normal speed forward");
}
else if (frontDistance > SAFE_DISTANCE) {
// Obstacle detected - reduce speed
robot.moveForward(SLOW_SPEED);
Serial.println("Reduced speed forward");
}
else {
// Obstacle too close - execute avoidance maneuver
avoidObstacle();
}
}
void avoidObstacle() {
Serial.println("Obstacle avoidance activated!");
// Stop immediately
robot.stop();
delay(500);
// Check left and right distances
robot.turnLeft(45);
delay(800);
float leftDistance = robot.getDistance();
robot.turnRight(90);
delay(800);
float rightDistance = robot.getDistance();
// Return to center
robot.turnLeft(45);
delay(800);
// Choose best direction
if (leftDistance > rightDistance && leftDistance > SAFE_DISTANCE) {
Serial.println("Turning left to avoid obstacle");
robot.turnLeft(90);
delay(1000);
}
else if (rightDistance > SAFE_DISTANCE) {
Serial.println("Turning right to avoid obstacle");
robot.turnRight(90);
delay(1000);
}
else {
Serial.println("No clear path - backing up");
robot.moveBackward(50);
delay(1000);
robot.turnRight(180);
delay(2000);
}
}
This autonomous program demonstrates key robotics concepts:
Program your miniAuto to autonomously explore a room while avoiding obstacles and mapping its environment.