If you are still wondering on how to do this, if you have encoders attached to your wheel axles, you can compare their values to your target position and rotate the motors accordingly. Something like (with a bit of refinement and edits):
//pseudocode
int a = value of encoder;
while(true)
{
int encodervalue = value of encoder;
int target = your target position;
while((encodervalue - a) < target)
{
motor = 127;
}
while((encodervalue - a) > target)
{
motor = -127;
}
if encodervalue = target
motor = 0;
}
//end pseudocode
I would appreciate if someone would verify this, this is my first year doing Vex
If you are still wondering on how to do this, if you have encoders attached to your wheel axles, you can compare their values to your target position and rotate the motors accordingly. Something like (with a bit of refinement and edits):
//pseudocode
int a = value of encoder;
while(true)
{
int encodervalue = value of encoder;
int target = your target position;
while((encodervalue - a) < target)
{
motor = 127;
}
while((encodervalue - a) > target)
{
motor = -127;
}
if encodervalue = target
motor = 0;
}
//end pseudocode
I would appreciate if someone would verify this, this is my first year doing Vex
What you have above is bang bang control. Essentially there are only two states, forward at full speed and backwards at full speed. The issue with this system is that it will have a lot of oscillation and drift. Your robot will most certainly not end up on the exact target value because it has drifted past. When it tries to recover, it will drive at full speed back, again drifting past the target and so on and so forth…
A better solution would be to use a PID control (or at the very least a P, or PD). You would have a proportional aspect
target-current
, an integral aspect
integral += time * current
and a derivative aspect
(current - last) / time
. The speed of the motors would be the total of the three aspects times their tuning value (k)
proportional * kp + integral * ki + derivative * kd
. Much of the difficulty comes of course from the way that you tune the PID. Different methods of tuning will have different amounts of oscillation as well as responsiveness. In this case, google is your friend, look up different methods of tuning a PID. You can also look into using OkapiLib with PROS. I believe it includes a way to make a PID (without having to do the math described above) but also a PID tuning class that can save you the trouble of having to do it yourself. If you find that a simple PID is to simple or not good enough for your purposes, you can try other control systems like feedforward.