You cannot. You can't even do it that well with GPS.
The only relevant OBD2 parameters are speed and RPMs. If the wheels are locked or traction is otherwise lost, neither of these can be used in the calculation.
Brake pedal position is also not a standard PID so you won't always even have a reference point.
I am assuming you want to log data from within the vehicle then calculate stopping distance later. You need one of the following sets of information:
- Either a complete actual speed profile, from which you can observe the braking point and full stop point then integrate to estimate distance, or
- Your location at the point where you applied the brake and your location at the point where you stopped, from which the distance measurement is straightforward.
Neither of these are available to you via OBD2. With other systems you'll still run into a variety of problems, including but not limited to:
- Speed Profile:
- The only commonly available device that can really measure this is GPS. With GPS you'll have the following problems:
- Refresh interval will compromise accuracy, both by making the brake / stop point hard to pinpoint, and by introducing inaccuracies in the distance integral.
- As of 2014, GPS was accurate to within 4.6m (15 feet), 95% of the time (source). This will translate to inaccuracies in speed measurement.
- Brake and stop locations:
- Again, you'd have to go with GPS here, and you would run into the following difficulties:
- Identifying the brake point. Subject to same inaccuracies as above, and hurt by the fact that OBD2 doesn't have to have a brake pedal PID, and synchronization between that and a GPS device would be difficult anyways.
- Identifying the stop point. GPS accuracy issue as described above.
By the way, in the comments you mentioned an example of capturing GPS data once per second. That's still not good enough. Let's say you are travelling at 30 MPH. Your brake point could be anywhere in that 1 second interval. 30 MPH = 44 ft/sec so you're already introducing +/- 22 feet there. Combine with GPS horizontal accuracy of +/- 15 feet and you've got a 95% chance of getting +/- 37 feet, and a 5% chance of getting worse. Combine it with GPS accuracy for the stop point too, and your final distance measurement is somewhere around +/- 52 feet. That's not nearly good enough.
Now, a less simple solution, which would work in theory but in practice may be subject to all sorts of inaccuracies, is to use your OBD2 device to track your wheel speed in combination with an accelerometer aligned with the road axis (we'll assume your car doesn't spin when you brake):
- Using the accelerometer, observe where braking deceleration (should be pretty sharp, as opposed to deceleration from just letting off the gas) begins.
- Using the OBD2 info, make note of your vehicle speed at that point (which you assume to be accurate as you have full traction). This is the speed when you started braking.
- Using your speed where you start braking as your initial speed, integrate the accelerometer readings to estimate a speed profile.
- Integrate the speed profile to estimate a stopping distance.
Main difficulty there being synchronizing OBD2 speed readings with your accelerometer. That won't be trivial although if you can get close you can at least calculate a lower and upper bound for stop distance (like, if you knew you were somewhere between 29 and 31 mph when you began braking). The other difficulty is there is plenty of opportunity to introduce errors, which will amplify with each integral.
I suppose if you were technically and electronically inclined, and have that DIY spirit, you could rig up something else, e.g. a computer that monitors stopping distance by processing video from a dash cam combined with an accelerometer or something on your brake pedal to identify stopping point. Or some sort of laser/image based actual speed sensor on the bottom of your car. But, you know...
All of this, though, is a ton of trouble. By far the easiest and most reliable way to measure your stopping distance is to drive a constant speed, pick a landmark on the road and hit the brakes there. Then, when you stop, get out and measure it.