SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi360_step_counter.ino
Go to the documentation of this file.
1
30#include <Wire.h>
31#include <SPI.h>
32#include <Arduino.h>
33#include <ImuDrv.hpp>
34
35
36// #define USE_I2C_INTERFACE true
37// #define USE_SPI_INTERFACE true
38
39#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
40#define USE_I2C_INTERFACE
41#warning "No interface type is selected, use I2C interface"
42#endif
43
44#if defined(USE_SPI_INTERFACE)
45#ifndef SPI_MOSI
46#define SPI_MOSI 33
47#endif
48
49#ifndef SPI_MISO
50#define SPI_MISO 34
51#endif
52
53#ifndef SPI_SCK
54#define SPI_SCK 35
55#endif
56
57// If BHI360_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
58#ifndef BHI360_IRQ
59#define BHI360_IRQ 37
60#endif
61
62#ifndef BHI360_CS
63#define BHI360_CS 36
64#endif
65
66#else //* I2C */
67
68#ifndef BHI360_SDA
69#define BHI360_SDA 2
70#endif
71
72#ifndef BHI360_SCL
73#define BHI360_SCL 3
74#endif
75
76// If BHI360_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
77#ifndef BHI360_IRQ
78#define BHI360_IRQ 8
79#endif
80#endif /*USE_SPI_INTERFACE*/
81
82#ifndef BHI360_RST
83#define BHI360_RST -1
84#endif
85
87
88/*
89* Define the USING_DATA_HELPER use of data assistants.
90* No callback function will be used. Data can be obtained directly through
91* the data assistant. Note that this method is not a thread-safe function.
92* Please pay attention to protecting data access security.
93* */
94#define USING_DATA_HELPER
95
96#ifdef USING_DATA_HELPER
97SensorStepCounter stepCounter(bhy);
98SensorStepDetector stepDetector(bhy);
99#endif
100
101// #define BOSCH_DATA_INJECT_BHI360
102// #define BOSCH_DATA_INJECT_BHI360_BMM150
103// #define BOSCH_DATA_INJECT_BHI360_BMM150_HEAD_ORIENTATION
104// #define BOSCH_DATA_INJECT_BHI360_BMM350
105// #define BOSCH_DATA_INJECT_BHI360_BMM350_HEAD_ORIENTATION
106// #define BOSCH_DATA_INJECT_BHI360_ENV
107// #define BOSCH_DATA_INJECT_BHI360_HEAD_ORIENTATION
108// #define BOSCH_DATA_INJECT_BHI360_MOTION_AI
109#define BOSCH_SHUTTLE3_BHI360
110// #define BOSCH_SHUTTLE3_BHI360_AUX_BMM150
111// #define BOSCH_SHUTTLE3_BHI360_BMM150
112// #define BOSCH_SHUTTLE3_BHI360_BMM150_BMP580_BME688
113// #define BOSCH_SHUTTLE3_BHI360_BMM150_HEAD_ORIENTATION
114// #define BOSCH_SHUTTLE3_BHI360_BMM350C
115// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BME688_IAQ
116// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580
117// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580_BME688
118// #define BOSCH_SHUTTLE3_BHI360_BMM350C_HEAD_ORIENTATION
119// #define BOSCH_SHUTTLE3_BHI360_BMM350C_POLL
120// #define BOSCH_SHUTTLE3_BHI360_BMM350C_TURBO
121// #define BOSCH_SHUTTLE3_BHI360_BMP580_TEST_EXAMPLE
122// #define BOSCH_SHUTTLE3_BHI360_HW_ACTIVITY
123// #define BOSCH_SHUTTLE3_BHI360_HW_ACTIVITY_TURBO
124// #define BOSCH_SHUTTLE3_BHI360_IMU_HEAD_ORIENTATION
125// #define BOSCH_SHUTTLE3_BHI360_POLLING_STEP_COUNTER
126// #define BOSCH_SHUTTLE3_BHI360_TEMP
127// #define BOSCH_SHUTTLE3_BHI360_TEST_DATA_SOURCE
128// #define BOSCH_SHUTTLE3_BHI360_TURBO
129
130#include <BoschFirmware.h>
131
132#if BHI360_IRQ > 0
133#define USING_SENSOR_IRQ_METHOD
134#endif
135
136#ifdef USING_SENSOR_IRQ_METHOD
137volatile bool isInterruptTriggered = false;
138
140{
142}
143#endif /*USING_SENSOR_IRQ_METHOD*/
144
145#ifndef USING_DATA_HELPER
146void step_detector_process_callback(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
147{
148 Serial.print(bhy.getSensorName(sensor_id));
149 Serial.println(" detected.");
150}
151
152void step_counter_process_callback(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
153{
154 Serial.print("Step Count: ");
155 Serial.println(bhy2_parse_step_counter(data_ptr));
156}
157#endif
158
159// Firmware update progress callback
160void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
161{
162 float progress = (float)transferred / total * 100;
163 Serial.print("Upload progress: ");
164 Serial.print(progress);
165 Serial.println("%");
166}
167
168void setup()
169{
170 Serial.begin(115200);
171 while (!Serial);
172
173 // Set the reset pin
174 bhy.setPins(BHI360_RST);
175
176 Serial.println("Initializing Sensors...");
177
178 // Set the firmware array address and firmware size
179 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size);
180
181 // Set the firmware update processing progress callback function
182 // bhy.setUpdateProcessCallback(progress_callback, NULL);
183
184 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
185 // bhy.setMaxiTransferSize(256);
186
187 // Set the processing fifo data buffer size,The default size is 512 bytes.
188 // bhy.setProcessBufferSize(1024);
189
190 Serial.println("Initializing Sensors...");
191
192#ifdef USE_I2C_INTERFACE
193 // Using I2C interface
194 // BHI360_SLAVE_ADDRESS_L = 0x28
195 // BHI360_SLAVE_ADDRESS_H = 0x29
196 if (!bhy.begin(Wire, BHI360_SLAVE_ADDRESS_L, BHI360_SDA, BHI360_SCL)) {
197 Serial.print("Failed to initialize sensor - error code:");
198 Serial.println(bhy.getError());
199 while (1) {
200 delay(1000);
201 }
202 }
203#endif
204
205#ifdef USE_SPI_INTERFACE
206 // Using SPI interface
207 if (!bhy.begin(SPI, BHI360_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
208 Serial.print("Failed to initialize sensor - error code:");
209 Serial.println(bhy.getError());
210 while (1) {
211 delay(1000);
212 }
213 }
214#endif
215
216 Serial.println("Initializing the sensor successfully!");
217
218 // Output all sensors info to Serial
219 BoschSensorInfo info = bhy.getSensorInfo();
220
221#ifdef ARDUINO
222 ArduinoStreamPrinter printer(Serial);
223 info.printInfo(printer);
224#else
225 info.printInfo([](const char *format, ...) -> int {
226 va_list args;
227 va_start(args, format);
228 int result = vprintf(format, args);
229 va_end(args);
230 return result;
231 });
232#endif
233
234 // The stepcount sensor will only report when it changes, so the value is 0 ~ 1
235 float sample_rate = 1.0;
236 uint32_t report_latency_ms = 0; /* Report immediately */
237
238#ifdef USING_DATA_HELPER
239 stepDetector.enable(sample_rate, report_latency_ms);
240 stepCounter.enable(sample_rate, report_latency_ms);
241#else
242 // Enable Step detector
243 bhy.configure(BoschSensorID::STEP_DETECTOR_LOW_POWER, sample_rate, report_latency_ms);
244 // Enable Step counter
245 bhy.configure(BoschSensorID::STEP_COUNTER_LOW_POWER, sample_rate, report_latency_ms);
246 // Set the number of steps to detect the callback function
247 bhy.onResultEvent(BoschSensorID::STEP_DETECTOR_LOW_POWER, step_detector_process_callback);
248 bhy.onResultEvent(BoschSensorID::STEP_COUNTER_LOW_POWER, step_counter_process_callback);
249#endif
250
251#ifdef USING_SENSOR_IRQ_METHOD
252 // Set the specified pin (BHI360_IRQ) ​​to an input pin.
253 // This makes the pin ready to receive external signals.
254 // If the interrupt is already connected, if BHI360_IRQ is equal to -1 then the polling method will be used
255 pinMode(BHI360_IRQ, INPUT);
256
257 // Attach an interrupt service routine (ISR) to the specified pin (BHI360_IRQ).
258 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
259 attachInterrupt(BHI360_IRQ, dataReadyISR, RISING);
260#endif
261
262
264 Serial.println("Get Step Counter configuration.");
265 if (!bhy.getStepCounterConfig(conf)) {
266 Serial.println("Failed to get Step Counter configuration");
267 return;
268 }
269 Serial.println(" Step Counter configuration:");
270 Serial.print(" - <env_min_dist_up>: ");
271 Serial.println(conf.env_min_dist_up); /* threshold to up envelope curve */
272 Serial.print(" - <env_coef_up>: ");
273 Serial.println(conf.env_coef_up); /* decay filter coefficient to up envelope curve */
274 Serial.print(" - <env_min_dist_down>: ");
275 Serial.println(conf.env_min_dist_down); /* threshold to down envelope curve */
276 Serial.print(" - <env_coef_down>: ");
277 Serial.println(conf.env_coef_down); /* decay filter coefficient to down envelope curve */
278 Serial.print(" - <step_buffer_size>: ");
279 Serial.println(conf.step_buffer_size); /* buffer size to step count, step can be count only buffer is full */
280 Serial.print(" - <mean_val_decay>: ");
281 Serial.println(conf.mean_val_decay); /* decay filter coefficient to mean value */
282 Serial.print(" - <mean_step_dur>: ");
283 Serial.println(conf.mean_step_dur); /* decay filter coefficient to step duration */
284 Serial.print(" - <filter_coeff_b2>: ");
285 Serial.println(conf.filter_coeff_b2); /* filter coefficient to input signal */
286 Serial.print(" - <filter_coeff_b1>: ");
287 Serial.println(conf.filter_coeff_b1); /* filter coefficient to input signal */
288 Serial.print(" - <filter_coeff_b0>: ");
289 Serial.println(conf.filter_coeff_b0); /* filter coefficient to input signal */
290 Serial.print(" - <filter_coeff_a2>: ");
291 Serial.println(conf.filter_coeff_a2); /* filter coefficient to input signal */
292 Serial.print(" - <filter_coeff_a1>: ");
293 Serial.println(conf.filter_coeff_a1); /* filter coefficient to input signal */
294 Serial.print(" - <filter_cascade_enabled>: ");
295 Serial.println(conf.filter_cascade_enabled); /* enable cascade filter for input signal */
296 Serial.print(" - <peak_duration_min_walking>: ");
297 Serial.println(conf.peak_duration_min_walking); /* minimal peak duration for walking activity */
298 Serial.print(" - <peak_duration_min_running>: ");
299 Serial.println(conf.peak_duration_min_running); /* minimal peak duration for running activity */
300 Serial.print(" - <step_duration_max>: ");
301 Serial.println(conf.step_duration_max); /* maximal samples since last step to reset step buffer and recognize activity as still */
302 Serial.print(" - <step_duration_window>: ");
303 Serial.println(conf.step_duration_window); /* step duration threshold to determine if one step is missed */
304 Serial.print(" - <half_step_enabled>: ");
305 Serial.println(conf.half_step_enabled); /* enable half step compensation function */
306 Serial.print(" - <activity_detection_factor>: ");
307 Serial.println(conf.activity_detection_factor); /* gain for signal magnitude variance in activity identify function */
308 Serial.print(" - <activity_detection_thres>: ");
309 Serial.println(conf.activity_detection_thres); /* threshold for signal magnitude variance in activity identify function */
310 Serial.print(" - <step_counter_increment>: ");
311 Serial.println(conf.step_counter_increment); /* scale factor of step counter */
312 Serial.print(" - <step_duration_pp_enabled>: ");
313 Serial.println(conf.step_duration_pp_enabled); /* enable post-process of duration between steps */
314 Serial.print(" - <step_dur_thres>: ");
315 Serial.println(conf.step_dur_thres); /* gain threshold of mean step duration in post-processing check */
316 Serial.print(" - <en_mcr_pp>: ");
317 Serial.println(conf.en_mcr_pp); /* enable post-process of mean crossings */
318 Serial.print(" - <mcr_thres>: ");
319 Serial.println(conf.mcr_thres); /* threshold of mean crossings in post-processing check */
320 Serial.print(" - <sc_26>: ");
321 Serial.println(conf.sc_26); /* reserved */
322 Serial.print(" - <sc_27>: ");
323 Serial.println(conf.sc_27); /* reserved */
324 Serial.println("Change Step Counter configuration.");
325 conf.env_min_dist_up = 10; /* threshold to up envelope curve */
326 if (!bhy.setStepCounterConfig(conf)) {
327 Serial.println("Failed to set Step Counter configuration");
328 return;
329 }
330 Serial.println("Get Step Counter configuration again.");
331 if (!bhy.getStepCounterConfig(conf)) {
332 Serial.println("Failed to get Step Counter configuration");
333 return;
334 }
335 Serial.println(" Step Counter configuration:");
336 Serial.print(" - <env_min_dist_up>: ");
337 Serial.println(conf.env_min_dist_up);
338 Serial.print(" - <env_coef_up>: ");
339 Serial.println(conf.env_coef_up);
340 Serial.print(" - <env_min_dist_down>: ");
341 Serial.println(conf.env_min_dist_down);
342 Serial.print(" - <env_coef_down>: ");
343 Serial.println(conf.env_coef_down);
344 Serial.print(" - <step_buffer_size>: ");
345 Serial.println(conf.step_buffer_size);
346 Serial.print(" - <mean_val_decay>: ");
347 Serial.println(conf.mean_val_decay);
348 Serial.print(" - <mean_step_dur>: ");
349 Serial.println(conf.mean_step_dur);
350 Serial.print(" - <filter_coeff_b2>: ");
351 Serial.println(conf.filter_coeff_b2);
352 Serial.print(" - <filter_coeff_b1>: ");
353 Serial.println(conf.filter_coeff_b1);
354 Serial.print(" - <filter_coeff_b0>: ");
355 Serial.println(conf.filter_coeff_b0);
356 Serial.print(" - <filter_coeff_a2>: ");
357 Serial.println(conf.filter_coeff_a2);
358 Serial.print(" - <filter_coeff_a1>: ");
359 Serial.println(conf.filter_coeff_a1);
360 Serial.print(" - <filter_cascade_enabled>: ");
361 Serial.println(conf.filter_cascade_enabled);
362 Serial.print(" - <peak_duration_min_walking>: ");
363 Serial.println(conf.peak_duration_min_walking);
364 Serial.print(" - <peak_duration_min_running>: ");
365 Serial.println(conf.peak_duration_min_running);
366 Serial.print(" - <step_duration_max>: ");
367 Serial.println(conf.step_duration_max);
368 Serial.print(" - <step_duration_window>: ");
369 Serial.println(conf.step_duration_window);
370 Serial.print(" - <half_step_enabled>: ");
371 Serial.println(conf.half_step_enabled);
372 Serial.print(" - <activity_detection_factor>: ");
373 Serial.println(conf.activity_detection_factor);
374 Serial.print(" - <activity_detection_thres>: ");
375 Serial.println(conf.activity_detection_thres);
376 Serial.print(" - <step_counter_increment>: ");
377 Serial.println(conf.step_counter_increment);
378 Serial.print(" - <step_duration_pp_enabled>: ");
379 Serial.println(conf.step_duration_pp_enabled);
380 Serial.print(" - <step_dur_thres>: ");
381 Serial.println(conf.step_dur_thres);
382 Serial.print(" - <en_mcr_pp>: ");
383 Serial.println(conf.en_mcr_pp);
384 Serial.print(" - <mcr_thres>: ");
385 Serial.println(conf.mcr_thres);
386 Serial.print(" - <sc_26>: ");
387 Serial.println(conf.sc_26);
388 Serial.print(" - <sc_27>: ");
389 Serial.println(conf.sc_27);
390}
391
392
393void loop()
394{
395
396
397#ifdef USING_SENSOR_IRQ_METHOD
399 isInterruptTriggered = false;
400#endif /*USING_SENSOR_IRQ_METHOD*/
401
402 /* If the interrupt is connected to the sensor and BHI360_IRQ is not equal to -1,
403 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
404 */
405 bhy.update();
406
407#ifdef USING_SENSOR_IRQ_METHOD
408 }
409#endif /*USING_SENSOR_IRQ_METHOD*/
410
411#ifdef USING_DATA_HELPER
412 uint32_t s;
413 uint32_t ns;
414 if (stepCounter.hasUpdated()) {
415 stepCounter.getLastTime(s, ns);
416#ifdef PLATFORM_HAS_PRINTF
417 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "]: Step Count:", s, ns);
418#else
419 Serial.print("[T: ");
420 Serial.print(s);
421 Serial.print(".");
422 Serial.print(ns);
423 Serial.print("]: Step Count:");
424#endif
425 Serial.println(stepCounter.getStepCount());
426 }
427 if (stepDetector.hasUpdated()) {
428 if (stepDetector.isDetected()) {
429 stepDetector.getLastTime(s, ns);
430#ifdef PLATFORM_HAS_PRINTF
431 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "]:", s, ns);
432#else
433 Serial.print("[T: ");
434 Serial.print(s);
435 Serial.print(".");
436 Serial.print(ns);
437 Serial.print("]:");
438#endif
439 Serial.println("Step detector detects steps");
440 }
441 }
442#endif
443 delay(50);
444}
445
446
@license MIT License
@license MIT License
SensorStepDetector stepDetector(bhy)
SensorBHI360 bhy
#define BHI360_SCL
void setup()
#define BHI360_SDA
void dataReadyISR()
volatile bool isInterruptTriggered
#define BHI360_RST
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
SensorStepCounter stepCounter(bhy)
#define BHI360_IRQ
void loop()
bool setStepCounterConfig(const StepCounterConfig &config)
Set the configuration for the Step Counter algorithm.
bool getStepCounterConfig(StepCounterConfig &config)
Get the current configuration of the Step Counter algorithm.
bhi360_phy_sensor_ctrl_param_step_counter StepCounterConfig