I was wondering how I can program a continuously updating while loop in teleop mode. for example if i wanted to complete a 90 degree turn: I’ve been trying it as while(temp_gyro_angle<900) but the robot freezes. How do I continuously update the while loop and get out the the while loop when finished?
The robot will freeze because the loop interferes with the default 26 ms loop of the robot, causing the code error. As the robot’s running its normal loop anyways, you should just be able to use an if statement, as the PWM value will last until the next refresh, and the condition will be re-evaluated.
If you use a while loop, the program essentially “pauses” there and stops communicating with everything. You should use a global state variable. So, you would have something like:
int gyro_state = 0;
void check_gyro()
{
if (gyro_state == 0 && temp_gyro_angle >= 900)
gyro_state = 1;
if (gyro_state == 0) //temp_gyro_angle has always been less than 900
//insert code here
if (gyro_state == 1) //temp_gyro_angle has been more than 900
//insert code here
}
Can’t you also use the GetData() or PutData() function? I have been wondering if I could use those in a loop. If you could it would make life a lot easier.
I would NOT recommend messing with the GetData and PutData functions. Calling them twice in a single main loop may cause you big problems. The correct solution to the problem is what has been previously posted which is to use an “if” statement. The only appropriate use of a while loop in this context is for iterating through a list of processor tasks. Example:
while (x < 7)
{
put (Math.pow(x, 2), table[x]);
x++;
//This is pseudocode (I forget the appropriate power function and I made up
// put (int, int])
}