/
nano33BLErev2.ino
104 lines (86 loc) · 3.28 KB
/
nano33BLErev2.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/******************************************************************
@file nano33BLErev2.ino
@brief Print roll, pitch, yaw and heading angles using the
BMI270/BMM150 IMUs on the Nano 33 BLE Sense rev2.
@author David Such
@copyright Please see the accompanying LICENSE file.
Code: David Such
Version: 2.2.0
Date: 10/02/23
1.0.0 Original Release. 22/02/22
1.1.0 Added NONE fusion option. 25/05/22
2.0.0 Changed Repo & Branding 15/12/22
2.0.1 Invert Gyro Values PR 24/12/22
2.1.0 Updated Fusion Library 30/12/22
2.2.0 Add support for Nano 33 BLE Sense Rev. 2 10/02/23
This sketch is configured to work with the MADGWICK, MAHONY,
CLASSIC, COMPLEMENTARY, KALMAN & NONE Sensor Fusion options. Set the
algorithm that you wish to use with:
imu.setFusionAlgorithm(SensorFusion::MADGWICK);
******************************************************************/
#include <ReefwingAHRS.h>
#include <Arduino_BMI270_BMM150.h>
ReefwingAHRS ahrs;
SensorData data;
// Display and Loop Frequency
int loopFrequency = 0;
const long displayPeriod = 1000;
unsigned long previousMillis = 0;
void setup() {
// Initialise the AHRS
// Use default fusion algo and parameters
ahrs.begin();
ahrs.setFusionAlgorithm(SensorFusion::MADGWICK);
ahrs.setDeclination(12.717); // Sydney, Australia
// Start Serial and wait for connection
Serial.begin(115200);
while (!Serial);
Serial.print("Detected Board - ");
Serial.println(ahrs.getBoardTypeString());
if (IMU.begin() && ahrs.getBoardType() == BoardType::NANO33BLE_SENSE_R2) {
Serial.println("BMI270 & BMM150 IMUs Connected.");
Serial.print("Gyroscope sample rate = ");
Serial.print(IMU.gyroscopeSampleRate());
Serial.println(" Hz");
Serial.println();
Serial.println("Gyroscope in degrees/second");
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println(" Hz");
Serial.println();
Serial.println("Acceleration in G's");
Serial.print("Magnetic field sample rate = ");
Serial.print(IMU.magneticFieldSampleRate());
Serial.println(" Hz");
Serial.println();
Serial.println("Magnetic Field in uT");
}
else {
Serial.println("BMI270 & BMM150 IMUs Not Detected.");
while(1);
}
}
void loop() {
if (IMU.gyroscopeAvailable()) { IMU.readGyroscope(data.gx, data.gy, data.gz); }
if (IMU.accelerationAvailable()) { IMU.readAcceleration(data.ax, data.ay, data.az); }
if (IMU.magneticFieldAvailable()) { IMU.readMagneticField(data.mx, data.my, data.mz); }
ahrs.setData(data);
ahrs.update();
if (millis() - previousMillis >= displayPeriod) {
// Display sensor data every displayPeriod, non-blocking.
Serial.print("--> Roll: ");
Serial.print(ahrs.angles.roll, 2);
Serial.print("\tPitch: ");
Serial.print(ahrs.angles.pitch, 2);
Serial.print("\tYaw: ");
Serial.print(ahrs.angles.yaw, 2);
Serial.print("\tHeading: ");
Serial.print(ahrs.angles.heading, 2);
Serial.print("\tLoop Frequency: ");
Serial.print(loopFrequency);
Serial.println(" Hz");
loopFrequency = 0;
previousMillis = millis();
}
loopFrequency++;
}