SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmi8658_madgwick_ahrs_deprecated.ino
Go to the documentation of this file.
1
30#include <Arduino.h>
31#include <Wire.h>
32#include <SPI.h>
33#include "SensorQMI8658.hpp"
34#include <MadgwickAHRS.h> //MadgwickAHRS from https://github.com/arduino-libraries/MadgwickAHRS
35#ifdef ARDUINO_T_BEAM_S3_SUPREME
36#include <XPowersAXP2101.tpp> //PMU Library https://github.com/lewisxhe/XPowersLib.git
37#endif
38
39
40// #define USE_I2C //Using the I2C interface
41
42#ifdef USE_I2C
43#ifndef SENSOR_SDA
44#define SENSOR_SDA 17
45#endif
46
47#ifndef SENSOR_SCL
48#define SENSOR_SCL 18
49#endif
50
51#else /* SPI interface */
52
53#ifndef SPI_MOSI
54#define SPI_MOSI (35)
55#endif
56
57#ifndef SPI_SCK
58#define SPI_SCK (36)
59#endif
60
61#ifndef SPI_MISO
62#define SPI_MISO (37)
63#endif
64
65#endif /* USE_I2C*/
66
67#ifndef IMU_CS
68#define IMU_CS 34 // IMU CS PIN
69#endif
70
71#ifndef IMU_IRQ
72#define IMU_IRQ 33 // IMU INT PIN
73#endif
74
75#ifndef OLED_SDA
76#define OLED_SDA 22 // Display Wire SDA Pin
77#endif
78
79#ifndef OLED_SCL
80#define OLED_SCL 21 // Display Wire SCL Pin
81#endif
82
84
87
88Madgwick filter;
90
91
93{
94 // T_BEAM_S3_SUPREME The PMU voltage needs to be turned on to use the sensor
95#if defined(ARDUINO_T_BEAM_S3_SUPREME)
96 XPowersAXP2101 power;
97 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
98 power.disableALDO1();
99 power.disableALDO2();
100 delay(250);
101 power.setALDO1Voltage(3300); power.enableALDO1();
102 power.setALDO2Voltage(3300); power.enableALDO2();
103#endif
104}
105
106
107void setup()
108{
109 Serial.begin(115200);
110 while (!Serial);
111
112 beginPower();
113
114 bool ret = false;
115#ifdef USE_I2C
116 ret = qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL);
117#else
118#if defined(SPI_MOSI) && defined(SPI_SCK) && defined(SPI_MISO)
119 ret = qmi.begin(SPI, IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK);
120#else
121 ret = qmi.begin(SPI, IMU_CS);
122#endif
123#endif
124
125 if (!ret) {
126 Serial.println("Failed to find QMI8658 - check your wiring!");
127 while (1) {
128 delay(1000);
129 }
130 }
131
132
133
134 /* Get chip id*/
135 Serial.print("Device ID:");
136 Serial.println(qmi.getChipID(), HEX);
137
139 /*
140 * ACC_RANGE_2G
141 * ACC_RANGE_4G
142 * ACC_RANGE_8G
143 * ACC_RANGE_16G
144 * */
146 /*
147 * ACC_ODR_1000H
148 * ACC_ODR_500Hz
149 * ACC_ODR_250Hz
150 * ACC_ODR_125Hz
151 * ACC_ODR_62_5Hz
152 * ACC_ODR_31_25Hz
153 * ACC_ODR_LOWPOWER_128Hz
154 * ACC_ODR_LOWPOWER_21Hz
155 * ACC_ODR_LOWPOWER_11Hz
156 * ACC_ODR_LOWPOWER_3H
157 * */
159 /*
160 * LPF_MODE_0 //2.66% of ODR
161 * LPF_MODE_1 //3.63% of ODR
162 * LPF_MODE_2 //5.39% of ODR
163 * LPF_MODE_3 //13.37% of ODR
164 * LPF_OFF // OFF Low-Pass Fitter
165 * */
167
168
170 /*
171 * GYR_RANGE_16DPS
172 * GYR_RANGE_32DPS
173 * GYR_RANGE_64DPS
174 * GYR_RANGE_128DPS
175 * GYR_RANGE_256DPS
176 * GYR_RANGE_512DPS
177 * GYR_RANGE_1024DPS
178 * */
180 /*
181 * GYR_ODR_7174_4Hz
182 * GYR_ODR_3587_2Hz
183 * GYR_ODR_1793_6Hz
184 * GYR_ODR_896_8Hz
185 * GYR_ODR_448_4Hz
186 * GYR_ODR_224_2Hz
187 * GYR_ODR_112_1Hz
188 * GYR_ODR_56_05Hz
189 * GYR_ODR_28_025H
190 * */
192 /*
193 * LPF_MODE_0 //2.66% of ODR
194 * LPF_MODE_1 //3.63% of ODR
195 * LPF_MODE_2 //5.39% of ODR
196 * LPF_MODE_3 //13.37% of ODR
197 * LPF_OFF // OFF Low-Pass Fitter
198 * */
200
201
202 /*
203 * If both the accelerometer and gyroscope sensors are turned on at the same time,
204 * the output frequency will be based on the gyroscope output frequency.
205 * The example configuration is 896.8HZ output frequency,
206 * so the acceleration output frequency is also limited to 896.8HZ
207 * */
210
211 // Print register configuration information
213
214 // start filter
215 filter.begin(25);
216
217 // initialize variables to pace updates to correct rate
218 microsPerReading = 1000000 / 25;
219 microsPrevious = micros();
220
221 Serial.println("Read data now...");
222}
223
224
225void loop()
226{
227 float roll, pitch, heading;
228
229 // check if it's time to read data and update the filter
230 if (micros() - microsPrevious >= microsPerReading) {
231
232 // read raw data from IMU
233 if (qmi.getDataReady()) {
236 // update the filter, which computes orientation
237 filter.updateIMU(gyr.x, gyr.y, gyr.z, acc.x, acc.y, acc.z);
238
239 // print the heading, pitch and roll
240 roll = filter.getRoll();
241 pitch = filter.getPitch();
242 heading = filter.getYaw();
243 Serial.print("Orientation: ");
244 Serial.print(heading);
245 Serial.print(" ");
246 Serial.print(pitch);
247 Serial.print(" ");
248 Serial.println(roll);
249 }
250 // increment previous time, so we keep proper pace
252 }
253}
254
255
256
#define SENSOR_SCL
#define SENSOR_SDA
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
QMI8658 IMU sensor driver class.
bool getGyroscope(float &x, float &y, float &z)
Get the raw gyroscope data.
void dumpCtrlRegister()
Dump the control register values.
uint8_t getChipID() override
Get the chip ID / WHO_AM_I value.
bool configAccelerometer(AccelRange range, AccelODR odr, LpfMode lpfOdr=LPF_MODE_0)
Configure the accelerometer.
bool configGyroscope(GyroRange range, GyroODR odr, LpfMode lpfOdr=LPF_MODE_0)
Configure the gyroscope.
bool enableGyroscope()
Enable the gyroscope.
bool enableAccelerometer()
Enable the accelerometer.
bool getDataReady()
Check if new data is available.
bool getAccelerometer(float &x, float &y, float &z)
Get the accelerometer data.
SensorQMI8658 qmi