Skip to content

Commit adda7f8

Browse files
committed
BMA421: Use internal FIFO for smoothing
1 parent 0dcfb2e commit adda7f8

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

src/drivers/Bma421.cpp

+39-21
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}
4242
bma.intf_ptr = this;
4343
bma.delay_us = user_delay;
4444
bma.read_write_len = 16;
45+
bma.resolution = 12;
4546
}
4647

4748
void Bma421::Init() {
48-
if (not isResetOk)
49+
if (!isResetOk)
4950
return; // Call SoftReset (and reset TWI device) first!
5051

5152
auto ret = bma423_init(&bma);
@@ -68,10 +69,6 @@ void Bma421::Init() {
6869
if (ret != BMA4_OK)
6970
return;
7071

71-
ret = bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma);
72-
if (ret != BMA4_OK)
73-
return;
74-
7572
ret = bma423_feature_enable(BMA423_STEP_CNTR, 1, &bma);
7673
if (ret != BMA4_OK)
7774
return;
@@ -84,10 +81,19 @@ void Bma421::Init() {
8481
if (ret != BMA4_OK)
8582
return;
8683

87-
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ;
84+
ret = bma4_set_fifo_config(BMA4_FIFO_ACCEL, 1, &bma);
85+
if (ret != BMA4_OK)
86+
return;
87+
fifo_frame.data = (uint8_t*) &fifo;
88+
fifo_frame.length = sizeof(fifo);
89+
fifo_frame.fifo_data_enable = BMA4_FIFO_A_ENABLE;
90+
fifo_frame.fifo_header_enable = 0;
91+
92+
struct bma4_accel_config accel_conf;
93+
accel_conf.odr = BMA4_OUTPUT_DATA_RATE_200HZ;
8894
accel_conf.range = BMA4_ACCEL_RANGE_2G;
8995
accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4;
90-
accel_conf.perf_mode = BMA4_CIC_AVG_MODE;
96+
accel_conf.perf_mode = BMA4_CONTINUOUS_MODE;
9197
ret = bma4_set_accel_config(&accel_conf, &bma);
9298
if (ret != BMA4_OK)
9399
return;
@@ -111,30 +117,42 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) {
111117
Bma421::Values Bma421::Process() {
112118
if (not isOk)
113119
return {};
114-
struct bma4_accel rawData;
115-
struct bma4_accel data;
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];
120+
121+
bma4_read_fifo_data(&fifo_frame, &bma);
122+
uint16_t length = 32; // Should be about 20 (200 Hz ODR / 10 Hz main loop)
123+
bma4_extract_accel((bma4_accel*) fifo, &length, &fifo_frame, &bma);
125124

126125
uint32_t steps = 0;
127126
bma423_step_counter_output(&steps, &bma);
128127

129-
int32_t temperature;
128+
int32_t temperature = 0;
130129
bma4_get_temperature(&temperature, BMA4_DEG, &bma);
131130
temperature = temperature / 1000;
132131

133132
uint8_t activity = 0;
134133
bma423_activity_output(&activity, &bma);
135134

136-
// X and Y axis are swapped because of the way the sensor is mounted in the PineTime
137-
return {steps, data.y, data.x, data.z};
135+
int16_t avgs[3] = {0};
136+
for (uint8_t i = 0; i < length; i++) {
137+
// X and Y axis are swapped because of the way the sensor is mounted in the PineTime
138+
int16_t swap = fifo[i][0];
139+
fifo[i][0] = fifo[i][1];
140+
fifo[i][1] = swap;
141+
for (uint8_t j = 0; j < 3; j++)
142+
avgs[j] += fifo[i][j];
143+
}
144+
145+
for (uint8_t j = 0; j < 3; j++)
146+
{
147+
avgs[j] /= length;
148+
// Scale the measured ADC counts to units of 'binary milli-g'
149+
// where 1g = 1024 'binary milli-g' units.
150+
// See https://github.com/InfiniTimeOrg/InfiniTime/pull/1950 for
151+
// discussion of why we opted for scaling to 1024 rather than 1000.
152+
avgs[j] = 1024 * avgs[j] / accelScaleFactors[accel_conf.range];
153+
}
154+
155+
return {steps, avgs[0], avgs[1], avgs[2]};
138156
}
139157

140158
bool Bma421::IsOk() const {

src/drivers/Bma421.h

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ namespace Pinetime {
4242
uint8_t deviceAddress = 0x18;
4343
struct bma4_dev bma;
4444
struct bma4_accel_config accel_conf; // Store the device configuration for later reference.
45+
struct bma4_fifo_frame fifo_frame;
46+
int16_t fifo[32][3] = {0};
4547
bool isOk = false;
4648
bool isResetOk = false;
4749
DeviceTypes deviceType = DeviceTypes::Unknown;

0 commit comments

Comments
 (0)