ROBOTC.net forums
http://robotc.net/forums/

Problems with while statements for turning?
http://robotc.net/forums/viewtopic.php?f=1&t=1811
Page 1 of 1

Author:  inn0v4t3 [ Thu Aug 20, 2009 5:33 pm ]
Post subject:  Problems with while statements for turning?

Hey guys,

Currently building a robot that can navigate around a course to rescue a can (this is for a robotics competition). I have chosen RobotC over NXT-G, and I must say it is better software. However, I'm having a few issues...

1) I want the robot to follow a black line (it does this fine) however when it reaches an intersection, there will be a green square. I wish to have the robot turn the direction it sees the square. The one exception of this is where it sees black on both sensors, which means I want it to go straight forward.

I have come up with the following code*:

*Note: S1 is the left light sensor, S2 the right light sensor, motorA the left motor, motorB the right motor.
greenleft/greenrightmax are the maximum readings for green. As black is darker, I use if less than or equal to statements.

Code:
while(SensorValue(S1) <= greenleftmax && SensorValue(S2) <= greenrightmax)
    // Both are seeing green/black
      {
         turning = 1; // Prevent normal line following
         if(turning == 1)
         {
         leftdifference = greenleftmax - SensorValue(S1);
         rightdifference = greenrightmax - SensorValue(S2);
         turning = 2;
        }
         if(leftdifference > rightdifference  && turning != 0) // Green is right, turn right
         {
         nxtDisplayTextLine(1, "Leftd: %d", turning);
         nxtDisplayTextLine(2, "Leftd: %d", leftdifference);
         nxtDisplayTextLine(3, "Rightd: %d", rightdifference);
        motor[motorA] = 25;
        motor[motorB] = -20;
        wait1Msec(1000);
        motor[motorA] = 20;
        motor[motorB] = 20;
        turning = 0;
        }
         if(rightdifference > leftdifference && turning != 0) // Green is left, turn left
         {
         nxtDisplayTextLine(2, "Leftd: %d", leftdifference);
         nxtDisplayTextLine(3, "Rightd: %d", rightdifference);
         motor[motorA] = -20;
        motor[motorB] = 25;
        wait1Msec(1000);
        motor[motorA] = 20;
        motor[motorB] = 20;
         turning = 0;
        }
         if(rightdifference >= 5 && leftdifference >= 5 && turning != 0)// Both are black, go straight
         {
         motor[motorA] = 20;
        motor[motorB] = 20;
        turning = 0;
        }
      }


Normal line following looks like this:
Code:
   while(SensorValue(S1) > greenleftmax && SensorValue(S2) > greenrightmax && turning == 0) //if both see white
      {
    nxtDisplayCenteredTextLine(2, "Both WHITE");
    nxtDisplayCenteredTextLine(2, "Both WHITE");
    motor[motorA] = 20;
    motor[motorB] = 20;
      }
   while(SensorValue(S1) <= greenleftmin && SensorValue(S2) > greenrightmax && turning == 0)  //if left sees black
         {
         nxtDisplayCenteredTextLine(2, "Left BLACK");
        nxtDisplayCenteredTextLine(4, "Right WHITE");
        motor[motorA] = -25;
        motor[motorB] = 20;
        }
  while(SensorValue(S2) <= greenrightmin && SensorValue(S1) > greenleftmax && turning == 0)  //if right sees black
      {
    nxtDisplayCenteredTextLine(2, "Left WHITE");
    nxtDisplayCenteredTextLine(4, "Right BLACK");
     motor[motorA] = 20;
     motor[motorB] = -25;
      }


The problem is when the robot comes up to an intersection, it just starts spinning around (as though executing motor[motorA] = -20; motor[motorB] = 25;) forever. It also doesn't go straight when hitting two black lines; it spins around in a circle. Hopefully this logic will complete the course (can't find how to do it properly anywhere, and have no mentor for this). I would love to do well in this competition and get others to use RobotC rather than NXT-G (which everyone seems to use).

Here is what I am working with:
Image

Example video:
http://www.youtube.com/watch?v=bRtBV5BZ4uQ

Any help with these issues would be greatly appreciated.

Thank you.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/