driving during "if" statements that effect the drive

how do I make my robot drive when “if” statements are going affecting the drive train.
like keeping the drive from drifting off course with gyro keeping it from drifting while driving forward. can you please tell me how to fix it? thanks.

Hi nathanpitts2002,

Are you using ROBOTC or Modkit for VEX?

Regards,

  • Art

I am using ROBOTC text-based

Hi Nathan,

We have several programs in our Sample Programs that may get you started with what you are attempting to do (from what it sounds like, you would like to generate an ‘Auto-straightening’ program). Would you be able to send us what you have (using the

 tags) and give us a more detailed explanation of what you would like to do?

Generally, you can use if statements to make decisions in your code as such:


//Rough psuedocode:

if (gyro sensor is greater than a value)
{
//turn one way
}

else if (gyro sensor is less than a value)
{
//turn the other way
}

else
{
//move straight forward
}



If you place that code inside of a loop (and fill in the psuedocode with actual ROBOTC code) you can perform an auto-straightened movement of sorts using the gyroscope.

what do you mean by putting the code in a loop?

#pragma config(Sensor, port1,  gyro1,          sensorVexIQ_Gyro)
#pragma config(Sensor, port3,  colorS1,        sensorVexIQ_ColorHue)
#pragma config(Motor,  motor6,          driveL,        tmotorVexIQ, PIDControl, driveLeft, encoder)
#pragma config(Motor,  motor7,          driveR,        tmotorVexIQ, PIDControl, reversed, driveRight, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

{
 while(true)
 {
  moveMotorTarget(driveL, 4000, 70);
  moveMotorTarget(driveR, 4000, 70);
 }
 if(getGyroDegrees(gyro1) > 10)
 {
  repeatUntil(getGyroDegrees(gyro1) == 0)
  {
   setMotorSpeed(driveL, 60);
   setMotorSpeed(driveR, 30);
  }
  setMotorSpeed(driveL, 0);
  setMotorSpeed(driveR, 0);
 }
 else if(getGyroDegrees(gyro1) < 350)
 {
  repeatUntil(getGyroDegrees(gyro1) == 0)
  {
   setMotorSpeed(driveL, 30);
   setMotorSpeed(driveR, 60);
  }
  setMotorSpeed(driveL, 0);
  setMotorSpeed(driveR, 0);
 }
}

here’s the code.

There are a couple of issues that you will run into with this code.

First, the code shown has no task main() designation (which should go before the first curly brace in your code, before the while(true) loop). Task main is required for every ROBOTC program to run properly.

Second, the first thing the program does is enters an infinite while(true) loop. This loop will only loop through the commands inside of its curly brace set; in this case, the two moveMotorTarget commands:



task main()
{
    //Reset the left encoder
    resetMotorEncoder(driveL);

    //While the left encoder's value is less than 4000
    while(getMotorEncoder(driveL) < 4000)
    {
        // If the robot is turned to the left
        if (getGyroDegrees(gyro1) > 10)
        {
            //Counter steer to the right
            setMotorSpeed(driveL, 60);
            setMotorSpeed(driveR, 30);
        }

        //If the robot is turned to the right
        else if(getGyroDegrees(gyro1) < 350)
        {
            //Counter steer to the left
            setMotorSpeed(driveL, 30);
            setMotorSpeed(driveR, 60);
        }
        //If the robot is running straight
        else
        {
            //Keep going straight
            setMotorSpeed(driveL, 60);
            setMotorSpeed(driveR, 60);
        }
    }
    //Once we reach the target value of 4000 counts
    //Turn the motors off
    setMotorSpeed(driveL, 0);
    setMotorSpeed(driveR, 0);

}

Let us know if you run into any questions and we’ll be more than happy to help.