Preventing Overshoot?

So I’m working on a simple method to prevent overshoot on my robot. Do you guys have any suggestion?



void drivestraight(int d)
{
SensorValue[leftencoder]=0; //initializes motor encoder
while(SensorValue[leftencoder]<=d) //while the encoder value is less than the desired distance
{
motor[leftDriveMotor]=round((127*(d - SensorValue[leftEncoder])/d)); //left motor is equal to the proportion of x/127 = error/desired
motor[rightDriveMotor]=round((127*(d - SensorValue[leftEncoder])/d));//left motor is equal to the proportion of x/127 =error/desired
}
}

task main()
{
drivestraight(400); //starts the drivestraight function with a specified distance of 400
}


I personally like using a a full PID loop instead of proportion to prevent overshoot. Or at least PD. Try adding derivative and see if it helps, just tune your proportion so that with no Kd it oscillates slightly, then increase the Kd until it completely stops(don’t be intimidated by a large Kd, I have had a Kd 3x the size of Kp). Derivative is basically a brake for proportion.