Trying to program maze solving bot. Beginner needs help

Hi, i just started using Vex robotics a little while ago. I started programming in easyC and wanted to start by building a maze solving robot. I’ve written some code, but it’s really buggy and the original does work, tho not what i want it to do. Can somebody tell me what i did wrong? Im trying to make it hit a wall, back up, and turn 90 degrees. It’ll do this for two bumbs, on the third it would make a 180 degree turn. Nobody that i knows can do this because our school club started only a year ago.
MazeSolvingRobot.zip (22.6 KB)

First Off, welcome to Vex Robotics… There are many experienced “Vexers” here, and they are willing to help with questions…

I loaded this in EasyC 2.x, but it looks like it could be an EasyC 1.1 program.


#include "Main.h"

void main ( void )
{
      int bumper1; 
      unsigned int x; 

      while ( 1 )
      {
            bumper1 = GetDigitalInput ( 9 ) ;
            if ( bumper1 == 1 )
            {
                  SetPWM ( 1 , 20 ) ;
                  SetPWM ( 2 , 235 ) ;
                  SetPWM ( 3 , 20 ) ;
                  SetPWM ( 4 , 235 ) ;
            }
            else
            {
                  x + 1 ;
                  while ( x <= 3 )
                  {
                        x ++ 1 ;
                        SetPWM ( 1 , 250 ) ;
                        SetPWM ( 2 , 5 ) ;
                        SetPWM ( 3 , 250 ) ;
                        SetPWM ( 4 , 5 ) ;
                        Wait ( 1000 ) ;
                        SetPWM ( 1 , 235 ) ;
                        SetPWM ( 2 , 235 ) ;
                        SetPWM ( 3 , 235 ) ;
                        SetPWM ( 4 , 235 ) ;
                        Wait ( 1000 ) ;
                        // estimate of how long to turn 90 degrees 
                        if ( bumper1 == 1 )
                        {
                              SetPWM ( 1 , 20 ) ;
                              SetPWM ( 2 , 235 ) ;
                              SetPWM ( 3 , 20 ) ;
                              SetPWM ( 4 , 235 ) ;
                        }
                        else
                        {
                              return  ;
                        }
                  }
                  while ( x <  3 )
                  {
                        SetPWM ( 1 , 250 ) ;
                        SetPWM ( 2 , 5 ) ;
                        SetPWM ( 3 , 250 ) ;
                        SetPWM ( 4 , 5 ) ;
                        Wait ( 1000 ) ;
                        SetPWM ( 1 , 20 ) ;
                        SetPWM ( 2 , 20 ) ;
                        SetPWM ( 3 , 20 ) ;
                        SetPWM ( 4 , 20 ) ;
                        Wait ( 1000 ) ; // estimate of how long takes to turn left
                        if ( bumper1 == 1 )
                        {
                              SetPWM ( 1 , 20 ) ;
                              SetPWM ( 2 , 235 ) ;
                              SetPWM ( 3 , 20 ) ;
                              SetPWM ( 4 , 235 ) ;
                        }
                        else
                        {
                              return  ;
                        }
                        continue ;
                  }
            }
      }
}

With the above code, I see your Pseudo Code looking like:




