Shop OBEX P1 Docs P2 Docs Learn Events
We have a problem with our robot running our code — Parallax Forums

We have a problem with our robot running our code

edited 2015-04-06 17:44 in Robotics
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.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-04-06 16:15
    As you can see, the forum software doesn't preserve indentations. You need to use code tags when posting code in order for the code to look correct. If you click on "Reply With Quote", you can seen the code tags I used in this post.
    #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);
    		} 
       }
    }
    

    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?
  • PublisonPublison Posts: 12,366
    edited 2015-04-06 16:19
    Here is a link to what Duane related to about posting code:

    attachment.php?attachmentid=78421&d=1297987572
  • edited 2015-04-06 16:22
    We were expecting the robot to go through a maze by detecting walls and using Sonar Sensors. We have functions for various things like turning, moving forward, etc. At the first turn however, it randomly stops mid function and moves forward. We have no idea why and have worked for nearly 5 hours trying to fix it.
  • Hal AlbachHal Albach Posts: 747
    edited 2015-04-06 16:40
    Just about all your if(....) statements are using the single "=" ASSIGNMENT operator instead of the double "==" EQUALITY operator inside the the IF parenthesis.
    if(orientation == 1)           //changing orientation for a left turn (needs polishing)     (This one is correct)
    {
      orientation = 4;
    }
    if(orientation = 2)             //  This one uses the assignment operator
    {
    orientation = 1;
    }
    if(orientation = 3)            // ... and this one
    {
    orientation = 2;
    }
    if(orientation = 4)           //  .... and this one
    {
    orientation = 3;
    }
                                      //  and many many more...
    

    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:
    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;
    
    
  • Hal AlbachHal Albach Posts: 747
    edited 2015-04-06 17:44
    You may also want to consider the flow of logic in your IF(...) statements. In the above listing in Post #5 suppose orientation was equal to 1 on entering this block of code. That satisfies the first IF(...) test and orientation now equals 4. The second and third test fall thru and orientation being equal to 4 now gets changed to 3 at the end of the block.
    Probably better to use the Switch-Case procedure instead. Something like...
    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);
    
      switch(orientation)
      {
        case 1:               // case lines must end with a colon
        orientation = 4;
        break;
    
        case 2:
        orientation = 1;
        break;
    
        case 3:
        orientation = 2;
        break;
    
        case 4:
        orientation = 3;
        break;
      }
    }
    
    

    This way orientation only changes once through the block.
Sign In or Register to comment.