SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_6dof.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
97SensorAcceleration accel(bhy);
98SensorGyroscope gyro(bhy);
99#endif
100
101// 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.
102#define BOSCH_APP30_SHUTTLE_BHI260_FW
103// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150FW
104// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X
105// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390
106// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO
107// #define BOSCH_BHI260_AUX_BEM280
108// #define BOSCH_BHI260_AUX_BMM150_BEM280
109// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
110// #define BOSCH_BHI260_AUX_BMM150_GPIO
111// #define BOSCH_BHI260_GPIO
112
113// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
114// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150_FLASH
115// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X_FLASH
116// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390_FLASH
117// #define BOSCH_APP30_SHUTTLE_BHI260_FLASH
118// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO_FLASH
119// #define BOSCH_BHI260_AUX_BEM280_FLASH
120// #define BOSCH_BHI260_AUX_BMM150_BEM280_FLASH
121// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
122// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
123// #define BOSCH_BHI260_GPIO_FLASH
124
125#include <BoschFirmware.h>
126
127// Force update of current firmware, whether it exists or not.
128// Only works when external SPI Flash is connected to BHI260.
129// After uploading firmware once, you can change this to false to speed up boot time.
131
132#if BHI260_IRQ > 0
133#define USING_SENSOR_IRQ_METHOD
134#endif
135
136#ifdef USING_SENSOR_IRQ_METHOD
137volatile bool isInterruptTriggered = false;
138
140{
142}
143#endif /*USING_SENSOR_IRQ_METHOD*/
144
145
146#ifndef USING_DATA_HELPER
147void xyz_process_callback(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
148{
149 struct bhy2_data_xyz data;
150 float scaling_factor = bhy.getScaling(sensor_id);
151 bhy2_parse_xyz(data_ptr, &data);
152 Serial.print(bhy.getSensorName(sensor_id));
153 Serial.print(" ");
154 Serial.print("x: ");
155 Serial.print(data.x * scaling_factor);
156 Serial.print(", y: ");
157 Serial.print(data.y * scaling_factor);
158 Serial.print(", z: ");
159 Serial.print(data.z * scaling_factor);
160 Serial.println(";");
161}
162#endif
163
164// Firmware update progress callback
165void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
166{
167 float progress = (float)transferred / total * 100;
168 Serial.print("Upload progress: ");
169 Serial.print(progress);
170 Serial.println("%");
171}
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 = 100.0; /* Read out data measured at 100Hz */
242 uint32_t report_latency_ms = 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#else
250 // Enable acceleration
251 bhy.configure(BoschSensorID::ACCEL_PASSTHROUGH, sample_rate, report_latency_ms);
252 // Enable gyroscope
253 bhy.configure(BoschSensorID::GYRO_PASSTHROUGH, sample_rate, report_latency_ms);
254 // Set the acceleration sensor result callback function
255 bhy.onResultEvent(BoschSensorID::ACCEL_PASSTHROUGH, xyz_process_callback);
256 // Set the gyroscope sensor result callback function
257 bhy.onResultEvent(BoschSensorID::GYRO_PASSTHROUGH, xyz_process_callback);
258#endif
259
260#ifdef USING_SENSOR_IRQ_METHOD
261 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
262 // This makes the pin ready to receive external signals.
263 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
264 pinMode(BHI260_IRQ, INPUT);
265
266 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
267 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
268 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
269#endif
270}
271
272
273void loop()
274{
275#ifdef USING_SENSOR_IRQ_METHOD
277 isInterruptTriggered = false;
278#endif /*USING_SENSOR_IRQ_METHOD*/
279
280 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
281 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
282 */
283 bhy.update();
284
285#ifdef USING_SENSOR_IRQ_METHOD
286 }
287#endif /*USING_SENSOR_IRQ_METHOD*/
288
289
290#ifdef USING_DATA_HELPER
291 uint32_t s;
292 uint32_t ns;
293 AccelerometerData accel_data;
294 if (accel.readData(accel_data)) {
295 accel.getLastTime(s, ns);
296#ifdef PLATFORM_HAS_PRINTF
297 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] [AX:%+7.2f AY:%+7.2f AZ:%+7.2f] \n",
298 s, ns, accel_data.mps2.x, accel_data.mps2.y, accel_data.mps2.z);
299#else
300 Serial.print("[T: ");
301 Serial.print(s);
302 Serial.print(".");
303 Serial.print(ns);
304 Serial.print("] [AX:");
305 Serial.print(accel_data.mps2.x, 2);
306 Serial.print(" AY:");
307 Serial.print(accel_data.mps2.y, 2);
308 Serial.print(" AZ:");
309 Serial.print(accel_data.mps2.z, 2);
310 Serial.println("]");
311#endif
312 }
313 GyroscopeData gyro_data;
314 if (gyro.readData(gyro_data)) {
315 gyro.getLastTime(s, ns);
316#ifdef PLATFORM_HAS_PRINTF
317 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] [GX:%+7.2f GY:%+7.2f GZ:%+7.2f] \n",
318 s, ns, gyro_data.dps.x, gyro_data.dps.y, gyro_data.dps.z);
319#else
320 Serial.print("[T: ");
321 Serial.print(s);
322 Serial.print(".");
323 Serial.print(ns);
324 Serial.print("] [GX:");
325 Serial.print(gyro_data.dps.x, 2);
326 Serial.print(" GY:");
327 Serial.print(gyro_data.dps.y, 2);
328 Serial.print(" GZ:");
329 Serial.print(gyro_data.dps.z, 2);
330 Serial.println("]");
331#endif
332 }
333#endif /*USING_DATA_HELPER*/
334 delay(50);
335}
336
337
338
@license MIT License
@license MIT License
SensorBHI260AP 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)
float x
X-axis component.
float y
Y-axis component.
float z
Z-axis component.