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

Stop encoder loop after a set time?
http://robotc.net/forums/viewtopic.php?f=52&t=10505
Page 1 of 1

Author:  Cyks [ Mon Feb 09, 2015 10:01 pm ]
Post subject:  Stop encoder loop after a set time?

Hi,

Our robot sometimes can run into the wall and get stuck in autonomous, because it uses encoders and it tries to keep going until the encoder tics reach the set amount.

Here is some of our code now:

//Set the motor encoders to zero
nMotorEncoder[motorC] = 0;
nMotorEncoder[motorD] = 0;
nMotorEncoder[motorE] = 0;
nMotorEncoder[motorF] = 0;

//move forward
while ((nMotorEncoder[motorE] + nMotorEncoder[motorF]) / 2 < 600)
{
motor[motorC] = -40;
motor[motorD] = -40;
motor[motorE] = 40;
motor[motorF] = 40;
}
motor[motorC] = 0;
motor[motorD] = 0;
motor[motorE] = 0;
motor[motorF] = 0;

We want to make the current while encoder loop stop after a set time so that the program will continue to the next one even if it gets stuck. I would think that putting this inside a while(time1(T1) < 2000) loop wouldn't work, because it would get stuck on the encoder while loop and not read the time loop. Here is another way we though of doing it: Would this work the same way?

//Set the motor encoders to zero
nMotorEncoder[motorC] = 0;
nMotorEncoder[motorD] = 0;
nMotorEncoder[motorE] = 0;
nMotorEncoder[motorF] = 0;

//move forward
while (((nMotorEncoder[motorE] + nMotorEncoder[motorF]) / 2 < 600) || time1(T1) < 2000)
{
motor[motorC] = -40;
motor[motorD] = -40;
motor[motorE] = 40;
motor[motorF] = 40;
}
motor[motorC] = 0;
motor[motorD] = 0;
motor[motorE] = 0;
motor[motorF] = 0;

Thanks for any help! :biggrin:

Author:  MHTS [ Mon Feb 09, 2015 11:34 pm ]
Post subject:  Re: Stop encoder loop after a set time?

Try this instead.
Code:
//Set the motor encoders to zero
nMotorEncoder[motorC] = 0;
nMotorEncoder[motorD] = 0;
nMotorEncoder[motorE] = 0;
nMotorEncoder[motorF] = 0;
clearTimer(T1);
//move forward
while (((nMotorEncoder[motorE] + nMotorEncoder[motorF]) / 2 < 600) && time1(T1) < 2000)
{
    motor[motorC] = -40;
    motor[motorD] = -40;
    motor[motorE] = 40;
    motor[motorF] = 40;
}
motor[motorC] = 0;
motor[motorD] = 0;
motor[motorE] = 0;
motor[motorF] = 0;

Author:  Cyks [ Tue Feb 10, 2015 2:08 pm ]
Post subject:  Re: Stop encoder loop after a set time?

MHTS wrote:
Try this instead.
Code:
//Set the motor encoders to zero
nMotorEncoder[motorC] = 0;
nMotorEncoder[motorD] = 0;
nMotorEncoder[motorE] = 0;
nMotorEncoder[motorF] = 0;
clearTimer(T1);
//move forward
while (((nMotorEncoder[motorE] + nMotorEncoder[motorF]) / 2 < 600) && time1(T1) < 2000)
{
    motor[motorC] = -40;
    motor[motorD] = -40;
    motor[motorE] = 40;
    motor[motorF] = 40;
}
motor[motorC] = 0;
motor[motorD] = 0;
motor[motorE] = 0;
motor[motorF] = 0;

Ok thanks! But will this make the robot wait two seconds even if the set encoder tics has been reached?

Author:  MHTS [ Tue Feb 10, 2015 5:08 pm ]
Post subject:  Re: Stop encoder loop after a set time?

The code said while it hasn't reached target AND it's within 2 seconds, keep running. It means it will exit the loop if either it has reached target OR 2 seconds have expired.

Author:  roymaribel45 [ Sat Aug 15, 2015 5:43 am ]
Post subject:  Re: Stop encoder loop after a set time?

thanks for the code worked for me

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