SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi360_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 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
97SensorAcceleration accel(bhy);
98SensorGyroscope gyro(bhy);
99#endif
100
101// #define BOSCH_DATA_INJECT_BHI360
102// #define BOSCH_DATA_INJECT_BHI360_BMM150
103// #define BOSCH_DATA_INJECT_BHI360_BMM150_HEAD_ORIENTATION
104// #define BOSCH_DATA_INJECT_BHI360_BMM350
105// #define BOSCH_DATA_INJECT_BHI360_BMM350_HEAD_ORIENTATION
106// #define BOSCH_DATA_INJECT_BHI360_ENV
107// #define BOSCH_DATA_INJECT_BHI360_HEAD_ORIENTATION
108// #define BOSCH_DATA_INJECT_BHI360_MOTION_AI
109#define BOSCH_SHUTTLE3_BHI360
110// #define BOSCH_SHUTTLE3_BHI360_AUX_BMM150
111// #define BOSCH_SHUTTLE3_BHI360_BMM150
112// #define BOSCH_SHUTTLE3_BHI360_BMM150_BMP580_BME688
113// #define BOSCH_SHUTTLE3_BHI360_BMM150_HEAD_ORIENTATION
114// #define BOSCH_SHUTTLE3_BHI360_BMM350C
115// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BME688_IAQ
116// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580
117// #define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580_BME688
118// #define BOSCH_SHUTTLE3_BHI360_BMM350C_HEAD_ORIENTATION
119// #define BOSCH_SHUTTLE3_BHI360_BMM350C_POLL
120// #define BOSCH_SHUTTLE3_BHI360_BMM350C_TURBO
121// #define BOSCH_SHUTTLE3_BHI360_BMP580_TEST_EXAMPLE
122// #define BOSCH_SHUTTLE3_BHI360_HW_ACTIVITY
123// #define BOSCH_SHUTTLE3_BHI360_HW_ACTIVITY_TURBO
124// #define BOSCH_SHUTTLE3_BHI360_IMU_HEAD_ORIENTATION
125// #define BOSCH_SHUTTLE3_BHI360_POLLING_STEP_COUNTER
126// #define BOSCH_SHUTTLE3_BHI360_TEMP
127// #define BOSCH_SHUTTLE3_BHI360_TEST_DATA_SOURCE
128// #define BOSCH_SHUTTLE3_BHI360_TURBO
129
130#include <BoschFirmware.h>
131
132
133#if BHI360_IRQ > 0
134#define USING_SENSOR_IRQ_METHOD
135#endif
136
137#ifdef USING_SENSOR_IRQ_METHOD
138volatile bool isInterruptTriggered = false;
139
141{
143}
144#endif /*USING_SENSOR_IRQ_METHOD*/
145
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
174
175void setup()
176{
177 Serial.begin(115200);
178 while (!Serial);
179
180 // Set the reset pin
181 bhy.setPins(BHI360_RST);
182
183 Serial.println("Initializing Sensors...");
184
185 // Set the firmware array address and firmware size
186 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size);
187
188 // Set the firmware update processing progress callback function
189 bhy.setUpdateProcessCallback(progress_callback, NULL);
190
191 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
192 // bhy.setMaxiTransferSize(256);
193
194 // Set the processing fifo data buffer size,The default size is 512 bytes.
195 // bhy.setProcessBufferSize(1024);
196
197#ifdef USE_I2C_INTERFACE
198 // Using I2C interface
199 // BHI360_SLAVE_ADDRESS_L = 0x28
200 // BHI360_SLAVE_ADDRESS_H = 0x29
201 if (!bhy.begin(Wire, BHI360_SLAVE_ADDRESS_L, BHI360_SDA, BHI360_SCL)) {
202 Serial.print("Failed to initialize sensor - error code:");
203 Serial.println(bhy.getError());
204 while (1) {
205 delay(1000);
206 }
207 }
208#endif
209
210#ifdef USE_SPI_INTERFACE
211 // Using SPI interface
212 if (!bhy.begin(SPI, BHI360_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
213 Serial.print("Failed to initialize sensor - error code:");
214 Serial.println(bhy.getError());
215 while (1) {
216 delay(1000);
217 }
218 }
219#endif
220
221 Serial.println("Initializing the sensor successfully!");
222
223 // Output all sensors info to Serial
224 BoschSensorInfo info = bhy.getSensorInfo();
225
226#ifdef ARDUINO
227 ArduinoStreamPrinter printer(Serial);
228 info.printInfo(printer);
229#else
230 info.printInfo([](const char *format, ...) -> int {
231 va_list args;
232 va_start(args, format);
233 int result = vprintf(format, args);
234 va_end(args);
235 return result;
236 });
237#endif
238
239
240 float sample_rate = 100.0; /* Read out data measured at 100Hz */
241 uint32_t report_latency_ms = 0; /* Report immediately */
242
243#ifdef USING_DATA_HELPER
244 // Enable acceleration
245 accel.enable(sample_rate, report_latency_ms);
246 // Enable gyroscope
247 gyro.enable(sample_rate, report_latency_ms);
248#else
249 // Enable acceleration
250 bhy.configure(BoschSensorID::ACCEL_PASSTHROUGH, sample_rate, report_latency_ms);
251 // Enable gyroscope
252 bhy.configure(BoschSensorID::GYRO_PASSTHROUGH, sample_rate, report_latency_ms);
253 // Set the acceleration sensor result callback function
254 bhy.onResultEvent(BoschSensorID::ACCEL_PASSTHROUGH, xyz_process_callback);
255 // Set the gyroscope sensor result callback function
256 bhy.onResultEvent(BoschSensorID::GYRO_PASSTHROUGH, xyz_process_callback);
257#endif
258
259#ifdef USING_SENSOR_IRQ_METHOD
260 // Set the specified pin (BHI360_IRQ) ​​to an input pin.
261 // This makes the pin ready to receive external signals.
262 // If the interrupt is already connected, if BHI360_IRQ is equal to -1 then the polling method will be used
263 pinMode(BHI360_IRQ, INPUT);
264
265 // Attach an interrupt service routine (ISR) to the specified pin (BHI360_IRQ).
266 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
267 attachInterrupt(BHI360_IRQ, dataReadyISR, RISING);
268#endif
269}
270
271
272void loop()
273{
274#ifdef USING_SENSOR_IRQ_METHOD
276 isInterruptTriggered = false;
277#endif /*USING_SENSOR_IRQ_METHOD*/
278
279 /* If the interrupt is connected to the sensor and BHI360_IRQ is not equal to -1,
280 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
281 */
282 bhy.update();
283
284#ifdef USING_SENSOR_IRQ_METHOD
285 }
286#endif /*USING_SENSOR_IRQ_METHOD*/
287
288
289#ifdef USING_DATA_HELPER
290 uint32_t s;
291 uint32_t ns;
292 AccelerometerData accel_data;
293 if (accel.readData(accel_data)) {
294 accel.getLastTime(s, ns);
295#ifdef PLATFORM_HAS_PRINTF
296 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] [AX:%+7.2f AY:%+7.2f AZ:%+7.2f] \n",
297 s, ns, accel_data.mps2.x, accel_data.mps2.y, accel_data.mps2.z);
298#else
299 Serial.print("[T: ");
300 Serial.print(s);
301 Serial.print(".");
302 Serial.print(ns);
303 Serial.print("] [AX:");
304 Serial.print(accel_data.mps2.x, 2);
305 Serial.print(" AY:");
306 Serial.print(accel_data.mps2.y, 2);
307 Serial.print(" AZ:");
308 Serial.print(accel_data.mps2.z, 2);
309 Serial.println("]");
310#endif
311 }
312 GyroscopeData gyro_data;
313 if (gyro.readData(gyro_data)) {
314 gyro.getLastTime(s, ns);
315#ifdef PLATFORM_HAS_PRINTF
316 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] [GX:%+7.2f GY:%+7.2f GZ:%+7.2f] \n",
317 s, ns, gyro_data.dps.x, gyro_data.dps.y, gyro_data.dps.z);
318#else
319 Serial.print("[T: ");
320 Serial.print(s);
321 Serial.print(".");
322 Serial.print(ns);
323 Serial.print("] [GX:");
324 Serial.print(gyro_data.dps.x, 2);
325 Serial.print(" GY:");
326 Serial.print(gyro_data.dps.y, 2);
327 Serial.print(" GZ:");
328 Serial.print(gyro_data.dps.z, 2);
329 Serial.println("]");
330#endif
331 }
332#endif /*USING_DATA_HELPER*/
333 delay(50);
334}
335
336
337
@license MIT License
@license MIT License
SensorBHI360 bhy
#define BHI360_SCL
void setup()
#define BHI360_SDA
void dataReadyISR()
volatile bool isInterruptTriggered
#define BHI360_RST
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
#define BHI360_IRQ
void loop()
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.