Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoder reports too low distance when requesting distance at high frequency (from micro-controller) #1

Open
benska13 opened this issue Jun 4, 2019 · 1 comment
Labels
bug Something isn't working

Comments

@benska13
Copy link
Collaborator

benska13 commented Jun 4, 2019

  • When you ask too often, <100 ms, the distance becomes incorrect.
@BjAlvestad
Copy link
Owner

In the micro-controller code for the encoder, inside the i2c_request() method (triggered every time data is requested), the following calculation is performed, and stored in a long:

(sign * encoder0Pos) / ticks_per_cm

After this the encoder0pos gets set to 0.

Since the calculation gives a decimal number, and it gets truncated when stored in the long. The reported distance may therefore be incorrect by up to 0.999... cm per request.

This would not show up as an issue when requesting infrequently (where the car has driven several meters prior to request). However with frequent requests where the distance traveled since last time is relatively short, up to 1 cm incorrect measurement per request will add up to a large total deviation.

Suggested fix:

  • Store the calculation first in a float.
  • Then store the truncated value that should be sent (as a long)
  • Store the difference between the two back into encoder0Pos (instead of just resetting to 0, which would cause data to be lost).

I.e.
Replace old code:

long longs_to_be_sent[] = { (sign * encoder0Pos) / ticks_per_cm, millisecond };
encoder0Pos = 0;

With somthing like:

float distanceTraveled = (sign * encoder0Pos) / ticks_per_cm;
long distanceTraveledInWholeCm = distanceTraveled;
long longs_to_be_sent[] = { distanceTraveledInWholeCm, millisecond };
encoder0Pos = distanceTraveled - distanceTraveledInWholeCm;

Of course making changes to the microcontroller code at this stage would require re-testing, therefore the issue has been logged here, together with information regarting what the team thinks the issue may be.
To be fixed next time modifications are made on the microcontroller code of the car.

@BjAlvestad BjAlvestad changed the title Encoder Encoder reports too low distance when requesting distance with high frequency (from micro-controller) Jun 5, 2019
@BjAlvestad BjAlvestad changed the title Encoder reports too low distance when requesting distance with high frequency (from micro-controller) Encoder reports too low distance when requesting distance at high frequency (from micro-controller) Jun 5, 2019
@BjAlvestad BjAlvestad added the bug Something isn't working label Jun 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants