A simple way to avoid post sensor reading fading issue.

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.

3 thoughts on “A simple way to avoid post sensor reading fading issue.

  1. Hmm I am using something similar and it’s works quite stable. But I have two differences:
    1.rightMiddleValue should be a little bigger then real right middle value (and leftMiddle Value of course too). It helps going straight when there’s only wall on one side.
    2. When we’re are between two walls:
    If(leftSensor > leftMiddleValue && rightSensor > rightMiddleValue)
    errorP = leftSensor-rightSensor;
    If we are using my first suggestion this condition is needed for stability.
    Well, to be honest, it’s only mix of this and previouse article 😉

    I have some difficulties with registration on forum. There’s no activation email for my account ;/

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.