Program Start
	Variables:  integer bumper
			unsigned integer x
				
	Endless While
		
		Read Bumper #1  ( Input #9 assigned to variable bumper )
		Test Bumper #1	(Has it been Pressed??)
		
		IF Bumper #1 PRESSED
			Set Motor #1 Almost FULL Clockwise
			Set Motor #2 Almost FULL Counter Clockwise
			Set Motor #3 Almost FULL Clockwise
			Set Motor #4 Almost FULL Counter Clockwise
			
		Bumper #1 NOT PRESSED
			
			( x + 1 )	What is the intent of this code??
			
	===>	The following line Tests a Variable (x) with an UNKNOWN Value
	While Variable x is LESS THAN or EQUAL to 3 
				
	===>	The following line will most likely increment the Variable x, by 1, but I can not be quite sure with out testing it..
		( x ++ 1 )	What is the intent of this code??

			Set Motor #1 FULL Counter Clockwise
			Set Motor #2 FULL Clockwise
			Set Motor #3 FULL Counter Clockwise
			Set Motor #4 FULL Clockwise
				
			Delay for 1000 Milliseconds

			Set Motor #1 Almost FULL Counter Clockwise
			Set Motor #2 Almost FULL Counter Clockwise
			Set Motor #3 Almost FULL Counter Clockwise
			Set Motor #4 Almost FULL Counter Clockwise
				
			Delay for 1000 Milliseconds

		===>	The following line will ALWAYS Test FALSE, because you have not READ Bumper #1 again..
		Test Bumper #1	(Has NOT been Pressed, otherwise we would not be in this part of the code )
				
			Set Motor #1 Almost FULL Clockwise
			Set Motor #2 Almost FULL Counter Clockwise
			Set Motor #3 Almost FULL Clockwise
			Set Motor #4 Almost FULL Counter Clockwise
					
		===>	The following section will ALWAYS Execute, because the Test above will ALWAYS Test FALSE..
		===>	This is actually BAD, because ( return) forces an EXIT from the Current Function, back 
			to the Calling Function, which in this case is the StartUp Code that calls main( )..
			This will most likely cause you Robot to stop operating.
				ELSE, Bumper #1 NOT PRESSED 
				
					RETURN from this Function to Calling Function.

				END OF Test Bumper #1
	
			END OF While Variable x is LESS THAN or EQUAL to 3 
				
	===>	The following line Tests a Variable (x) with an UNKNOWN Value
	===>	The following line Tests a Variable (x) with a Value of 4, if 
			the above While executed until a number greater than 3 was reached.
	But if the  Variable (x) was unknown, it could be any Value 
	Greater than 3.
	
	While Variable x is LESS THAN 3 

		Set Motor #1 FULL Counter Clockwise
		Set Motor #2 FULL Clockwise
		Set Motor #3 FULL Counter Clockwise
		Set Motor #4 FULL Clockwise
				
		Delay for 1000 Milliseconds

		Set Motor #1 Almost FULL Counter Clockwise
		Set Motor #2 Almost FULL Counter Clockwise
		Set Motor #3 Almost FULL Counter Clockwise
		Set Motor #4 Almost FULL Counter Clockwise
				
		Delay for 1000 Milliseconds

		===>	The following line will ALWAYS Test FALSE, because you have not READ Bumper #1 again..
		Test Bumper #1	(Has NOT been Pressed, otherwise we would not be in this part of the code )

			Set Motor #1 Almost FULL Clockwise
			Set Motor #2 Almost FULL Counter Clockwise
			Set Motor #3 Almost FULL Clockwise
			Set Motor #4 Almost FULL Counter Clockwise

		===>	The following section will ALWAYS Execute, because the Test above will ALWAYS Test FALSE..
		===>	This is actually BAD, because ( return) forces an EXIT from the Current Function, back 
			to the Calling Function, which in this case is the StartUp Code that calls main( )..
			This will most likely cause you Robot to stop operating.
			ELSE, Bumper #1 NOT PRESSED 
				
				RETURN from this Function to Calling Function.

			END OF Test Bumper #1
				
		===>	The following line is Superfluous, because there is NO Test Condition, SO the Default is to Continue..
			CONTINUE		
				
			END OF WHILE X LESS THAN 3
			
		END OF BUMPER #1 NOT PRESSED
		
	END OF Endless While
	
END OF MAIN FUNCTION
		

Start by writing a little story of what you want the Robot to do, like above, but include direction (left or right), and then work on each part, one step at a time… If it helps, you can be the robot… You move Forward to the obstacle, bump, backup, turn Left 90 Degrees, move Forward, bump, backup, turn Left 90 Degrees… Note ALL steps and actions, make a list, make a story…

For instance, Have you Robot move forward, and when the bumper is hit, Back-Up and turn Left 90 Degrees, then Stop. If you do that twice, you should be pointed back the direction you started from.

First off, i want to say thanks for the help.
I progressed with the robot and i can make it do some things that i want. It hits an object, backs up and turns 90 degrees to the left and continues. When it hits another object, it backs up and turns 180 degrees. To do this, i used a For loop while trying to tell it to only run until my “Bumper” variable is equal to one. Still, the robot continues to turn 180 instead of returning to the previous loop. Can anyone tell me why it does this?
maze solver.zip (22.6 KB)

I’m rather handicapped by not being able to see your more recent code posting, but I took a quick look at your first posting and I don’t see anywhere that you are resetting the “x” after escaping from the first encounter.

On what may be a related note, you have a “x + 1” in a place where, it appears, you might well want to initialize the variable. Is, perhaps, the problem due to a typographical error?

The other thought that occurs to me is that it would be helpful to define a waiting period for resetting the bump counter. That is, if the robot moves for some specified time, you consider it to have progressed to the next encounter. Put very simply:

  1. Check bumper
    2a) If bumper has not been hit, keep moving.
    2b) If bumper has been hit, turn left, then advance.
  2. Wait a couple of tenths of a second.
  3. Check bumper
    5a) If bumper has not been hit, keep moving. If still moving after 1 second, reset the bump counter.
    5b) If bumper has been hit:
    i) turn backwards and advance
    ii) wait a couple of tenths of a second.
    iii) check bumper
    iv,a) if bumper has not been hit, keep moving. If still moving after 1 second, reset the bump counter.
    iv,b) if bumper has been hit, turn right and advance (This should have you moving in the reverse of your original direction, which is what it appears you need to do if you’ve hit a dead end.)

However, I leave to you to figure out what to do next, as this algorithm could be frustrated by having a maze that requires the robot to turn through a gap in a wall of a clear passage. For example, I believe the robot would exit through the entrance of a maze that started thusly (ignore the periods, please; this systems suppresses multiple spaces, making 'ASCII art" a bit of a challenge):


R>… |
-------… ------------
…| |
…| |
…| |

Good Luck,
Eric