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
2 changes: 1 addition & 1 deletion src/displayapp/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if(DEFINED ENABLE_USERAPPS)
set(USERAPP_TYPES ${ENABLE_USERAPPS} CACHE STRING "List of user apps to build into the firmware")
else ()
set(USERAPP_TYPES "Apps::Navigation, Apps::StopWatch, Apps::Alarm, Apps::Timer, Apps::Steps, Apps::HeartRate, Apps::Music, Apps::Paint, Apps::Paddle, Apps::Twos, Apps::Metronome" CACHE STRING "List of user apps to build into the firmware")
set(USERAPP_TYPES "Apps::Navigation, Apps::StopWatch, Apps::Alarm, Apps::Timer, Apps::Steps, Apps::Motion, Apps::HeartRate, Apps::Music, Apps::Paint, Apps::Paddle, Apps::Twos, Apps::Metronome" CACHE STRING "List of user apps to build into the firmware")
endif ()

add_library(infinitime_apps INTERFACE)
Expand Down
8 changes: 4 additions & 4 deletions src/displayapp/screens/Motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ void Motion::Refresh() {
lv_label_set_text_fmt(labelStep, "Steps %lu", motionController.NbSteps());

lv_label_set_text_fmt(label,
"X #FF0000 %d# Y #00B000 %d# Z #FFFF00 %d#",
motionController.X() / 0x10,
motionController.Y() / 0x10,
motionController.Z() / 0x10);
"X #FF0000 %d# Y #00B000 %d# Z #FFFF00 %d# mg",
motionController.X(),
motionController.Y(),
motionController.Z());
lv_obj_align(label, nullptr, LV_ALIGN_IN_TOP_MID, 0, 10);
}
23 changes: 21 additions & 2 deletions src/drivers/Bma421.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ namespace {
}
}

// Scale factors to convert accelerometer counts to milli-g
// from datasheet: https://files.pine64.org/doc/datasheet/pinetime/BST-BMA421-FL000.pdf
// The array index to use is stored in accel_conf.range
const short Bma421::accelScaleFactors[] = {
1024, // LSB/g +/- 2g range
512, // LSB/g +/- 4g range
256, // LSB/g +/- 8g range
128 // LSB/g +/- 16g range
};


Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}, deviceAddress {twiAddress} {
bma.intf = BMA4_I2C_INTF;
bma.bus_read = user_i2c_read;
Expand Down Expand Up @@ -74,7 +85,6 @@ void Bma421::Init() {
if (ret != BMA4_OK)
return;

struct bma4_accel_config accel_conf;
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
accel_conf.range = BMA4_ACCEL_RANGE_2G;
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
Expand Down Expand Up @@ -102,8 +112,17 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
Bma421::Values Bma421::Process() {
if (not isOk)
return {};
struct bma4_accel rawData;
struct bma4_accel data;
bma4_read_accel_xyz(&data, &bma);
bma4_read_accel_xyz(&rawData, &bma);

// Scale the measured ADC counts to units of 'binary milli-g'
// where 1g = 1024 'binary milli-g' units.
// See https://github.com/InfiniTimeOrg/InfiniTime/pull/1950 for
// discussion of why we opted for scaling to 1024 rather than 1000.
data.x = 1024 * rawData.x / accelScaleFactors[accel_conf.range];
data.y = 1024 * rawData.y / accelScaleFactors[accel_conf.range];
data.z = 1024 * rawData.z / accelScaleFactors[accel_conf.range];

uint32_t steps = 0;
bma423_step_counter_output(&steps, &bma);
Expand Down
5 changes: 5 additions & 0 deletions src/drivers/Bma421.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ namespace Pinetime {
public:
enum class DeviceTypes : uint8_t { Unknown, BMA421, BMA425 };

// Scale factors to convert accelerometer counts to milli-g
// The array values are initialised in Bma421.cpp
static const short accelScaleFactors[];

struct Values {
uint32_t steps;
int16_t x;
Expand Down Expand Up @@ -41,6 +45,7 @@ namespace Pinetime {
TwiMaster& twiMaster;
uint8_t deviceAddress = 0x18;
struct bma4_dev bma;
struct bma4_accel_config accel_conf; // Store the device configuration for later reference.
bool isOk = false;
bool isResetOk = false;
DeviceTypes deviceType = DeviceTypes::Unknown;
Expand Down