Distance Sensor programming trouble

I’m trying to make my robot stop at a perfect position about 114 - 120 mm away from the wall and right now I have in my code that should make it stop at a certain distance but it goes forward to far and I have checked up and changed the distance so it is really low and it still went to far. Suggestions will be welcome.

How fast are you moving the bot before you get to the point and what brake type are you using? I would suggest having the robot start at about 80 percent speed and getting slower as it approaches, then using the Hold brake type.

Here’s a rough example of making it slower as it gets closer

S = starting distance from wall
D = current distance from wall
W = distance to stop at
M = Motor speed in percent

while(D != W || D > W){

M = (((D - W) / (S - W)) * 80) + 10



This is not a good application for the V5 distance sensor, I’ve heard of some teams having problems with the old field walls. I would reccomend an ultrasonic sensor, or just make a better drive controller.

So, I actually run a distance sensor through a PID control loop to achieve a similar effect. It works fairly well, but under 200 mm there is about 10 mm of error present in the sensor. Secondly, if your walls are not squared well, then the values may be inaccurate. There are a few ways around that, such as making sure that your walls are tightened, and adding in a timer that kills you PID loop if your value is lower than what the bot can physically hit (which happens more often than I would like). But like the previous comment said, these sensors struggle on the old fields, as they use a low grade laser which doesn’t play nice with steel and plexi. However, on the new competition fields they have the advantage of being significantly less noisy than the ultrasonic sensors, so it depends on what you have, how good you are with filters, and what fields you are going to be running on in your state (in my state we mostly have the new fields so it is a good option).