How do you prevent a lift from going higher than a given point using RobotC.
Try using a physical stop as they will probably be more efficient and accurate, unless you want to have your lift stop at different levels.
If your motor(s) dont have enough power, then physical stopper. It’s better either way
Use an encoder on one of the gears. Once it hits a certain number of degrees, tell the program to stop. Rubber band the lift to prevent it from going back down.
That’s just another way to do it. A mechanical stop should l work just fine
Edit: Potentiometer, not encoder. Thanks @Connor
Thx for the correction, I get those mixed up on occasion
If you are unable to have a physical stop or your lift has multiple positions, I would suggest getting into PID. Here is an example code I have written in ROBOTC a while ago to test my PID skills:
#pragma config(Motor, motor1, PIDMotor, tmotorVexIQ, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main()
{
float kP = 1.2;//The sensitivity multiplier for the "P"
float kI = 0.2;//The sensitivity multiplier for the "I"
float kD = 5;//The sensitivity multiplier for the "D"
float deadzone = 5;//This is the zone to be considered centered
int error;//Create a variable called "error"
int motorPower;
int previousError;
int totalError = 0;
int derivative;
int desiredValue;
while(1==1){
if(getJoystickValue(BtnLDown)) desiredValue = 100;
else desiredValue = 300;
error = desiredValue - SensorValue[PIDMotor];//This is the "P"
totalError += error;//This is the "I" which is calculated from "P"
if(abs(error) < (deadzone/2)){//This is the deadzone. This resets the "I" to 0 so the value doesn't persist after the position is reached
totalError = 0;
}
derivative = error - previousError;//This is the "D" which is calculated from "P" and "P" 20 miliseconds ago.
motorPower = error * kP + totalError * kI + derivative * kD;//Apply the P, I, and D with their multipliers to a variable
motor[PIDMotor] = motorPower;//Apply the motor power variable to the motor
wait1Msec(20);
previousError = error;//This is used to calculate the "D"
}
}
If you have any questions, feel free to ask
If you’re unaware and need a refresher of coding knowledge:
- floats are numbers with decimal places
- integers are numbers without decimal places
totalError += error;
is considered the same as
totalError = totalError + error;
- P is error, or the difference between desired and current value
- D is derivative, or a value that would dampen the speed so the robot reaches the spot more precisely without oscillations
- I is integral or totalError, or a value that would continuously add error. This is so if there’s a slight difference in weight or the arm didn’t reach its position the I would increase more and more until the arm starts moving towards the position
- kP, kI, and kD are just tuning variables that you use as a multiple to change the sensitivity of error, totalError, and derivative.
- abs() is the same as “absolute value of” what’s inside the parenthesis
- All code reads from the top to bottom
- While loops repeat if the conditions are true. So if it’s “while(1==1)” since 1 is equal to 1 then the while loop will continuously run forever until the robot is turned off.
- Using a mere “wait1Msec(20);” in your while loops will free up the Cortex’s CPU so it can prevent crashing (I’ve learned the hard way)
EDIT//: I didn’t realize that the category is “VEX IQ General Discussion” and “Official Answers - Ask the VEX Staff!” Although I am not a staff member, and this is VEX IQ, you can still use PID on your robot as it’s more reliable and dependable. You should be able to translate this into VEXIQ ROBOTC, so if you need help doing so I’m definitely available. Shame on us for not realizing the categories correctly. I am deeply sorry, @DRow would it be possible to check these responses and see if they are related to this thread or not?
EDIT #2//: I reconfigured the code to VEXIQ, but you may need to test it out.