There have been several questions recently about moving part of the robot until it has reached a certain position. I though I would post a RobotC example showing how to drive a motor forwards or backwards while monitoring an encoder. This same idea could be used for raising a lift or anything else where an encoder is used to monitor the rotation of a motor.
This is a great topic to talk about, so I'm going to do a related post about keeping an arm at a certain position with a potentiometer. I want to demonstrate the use of "fuzzy logic". I will use a simple mathematical function to prevent jerkiness in movement while keeping the arm at a certain position. If you want to find out more information on fuzzy logic (and I hope you do), look at this website: http://www.societyofrobots.com/programming_fuzzy_logic.shtml
Society of robots is a great site to visit for anything robotics related in general.
Anyway, here is an example:
const double ERR_MTRPWR_RATIO = .05;
const int MTR_THRESH = 20
const int MAX_ARM = 3000;
const int MIN_ARM = 500;
int armPosition = 0;
int motorPower = 0;
motorPower = (armPosition - SensorValue[armPot]) * ERR_MTRPWR_RATIO;
motor[arm] = abs(motorPower) > MTR_THRESH ? motorPower : 0;
armPos = 300;
This code keeps the motor power proportional to the error in the potentiometer value (the difference in the potentiometer value of where you want the arm to be, and where the arm is).
Here is a rundown of the constants:
ERR_MTRPWR_RATIO: How much the motor responds to one unit of error. How much the motor power will change for every potentiometer tick away from your expected arm position.
MTR_THRESH: Threshold below which the motor will not run. This is to prevent burned out motors and jerkiness. Since you will never exactly stay at the potentiometer value that you want (the ticks are very very small) this is to stop the motor from spasming when the error is very small.
MAX_ARM: The maximum potentiometr value that you want your arm to go to.
MIN_ARM: The minimum potentiometer value that you want your arm to go to.