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

Encoder program won't stop when it's supposed to
http://robotc.net/forums/viewtopic.php?f=11&t=9511
Page 1 of 1

Author:  aeichelberger [ Wed Oct 08, 2014 2:58 pm ]
Post subject:  Encoder program won't stop when it's supposed to

I am new to teaching ROBOTC, so I am sorry if this question is silly. A student of mine wrote a program that's meant to use shaft encoders to control distance. He wrote and successfully compiled the program. However, when he downloaded it to the robot, the program ran and ran without stopping. I can't seem to spot the error because I'm new at this as well. I've included his program below. Any help would be greatly appreciated :-)

task main ()
{
SensorValue[rightEncoder] = 0;
SensorValue[leftEncoder} = 0;

while(SensorValue[rightEncoder] < 687)
{
motor[rightMotor] = 63;
motor[leftMotor] = 63;
}

}

Author:  CARBOT [ Thu Oct 09, 2014 12:19 pm ]
Post subject:  Re: Encoder program won't stop when it's supposed to

There are a couple things that could be going on here:
1) It is possible that the encoder count is decreasing instead of increasing.
2) The commands to shut the motors off needs to be added before the last brace.
Good luck

Author:  aeichelberger [ Thu Oct 09, 2014 12:38 pm ]
Post subject:  Re: Encoder program won't stop when it's supposed to

Thanks for your answer! I had thought about your first suggestion, but if the robot is moving forward, shouldn't the numbers count up? Also, I didn't think you had to program the motors to stop. I thought they would stop on their own once the value of the encoder was reached.

Author:  terbos [ Thu Oct 09, 2014 12:41 pm ]
Post subject:  Re: Encoder program won't stop when it's supposed to

Also, as a matter of style, it's not necessary to keep setting the motor[] values inside the while loop. You'll typically want to turn the motors on once, then wait for the encoder, then turn the motors off. Like this:
Code:
motor[rightMotor] = 63;
motor[leftMotor] = 63;

while(SensorValue[rightEncoder] < 687)
{
}

motor[rightMotor] = 0;
motor[leftMotor] = 0;


Terry

Author:  terbos [ Thu Oct 09, 2014 12:49 pm ]
Post subject:  Re: Encoder program won't stop when it's supposed to

aeichelberger wrote:
Thanks for your answer! I had thought about your first suggestion, but if the robot is moving forward, shouldn't the numbers count up? Also, I didn't think you had to program the motors to stop. I thought they would stop on their own once the value of the encoder was reached.


The sign of the direction of the encoder is independent from the sign of the direction of the motor. You can typically control this in the Motor and Sensors setup if you want them to match, but not always. If a 2-wire motor's cable is flipped where it's plugged in, that will invert the direction of the motor in the same way that the "reverse" checkbox does in the Motor and Sensors setup.

motors will never stop on their own. They will remember whatever value you last set into them. That's why you only have to turn them on once before waiting for the encoder to count up. You then have to tell them to stop by assigning 0.

Granted, all motors should stop when the program ends as a safety feature, but it's considered good form to stop them on purpose before exiting from the main task.

Terry

Author:  JohnWatson [ Thu Oct 09, 2014 1:10 pm ]
Post subject:  Re: Encoder program won't stop when it's supposed to

aeichelberger wrote:
Thanks for your answer! I had thought about your first suggestion, but if the robot is moving forward, shouldn't the numbers count up? Also, I didn't think you had to program the motors to stop. I thought they would stop on their own once the value of the encoder was reached.


Quick note on this; if you are using the Optical Shaft Encoders (and not the Integrated Motor Encoders), then the polarity of the encoders (and if they increment/decrement when turned in a 'forward' direction) is going to be determined by the order that the encoder's wires are plugged into the Cortex. Optical Shaft Encoders use a two wire input system, and the two wires must be in sequential ports in order to work properly in ROBOTC.

If an Optical Encoder is decrementing (counting down) while the motor is moving in the forward direction, try flipping the ports that the wires are plugged into. For instance, if you are encountering this issue and the wires are plugged into ports 5 and 6, take the wire from 6 and plug it into 5, and the wire from 5 into 6. This should flip the polarity and resolve the issue for you.

Author:  aeichelberger [ Thu Oct 09, 2014 2:41 pm ]
Post subject:  Re: Encoder program won't stop when it's supposed to

Flipping the ports worked! Thanks so much for the tip!
-Amanda

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