View unanswered posts | View active topics It is currently Fri Mar 06, 2015 4:18 am






Reply to topic  [ 4 posts ] 
Stop encoder loop after a set time? 
Author Message
Rookie

Joined: Mon Dec 15, 2014 9:48 pm
Posts: 14
Post 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:


Mon Feb 09, 2015 10:01 pm
Profile
Online
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1503
Post 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;


Mon Feb 09, 2015 11:34 pm
Profile
Rookie

Joined: Mon Dec 15, 2014 9:48 pm
Posts: 14
Post 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?


Tue Feb 10, 2015 2:08 pm
Profile
Online
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1503
Post 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.


Tue Feb 10, 2015 5:08 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: MHTS and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.