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

Bug: AccumulatedEnergy in PowerSampling fails to increment for high values due to float addition #186

Closed
martijnvandermarel opened this issue Sep 1, 2022 · 1 comment

Comments

@martijnvandermarel
Copy link
Contributor

Describe the bug
For some crownstones, the accumulatedEnergy advertised in serviceData remains constant, even though the advertised realPowerUsage is high enough that the accumulatedEnergy should increase over time.

For example, I have a crownstone that consumes ~27 Watt as per the consumer app (see screenshot). Since the accumulatedEnergy field in the advertisement data contains energy in 64J units, the accumulatedEnergy value should increment every ~2 seconds or so. Instead, it remains constant for at least hours (see second screenshot).

To Reproduce
onlinegdb simulation for reproducing the bug and the proposed solution.
The bug is not easily reproduced on a crownstone since it only occurs for specific (high enough) initial values of the _energyUsedmicroJoule variable in cs_PowerSampling.cpp. In my case it was 17694528000000 (it is an int64_t).

Cause and solution
Together with @vliedel I found the cause of the bug to stem from a bug in the incrementation of the energyUsedmicroJoule on line 1036 of cs_PowerSampling.cpp. This operation adds a float to an int64_t using float addition. For high values of the int64_t variable, it seems that the float addition fails (probably due to bad resolution for floats for high numbers). The solution is simple: cast to int64_t before performing the addition. @vliedel and I tested this in the onlinegdb simulation and it seems to work.

Screenshots
image
image

Binary (please complete the following information):

  • Firmware version: 5.6.4
  • Softdevice version: irrelevant
  • Bootloader version: irrelevant
@vliedel
Copy link
Contributor

vliedel commented Sep 6, 2022

Very interesting bug indeed :)
Fixed with 6c1ec3d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants