SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmi8658_interrupt_block_deprecated.ino
Go to the documentation of this file.
1
30#include <Arduino.h>
31#include <Wire.h>
32#include <SPI.h>
33#ifdef ARDUINO_ARCH_ESP32
34#include "SensorQMI8658.hpp"
35#include <MadgwickAHRS.h> //MadgwickAHRS from https://github.com/arduino-libraries/MadgwickAHRS
36#include "SH1106Wire.h" //Oled display from https://github.com/ThingPulse/esp8266-oled-ssd1306
37#ifdef ARDUINO_T_BEAM_S3_SUPREME
38#include <XPowersAXP2101.tpp> //PMU Library https://github.com/lewisxhe/XPowersLib.git
39#endif
40
41// #define USE_I2C //Using the I2C interface
42
43#ifdef USE_I2C
44#ifndef SENSOR_SDA
45#define SENSOR_SDA 17
46#endif
47
48#ifndef SENSOR_SCL
49#define SENSOR_SCL 18
50#endif
51
52#else /*SPI interface*/
53
54#ifndef SPI_MOSI
55#define SPI_MOSI (35)
56#endif
57
58#ifndef SPI_SCK
59#define SPI_SCK (36)
60#endif
61
62#ifndef SPI_MISO
63#define SPI_MISO (37)
64#endif
65
66#endif /*USE_I2C*/
67
68#ifndef IMU_CS
69#define IMU_CS 34 // IMU CS PIN
70#endif
71
72#ifndef IMU_IRQ
73#define IMU_IRQ 33 // IMU INT PIN
74#endif
75
76#ifndef OLED_SDA
77#define OLED_SDA 22 // Display Wire SDA Pin
78#endif
79
80#ifndef OLED_SCL
81#define OLED_SCL 21 // Display Wire SCL Pin
82#endif
83
84
85SH1106Wire display(0x3c, OLED_SDA, OLED_SCL);
87
90
91Madgwick filter;
92unsigned long microsPerReading, microsPrevious;
93
94float posX = 64;
95float posY = 32;
96float lastPosX = posX;
97float lastPosY = posY;
98
99
100void beginPower()
101{
102 // T_BEAM_S3_SUPREME The PMU voltage needs to be turned on to use the sensor
103#if defined(ARDUINO_T_BEAM_S3_SUPREME)
104 XPowersAXP2101 power;
105 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
106 power.disableALDO1();
107 power.disableALDO2();
108 delay(250);
109 power.setALDO1Voltage(3300); power.enableALDO1();
110 power.setALDO2Voltage(3300); power.enableALDO2();
111#endif
112}
113
114void setup()
115{
116 Serial.begin(115200);
117 while (!Serial);
118
119
120 beginPower();
121
122 display.init();
123 display.flipScreenVertically();
124
125 bool ret = false;
126#ifdef USE_I2C
127 ret = qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL);
128#else
129#if defined(SPI_MOSI) && defined(SPI_SCK) && defined(SPI_MISO)
130 ret = qmi.begin(SPI, IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK);
131#else
132 ret = qmi.begin(SPI, IMU_CS);
133#endif
134#endif
135
136 if (!ret) {
137 Serial.println("Failed to find QMI8658 - check your wiring!");
138 while (1) {
139 delay(1000);
140 }
141 }
142
143 /* Get chip id*/
144 Serial.print("Device ID:");
145 Serial.println(qmi.getChipID(), HEX);
146
147
149 /*
150 * ACC_RANGE_2G
151 * ACC_RANGE_4G
152 * ACC_RANGE_8G
153 * ACC_RANGE_16G
154 * */
156 /*
157 * ACC_ODR_1000H
158 * ACC_ODR_500Hz
159 * ACC_ODR_250Hz
160 * ACC_ODR_125Hz
161 * ACC_ODR_62_5Hz
162 * ACC_ODR_31_25Hz
163 * ACC_ODR_LOWPOWER_128Hz
164 * ACC_ODR_LOWPOWER_21Hz
165 * ACC_ODR_LOWPOWER_11Hz
166 * ACC_ODR_LOWPOWER_3H
167 * */
169 /*
170 * LPF_MODE_0 //2.66% of ODR
171 * LPF_MODE_1 //3.63% of ODR
172 * LPF_MODE_2 //5.39% of ODR
173 * LPF_MODE_3 //13.37% of ODR
174 * LPF_OFF // OFF Low-Pass Fitter
175 * */
177
178
180 /*
181 * GYR_RANGE_16DPS
182 * GYR_RANGE_32DPS
183 * GYR_RANGE_64DPS
184 * GYR_RANGE_128DPS
185 * GYR_RANGE_256DPS
186 * GYR_RANGE_512DPS
187 * GYR_RANGE_1024DPS
188 * */
190 /*
191 * GYR_ODR_7174_4Hz
192 * GYR_ODR_3587_2Hz
193 * GYR_ODR_1793_6Hz
194 * GYR_ODR_896_8Hz
195 * GYR_ODR_448_4Hz
196 * GYR_ODR_224_2Hz
197 * GYR_ODR_112_1Hz
198 * GYR_ODR_56_05Hz
199 * GYR_ODR_28_025H
200 * */
202 /*
203 * LPF_MODE_0 //2.66% of ODR
204 * LPF_MODE_1 //3.63% of ODR
205 * LPF_MODE_2 //5.39% of ODR
206 * LPF_MODE_3 //13.37% of ODR
207 * LPF_OFF // OFF Low-Pass Fitter
208 * */
210
211
212 /*
213 * If both the accelerometer and gyroscope sensors are turned on at the same time,
214 * the output frequency will be based on the gyroscope output frequency.
215 * The example configuration is 896.8HZ output frequency,
216 * so the acceleration output frequency is also limited to 896.8HZ
217 * */
220
221 pinMode(IMU_IRQ, INPUT);
222
223 // qmi.enableINT(SensorQMI8658::INTERRUPT_PIN_1); //no use
224 // Enable data ready to interrupt pin2
227
228 // Print register configuration information
230
231 // start filter
232 filter.begin(25);
233
234 // initialize variables to pace updates to correct rate
235 microsPerReading = 1000000 / 25;
236 microsPrevious = micros();
237
238 Serial.println("Read data now...");
239}
240
241const uint8_t rectWidth = 10;
242
243void loop()
244{
245 float roll, pitch, heading;
246
247 // check if it's time to read data and update the filter
248 if (micros() - microsPrevious >= microsPerReading) {
249
250 // read raw data from IMU
251 if (digitalRead(IMU_IRQ) == HIGH) {
252
255
256 // update the filter, which computes orientation
257 filter.updateIMU(gyr.x, gyr.y, gyr.z, acc.x, acc.y, acc.z);
258
259 // print the heading, pitch and roll
260 roll = filter.getRoll();
261 pitch = filter.getPitch();
262 heading = filter.getYaw();
263
264 posX -= roll * 2;
265 posY += pitch;
266
267 posX = constrain(posX, 0, display.width() - rectWidth);
268 posY = constrain(posY, 0, display.height() - rectWidth);
269
270 display.setColor(BLACK);
271 display.fillRect(lastPosX, lastPosY, 10, 10);
272 display.setColor(WHITE);
273 display.fillRect(posX, posY, 10, 10);
274 display.display();
275
276 lastPosX = posX;
277 lastPosY = posY;
278 }
279 // increment previous time, so we keep proper pace
281 }
282}
283#else
284void setup()
285{
286 Serial.begin(115200);
287}
288
289void loop()
290{
291 Serial.println("The graphics library may not support your esp32 platform"); delay(1000);
292}
293#endif
294
295
#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.
void enableINT(IntPin pin, bool enable=true)
Enable or disable the interrupt.
void enableDataReadyINT(bool enable=true)
Enable or disable the Data Ready interrupt.
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 getAccelerometer(float &x, float &y, float &z)
Get the accelerometer data.
void beginPower()
#define IMU_IRQ
SensorQMI8658 qmi