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

Change Acceleration Values to milli-g units rather than Raw Counts #1950

Merged
merged 8 commits into from
Feb 11, 2024

Conversation

jones139
Copy link
Contributor

@jones139 jones139 commented Jan 5, 2024

This change adds the required scaling factors to the Bma421 accelerometer driver so that the accelerometer readings can be converted to milli-g for any of the valid instrument ranges (the default 2g up to 16g).

This addresses part of issue #1949.

To end users the change should not be significant because the scaling factor is such that for the default 2g range, the raw values are very close to the milli-g values anyway - it is much more noticeable if you switch the device range as this change will mean the returned values are unchanged (previously the acceleration values halved if you selected the +/-4g range).

I have also updated the Motion screen to display the milli-g values to check it is working.

I have checked it on a physical pinetime device using both the +/-2g and +/-4g ranges to confirm that the static value returned is close to 1000 mg.

Copy link

github-actions bot commented Jan 5, 2024

Build size and comparison to main:

Section Size Difference
text 373172B 32B
data 940B 0B
bss 63516B 0B

@FintasticMan
Copy link
Member

I like the idea of normalising the motion values so that the same value corresponds to the same force, regardless of the precision it's configured for. I do think it's a shame that conversion to milli-Gs from the raw values is a (slightly) lossy calculation. Probably not really an issue though.

I do know that the current raise to wake and lower to sleep implementations rely on 1G being 1024, and they would need to be adjusted slightly if it were 1000 instead (it would make the trig slightly harder as we use 32767 as the range for sin, which is just 1024 * 32 - 1). I presume that things such as Sleep as Android (and maybe OpenTracks) also rely on that.

Maybe we can compromise and use mibi-Gs (a term I just came up with for a 1024th of a G ;p)?

@jones139
Copy link
Contributor Author

jones139 commented Jan 7, 2024

Strangely on my device I am seeing over 1000 milli-g so I think there is an offset (or calibration error) on the accelerometer in my watch.
I do not think that the 1000/1024 reduction in precision will really be significant unless you are trying to do something very precise, which is unlikely on a watch?

You are right though that we do not want to make calculations any more complex because I am already concerned about the amount of calculations we are doing, in case that is causing the issue I am seeing with not being able to achieve 25Hz sampling via the BLE MotionService.

I could make it return raw values and provide a conversion function toMillig() that would convert the values in the structure to milli-g on demand if you prefer?

Graham.

@FintasticMan
Copy link
Member

FintasticMan commented Jan 7, 2024

I would say that normalizing the data so that 1024 == 1G makes sense, and providing a function that converts to milli-Gs. All the function would need to do is val * 1000 / 1024.

The reason you aren't seeing the 25Hz on the BLE service is likely due to the fact that the SystemTask refresh is 10Hz, and updating the service only happens every SystemTask refresh.

@jones139
Copy link
Contributor Author

jones139 commented Jan 7, 2024

Ok - I'll update it to return a 'binary milli-g' where 1G=1024 counts for all ranges - then we do not need to change anything else - it will be this evening (UK time) before I do it though.

@jones139
Copy link
Contributor Author

jones139 commented Jan 7, 2024

The reason you aren't seeing the 25Hz on the BLE service is likely due to the fact that the SystemTask refresh is 10Hz, and updating the service only happens every SystemTask refresh.

Ah, that is it then - I do see about 10Hz. I'll have a think about that because I definitely need 25Hz, but will be happy with returning several samples at each refresh, so if I can get buffering of the samples working the 10Hz refresh will be ok - thanks for pointing that out as I had missed it!

…000) - this means that the values for the default range of +/-2 g are unchanged, and all ranges give something very close to milli-g - see discussion fof pull request InfiniTimeOrg#1950 for information.

InfiniTimeOrg#1950
@jones139
Copy link
Contributor Author

jones139 commented Jan 7, 2024

Commit c261579 does the change to 'binary milli-g'

Copy link
Member

@FintasticMan FintasticMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of small things.

src/drivers/Bma421.cpp Outdated Show resolved Hide resolved
src/displayapp/apps/CMakeLists.txt Outdated Show resolved Hide resolved
Changes to array declaration as suggested during pull request review.

Co-authored-by: FintasticMan <finlay.neon.kid@gmail.com>
@jones139
Copy link
Contributor Author

jones139 commented Jan 7, 2024

The reason you aren't seeing the 25Hz on the BLE service is likely due to the fact that the SystemTask refresh is 10Hz, and updating the service only happens every SystemTask refresh.

Ah, that is it then - I do see about 10Hz. I'll have a think about that because I definitely need 25Hz, but will be happy with returning several samples at each refresh, so if I can get buffering of the samples working the 10Hz refresh will be ok - thanks for pointing that out as I had missed it!

This is more complicated than I had hoped - buffering within MotionService did not help - I still only achieved 10Hz sample frequency (but had to read the characeristic less often). I think we are polling the accelerometer from within SystemTask, rather than using the chip's FIFO and interrupt capabilities? I have put a few thoughts here: #1949 (comment)

I'd welcome any thoughts on the best way to implement higher frequency sampling without breaking other parts of InfiniTime!

@FintasticMan
Copy link
Member

I've just commented in the issue. If you can move the array out of the class, into the anonymous namespace and rebase to fix the conflicts, I'd be happy to approve this PR. Then we could figure out where to go from there afterwards.

@jones139
Copy link
Contributor Author

jones139 commented Jan 8, 2024

I think this is now aligned with the latest commits to the upstream repository - is that what you meant by 'rebase'?

@FintasticMan
Copy link
Member

@jones139 Could you fix the formatting issues as shown by the test-format check?

@FintasticMan FintasticMan added the maintenance Background work label Jan 10, 2024
@FintasticMan FintasticMan requested a review from a team January 10, 2024 09:13
@jones139
Copy link
Contributor Author

jones139 commented Jan 10, 2024 via email

@FintasticMan
Copy link
Member

If you go to the details page for the failing check, then click summary, there should be a file you can download called Patches. This is a zip file with patches you can apply to your code that will fix the formatting. It is probably better to install and use clang-format yourself though, as then you can format the code before you commit and push.

Copy link
Collaborator

@JF002 JF002 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those changes look good to me.
If I understand correctly, the values that are exposed on the BLE Motion service will also change, right? If so, I think we should also update the documentation to reflect that change.

@FintasticMan
Copy link
Member

In the end, we decided on a way of doing this in which the values aren't changed in the default configuration. 1G is still 1024. The change comes when you change the range of the accelerometer, as then 1G stays 1024. Before, 1G would be lower if the range was increased.

@jones139
Copy link
Contributor Author

I have updated the MotionService documentation to make it clear that the unit are "binary milli-g" because the units were not specified on that page previously.

@FintasticMan FintasticMan merged commit c2c53bc into InfiniTimeOrg:main Feb 11, 2024
6 checks passed
@jones139 jones139 deleted the milli-g branch February 11, 2024 19:52
@FintasticMan FintasticMan added this to the 1.15.0 milestone Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Background work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants