SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_aux_bmm150.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
37// #define USE_I2C_INTERFACE true
38// #define USE_SPI_INTERFACE true
39
40#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
41#define USE_I2C_INTERFACE
42#warning "No interface type is selected, use I2C interface"
43#endif
44
45#if defined(USE_SPI_INTERFACE)
46#ifndef SPI_MOSI
47#define SPI_MOSI 4
48#endif
49
50#ifndef SPI_MISO
51#define SPI_MISO 7
52#endif
53
54#ifndef SPI_SCK
55#define SPI_SCK 26
56#endif
57
58// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
59#ifndef BHI260_IRQ
60#define BHI260_IRQ 5
61#endif
62
63#ifndef BHI260_CS
64#define BHI260_CS 27
65#endif
66
67#else //* I2C */
68
69#ifndef BHI260_SDA
70#define BHI260_SDA 2
71#endif
72
73#ifndef BHI260_SCL
74#define BHI260_SCL 3
75#endif
76
77// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
78#ifndef BHI260_IRQ
79#define BHI260_IRQ 8
80#endif
81#endif /*USE_SPI_INTERFACE*/
82
83#ifndef BHI260_RST
84#define BHI260_RST -1
85#endif
86
88
89/*
90* Define the USING_DATA_HELPER use of data assistants.
91* No callback function will be used. Data can be obtained directly through
92* the data assistant. Note that this method is not a thread-safe function.
93* Please pay attention to protecting data access security.
94* */
95#define USING_DATA_HELPER
96
97#ifdef USING_DATA_HELPER
98SensorAcceleration accel(bhy);
99SensorGyroscope gyro(bhy);
100SensorMagnetometer mag(bhy);
101#endif
102
103// 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.
104// #define BOSCH_APP30_SHUTTLE_BHI260_FW
105#define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150FW
106// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X
107// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390
108// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO
109// #define BOSCH_BHI260_AUX_BEM280
110// #define BOSCH_BHI260_AUX_BMM150_BEM280
111// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
112// #define BOSCH_BHI260_AUX_BMM150_GPIO
113// #define BOSCH_BHI260_GPIO
114
115// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
116// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150_FLASH
117// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X_FLASH
118// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390_FLASH
119// #define BOSCH_APP30_SHUTTLE_BHI260_FLASH
120// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO_FLASH
121// #define BOSCH_BHI260_AUX_BEM280_FLASH
122// #define BOSCH_BHI260_AUX_BMM150_BEM280_FLASH
123// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
124// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
125// #define BOSCH_BHI260_GPIO_FLASH
126
127#include <BoschFirmware.h>
128
129// Force update of current firmware, whether it exists or not.
130// Only works when external SPI Flash is connected to BHI260.
131// After uploading firmware once, you can change this to false to speed up boot time.
133
134#if BHI260_IRQ > 0
135#define USING_SENSOR_IRQ_METHOD
136#endif
137
138#ifdef USING_SENSOR_IRQ_METHOD
139volatile bool isInterruptTriggered = false;
140
142{
144}
145#endif /*USING_SENSOR_IRQ_METHOD*/
146
147#ifndef USING_DATA_HELPER
148void xyz_process_callback(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
149{
150 struct bhy2_data_xyz data;
151 float scaling_factor = bhy.getScaling(sensor_id);
152 bhy2_parse_xyz(data_ptr, &data);
153 Serial.print(bhy.getSensorName(sensor_id));
154 Serial.print(" ");
155 Serial.print("x: ");
156 Serial.print(data.x * scaling_factor);
157 Serial.print(", y: ");
158 Serial.print(data.y * scaling_factor);
159 Serial.print(", z: ");
160 Serial.print(data.z * scaling_factor);
161 Serial.println(";");
162}
163#endif
164
165// Firmware update progress callback
166void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
167{
168 float progress = (float)transferred / total * 100;
169 Serial.print("Upload progress: ");
170 Serial.print(progress);
171 Serial.println("%");
172}
173
174void setup()
175{
176 Serial.begin(115200);
177 while (!Serial);
178
179 // Set the reset pin
180 bhy.setPins(BHI260_RST);
181
182 Serial.println("Initializing Sensors...");
183
184 // Set the firmware array address and firmware size
185 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
186
187 // Set the firmware update processing progress callback function
188 // bhy.setUpdateProcessCallback(progress_callback, NULL);
189
190 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
191 // bhy.setMaxiTransferSize(256);
192
193 // Set the processing fifo data buffer size,The default size is 512 bytes.
194 // bhy.setProcessBufferSize(1024);
195
196 // Set to load firmware from flash
197 bhy.setBootFromFlash(bosch_firmware_type);
198
199#ifdef USE_I2C_INTERFACE
200 // Using I2C interface
201 // BHI260AP_SLAVE_ADDRESS_L = 0x28
202 // BHI260AP_SLAVE_ADDRESS_H = 0x29
203 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
204 Serial.print("Failed to initialize sensor - error code:");
205 Serial.println(bhy.getError());
206 while (1) {
207 delay(1000);
208 }
209 }
210#endif
211
212#ifdef USE_SPI_INTERFACE
213 // Using SPI interface
214 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
215 Serial.print("Failed to initialize sensor - error code:");
216 Serial.println(bhy.getError());
217 while (1) {
218 delay(1000);
219 }
220 }
221#endif
222
223 Serial.println("Initializing the sensor successfully!");
224
225 // Output all sensors info to Serial
226 BoschSensorInfo info = bhy.getSensorInfo();
227
228#ifdef ARDUINO
229 ArduinoStreamPrinter printer(Serial);
230 info.printInfo(printer);
231#else
232 info.printInfo([](const char *format, ...) -> int {
233 va_list args;
234 va_start(args, format);
235 int result = vprintf(format, args);
236 va_end(args);
237 return result;
238 });
239#endif
240
241 float sample_rate = 10.0; /* Read out data measured at 10Hz */
242 uint32_t report_latency_ms = 0; /* 0 = report immediately */
243
244#ifdef USING_DATA_HELPER
245 // Enable acceleration
246 accel.enable(sample_rate, report_latency_ms);
247 // Enable gyroscope
248 gyro.enable(sample_rate, report_latency_ms);
249 // Enable magnetometer
250 mag.enable(sample_rate, report_latency_ms);
251#else
252 // Enable acceleration
253 bhy.configure(BoschSensorID::ACCEL_PASSTHROUGH, sample_rate, report_latency_ms);
254 // Enable gyroscope
255 bhy.configure(BoschSensorID::GYRO_PASSTHROUGH, sample_rate, report_latency_ms);
256 // Enable magnetometer
257 bhy.configure(BoschSensorID::MAGNETOMETER_PASSTHROUGH, sample_rate, report_latency_ms);
258 // Set the acceleration sensor result callback function
259 bhy.onResultEvent(BoschSensorID::ACCEL_PASSTHROUGH, xyz_process_callback);
260 // Set the gyroscope sensor result callback function
261 bhy.onResultEvent(BoschSensorID::GYRO_PASSTHROUGH, xyz_process_callback);
262 // Set the magnetometer sensor result callback function
263 bhy.onResultEvent(BoschSensorID::MAGNETOMETER_PASSTHROUGH, xyz_process_callback);
264#endif
265
266#ifdef USING_SENSOR_IRQ_METHOD
267 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
268 // This makes the pin ready to receive external signals.
269 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
270 pinMode(BHI260_IRQ, INPUT);
271
272 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
273 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
274 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
275#endif
276
277}
278
279
280void loop()
281{
282#ifdef USING_SENSOR_IRQ_METHOD
284 isInterruptTriggered = false;
285#endif /*USING_SENSOR_IRQ_METHOD*/
286
287 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
288 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
289 */
290 bhy.update();
291
292#ifdef USING_SENSOR_IRQ_METHOD
293 }
294#endif /*USING_SENSOR_IRQ_METHOD*/
295
296#ifdef USING_DATA_HELPER
297 uint32_t s;
298 uint32_t ns;
299
300 AccelerometerData accel_data;
301 if (accel.readData(accel_data)) {
302 accel.getLastTime(s, ns);
303#ifdef PLATFORM_HAS_PRINTF
304 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] [AX:%+7.2f AY:%+7.2f AZ:%+7.2f] \n",
305 s, ns, accel_data.mps2.x, accel_data.mps2.y, accel_data.mps2.z);
306#else
307 Serial.print("[T: ");
308 Serial.print(s);
309 Serial.print(".");
310 Serial.print(ns);
311 Serial.print("] [AX:");
312 Serial.print(accel_data.mps2.x, 2);
313 Serial.print(" AY:");
314 Serial.print(accel_data.mps2.y, 2);
315 Serial.print(" AZ:");
316 Serial.print(accel_data.mps2.z, 2);
317 Serial.println("]");
318#endif
319 }
320 GyroscopeData gyro_data;
321 if (gyro.readData(gyro_data)) {
322 gyro.getLastTime(s, ns);
323#ifdef PLATFORM_HAS_PRINTF
324 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] [GX:%+7.2f GY:%+7.2f GZ:%+7.2f] \n",
325 s, ns, gyro_data.dps.x, gyro_data.dps.y, gyro_data.dps.z);
326#else
327 Serial.print("[T: ");
328 Serial.print(s);
329 Serial.print(".");
330 Serial.print(ns);
331 Serial.print("] [GX:");
332 Serial.print(gyro_data.dps.x, 2);
333 Serial.print(" GY:");
334 Serial.print(gyro_data.dps.y, 2);
335 Serial.print(" GZ:");
336 Serial.print(gyro_data.dps.z, 2);
337 Serial.println("]");
338#endif
339 }
340
341 MagnetometerData mag_data;
342 if (mag.readData(mag_data)) {
343 mag.getLastTime(s, ns);
344#ifdef PLATFORM_HAS_PRINTF
345 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] [MX:%+7.2f MY:%+7.2f MZ:%+7.2f] \n",
346 s, ns, mag_data.magnetic_field.x, mag_data.magnetic_field.y, mag_data.magnetic_field.z);
347#else
348 Serial.print("[T: ");
349 Serial.print(s);
350 Serial.print(".");
351 Serial.print(ns);
352 Serial.print("] [MX:");
353 Serial.print(mag_data.magnetic_field.x, 2);
354 Serial.print(" MY:");
355 Serial.print(mag_data.magnetic_field.y, 2);
356 Serial.print(" MZ:");
357 Serial.print(mag_data.magnetic_field.z, 2);
358 Serial.println("]");
359#endif
360 }
361#endif
362 delay(50);
363}
@license MIT License
@license MIT License
SensorBHI260AP bhy
SensorMagnetometer mag(bhy)
#define BHI260_SCL
void setup()
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 loop()
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash
IMUdata gyro[buffer_size]
IMUdata accel[buffer_size]
Structure representing accelerometer data.
SensorVector mps2
Acceleration in meters per second squared (m/s²)
Structure representing gyroscope data.
SensorVector dps
Angular velocity in degrees per second (°/s)
Structure representing magnetometer data.
SensorVector magnetic_field
magnetic field strength (Gauss)
float x
X-axis component.
float y
Y-axis component.
float z
Z-axis component.