We have a problem with our robot running our code
DarkMatterField777
Posts: 2
We have been working on a maze solving robot for s ometime. Recently though, the robot seemingly refuses to follow what we write, and also seems to skip to other functions while it is still running functions. I was wondering if we could get some help on this. The code will be listed below.
#include "simpletools.h"
#include "ping.h"
#include "abdrive.h"
int sizeOfMaze = 0;
int positionx = 0; //Bunch of variables (position of the robot in the x axis)
int positiony = 0; //position of the robot in the y axis
int orientation = 1; //cardinal direction of robot
int donechecking = 0;
int big = 0;
int finished = 0;
/*
orientation:
1 = foreward
2 = facing right
3 = facing backwards
4 = facing left
*/
int leftcheck = 0; //variables for checking which walls are there are which ones aren't
int rightcheck = 0;
int frontcheck = 0;
/*
1 = increase positionx
2 = increase positiony
3 = decrease positionx
4 = decrease positiony
*/
int pathing[500];
/*
1 = left turn
2 = forward
3 = right turn
*/
int num = 0;
void leftDegreeTurn(void) //90 degree turning code
{
high(26);
drive_goto(-29,-29);
drive_speed(0,0);
drive_speed(6, 27);
pause(2475);
drive_speed(0,0);
drive_ramp(-20,-20);
pause(1000);
drive_speed(0,0);
low(26);
if(orientation == 1) //changing orientation for a left turn (needs polishing)
{
orientation = 4;
}
if(orientation = 2)
{
orientation = 1;
}
if(orientation = 3)
{
orientation = 2;
}
if(orientation = 4)
{
orientation = 3;
}
}
void rightDegreeTurn(void)
{
high(26);
drive_goto(-29,-29);
drive_speed(0,0);
drive_speed(27, 6);
pause(2475);
drive_speed(0,0);
drive_ramp(-20,-20);
pause(1000);
drive_speed(0,0);
low(26);
if(orientation = 1) //changing orientation for a right turn (needs position)
{
orientation = 2;
}
if(orientation = 2)
{
orientation = 3;
}
if(orientation = 3)
{
orientation = 4;
}
if(orientation = 4)
{
orientation = 1;
}
}
void moveForward(void) //function for going foreward
{
drive_goto(55,55); //actually go forward
if(orientation = 1) //change in position
{
positionx += 1;
}
if(orientation = 3)
{
positionx -= 1;
}
}
void leftDegreeTurnBack() //function for turning backwards (used in function for escaping dead end)
{
high(26);
drive_goto(-29,-29);//BACK LEFT TURNchange orientation
drive_speed(0,0);
drive_speed(-6, -27);
pause(2475);
drive_speed(0,0);
drive_ramp(-20,-20);
pause(1000);
drive_speed(0,0);
low(26);
if(orientation = 1)
{
orientation = 2;
}
if(orientation = 2)
{
orientation = 3;
}
if(orientation = 3)
{
orientation = 4;
}
if(orientation = 4)
{
orientation = 1;
}
}
void rightDegreeTurnBack() //BACK RIGHT TURN same as other turn just backwards (needs polishing)
{
high(26);
drive_goto(-29,-29);
drive_speed(0,0);
drive_speed(-27, -6);
pause(2475);
drive_speed(0,0);
drive_ramp(-20,-20);
pause(1000);
drive_speed(0,0);
low(26);
if(orientation = 1)
{
orientation = 4;
}
if(orientation = 2)
{
orientation = 1;
}
if(orientation = 3)
{
orientation = 2;
}
if(orientation = 4)
{
orientation = 3;
}
}
void moveBackward(void) //function for going backwards
{
drive_goto(-55,-55); //go backwards
if(orientation = 1) //change in position
{
positionx -= 1;
}
if(orientation = 3)
{
positionx += 1;
}
}
void backupFromDead(void) //fucntion for backing up through the maze
{
while(pathing[num] != 0)
{
low(26);
if(pathing[num] = 1)
{
rightDegreeTurnBack();
pathing[num] = 0;
num -= 1;
pause(1);
}
else if (pathing[num] = 2)
{
moveBackward();
pathing[num] = 0;
num -= 1;
pause(1);
}
else if (pathing[num] = 3)
{
leftDegreeTurnBack();
pathing[num] = 0;
num -= 1;
pause(1);
}
}
pathing[num] = 5;
}
void backThroughTheMaze() //function for going back through the maze after completing it the first time
{
while(positionx !=1)
{
if (pathing[num] = 1)
{
rightDegreeTurnBack();
num -= 1;
pause(1);
}
else if (pathing[num] = 2)
{
moveBackward();
num -= 1;
pause(1);
}
else if (pathing[num] = 3)
{
leftDegreeTurnBack();
num -= 1;
pause(1);
}
}
}
int main()
{
sizeOfMaze = 11;
if(big == 0)
{
moveForward();
pause(5);
big = 1;
pause(5);
}
while(positionx != sizeOfMaze) //while not at the end of the maze
{
if(ping_cm(8) < 6) //check for left wall
{
leftcheck = 1;
pause(5);
}
else
{
leftcheck = 0;
pause(5);
}
pause(5);
if(ping_cm(7) < 10) //check for forward wall
{
frontcheck = 1;
pause(5);
}
else
{
frontcheck = 0;
pause(5);
}
pause(5);
if(ping_cm(6) < 6) //check for right wall
{
rightcheck = 1;
pause(5);
}
else
{
rightcheck = 0;
pause(5);
}
pause(10);
/*if(leftcheck == 0 && frontcheck == 1 && rightcheck == 1)
{
leftDegreeTurn(); //left
pathing[num] = 1;
pause(5);
num += 1;
moveForward();
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
}
if(leftcheck == 1 && frontcheck == 0 && rightcheck == 1)
{
moveForward(); //forward
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
} */
if(leftcheck == 1 && frontcheck == 1 && rightcheck == 0)
{
rightDegreeTurn(); //right
pathing[num] = 3;
pause(5);
num += 1;
moveForward();
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
}
/*if(leftcheck == 0 && frontcheck == 1 && rightcheck == 0)
{
leftDegreeTurn(); //left zero
pathing[num] = 1;
pause(5);
num += 1;
pause(5);
pathing[num] = 0;
pause(5);
num += 1;
moveForward();
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
}
if(leftcheck == 0 && frontcheck == 0 && rightcheck == 1)
{
leftDegreeTurn(); //left zero
pathing[num] = 1;
pause(5);
num += 1;
pause(5);
pathing[num] = 0;
pause(5);
num += 1;
moveForward();
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
}
if(leftcheck == 0 && frontcheck == 0 && rightcheck == 0)
{
leftDegreeTurn(); //left zero
pathing[num] = 1;
pause(5);
num += 1;
pause(5);
pathing[num] = 0;
pause(5);
num += 1;
moveForward();
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
}
if(leftcheck == 1 && frontcheck == 0 && rightcheck == 0)
{
moveForward(); //forward zero
pathing[num] = 2;
pause(5);
num += 1;
pause(5);
pathing[num] = 0;
pause(5);
num += 1;
pause(10);
}
if(leftcheck == 1 && frontcheck == 1 && rightcheck == 1) //if all walls are detected, go back
{
high(26);
backupFromDead(); //backup till latest zero
}*/
}
finished = 1;
backThroughTheMaze(); //fucntion for going back through the maze
pause(100); //wait a bit
if(finished = 1)
{
num = 1;
if (pathing[num] = 1) //final runthrough which doesn't need a function
{
leftDegreeTurn();
pause(5);
num += 1;
pause(5);
}
else if (pathing[num] = 2)
{
moveForward();
pause(5);
num += 1;
pause(5);
}
else if (pathing[num] = 3)
{
rightDegreeTurn();
pause(5);
num += 1;
pause(5);
}
}
}
Any help would be appreciated. Thank you.
#include "simpletools.h"
#include "ping.h"
#include "abdrive.h"
int sizeOfMaze = 0;
int positionx = 0; //Bunch of variables (position of the robot in the x axis)
int positiony = 0; //position of the robot in the y axis
int orientation = 1; //cardinal direction of robot
int donechecking = 0;
int big = 0;
int finished = 0;
/*
orientation:
1 = foreward
2 = facing right
3 = facing backwards
4 = facing left
*/
int leftcheck = 0; //variables for checking which walls are there are which ones aren't
int rightcheck = 0;
int frontcheck = 0;
/*
1 = increase positionx
2 = increase positiony
3 = decrease positionx
4 = decrease positiony
*/
int pathing[500];
/*
1 = left turn
2 = forward
3 = right turn
*/
int num = 0;
void leftDegreeTurn(void) //90 degree turning code
{
high(26);
drive_goto(-29,-29);
drive_speed(0,0);
drive_speed(6, 27);
pause(2475);
drive_speed(0,0);
drive_ramp(-20,-20);
pause(1000);
drive_speed(0,0);
low(26);
if(orientation == 1) //changing orientation for a left turn (needs polishing)
{
orientation = 4;
}
if(orientation = 2)
{
orientation = 1;
}
if(orientation = 3)
{
orientation = 2;
}
if(orientation = 4)
{
orientation = 3;
}
}
void rightDegreeTurn(void)
{
high(26);
drive_goto(-29,-29);
drive_speed(0,0);
drive_speed(27, 6);
pause(2475);
drive_speed(0,0);
drive_ramp(-20,-20);
pause(1000);
drive_speed(0,0);
low(26);
if(orientation = 1) //changing orientation for a right turn (needs position)
{
orientation = 2;
}
if(orientation = 2)
{
orientation = 3;
}
if(orientation = 3)
{
orientation = 4;
}
if(orientation = 4)
{
orientation = 1;
}
}
void moveForward(void) //function for going foreward
{
drive_goto(55,55); //actually go forward
if(orientation = 1) //change in position
{
positionx += 1;
}
if(orientation = 3)
{
positionx -= 1;
}
}
void leftDegreeTurnBack() //function for turning backwards (used in function for escaping dead end)
{
high(26);
drive_goto(-29,-29);//BACK LEFT TURNchange orientation
drive_speed(0,0);
drive_speed(-6, -27);
pause(2475);
drive_speed(0,0);
drive_ramp(-20,-20);
pause(1000);
drive_speed(0,0);
low(26);
if(orientation = 1)
{
orientation = 2;
}
if(orientation = 2)
{
orientation = 3;
}
if(orientation = 3)
{
orientation = 4;
}
if(orientation = 4)
{
orientation = 1;
}
}
void rightDegreeTurnBack() //BACK RIGHT TURN same as other turn just backwards (needs polishing)
{
high(26);
drive_goto(-29,-29);
drive_speed(0,0);
drive_speed(-27, -6);
pause(2475);
drive_speed(0,0);
drive_ramp(-20,-20);
pause(1000);
drive_speed(0,0);
low(26);
if(orientation = 1)
{
orientation = 4;
}
if(orientation = 2)
{
orientation = 1;
}
if(orientation = 3)
{
orientation = 2;
}
if(orientation = 4)
{
orientation = 3;
}
}
void moveBackward(void) //function for going backwards
{
drive_goto(-55,-55); //go backwards
if(orientation = 1) //change in position
{
positionx -= 1;
}
if(orientation = 3)
{
positionx += 1;
}
}
void backupFromDead(void) //fucntion for backing up through the maze
{
while(pathing[num] != 0)
{
low(26);
if(pathing[num] = 1)
{
rightDegreeTurnBack();
pathing[num] = 0;
num -= 1;
pause(1);
}
else if (pathing[num] = 2)
{
moveBackward();
pathing[num] = 0;
num -= 1;
pause(1);
}
else if (pathing[num] = 3)
{
leftDegreeTurnBack();
pathing[num] = 0;
num -= 1;
pause(1);
}
}
pathing[num] = 5;
}
void backThroughTheMaze() //function for going back through the maze after completing it the first time
{
while(positionx !=1)
{
if (pathing[num] = 1)
{
rightDegreeTurnBack();
num -= 1;
pause(1);
}
else if (pathing[num] = 2)
{
moveBackward();
num -= 1;
pause(1);
}
else if (pathing[num] = 3)
{
leftDegreeTurnBack();
num -= 1;
pause(1);
}
}
}
int main()
{
sizeOfMaze = 11;
if(big == 0)
{
moveForward();
pause(5);
big = 1;
pause(5);
}
while(positionx != sizeOfMaze) //while not at the end of the maze
{
if(ping_cm(8) < 6) //check for left wall
{
leftcheck = 1;
pause(5);
}
else
{
leftcheck = 0;
pause(5);
}
pause(5);
if(ping_cm(7) < 10) //check for forward wall
{
frontcheck = 1;
pause(5);
}
else
{
frontcheck = 0;
pause(5);
}
pause(5);
if(ping_cm(6) < 6) //check for right wall
{
rightcheck = 1;
pause(5);
}
else
{
rightcheck = 0;
pause(5);
}
pause(10);
/*if(leftcheck == 0 && frontcheck == 1 && rightcheck == 1)
{
leftDegreeTurn(); //left
pathing[num] = 1;
pause(5);
num += 1;
moveForward();
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
}
if(leftcheck == 1 && frontcheck == 0 && rightcheck == 1)
{
moveForward(); //forward
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
} */
if(leftcheck == 1 && frontcheck == 1 && rightcheck == 0)
{
rightDegreeTurn(); //right
pathing[num] = 3;
pause(5);
num += 1;
moveForward();
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
}
/*if(leftcheck == 0 && frontcheck == 1 && rightcheck == 0)
{
leftDegreeTurn(); //left zero
pathing[num] = 1;
pause(5);
num += 1;
pause(5);
pathing[num] = 0;
pause(5);
num += 1;
moveForward();
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
}
if(leftcheck == 0 && frontcheck == 0 && rightcheck == 1)
{
leftDegreeTurn(); //left zero
pathing[num] = 1;
pause(5);
num += 1;
pause(5);
pathing[num] = 0;
pause(5);
num += 1;
moveForward();
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
}
if(leftcheck == 0 && frontcheck == 0 && rightcheck == 0)
{
leftDegreeTurn(); //left zero
pathing[num] = 1;
pause(5);
num += 1;
pause(5);
pathing[num] = 0;
pause(5);
num += 1;
moveForward();
pathing[num] = 2;
pause(5);
num += 1;
pause(10);
}
if(leftcheck == 1 && frontcheck == 0 && rightcheck == 0)
{
moveForward(); //forward zero
pathing[num] = 2;
pause(5);
num += 1;
pause(5);
pathing[num] = 0;
pause(5);
num += 1;
pause(10);
}
if(leftcheck == 1 && frontcheck == 1 && rightcheck == 1) //if all walls are detected, go back
{
high(26);
backupFromDead(); //backup till latest zero
}*/
}
finished = 1;
backThroughTheMaze(); //fucntion for going back through the maze
pause(100); //wait a bit
if(finished = 1)
{
num = 1;
if (pathing[num] = 1) //final runthrough which doesn't need a function
{
leftDegreeTurn();
pause(5);
num += 1;
pause(5);
}
else if (pathing[num] = 2)
{
moveForward();
pause(5);
num += 1;
pause(5);
}
else if (pathing[num] = 3)
{
rightDegreeTurn();
pause(5);
num += 1;
pause(5);
}
}
}
Any help would be appreciated. Thank you.
Comments
I don't have much else to offer at this point. I'll take a look at the properly formatted code (in this post) and see if anything pops out.
I generally find microcontrollers are pretty code at following what the code says. They tend to do what we tell them to do, which doesn't always match what we want them to do.
Edit: Actually I'm having a hard time knowing what to look for. What were you expecting the robot to do? What did the robot do?
Just a suggestion to help shorten your source listing a little. When a single statement follows an IF(...) statement the curly braces aren't needed. Simply place the following line below the IF(...) statement. Such as the corrected snippet below:
Probably better to use the Switch-Case procedure instead. Something like...
This way orientation only changes once through the block.