For a school project, we are designing a non-tactile maze robot, designed to navigate its way thru a maze without touching the walls.
We are attempting this with one IR sensor in the front and two on the right side, designed to follow the right wall.
We are using the arduino duemilanove 328 and the ardumotomotor driver shield.
we cannot get the robot to re-adjust to make it drive in a straight line.
when we try to make it do 90-deg right and left hand turn, sometimes it will do a 270-deg turn.
we cannot figure out what we're doing wrong here.
please help!
below is our current code in its entirety.
//ONU MAZE ROBOT 2012
#include <EEPROM.h>
const int STATE_FORWARD = 1;
const int STATE_TURN_RIGHT = 2;
const int STATE_BACKWARD = 3;
const int STATE_TURN_LEFT = 4;
const int STATE_ADJ_RIGHT = 5;
const int STATE_ADJ_LEFT = 6;
int IRpin1 = 1;
int IRpin2 = 2;
int IRpin3 = 3;
int IRpin4 = 4;
int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3
int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11
int dir_a = 12; //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 13
int state; //current state
int lastState; //previous state
void setup()
{
Serial.begin(9600); // start the serial port
pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
analogWrite(pwm_a, 80); //set both motors to run at (100/255 = 39)% duty cycle (slow)
analogWrite(pwm_b, 80);
lastState = state = STATE_FORWARD;
}
void loop()
{
float volts1 = analogRead(IRpin1)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float Front = 65*pow(volts1, -1.10); // worked out from graph 65 = theretical distance / (1/Volts1)S - luckylarry.co.uk
Serial.println(Front); // print the distance
delay(20); // arbitary wait time.
float volts2 = analogRead(IRpin2)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float FrontSide = 65*pow(volts2, -1.10); // worked out from graph 65 = theretical distance / (1/Volts1)S - luckylarry.co.uk
Serial.println(FrontSide); // print the distance
delay(20); // arbitary wait time.
float volts3 = analogRead(IRpin3)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float BackSide = 65*pow(volts3, -1.10); // worked out from graph 65 = theretical distance / (1/Volts1)S - luckylarry.co.uk
Serial.println(BackSide); // print the distance
delay(20); // arbitary wait time.
float volts4 = analogRead(IRpin4)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float LeftSide = 65*pow(volts3, -1.10); // worked out from graph 65 = theretical distance / (1/Volts1)S - luckylarry.co.uk
Serial.println(LeftSide); // print the distance
delay(20); // arbitary wait time.
Serial.print('\n'); // skips a line
if(state == 0)
{
moveRobot();
return;
}
if (FrontSide < 90 && BackSide < 90 && Front > 100)
{
state = STATE_FORWARD;
analogWrite(pwm_a, 80);
analogWrite(pwm_b, 80);
if (FrontSide > (BackSide+3))
{
state = STATE_ADJ_RIGHT;
}
else if (FrontSide < (BackSide+3))
{
state = STATE_ADJ_LEFT;
}
}
else if (FrontSide > 90 && BackSide > 90)
{
analogWrite(pwm_a, 80);
analogWrite(pwm_b, 80);
state = STATE_TURN_RIGHT;
}
else if (Front < 90)
{
analogWrite(pwm_a, 80);
analogWrite(pwm_b, 80);
state = STATE_TURN_LEFT;
}
Serial.println(state);
moveRobot();
}
void moveRobot()
{
switch( state ) {
case 1: //forward
{
digitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low
break;
}
case 3: //backward
{
digitalWrite(dir_a, HIGH); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, HIGH); //Set motor direction, 3 high, 4 low
delay (1000);
break;
}
case 2: //right
{
delay(300);
digitalWrite(dir_a, LOW); //turn right
digitalWrite(dir_b, HIGH); //
delay (550);
digitalWrite(dir_a, LOW); //straight
digitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low
delay (1300);
digitalWrite(dir_a, LOW); //turn right
digitalWrite(dir_b, HIGH); //Set motor direction, 3 high, 4 low
delay (600);
digitalWrite(dir_a, LOW); //straight
digitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low
delay (2200);
break;
}
case 4: //left
{
digitalWrite(dir_a, HIGH); //Set motor direction, 1 low, 2 high
digitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low
delay (300);
break;
}
case 5: //state adj right
{
analogWrite(pwm_a, 82);
delay (400);
break;
}
case 6: //state adj left
{
analogWrite(pwm_a, 75);
delay(400);
break;
}
}
}
//END OF LINE