SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi360_aux_bmm350_euler.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
97SensorEuler euler(bhy);
98#endif
99
100// #define BOSCH_DATA_INJECT_BHI360
101// #define BOSCH_DATA_INJECT_BHI360_BMM150
102// #define BOSCH_DATA_INJECT_BHI360_BMM150_HEAD_ORIENTATION
103// #define BOSCH_DATA_INJECT_BHI360_BMM350
104// #define BOSCH_DATA_INJECT_BHI360_BMM350_HEAD_ORIENTATION
105// #define BOSCH_DATA_INJECT_BHI360_ENV
106// #define BOSCH_DATA_INJECT_BHI360_HEAD_ORIENTATION
107// #define BOSCH_DATA_INJECT_BHI360_MOTION_AI
108// #define BOSCH_SHUTTLE3_BHI360
109// #define BOSCH_SHUTTLE3_BHI360_AUX_BMM150
110// #define BOSCH_SHUTTLE3_BHI360_BMM150
111// #define BOSCH_SHUTTLE3_BHI360_BMM150_BMP580_BME688
112// #define BOSCH_SHUTTLE3_BHI360_BMM150_HEAD_ORIENTATION
113#define BOSCH_SHUTTLE3_BHI360_BMM350C
114// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BME688_IAQ
115// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580
116// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580_BME688
117// #define BOSCH_SHUTTLE3_BHI360_BMM350C_HEAD_ORIENTATION
118// #define BOSCH_SHUTTLE3_BHI360_BMM350C_POLL
119// #define BOSCH_SHUTTLE3_BHI360_BMM350C_TURBO
120// #define BOSCH_SHUTTLE3_BHI360_BMP580_TEST_EXAMPLE
121// #define BOSCH_SHUTTLE3_BHI360_HW_ACTIVITY
122// #define BOSCH_SHUTTLE3_BHI360_HW_ACTIVITY_TURBO
123// #define BOSCH_SHUTTLE3_BHI360_IMU_HEAD_ORIENTATION
124// #define BOSCH_SHUTTLE3_BHI360_POLLING_STEP_COUNTER
125// #define BOSCH_SHUTTLE3_BHI360_TEMP
126// #define BOSCH_SHUTTLE3_BHI360_TEST_DATA_SOURCE
127// #define BOSCH_SHUTTLE3_BHI360_TURBO
128
129#include <BoschFirmware.h>
130
131#if BHI360_IRQ > 0
132#define USING_SENSOR_IRQ_METHOD
133#endif
134
135#ifdef USING_SENSOR_IRQ_METHOD
136bool isReadyFlag = false;
137
139{
140 isReadyFlag = true;
141}
142#endif /*USING_SENSOR_IRQ_METHOD*/
143
144// Firmware update progress callback
145void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
146{
147 float progress = (float)transferred / total * 100;
148 Serial.print("Upload progress: ");
149 Serial.print(progress);
150 Serial.println("%");
151}
152
168#ifndef USING_DATA_HELPER
169void parse_euler_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
170{
171 struct bhy2_data_orientation data;
172 uint32_t s, ns;
173 uint64_t tns;
174 // Function to parse FIFO frame data into orientation
175 bhy2_parse_orientation(data_ptr, &data);
176 uint64_t _timestamp = *timestamp;
177 BoschSensorUtils::time_to_s_ns(_timestamp, &s, &ns, &tns);
178 uint8_t accuracy = bhy.getAccuracy();
179 if (accuracy) {
180 Serial.print(" T:"); Serial.print(s);
181 Serial.print("."); Serial.print(ns);
182 Serial.print(" R:"); Serial.print(data.roll * 360.0f / 32768.0f);
183 Serial.print(" P:"); Serial.print(data.pitch * 360.0f / 32768.0f);
184 Serial.print(" H:"); Serial.print(data.heading * 360.0f / 32768.0f);
185 Serial.print(" A:"); Serial.println(accuracy);
186 } else {
187 Serial.print(" T:"); Serial.print(s);
188 Serial.print("."); Serial.print(ns);
189 Serial.print(" R:"); Serial.print(data.roll * 360.0f / 32768.0f);
190 Serial.print(" P:"); Serial.print(data.pitch * 360.0f / 32768.0f);
191 Serial.print(" H:"); Serial.println(data.heading * 360.0f / 32768.0f);
192 }
193}
194#endif
195
196void setup()
197{
198 Serial.begin(115200);
199 while (!Serial);
200
201 // Set the reset pin
202 bhy.setPins(BHI360_RST);
203
204 // Set the firmware array address and firmware size
205 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size);
206
207 // Set the firmware update processing progress callback function
208 // bhy.setUpdateProcessCallback(progress_callback, NULL);
209
210 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
211 // bhy.setMaxiTransferSize(256);
212
213 // Set the processing fifo data buffer size,The default size is 512 bytes.
214 // bhy.setProcessBufferSize(1024);
215
216 Serial.println("Initializing Sensors...");
217
218#ifdef USE_I2C_INTERFACE
219 // Using I2C interface
220 // BHI360_SLAVE_ADDRESS_L = 0x28
221 // BHI360_SLAVE_ADDRESS_H = 0x29
222 if (!bhy.begin(Wire, BHI360_SLAVE_ADDRESS_L, BHI360_SDA, BHI360_SCL)) {
223 Serial.print("Failed to initialize sensor - error code:");
224 Serial.println(bhy.getError());
225 while (1) {
226 delay(1000);
227 }
228 }
229#endif
230
231#ifdef USE_SPI_INTERFACE
232 // Using SPI interface
233 if (!bhy.begin(SPI, BHI360_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
234 Serial.print("Failed to initialize sensor - error code:");
235 Serial.println(bhy.getError());
236 while (1) {
237 delay(1000);
238 }
239 }
240#endif
241
242 Serial.println("Initializing the sensor successfully!");
243
244 // Output all sensors info to Serial
245 BoschSensorInfo info = bhy.getSensorInfo();
246
247#ifdef ARDUINO
248 ArduinoStreamPrinter printer(Serial);
249 info.printInfo(printer);
250#else
251 info.printInfo([](const char *format, ...) -> int {
252 va_list args;
253 va_start(args, format);
254 int result = vprintf(format, args);
255 va_end(args);
256 return result;
257 });
258#endif
259
272 // Set the sensor's axis remapping based on the chip's orientation.
273 // These commented-out lines show different ways to configure the sensor according to the chip's corners.
274 // When the chip is viewed from the top, set the orientation to the top-left corner.
275 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_LEFT_CORNER);
276 // When the chip is viewed from the top, set the orientation to the top-right corner.
277 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_RIGHT_CORNER);
278 // When the chip is viewed from the top, set the orientation to the bottom-right corner of the top layer.
279 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_BOTTOM_RIGHT_CORNER);
280 // When the chip is viewed from the top, set the orientation to the bottom-left corner of the top layer.
281 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_BOTTOM_LEFT_CORNER);
282 // When the chip is viewed from the bottom, set the orientation to the top-left corner of the bottom layer.
283 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_TOP_LEFT_CORNER);
284 // When the chip is viewed from the bottom, set the orientation to the top-right corner of the bottom layer.
285 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_TOP_RIGHT_CORNER);
286 // When the chip is viewed from the bottom, set the orientation to the bottom-right corner of the bottom layer.
287 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_BOTTOM_RIGHT_CORNER);
288 // When the chip is viewed from the bottom, set the orientation to the bottom-left corner of the bottom layer.
289 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_BOTTOM_LEFT_CORNER);
290
291 // Define the sample rate for data reading.
292 // The sensor will read out data measured at a frequency of 100Hz.
293 float sample_rate = 100.0;
294 // Define the report latency in milliseconds.
295 // A value of 0 means the sensor will report the measured data immediately.
296 uint32_t report_latency_ms = 0;
297
298 /*
299 * Enable Euler function
300 * The Euler function depends on BMM150 or BMM350.
301 * If the hardware is not connected to BMM150 or BMM350, the Euler function cannot be used.
302 * * */
303#ifdef USING_DATA_HELPER
304 euler.enable(sample_rate, report_latency_ms);
305#else
306 // Set the sample rate and report latency for this measurement.
307 bhy.configure(BoschSensorID::ORIENTATION_WAKE_UP, sample_rate, report_latency_ms);
308 // Register a callback function 'parse_euler_data' to handle the result events of the orientation wake-up vector measurement.
309 // When the sensor has new data for the orientation wake-up vector, the 'parse_euler_data' function will be called.
310 bhy.onResultEvent(BoschSensorID::ORIENTATION_WAKE_UP, parse_euler_data);
311#endif
312
313#ifdef USING_SENSOR_IRQ_METHOD
314 // Set the specified pin (BHI360_IRQ) ​​to an input pin.
315 // This makes the pin ready to receive external signals.
316 // If the interrupt is already connected, if BHI360_IRQ is equal to -1 then the polling method will be used
317 pinMode(BHI360_IRQ, INPUT);
318
319 // Attach an interrupt service routine (ISR) to the specified pin (BHI360_IRQ).
320 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
321 attachInterrupt(BHI360_IRQ, dataReadyISR, RISING);
322#endif
323}
324
325
326void loop()
327{
328#ifdef USING_SENSOR_IRQ_METHOD
329 if (isReadyFlag) {
330 isReadyFlag = false;
331#endif /*USING_SENSOR_IRQ_METHOD*/
332
333 /* If the interrupt is connected to the sensor and BHI360_IRQ is not equal to -1,
334 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
335 */
336 bhy.update();
337
338#ifdef USING_SENSOR_IRQ_METHOD
339 }
340#endif /*USING_SENSOR_IRQ_METHOD*/
341
342#ifdef USING_DATA_HELPER
343 if (euler.hasUpdated()) {
344 // Print the roll angle to the serial monitor.
345 Serial.print(euler.getRoll());
346 // Print a comma as a separator between the roll and pitch angles.
347 Serial.print(",");
348 // Print the pitch angle to the serial monitor.
349 Serial.print(euler.getPitch());
350 // Print a comma as a separator between the pitch and yaw angles.
351 Serial.print(",");
352 // Print the yaw angle to the serial monitor and start a new line.
353 Serial.println(euler.getHeading());
354 }
355#endif
356
357 delay(50);
358}
359
360
361
@license MIT License
@license MIT License
SensorEuler euler(bhy)
void parse_euler_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
Parse the quaternion data from the sensor and convert it to Euler angles.
SensorBHI360 bhy
#define BHI360_SCL
#define BHI360_SDA
void dataReadyISR()
#define BHI360_RST
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
#define BHI360_IRQ