SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmc6310_calibrate.ino
Go to the documentation of this file.
1
30#include <MagnetometerDrv.hpp>
31#ifdef ARDUINO_T_BEAM_S3_SUPREME
32#include <XPowersAXP2101.tpp> //PMU Library https://github.com/lewisxhe/XPowersLib.git
33#endif
34
35#ifndef SENSOR_SDA
36#define SENSOR_SDA 17
37#endif
38
39#ifndef SENSOR_SCL
40#define SENSOR_SCL 18
41#endif
42
44
46{
47#if defined(ARDUINO_T_BEAM_S3_SUPREME)
48 XPowersAXP2101 power;
49 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
50 power.disableALDO1();
51 power.disableALDO2();
52 delay(250);
53 power.setALDO1Voltage(3300);
54 power.enableALDO1();
55 power.setALDO2Voltage(3300);
56 power.enableALDO2();
57#endif
58}
59
61{
62 if (!magnetometer.setOutputDataRate(200.0f)) {
63 Serial.println("Failed to set output data rate");
64 return ;
65 }
66
67 Serial.println("========================================");
68 Serial.println("Calibration Instructions:");
69 Serial.println("1. Rotate sensor in FIGURE-8 pattern");
70 Serial.println("2. Cover all axes (X, Y, Z directions)");
71 Serial.println("3. Rotate slowly and completely");
72 Serial.println("4. Wait for progress bar to complete");
73 Serial.println("5. Expected: Magnetic Strength ~25-65 uT");
74 Serial.println("========================================");
75 Serial.println();
76
77 Serial.println("Place the sensor on the plane and slowly rotate the sensor...");
78 Serial.println("Rotate in FIGURE-8 pattern to cover all directions!");
79 Serial.println();
80
81 int32_t x_min = 65535;
82 int32_t x_max = -65535;
83 int32_t y_min = 65535;
84 int32_t y_max = -65535;
85 int32_t z_min = 65535;
86 int32_t z_max = -65535;
87
88 int32_t range = 1000;
89 int32_t i = 0;
90 int32_t x = 0, y = 0, z = 0;;
91 int16_t x_offset = 0;
92 int16_t y_offset = 0;
93 int16_t z_offset = 0;
94
96 while (i < range) {
97 i += 1;
98
100
102
103 x = (data.raw.x + x) / 2;
104 y = (data.raw.y + y) / 2;
105 z = (data.raw.z + z) / 2;
106 if (x < x_min) {
107 x_min = x;
108 i = 0;
109 }
110 if (x > x_max) {
111 x_max = x;
112 i = 0;
113 }
114 if (y < y_min) {
115 y_min = y;
116 i = 0;
117 }
118 if (y > y_max) {
119 y_max = y;
120 i = 0;
121 }
122 if (z < z_min) {
123 z_min = z;
124 i = 0;
125 }
126 if (z > z_max) {
127 z_max = z;
128 i = 0;
129 }
130 int j = round(10 * i / range);
131
132 Serial.print("[");
133 for (int k = 0; k < j; ++k) {
134 Serial.print("*");
135 }
136 Serial.println("]");
137 }
138 delay(5);
139 }
140
141 x_offset = (x_max + x_min) / 2;
142 y_offset = (y_max + y_min) / 2;
143 z_offset = (z_max + z_min) / 2;
144
145 Serial.print("x_min:");
146 Serial.println(x_min);
147
148 Serial.print("x_max:");
149 Serial.println(x_max);
150
151 Serial.print("y_min:");
152 Serial.println(y_min);
153
154 Serial.print("y_max:");
155 Serial.println(y_max);
156
157 Serial.print("z_min:");
158 Serial.println(z_min);
159
160 Serial.print("z_max:");
161 Serial.println(z_max);
162
163 Serial.print("x_offset:");
164 Serial.println(x_offset);
165
166 Serial.print("y_offset:");
167 Serial.println(y_offset);
168
169 Serial.print("z_offset:");
170 Serial.println(z_offset);
171
172
173 // Set the calibration value and the user calculates the deviation
174 magnetometer.setOffset(x_offset, y_offset, z_offset);
175
176 Serial.println();
177 Serial.println("Calibration complete!");
178 Serial.println("Check if Magnetic Strength is ~25-65 uT");
179 Serial.println("If too low, repeat calibration with better rotation");
180}
181
182
183void setup()
184{
185 Serial.begin(115200);
186 while (!Serial);
187
188 // LilyGo T-Beam-Supreme sensor requires a power source to function.
189 beginPower();
190
197 uint8_t address = QMC6310U_SLAVE_ADDRESS;
198 // uint8_t address = QMC6310N_SLAVE_ADDRESS;
199
200 if (!magnetometer.begin(Wire, address, SENSOR_SDA, SENSOR_SCL)) {
201 while (1) {
202 Serial.println("Failed to find QMC6310 - check your wiring!");
203 delay(1000);
204 }
205 }
206
207 // The desired output data rate in Hz. Allowed values are 10.0, 50.0, 100.0 and 200.0HZ.
208 float data_rate_hz = 200.0f;
209 // op_mode: Allowed values are SUSPEND, NORMAL, SINGLE_MEASUREMENT, CONTINUOUS_MEASUREMENT
211 // full_scale: Allowed values are FS_2G, FS_8G, FS_12G ,FS_30G
213 // over_sample_ratio: Allowed values are OSR_1, OSR_2, OSR_4, OSR_8
215 // down_sample_ratio: Allowed values are DSR_1, DSR_2, DSR_4, DSR_8
217
218 /* Config Magnetometer */
220 op_mode,
221 full_scale,
222 data_rate_hz,
223 over_sample_ratio,
224 down_sample_ratio)) {
225 Serial.println("Magnetometer configured successfully.");
226 } else {
227 Serial.println("Magnetometer configuration failed.");
228 while (1);
229 }
230
231 // Calibration algorithm reference from
232 // https://github.com/CoreElectronics/CE-PiicoDev-QMC6310-MicroPython-Module
233 calibrate();
234
235 Serial.println("Calibration done.");
236 delay(5000);
237
239 Serial.print("Manufacturer: "); Serial.println(info.manufacturer);
240 Serial.print("Model: "); Serial.println(info.model);
241 Serial.print("I2C Address: 0x"); Serial.println(info.i2c_address, HEX);
242 Serial.print("Version: "); Serial.println(info.version);
243 Serial.print("UID: 0x"); Serial.println(info.uid);
244 Serial.print("Type: "); Serial.println(SensorUtils::typeToString(info.type));
245
247 Serial.print("DataRate: "); Serial.println(cfg.sample_rate);
248 Serial.print("FullScaleRange: "); Serial.println(cfg.range);
249 Serial.print("Mode: "); Serial.println((uint8_t)cfg.mode);
250 Serial.println();
251
252 //Find the magnetic declination : https://www.magnetic-declination.com/
253 float declination_deg = MagnetometerUtils::dmsToDecimalDegrees(-3, 20); // -3.3333
254
255 magnetometer.setDeclination(declination_deg);
256
257 Serial.print(" Magnetic Declination: ");
258 Serial.print(declination_deg, 2);
259 Serial.println("°");
260
261 Serial.print(" Sensitivity: ");
262 Serial.print(magnetometer.getSensitivity(), 6);
263 Serial.println(" Gauss/LSB");
264
265 delay(3000);
266
267 Serial.println("Read data now...");
268}
269
270void loop()
271{
272 MagnetometerData data;
273
274 if (magnetometer.readData(data)) {
275
276 // Gauss to μT
280
281 Serial.print("Mag:");
282 Serial.print(" X:"); Serial.print(x);
283 Serial.print(" Y:"); Serial.print(y);
284 Serial.print(" Z:"); Serial.print(z);
285 Serial.print(" μT");
286
287 Serial.print(" Sensitivity: ");
288 Serial.print(magnetometer.getSensitivity(), 6);
289 Serial.print(" Gauss/LSB");
290
291 Serial.print(" Metadata:");
292 Serial.print(" X:");
293 Serial.print(data.raw.x);
294 Serial.print(" Y:");
295 Serial.print(data.raw.y);
296 Serial.print(" Z:");
297 Serial.print(data.raw.z);
298
299 Serial.print(" Heading (rad): ");
300 Serial.print(data.heading, 6);
301 Serial.print(" rad");
302
303 Serial.print(" Heading (deg): ");
304 Serial.print(data.heading_degrees, 2);
305 Serial.print("°");
306
308 strength = MagnetometerUtils::gaussToMicroTesla(strength);
309 Serial.print(" Magnetic Strength: ");
310 Serial.print(strength, 2);
311 Serial.println(" μT");
312
313 if (data.overflow) {
314 Serial.println("\tWarning: Data Overflow occurred!");
315 }
316 }
317 delay(10);
318}
319
320
321
@license MIT License
OperationMode
Enumeration of sensor operation modes.
MagFullScaleRange
Enumeration defining full-scale range settings for the sensor.
MagDownSampleRatio
Enumeration defining down-sample ratios for the sensor.
@ DSR_1
1x down-sample ratio
MagOverSampleRatio
Enumeration defining over-sample ratios for the sensor.
@ OSR_1
1x over-sample ratio
bool begin(SensorCommCustom::CustomCallback cb, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialize the sensor using custom callback interface.
virtual void setDeclination(float declination_deg)
Set the magnetic declination correction value.
void setOffset(int16_t x, int16_t y, int16_t z)
Set sensor calibration offsets.
virtual SensorConfig getConfig() const
Get current sensor configuration.
float getSensitivity() const
Get sensor sensitivity.
SensorInfo getSensorInfo() const
Get sensor information.
bool setOutputDataRate(float odr) override
Sets the output data rate for the magnetometer.
bool isDataReady() override
Checks if new data is available from the sensor.
bool configMagnetometer(OperationMode mode, MagFullScaleRange range, float odr, MagOverSampleRatio osr, MagDownSampleRatio dsr) override
Configures the magnetometer with multiple parameters.
bool readData(MagnetometerData &data) override
Reads the magnetic field data from the sensor.
static const char * typeToString(SensorType type)
Convert sensor type enumeration to string representation.
float dmsToDecimalDegrees(int degrees, int minutes, float seconds=0.0f)
Convert degrees-minutes-seconds to decimal degrees.
float calculateMagneticStrength(const MagnetometerData &data)
Calculate total magnetic field strength.
float gaussToMicroTesla(float gauss)
Converts Gauss to microTesla.
uint8_t i
SensorQMC6310 magnetometer
void setup()
#define SENSOR_SCL
#define SENSOR_SDA
void beginPower()
void calibrate()
void loop()
Structure representing magnetometer data.
RawVector raw
raw data
float heading_degrees
heading angle (degrees)
float heading
heading angle (radians)
SensorVector magnetic_field
magnetic field strength (Gauss)
bool overflow
data overflow flag
int16_t x
X-axis raw value.
int16_t y
Y-axis raw value.
int16_t z
Z-axis raw value.
Structure representing sensor configuration parameters.
float sample_rate
Sample rate in Hz.
float range
Measurement range.
OperationMode mode
Operation mode.
Structure containing sensor identification information.
uint8_t version
Hardware/firmware version.
uint8_t i2c_address
Default I2C address.
uint32_t uid
Unique identifier.
SensorType type
Sensor type.
const char * manufacturer
Manufacturer name.
const char * model
Model name/identifier.
float x
X-axis component.
float y
Y-axis component.
float z
Z-axis component.
int16_t x[5]
int16_t y[5]