View unanswered posts | View active topics It is currently Sat Jul 26, 2014 1:16 am






Reply to topic  [ 2 posts ] 
nMotorEncoderTarget Freeze 
Author Message
Rookie

Joined: Wed Nov 13, 2013 7:44 pm
Posts: 1
Post nMotorEncoderTarget Freeze
I am attempting to use the nMotorEncoderTarget to get controlled movement out of my robot. The problem is that if the target is not met, the motors have slowed down so much they cannot reach the target and the robot needs a bump to satisfy the target and move on in the program. Is there a way to abort the command or task after a set time and continue down the program? Please Help!


Wed Nov 13, 2013 7:49 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 556
Post Re: nMotorEncoderTarget Freeze
AHComets wrote:
I am attempting to use the nMotorEncoderTarget to get controlled movement out of my robot. The problem is that if the target is not met, the motors have slowed down so much they cannot reach the target and the robot needs a bump to satisfy the target and move on in the program. Is there a way to abort the command or task after a set time and continue down the program? Please Help!


The quickest and easiest way to solve this problem is to add a time condition in the while loop (assuming you are using a while loop to wait for the nMotorRunState of a motor to reach runStateIdle, as shown in the sample programs on the ROBOTC wiki: http://www.robotc.net/wiki/NXT_Function ... oderTarget)

Code:
nMotorEncoder[motorB] = 0;                // reset the Motor Encoder of Motor B
nMotorEncoder[motorC] = 0;                // reset the Motor Encoder of Motor C
nMotorEncoderTarget[motorB] = 360;        // set the  target for Motor Encoder of Motor B to 360
nMotorEncoderTarget[motorC] = 360;        // set the  target for Motor Encoder of Motor C to 360
motor[motorB] = 75;                       // motor B is run at a power level of 75
motor[motorC] = 75;                       // motor C is run at a power level of 75
 
while(nMotorRunState[motorB] != runStateIdle && nMotorRunState[motorC] != runStateIdle) 
// while Motor B AND Motor C are still running (haven't yet reached their target):
{
  // do not continue
}
motor[motorB] = 0;                       // motor B is stopped at a power level of 0
motor[motorC] = 0;                       // motor C is stopped at a power level of 0


To do this, we first need to choose a timer and clear it before we use it; the timers start tracking time from the start of the program, so any elapsed time up to the point it is used in the program will be returned by the timer. By clearing it, we ensure that we start the timer from a known quantity instead (a zero value). More information on using timers in a program can be found on the ROBOTC wiki: http://www.robotc.net/wiki/VEX2_Functions_Timing.

Clearing the timer can be done with the ClearTimer() command:

Code:
ClearTimer(T1);


Next, we simply need to add this into the while loop's condition with a logical AND (&&) operator:

Code:
nMotorEncoder[motorB] = 0;                // reset the Motor Encoder of Motor B
nMotorEncoder[motorC] = 0;                // reset the Motor Encoder of Motor C
nMotorEncoderTarget[motorB] = 360;        // set the  target for Motor Encoder of Motor B to 360
nMotorEncoderTarget[motorC] = 360;        // set the  target for Motor Encoder of Motor C to 360
motor[motorB] = 75;                       // motor B is run at a power level of 75
motor[motorC] = 75;                       // motor C is run at a power level of 75

ClearTimer(T1);

while(nMotorRunState[motorB] != runStateIdle && nMotorRunState[motorC] != runStateIdle && time1[T1] < 5000) 
// while Motor B AND Motor C are still running (haven't yet reached their target):
{
  // do not continue
}
motor[motorB] = 0;                       // motor B is stopped at a power level of 0
motor[motorC] = 0;                       // motor C is stopped at a power level of 0


For this example, I used the time1 command (time in milliseconds) and 5000 as the maximum value. The while loop now checks to make sure that motorB is not in a runStateIdle mode AND motor C is not in a runStateIdle mode AND that the T1 timer is less than 5000. Once one of these conditions becomes false (either by a motor going to the runStateIdle mode or by the T1 timer reaching a value of 5000ms), the motors will stop.

You may need to tweak the time value in order to increase/decrease the amount of time used by while loop. Decrease the value too much, and the loop may exit early. Increase the value too much, and the program will be wasting more time.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Thu Nov 14, 2013 1:35 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users 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.