# Proportion Control and Deceleration Programming Help

We are trying to get our autonomous routines perfect. We want to be able to drive forward 500 clicks in a straight perfect line, and not worry about error (such as curving, jolting past desired position, and getting off track). How would we implement Proportion Control (is that PID?) and Acceleration/Deceleration into this?

Right now, we can mostly go to 500 clicks and be within 30 or so clicks of the desired location. Again, how can we implement those items into this Move code to work? Would using the gyro with this function help keep the robot in a straight line, or would the Encoders work better for that? Maybe both working together?

Thanks guys!

Most of the time the extra distance would be steady-state error,so it wouldn’t obviously effect the behaviour.Using line follower to detect white line would be a good way to know where you exactly locate.PID would be a good way,but it would cost more time in order to decrease the error.Most time i use a normal while loop and a brake to use encoder,and use line follower to detect the position as well as line up with it to remove deviation.

For moving straightly,24C used a remapping to pair different speed.24C’s Motor Control Value Remapping

And Mr Jeams told me to use encoder real time and make one motor parented to another.

But i think that using line follower to remove deviation is good enough for me.If i have time,i would try to use encoder to synchronize both sides.

(Please note, the code in this post is (for the most part) NOT tested.)

``````
while((leftExit == false) || (rightExit == false)) { //You should be able to replace your while loop for this code...
leftOutput = leftTicks-nMotorEncoder[LB]; //Proportional, works for fwd and rev
rightOutput = rightTicks-nMotorEncoder[RB]; //Proportional, works for fwd and rev

if (abs(leftOutput)<EC_tolerance) leftExit=true; //Exit code
if (abs(rightOutput)<EC_tolerance) rightExit=true; //Exit code

capValue(REV, leftOutput , FWD); //sync left/right
capValue(REV, rightOutput , FWD); //sync left/right

int addL = nMotorEncoder[RB]-nMotorEncoder[LB]; //sync left/right
int addR = nMotorEncoder[LB]-nMotorEncoder[RB]; //sync left/right

}
``````

Now of course you need capValue(x,y,z). You can define it…

``````#define capValue(Min,Value,Max)   Value = (Value<Min)? (Min):(Value); Value = (Value>Max)? (Max):(Value)
capValue(REV, rightOutput , FWD);
``````

… or you can use pointers. (Above code would need to be modified for pointer use, I believe.)

``````int capValue(int min, int* val, int max) {
int result = &val;
result = (result < min) ? min : result;
result = (result > max) ? max : result;
return result;
}
capValue(REV, *rightOutput , FWD); //note the asterisk!!!
``````

Also, REV and FWD need to be defined:

``````#define FWD 127
#define REV (-127)``````

Questions? Comments? EDIT: The proportional control has controlled deceleration, but not acceleration. But I think that it is the deceleration that is important.
EDIT 2: The encoders probably will keep you straighter due to gyro drift, but I am not sure.