0

I am trying to read the distance traveled since some codes were cleared from the OBD with an Android application.

I have managed to get the RPM and fuel % to work but the distance traveled is higher than what I would expect.

case 49: //PID(31): Distance Travelled(A*256)+B
  Double miles = (((valueA*256)+valueB)*0.62137;
  String displayDistance = String.valueOf("Miles: " + miles);    

Anyone know where I've gone wrong?

APerson
  • 8,140
  • 8
  • 35
  • 49
user3010383
  • 91
  • 1
  • 2
  • 12
  • How much higher? There's not nearly enough information here. – 323go Oct 29 '14 at 16:37
  • Sorry I can't get the wifi to work on my PC so doing this from my phone! The car is sitting at roughly 5 thousand miles but the app was reading 7811.24227 miles. Also made it spit out ValueA = 49 and ValueB = 27. – user3010383 Oct 29 '14 at 16:44
  • I'd try a different car, just to be safe. It's not total mileage, but miles since DTC cleared for 0x31. But yes, still seems high. – 323go Oct 29 '14 at 16:57
  • Should valueA always be 49? I think that part might be wrong... Because 49 is the decimal of the PID31 – user3010383 Oct 29 '14 at 17:00
  • Any chance you have A and B in the wrong order? Done the other way it gives 4325 miles. Or drive a km or two and see which number changes. – Chris Stratton Oct 29 '14 at 17:18
  • Just tried swapping and results were.. A:49, B:28. Miles: 4484.4272. My car has done 4,473 miles. – user3010383 Oct 29 '14 at 17:31
  • It apparently works if valueA is 31 which is the hex of 49 :S – user3010383 Oct 29 '14 at 17:37
  • Well, since B went from 27 to 28 in the course of these comments, it would seem that you have the correct order. The thing I noticed right away is that you have the km/mileage factor in there again. You're coming up with 7811, which x 0.62 = 4,842. That's close. But that wouldn't make sense. I'd get in the car and drive until B flips to 29, reset the trip odo and drive to 37. That should be 5 miles. – 323go Oct 29 '14 at 18:12
  • @user3010383, Actually I have a question since I'm working on something similar but I need to calculate distance from point A to point B. Do you know how to clear code (Distance traveled since codes cleared)? – Hesam Jan 02 '15 at 02:31
  • Please have a look at this question http://stackoverflow.com/q/27736828/513413 – Hesam Jan 02 '15 at 02:45

1 Answers1

0

I have discovered the code is right but not 100% accurate because I parsing the bytes into an int and therefore calculation is out by about 7 miles. Will need to find a way to parse as a double. The issue when I am thousands of miles out is a concurrent issue with interleaving of a shared variable.

user3010383
  • 91
  • 1
  • 2
  • 12