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