Skip to content

Commit c2c53bc

Browse files
bma421: Change acceleration values to 'binary milli-G' units
Co-authored-by: FintasticMan <finlay.neon.kid@gmail.com>
1 parent a49dc15 commit c2c53bc

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

doc/MotionService.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ The current raw motion values. This is a 3 `int16_t` array:
2121
- [0] : X
2222
- [1] : Y
2323
- [2] : Z
24+
25+
The three motion values are in units of "binary milli-g", where 1g is represented by a value of 1024.

src/displayapp/screens/Motion.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ void Motion::Refresh() {
5353
lv_label_set_text_fmt(labelStep, "Steps %lu", motionController.NbSteps());
5454

5555
lv_label_set_text_fmt(label,
56-
"X #FF0000 %d# Y #00B000 %d# Z #FFFF00 %d#",
57-
motionController.X() / 0x10,
58-
motionController.Y() / 0x10,
59-
motionController.Z() / 0x10);
56+
"X #FF0000 %d# Y #00B000 %d# Z #FFFF00 %d# mg",
57+
motionController.X(),
58+
motionController.Y(),
59+
motionController.Z());
6060
lv_obj_align(label, nullptr, LV_ALIGN_IN_TOP_MID, 0, 10);
6161
}

src/drivers/Bma421.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ namespace {
2222
void user_delay(uint32_t period_us, void* /*intf_ptr*/) {
2323
nrf_delay_us(period_us);
2424
}
25+
26+
// Scale factors to convert accelerometer counts to milli-g
27+
// from datasheet: https://files.pine64.org/doc/datasheet/pinetime/BST-BMA421-FL000.pdf
28+
// The array index to use is stored in accel_conf.range
29+
constexpr int16_t accelScaleFactors[] = {
30+
[BMA4_ACCEL_RANGE_2G] = 1024, // LSB/g +/- 2g range
31+
[BMA4_ACCEL_RANGE_4G] = 512, // LSB/g +/- 4g range
32+
[BMA4_ACCEL_RANGE_8G] = 256, // LSB/g +/- 8g range
33+
[BMA4_ACCEL_RANGE_16G] = 128 // LSB/g +/- 16g range
34+
};
2535
}
2636

2737
Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}, deviceAddress {twiAddress} {
@@ -74,7 +84,6 @@ void Bma421::Init() {
7484
if (ret != BMA4_OK)
7585
return;
7686

77-
struct bma4_accel_config accel_conf;
7887
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
7988
accel_conf.range = BMA4_ACCEL_RANGE_2G;
8089
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
@@ -102,8 +111,17 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
102111
Bma421::Values Bma421::Process() {
103112
if (not isOk)
104113
return {};
114+
struct bma4_accel rawData;
105115
struct bma4_accel data;
106-
bma4_read_accel_xyz(&data, &bma);
116+
bma4_read_accel_xyz(&rawData, &bma);
117+
118+
// Scale the measured ADC counts to units of 'binary milli-g'
119+
// where 1g = 1024 'binary milli-g' units.
120+
// See https://github.com/InfiniTimeOrg/InfiniTime/pull/1950 for
121+
// discussion of why we opted for scaling to 1024 rather than 1000.
122+
data.x = 1024 * rawData.x / accelScaleFactors[accel_conf.range];
123+
data.y = 1024 * rawData.y / accelScaleFactors[accel_conf.range];
124+
data.z = 1024 * rawData.z / accelScaleFactors[accel_conf.range];
107125

108126
uint32_t steps = 0;
109127
bma423_step_counter_output(&steps, &bma);

src/drivers/Bma421.h

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace Pinetime {
4141
TwiMaster& twiMaster;
4242
uint8_t deviceAddress = 0x18;
4343
struct bma4_dev bma;
44+
struct bma4_accel_config accel_conf; // Store the device configuration for later reference.
4445
bool isOk = false;
4546
bool isResetOk = false;
4647
DeviceTypes deviceType = DeviceTypes::Unknown;

0 commit comments

Comments
 (0)