SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi360_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
97SensorQuaternion quaternion(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_quaternion(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
170{
171 // Declare variables to store the Euler angles (roll, pitch, and yaw).
172 float roll, pitch, yaw;
173 // Call the bhy2_quaternion_to_euler function to convert the raw quaternion data
174 // pointed to by data_ptr into Euler angles (roll, pitch, and yaw).
175 bhy2_quaternion_to_euler(data_ptr, &roll, &pitch, &yaw);
176 // Print the roll angle to the serial monitor.
177 Serial.print(roll);
178 // Print a comma as a separator between the roll and pitch angles.
179 Serial.print(",");
180 // Print the pitch angle to the serial monitor.
181 Serial.print(pitch);
182 // Print a comma as a separator between the pitch and yaw angles.
183 Serial.print(",");
184 // Print the yaw angle to the serial monitor and start a new line.
185 Serial.println(yaw);
186}
187#endif
188
189void setup()
190{
191 Serial.begin(115200);
192 while (!Serial);
193
194 // Set the reset pin
195 bhy.setPins(BHI360_RST);
196
197 // Set the firmware array address and firmware size
198 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size);
199
200 // Set the firmware update processing progress callback function
201 // bhy.setUpdateProcessCallback(progress_callback, NULL);
202
203 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
204 // bhy.setMaxiTransferSize(256);
205
206 // Set the processing fifo data buffer size,The default size is 512 bytes.
207 // bhy.setProcessBufferSize(1024);
208
209 Serial.println("Initializing Sensors...");
210
211#ifdef USE_I2C_INTERFACE
212 // Using I2C interface
213 // BHI360_SLAVE_ADDRESS_L = 0x28
214 // BHI360_SLAVE_ADDRESS_H = 0x29
215 if (!bhy.begin(Wire, BHI360_SLAVE_ADDRESS_L, BHI360_SDA, BHI360_SCL)) {
216 Serial.print("Failed to initialize sensor - error code:");
217 Serial.println(bhy.getError());
218 while (1) {
219 delay(1000);
220 }
221 }
222#endif
223
224#ifdef USE_SPI_INTERFACE
225 // Using SPI interface
226 if (!bhy.begin(SPI, BHI360_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
227 Serial.print("Failed to initialize sensor - error code:");
228 Serial.println(bhy.getError());
229 while (1) {
230 delay(1000);
231 }
232 }
233#endif
234
235 Serial.println("Initializing the sensor successfully!");
236
237 // Output all sensors info to Serial
238 BoschSensorInfo info = bhy.getSensorInfo();
239
240#ifdef ARDUINO
241 ArduinoStreamPrinter printer(Serial);
242 info.printInfo(printer);
243#else
244 info.printInfo([](const char *format, ...) -> int {
245 va_list args;
246 va_start(args, format);
247 int result = vprintf(format, args);
248 va_end(args);
249 return result;
250 });
251#endif
252
265 // Set the sensor's axis remapping based on the chip's orientation.
266 // These commented-out lines show different ways to configure the sensor according to the chip's corners.
267 // When the chip is viewed from the top, set the orientation to the top-left corner.
268 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_LEFT_CORNER);
269 // When the chip is viewed from the top, set the orientation to the top-right corner.
270 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_RIGHT_CORNER);
271 // When the chip is viewed from the top, set the orientation to the bottom-right corner of the top layer.
272 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_BOTTOM_RIGHT_CORNER);
273 // When the chip is viewed from the top, set the orientation to the bottom-left corner of the top layer.
274 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_BOTTOM_LEFT_CORNER);
275 // When the chip is viewed from the bottom, set the orientation to the top-left corner of the bottom layer.
276 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_TOP_LEFT_CORNER);
277 // When the chip is viewed from the bottom, set the orientation to the top-right corner of the bottom layer.
278 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_TOP_RIGHT_CORNER);
279 // When the chip is viewed from the bottom, set the orientation to the bottom-right corner of the bottom layer.
280 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_BOTTOM_RIGHT_CORNER);
281 // When the chip is viewed from the bottom, set the orientation to the bottom-left corner of the bottom layer.
282 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_BOTTOM_LEFT_CORNER);
283
284 // Define the sample rate for data reading.
285 // The sensor will read out data measured at a frequency of 100Hz.
286 float sample_rate = 100.0;
287 // Define the report latency in milliseconds.
288 // A value of 0 means the sensor will report the measured data immediately.
289 uint32_t report_latency_ms = 0;
290
291#ifdef USING_DATA_HELPER
292 quaternion.enable(sample_rate, report_latency_ms);
293#else
294 // GAME_ROTATION_VECTOR virtual sensor does not rely on external magnetometers, such as BMM150, BMM350
295 // Configure the sensor to measure the game rotation vector.
296 // Set the sample rate and report latency for this measurement.
297 bhy.configure(BoschSensorID::GAME_ROTATION_VECTOR, sample_rate, report_latency_ms);
298 // Register a callback function 'parse_quaternion' to handle the result events of the game rotation vector measurement.
299 // When the sensor has new data for the game rotation vector, the 'parse_quaternion' function will be called.
300 bhy.onResultEvent(BoschSensorID::GAME_ROTATION_VECTOR, parse_quaternion);
301#endif
302
303#ifdef USING_SENSOR_IRQ_METHOD
304 // Set the specified pin (BHI360_IRQ) ​​to an input pin.
305 // This makes the pin ready to receive external signals.
306 // If the interrupt is already connected, if BHI360_IRQ is equal to -1 then the polling method will be used
307 pinMode(BHI360_IRQ, INPUT);
308
309 // Attach an interrupt service routine (ISR) to the specified pin (BHI360_IRQ).
310 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
311 attachInterrupt(BHI360_IRQ, dataReadyISR, RISING);
312#endif
313}
314
315
316void loop()
317{
318#ifdef USING_SENSOR_IRQ_METHOD
319 if (isReadyFlag) {
320 isReadyFlag = false;
321#endif /*USING_SENSOR_IRQ_METHOD*/
322
323 /* If the interrupt is connected to the sensor and BHI360_IRQ is not equal to -1,
324 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
325 */
326 bhy.update();
327
328#ifdef USING_SENSOR_IRQ_METHOD
329 }
330#endif /*USING_SENSOR_IRQ_METHOD*/
331
332#ifdef USING_DATA_HELPER
333 if (quaternion.hasUpdated()) {
334 // Convert rotation vector to Euler angles
335 quaternion.toEuler();
336 // Print the roll angle to the serial monitor.
337 Serial.print(quaternion.getRoll());
338 // Print a comma as a separator between the roll and pitch angles.
339 Serial.print(",");
340 // Print the pitch angle to the serial monitor.
341 Serial.print(quaternion.getPitch());
342 // Print a comma as a separator between the pitch and yaw angles.
343 Serial.print(",");
344 // Print the yaw angle to the serial monitor and start a new line.
345 Serial.println(quaternion.getHeading());
346 }
347#endif
348
349 delay(50);
350}
351
352
353
@license MIT License
@license MIT License
void parse_quaternion(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
bool isReadyFlag
SensorBHI360 bhy
#define BHI360_SCL
void setup()
Parse the quaternion data from the sensor and convert it to Euler angles.
#define BHI360_SDA
void dataReadyISR()
#define BHI360_RST
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
SensorQuaternion quaternion(bhy)
#define BHI360_IRQ
void loop()