Since lots of people have this issue when mouse running though the post, the unstable reading makes mouse wobbling around this area. To solve this, you can just simply use your side sensors and diagonal sensors alternatively to avoid the post reading fading issue if your mouse has 6 sensors. However, for a 4 sensor mouse, there is no extra sensor you can use to void post when running by this way. So here is a simpler way to fix this without doing too much extra work.(assume all sensors are un-linearized)
If(leftSensor > leftMiddleValue && rightSensor < rightMiddleValue)//case 1
errorP = leftMiddleValue – leftSensor;
Else if(rightSensor > rightMiddleValue && leftSensor < leftMiddleValue)//case 2
errorP = rightSensor – rightMiddleValue;
Else //case 3 do nothing, let encoder to align the mouse
errorP = 0;
errorD = errorP – oldErrorP;
oldErrorP = errorP;
totalError = kp*errorP + kd*errorD;
setLeftPwm(leftBaseSpeed – totalError);
setRightPwm(rightBaseSpeed + totalError);
the advantage for this code is it only use sensor to alignment when one side is larger than threshold where other side smaller than threshold. So the mouse won’t use sensor value when both sensor smaller than threshold when passing the post, and it automatically goes to case 3 only use encoder to align the mouse. I was inspired from the lecture that Ng Beng Kiat made at the year 2009 in Taiwan and thanks for his help here :).
The disadvantage for this code is if you don’t have speed profile to make your mouse running straight with encoder feedback, your mouse won’t be able to align itself when the mouse is away from middle point when there is only one wall available to use. You will need to add more else if case to fix this.
Even thought it will avoid most of the post fading issue but there is still some exceptions. If the post is plastic and the wall is wood, since the reading from plastic post is higher than wood wall, the mouse will still jerk during the process.
Note that if you have rotational speed profile, you only scale and add errorP to the rotational error. If you don’t have any speed profile due to the low resolution of your encoder, you can just simply use the entire code above to make mouse go straight during the wall-to-no-wall or no-wall-to-wall transitions. The method I mentioned here is also valid except you will need more efforts to avoid the post when passing through.