SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_aux_bmm150_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
97SensorEuler euler(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#ifndef USING_DATA_HELPER
145void parse_euler(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
146{
147 struct bhy2_data_orientation data;
148 uint32_t s, ns;
149 uint64_t tns;
150 // Function to parse FIFO frame data into orientation
151 bhy2_parse_orientation(data_ptr, &data);
152 uint64_t _timestamp = *timestamp;
153 BoschSensorUtils::time_to_s_ns(_timestamp, &s, &ns, &tns);
154 uint8_t accuracy = bhy.getAccuracy();
155 if (accuracy) {
156 Serial.print(" T:"); Serial.print(s);
157 Serial.print("."); Serial.print(ns);
158 Serial.print(" R:"); Serial.print(data.roll * 360.0f / 32768.0f);
159 Serial.print(" P:"); Serial.print(data.pitch * 360.0f / 32768.0f);
160 Serial.print(" H:"); Serial.print(data.heading * 360.0f / 32768.0f);
161 Serial.print(" A:"); Serial.println(accuracy);
162 } else {
163 Serial.print(" T:"); Serial.print(s);
164 Serial.print("."); Serial.print(ns);
165 Serial.print(" R:"); Serial.print(data.roll * 360.0f / 32768.0f);
166 Serial.print(" P:"); Serial.print(data.pitch * 360.0f / 32768.0f);
167 Serial.print(" H:"); Serial.println(data.heading * 360.0f / 32768.0f);
168 }
169}
170#endif
171
172// Firmware update progress callback
173void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
174{
175 float progress = (float)transferred / total * 100;
176 Serial.print("Upload progress: ");
177 Serial.print(progress);
178 Serial.println("%");
179}
180
181void setup()
182{
183 Serial.begin(115200);
184 while (!Serial);
185
186 // Set the reset pin
187 bhy.setPins(BHI260_RST);
188
189 Serial.println("Initializing Sensors...");
190
191 // Set the firmware array address and firmware size
192 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
193
194 // Set the firmware update processing progress callback function
195 // bhy.setUpdateProcessCallback(progress_callback, NULL);
196
197 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
198 // bhy.setMaxiTransferSize(256);
199
200 // Set the processing fifo data buffer size,The default size is 512 bytes.
201 // bhy.setProcessBufferSize(1024);
202
203 // Set to load firmware from flash
204 bhy.setBootFromFlash(bosch_firmware_type);
205
206#ifdef USE_I2C_INTERFACE
207 // Using I2C interface
208 // BHI260AP_SLAVE_ADDRESS_L = 0x28
209 // BHI260AP_SLAVE_ADDRESS_H = 0x29
210 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
211 Serial.print("Failed to initialize sensor - error code:");
212 Serial.println(bhy.getError());
213 while (1) {
214 delay(1000);
215 }
216 }
217#endif
218
219#ifdef USE_SPI_INTERFACE
220 // Using SPI interface
221 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
222 Serial.print("Failed to initialize sensor - error code:");
223 Serial.println(bhy.getError());
224 while (1) {
225 delay(1000);
226 }
227 }
228#endif
229
230 Serial.println("Initializing the sensor successfully!");
231
232 // Output all sensors info to Serial
233 BoschSensorInfo info = bhy.getSensorInfo();
234
235#ifdef ARDUINO
236 ArduinoStreamPrinter printer(Serial);
237 info.printInfo(printer);
238#else
239 info.printInfo([](const char *format, ...) -> int {
240 va_list args;
241 va_start(args, format);
242 int result = vprintf(format, args);
243 va_end(args);
244 return result;
245 });
246#endif
247
248 float sample_rate = 100.0; /* Read out data measured at 100Hz */
249 uint32_t report_latency_ms = 0; /* Report immediately */
250
251 /*
252 * Enable Euler function
253 * The Euler function depends on BMM150.
254 * If the hardware is not connected to BMM150, the Euler function cannot be used.
255 * * */
256#ifdef USING_DATA_HELPER
257 euler.enable(sample_rate, report_latency_ms);
258#else
259 bhy.configure(BoschSensorID::ORIENTATION_WAKE_UP, sample_rate, report_latency_ms);
260 // Register event callback function
261 bhy.onResultEvent(BoschSensorID::ORIENTATION_WAKE_UP, parse_euler);
262#endif
263
264#ifdef USING_SENSOR_IRQ_METHOD
265 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
266 // This makes the pin ready to receive external signals.
267 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
268 pinMode(BHI260_IRQ, INPUT);
269
270 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
271 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
272 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
273#endif
274}
275
276
277void loop()
278{
279#ifdef USING_SENSOR_IRQ_METHOD
281 isInterruptTriggered = false;
282#endif /*USING_SENSOR_IRQ_METHOD*/
283
284 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
285 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
286 */
287 bhy.update();
288
289#ifdef USING_SENSOR_IRQ_METHOD
290 }
291#endif /*USING_SENSOR_IRQ_METHOD*/
292
293#ifdef USING_DATA_HELPER
294 if (euler.hasUpdated()) {
295 // Print the roll angle to the serial monitor.
296 Serial.print(euler.getRoll());
297 // Print a comma as a separator between the roll and pitch angles.
298 Serial.print(",");
299 // Print the pitch angle to the serial monitor.
300 Serial.print(euler.getPitch());
301 // Print a comma as a separator between the pitch and yaw angles.
302 Serial.print(",");
303 // Print the yaw angle to the serial monitor and start a new line.
304 Serial.println(euler.getHeading());
305 }
306#endif
307 delay(50);
308}
309
310
311
@license MIT License
@license MIT License
SensorBHI260AP bhy
#define BHI260_SCL
SensorEuler euler(bhy)
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
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash