I have a 4 bar that I’m trying to limit with a shaft encoder. My goal is something as follows: When the lift is at the bottom and the shaft coder reads “x” value, don’t let it go down any more, and same thing with the up position. I’ve tried for hours to format if statements in robot C, and I haven’t had any luck. Can anyone help me do this the right way. Thanks
Have you tried using logical “AND” in your if-statements?
http://cdn.robotc.net/pdfs/natural-language/hp_boolean_logic.pdf
The general idea might be something like this:
Zero your encoder value at the start.
If ((UpButton is pressed) AND (EncoderValue <MaxHeightValue))
Have the arm motor go up.
If ((DownButton is pressed) AND (EncoderValue >MinHeightValue))
Have the arm motor go down.
Otherwise, be sure to shut off the motors.
Be on the lookout for overshoot - that your arm isn’t going too high or banging against the floor, etc.
Driver or Autonomous?
In driver, I’d use something like this:
int loweringAllowed;
int liftingAllowed;
// test whether the lift is below lower limit
if (SensorValue[myEncoder] < lowerLimit)
{
loweringAllowed = 0;
}
else
{
loweringAllowed = 1;
}
// test whether the lift is at or above upper limit
if (SensorValue[myEncoder] > upperLimit)
{
liftingAllowed = 0;
}
else
{
liftingAllowed = 1;
}
motor[lift] = ((vexRT[Btn8U] * liftingAllowed * 127) - (vexRT[Btn8D] * loweringAllowed * 127));
Note that this assumes myEncoder is set up as the sensor you’re reading, and that lowerLimit and upperLimit are integer variables preset to your desired limits. You could, of course, put hard numbers in place of that.
Also, this example assumes you’re using 8U and 8D to control the lift. You probably aren’t, so that would have to change to whatever you use in your driver code.
@6916H My team used a button for a limit switch for the past two years. It make it go up a little higher at the start, but I set it so that when the button is pressed, the motors are set to 0 at the lowest point on our lift. It works really well for going down. For up I would recommend something similar to what FullMetalMentor posted. If your lift goes down immediately after it reaches max height, the lift might need more rubber bands, use pid, or you could set the motors to a value of around +/-15 in the up direction, but I would recommend one of the first two. There also might be overshoot due to the momentum going up fast and making the arm continue to rotate when no power is being used. I also often find potentiometers are better for lifts than quad encoders due to them being smaller and often lifts have a low enough rotation that potentiometers are very accurate for the lift. Hope this helps