Here we implement some simple proportional control. Based on the distance from the wall, we begin by adjust the speed of only one motor. This method works as follows:
Note: The left motor is held at a constant speed.
void followWallBeta() {
delay(500); //wait for us to lift our finger off the button.
//sets default motor speeds
leftMotorSpeed=leftMotorDefaultSpeed;
rightMotorSpeed=rightMotorDefaultSpeed;
long error;
while (digitalRead(LineSensor)==HIGH) {
long pingTime = ping();
if (pingTime!=0) {
currentPingDistance = microsecondsToCentimeters(pingTime);
error = wallDistance-currentPingDistance; //error
rightMotorSpeed=rightMotorDefaultSpeed+(Kp*error);
} if (pingTime==0) {
currentPingDistance = microsecondsToCentimeters(pingTime); //this simply sets it up to log that a 0 value was returned from ping sensor
}
rightMotorSpeed = constrain(rightMotorSpeed, 40, 255); //constrains data
Forward(0); //this updates the motor speed values we just set above
if (useSave) saveData(useSave); //logs data
delay(100); //only check 10 times a second. :)
}
Stop();
currentMenu = draw;
Kd=-1;
Kp=-1;
}
Below are the results from testing some various Kp runs. Alongside each chart is a video of that run.
Note: In some cases, we removed the data after our robot lost the wall, so that all charts were the same scale, and to show it was performing before it lost the wall.
No video available for this run.
In our next trial, the robot over-corrected and ran into the wall.
This seems to be the best result so far.
This is just a closer look at the data from above before the robot veered off course.
After these initial tests, it looks like a Kp in the range around 6 seems to work the best.
One of the issues we faced at first was failing to correct for a maximum motor adjustment. For example. If the robot is out by 10 cm, and the Kp value is very high, then the robot could potentially set its motor speeds to a higher-than-maximum value, resulting in the motors going to unexpected speeds. For example, if our base speed is 160, and the Kp value is 10, an error of just 10 cm would produce a new motor speed of 260, over and above the max value of 255! As a result, we had to cap our motor speeds, so a really high error would not over set our motor speeds.
Currently the robot will go for about once oscillation before the robot completely loses the wall and crashes into it. Additionally, if there is a sharp change in the wall, the robot does not correct fast enough.