SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_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 BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
58#ifndef BHI260_IRQ
59#define BHI260_IRQ 37
60#endif
61
62#ifndef BHI260_CS
63#define BHI260_CS 36
64#endif
65
66#else //* I2C */
67
68#ifndef BHI260_SDA
69#define BHI260_SDA 2
70#endif
71
72#ifndef BHI260_SCL
73#define BHI260_SCL 3
74#endif
75
76// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
77#ifndef BHI260_IRQ
78#define BHI260_IRQ 8
79#endif
80#endif /*USE_SPI_INTERFACE*/
81
82#ifndef BHI260_RST
83#define BHI260_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// The firmware runs in RAM and will be lost if the power is off. The firmware will be loaded from RAM each time it is run.
101#define BOSCH_APP30_SHUTTLE_BHI260_FW
102// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150FW
103// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X
104// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390
105// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO
106// #define BOSCH_BHI260_AUX_BEM280
107// #define BOSCH_BHI260_AUX_BMM150_BEM280
108// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
109// #define BOSCH_BHI260_AUX_BMM150_GPIO
110// #define BOSCH_BHI260_GPIO
111
112// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
113// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150_FLASH
114// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X_FLASH
115// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390_FLASH
116// #define BOSCH_APP30_SHUTTLE_BHI260_FLASH
117// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO_FLASH
118// #define BOSCH_BHI260_AUX_BEM280_FLASH
119// #define BOSCH_BHI260_AUX_BMM150_BEM280_FLASH
120// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
121// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
122// #define BOSCH_BHI260_GPIO_FLASH
123
124#include <BoschFirmware.h>
125
126// Force update of current firmware, whether it exists or not.
127// Only works when external SPI Flash is connected to BHI260.
128// After uploading firmware once, you can change this to false to speed up boot time.
130
131#if BHI260_IRQ > 0
132#define USING_SENSOR_IRQ_METHOD
133#endif
134
135#ifdef USING_SENSOR_IRQ_METHOD
136volatile bool isInterruptTriggered = false;
137
139{
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(BHI260_RST);
196
197 // Set the firmware array address and firmware size
198 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
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 // Set to load firmware from flash
210 bhy.setBootFromFlash(bosch_firmware_type);
211
212 Serial.println("Initializing Sensors...");
213
214#ifdef USE_I2C_INTERFACE
215 // Using I2C interface
216 // BHI260AP_SLAVE_ADDRESS_L = 0x28
217 // BHI260AP_SLAVE_ADDRESS_H = 0x29
218 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
219 Serial.print("Failed to initialize sensor - error code:");
220 Serial.println(bhy.getError());
221 while (1) {
222 delay(1000);
223 }
224 }
225#endif
226
227#ifdef USE_SPI_INTERFACE
228 // Using SPI interface
229 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
230 Serial.print("Failed to initialize sensor - error code:");
231 Serial.println(bhy.getError());
232 while (1) {
233 delay(1000);
234 }
235 }
236#endif
237
238 Serial.println("Initializing the sensor successfully!");
239
240 // Output all sensors info to Serial
241 BoschSensorInfo info = bhy.getSensorInfo();
242
243#ifdef ARDUINO
244 ArduinoStreamPrinter printer(Serial);
245 info.printInfo(printer);
246#else
247 info.printInfo([](const char *format, ...) -> int {
248 va_list args;
249 va_start(args, format);
250 int result = vprintf(format, args);
251 va_end(args);
252 return result;
253 });
254#endif
255
268 // Set the sensor's axis remapping based on the chip's orientation.
269 // These commented-out lines show different ways to configure the sensor according to the chip's corners.
270 // When the chip is viewed from the top, set the orientation to the top-left corner.
271 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_LEFT_CORNER);
272 // When the chip is viewed from the top, set the orientation to the top-right corner.
273 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_RIGHT_CORNER);
274 // When the chip is viewed from the top, set the orientation to the bottom-right corner of the top layer.
275 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_BOTTOM_RIGHT_CORNER);
276 // When the chip is viewed from the top, set the orientation to the bottom-left corner of the top layer.
277 // bhy.setRemapAxes(SensorRemap::TOP_LAYER_BOTTOM_LEFT_CORNER);
278 // When the chip is viewed from the bottom, set the orientation to the top-left corner of the bottom layer.
279 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_TOP_LEFT_CORNER);
280 // When the chip is viewed from the bottom, set the orientation to the top-right corner of the bottom layer.
281 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_TOP_RIGHT_CORNER);
282 // When the chip is viewed from the bottom, set the orientation to the bottom-right corner of the bottom layer.
283 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_BOTTOM_RIGHT_CORNER);
284 // When the chip is viewed from the bottom, set the orientation to the bottom-left corner of the bottom layer.
285 // bhy.setRemapAxes(SensorRemap::BOTTOM_LAYER_BOTTOM_LEFT_CORNER);
286
287 // Define the sample rate for data reading.
288 // The sensor will read out data measured at a frequency of 100Hz.
289 float sample_rate = 100.0;
290 // Define the report latency in milliseconds.
291 // A value of 0 means the sensor will report the measured data immediately.
292 uint32_t report_latency_ms = 0;
293
294#ifdef USING_DATA_HELPER
295 quaternion.enable(sample_rate, report_latency_ms);
296#else
297 // GAME_ROTATION_VECTOR virtual sensor does not rely on external magnetometers, such as BMM150, BMM350
298 // Configure the sensor to measure the game rotation vector.
299 // Set the sample rate and report latency for this measurement.
300 bhy.configure(BoschSensorID::GAME_ROTATION_VECTOR, sample_rate, report_latency_ms);
301 // Register a callback function 'parse_quaternion' to handle the result events of the game rotation vector measurement.
302 // When the sensor has new data for the game rotation vector, the 'parse_quaternion' function will be called.
303 bhy.onResultEvent(BoschSensorID::GAME_ROTATION_VECTOR, parse_quaternion);
304#endif
305
306#ifdef USING_SENSOR_IRQ_METHOD
307 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
308 // This makes the pin ready to receive external signals.
309 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
310 pinMode(BHI260_IRQ, INPUT);
311
312 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
313 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
314 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
315#endif
316}
317
318
319void loop()
320{
321#ifdef USING_SENSOR_IRQ_METHOD
323 isInterruptTriggered = false;
324#endif /*USING_SENSOR_IRQ_METHOD*/
325
326 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
327 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
328 */
329 bhy.update();
330
331#ifdef USING_SENSOR_IRQ_METHOD
332 }
333#endif /*USING_SENSOR_IRQ_METHOD*/
334
335#ifdef USING_DATA_HELPER
336 if (quaternion.hasUpdated()) {
337 // Convert rotation vector to Euler angles
338 quaternion.toEuler();
339 // Print the roll angle to the serial monitor.
340 Serial.print(quaternion.getRoll());
341 // Print a comma as a separator between the roll and pitch angles.
342 Serial.print(",");
343 // Print the pitch angle to the serial monitor.
344 Serial.print(quaternion.getPitch());
345 // Print a comma as a separator between the pitch and yaw angles.
346 Serial.print(",");
347 // Print the yaw angle to the serial monitor and start a new line.
348 Serial.println(quaternion.getHeading());
349 }
350#endif
351
352 delay(50);
353}
354
355
356
@license MIT License
@license MIT License
SensorBHI260AP bhy
#define BHI260_SCL
void setup()
Parse the quaternion data from the sensor and convert it to Euler angles.
void dataReadyISR()
volatile bool isInterruptTriggered
#define BHI260_IRQ
#define BHI260_SDA
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
bool force_update_flash_firmware
#define BHI260_RST
SensorQuaternion quaternion(bhy)
void loop()
void parse_quaternion(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash