Sunday, April 8, 2012

Object collision avoidance test #2

I've replaced the IR sensor with an ultrasonic sensor. The new sensor actually returns the distance from the object, so the robot can be programmed to avoid object by proximity. In this version it is only forward looking, so it still can't see what is beside or behind it but it manages still to get itself out of tricky corners. Eventually it does get stuck in a corner, however using a pan & tilt assembly for the sensors will allow it to 'look around' and find a solution out of a tight spot.



Once again, the code is very simple:
#define MOTOR_OFF B00000000;
#define MOTOR_FWD B10010000;
#define MOTOR_REV B01100000;
#define MOTOR_LFT B10100000;
#define MOTOR_RHT B01010000;
#include "Ultrasonic.h"

Ultrasonic ultrasonic(12,13);
int distance = 0;
const int maxDistance = 100;
const int range = 25;

void setup() {
PORTD = MOTOR_OFF;
delay(500);
PORTD = MOTOR_FWD;
Serial.begin(9600);
}


void loop() {

distance = ultrasonic.Ranging(CM);

if(distance < range) {
PORTD = MOTOR_OFF;
do {
distance = ultrasonic.Ranging(CM);
if(distance > maxDistance) {
delay(5);
distance = ultrasonic.Ranging(CM);
continue;
}

PORTD = MOTOR_REV;
} while(distance < range);
PORTD = MOTOR_OFF;
PORTD = MOTOR_LFT;
delay(1000);
PORTD = MOTOR_OFF;
PORTD = MOTOR_FWD;
}
}

No comments:

Post a Comment