SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
qmc6310_compass.ino
Go to the documentation of this file.
1
30#include <MagnetometerDrv.hpp>
31
32#if defined(ARDUINO_ARCH_ESP32)
33#include "SH1106Wire.h" //Oled display from https://github.com/ThingPulse/esp8266-oled-ssd1306
34#ifdef ARDUINO_T_BEAM_S3_SUPREME
35#include <XPowersAXP2101.tpp> //PMU Library https://github.com/lewisxhe/XPowersLib.git
36#endif
37
38#ifndef SENSOR_SDA
39#define SENSOR_SDA 17
40#endif
41
42#ifndef SENSOR_SCL
43#define SENSOR_SCL 18
44#endif
45
46#ifndef OLED_SDA
47#define OLED_SDA 22 // Display Wire SDA Pin
48#endif
49
50#ifndef OLED_SCL
51#define OLED_SCL 21 // Display Wire SCL Pin
52#endif
53
54SH1106Wire display(0x3c, OLED_SDA, OLED_SCL);
56
57//Compass application from https://github.com/G6EJD/ESP8266_micro_compass_HMC5883_OLED
58void arrow(int x2, int y2, int x1, int y1, int alength, int awidth, OLEDDISPLAY_COLOR color)
59{
60 display.setColor(color);
61 float distance;
62 int dx, dy, x2o, y2o, x3, y3, x4, y4, k;
63 distance = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
64 dx = x2 + (x1 - x2) * alength / distance;
65 dy = y2 + (y1 - y2) * alength / distance;
66 k = awidth / alength;
67 x2o = x2 - dx;
68 y2o = dy - y2;
69 x3 = y2o * k + dx;
70 y3 = x2o * k + dy;
71 x4 = dx - y2o * k;
72 y4 = dy - x2o * k;
73 display.drawLine(x1, y1, x2, y2);
74 display.drawLine(x1, y1, dx, dy);
75 display.drawLine(x3, y3, x4, y4);
76 display.drawLine(x3, y3, x2, y2);
77 display.drawLine(x2, y2, x4, y4);
78}
79
80void beginPower()
81{
82#if defined(ARDUINO_T_BEAM_S3_SUPREME)
83 XPowersAXP2101 power;
84 power.begin(Wire1, AXP2101_SLAVE_ADDRESS, 42, 41);
85 power.disableALDO1();
86 power.disableALDO2();
87 delay(250);
88 power.setALDO1Voltage(3300);
89 power.enableALDO1();
90 power.setALDO2Voltage(3300);
91 power.enableALDO2();
92#endif
93}
94
95void setup()
96{
97 Serial.begin(115200);
98 while (!Serial);
99
100 // LilyGo T-Beam-Supreme sensor requires a power source to function.
101 beginPower();
102
109 uint8_t address = QMC6310U_SLAVE_ADDRESS;
110 // uint8_t address = QMC6310N_SLAVE_ADDRESS;
111
112 if (!magnetometer.begin(Wire, address, SENSOR_SDA, SENSOR_SCL)) {
113 while (1) {
114 Serial.println("Failed to find QMC6310 - check your wiring!");
115 delay(1000);
116 }
117 }
118
119 // The desired output data rate in Hz. Allowed values are 10.0, 50.0, 100.0 and 200.0HZ.
120 float data_rate_hz = 200.0f;
121 // op_mode: Allowed values are SUSPEND, NORMAL, SINGLE_MEASUREMENT, CONTINUOUS_MEASUREMENT
123 // full_scale: Allowed values are FS_2G, FS_8G, FS_12G ,FS_30G
125 // over_sample_ratio: Allowed values are OSR_1, OSR_2, OSR_4, OSR_8
127 // down_sample_ratio: Allowed values are DSR_1, DSR_2, DSR_4, DSR_8
129
130 /* Config Magnetometer */
132 op_mode,
133 full_scale,
134 data_rate_hz,
135 over_sample_ratio,
136 down_sample_ratio)) {
137 Serial.println("Magnetometer configured successfully.");
138 } else {
139 Serial.println("Magnetometer configuration failed.");
140 while (1);
141 }
142
144 Serial.print("Manufacturer: "); Serial.println(info.manufacturer);
145 Serial.print("Model: "); Serial.println(info.model);
146 Serial.print("I2C Address: 0x"); Serial.println(info.i2c_address, HEX);
147 Serial.print("Version: "); Serial.println(info.version);
148 Serial.print("UID: 0x"); Serial.println(info.uid);
149 Serial.print("Type: "); Serial.println(SensorUtils::typeToString(info.type));
150
152 Serial.print("DataRate: "); Serial.println(cfg.sample_rate);
153 Serial.print("FullScaleRange: "); Serial.println(cfg.range);
154 Serial.print("Mode: "); Serial.println((uint8_t)cfg.mode);
155 Serial.println();
156
157
158 display.init();
159
160 //Find the magnetic declination : https://www.magnetic-declination.com/
161 float declination_deg = MagnetometerUtils::dmsToDecimalDegrees(-3, 20); // -3.3333
162
163 magnetometer.setDeclination(declination_deg);
164
165 Serial.print(" Magnetic Declination: ");
166 Serial.print(declination_deg, 2);
167 Serial.println("°");
168
169 Serial.print(" Sensitivity: ");
170 Serial.print(magnetometer.getSensitivity(), 6);
171 Serial.println(" Gauss/LSB");
172
173}
174
175void loop()
176{
177 MagnetometerData data;
178
179 static int last_angle = -1;
180
181 if (magnetometer.readData(data)) {
182 int angle = static_cast<int>(data.heading_degrees + 0.5f);
183
184 if (angle != last_angle) {
185 display.clear();
186
187 display.setFont(ArialMT_Plain_10);
188 display.setTextAlignment(TEXT_ALIGN_CENTER);
189 display.drawString(32, 0, "N"); // North
190 display.drawString(0, 28, "W"); // West
191 display.drawString(64, 28, "E"); // East
192 display.drawString(32, 53, "S"); // South
193
194 display.drawCircle(32, 32, 20);
195
196 // Calculate arrow direction
197 // Note: data.heading_degrees is already 0-360°, 0° = North, 90° = East
198 // Display coordinate system: 0° points to the top of the screen (North), and the angle increases clockwise.
199 float arrow_angle_rad = data.heading_degrees * M_PI / 180.0f;
200 // Note: sin corresponds to the x-axis
201 int arrow_x = 32 + static_cast<int>(25 * cosf(arrow_angle_rad));
202 // Note: cos corresponds to the y-axis, the negative sign is because the screen's y-axis points downwards.
203 int arrow_y = 32 - static_cast<int>(25 * sinf(arrow_angle_rad));
204
205 display.drawLine(32, 32, arrow_x, arrow_y);
206 display.fillCircle(arrow_x, arrow_y, 2);
207
208 display.setTextAlignment(TEXT_ALIGN_LEFT);
209 display.drawString(75, 5, "Angle:" + String(angle) + "°");
210 display.drawString(75, 25, "Decl:" + String(magnetometer.getDeclinationDeg(), 1) + "°");
212 display.drawString(75, 45, "Str:" + String(strength, 1) + "uT");
213
214 display.display();
215
216 Serial.print("Heading: ");
217 Serial.print(angle);
218 Serial.print("°, Raw: ");
219 Serial.print(data.heading_degrees, 2);
220 Serial.print("°, Declination: ");
221 Serial.print(magnetometer.getDeclinationDeg(), 2);
222 Serial.println("°");
223
224 last_angle = angle;
225 }
226 }
227
228 delay(100);
229}
230#else
231void setup()
232{
233 Serial.begin(115200);
234}
235
236void loop()
237{
238 Serial.println("The graphics library may not be supported on the esp32 platform"); delay(1000);
239}
240#endif
@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
#define M_PI
MagOverSampleRatio
Enumeration defining over-sample ratios for the sensor.
@ OSR_1
1x over-sample ratio
#define SENSOR_SCL
#define SENSOR_SDA
SensorBMM150 magnetometer
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
virtual float getDeclinationDeg() const
Get the currently set magnetic declination in degrees.
virtual void setDeclination(float declination_deg)
Set the magnetic declination correction value.
bool readData(MagnetometerData &data) override
Read one magnetometer sample.
bool configMagnetometer(OperationMode mode, MagFullScaleRange range, float odr, MagOverSampleRatio osr, MagDownSampleRatio dsr=MagDownSampleRatio::DSR_1) override
Apply combined magnetometer configuration.
virtual SensorConfig getConfig() const
Get current sensor configuration.
float getSensitivity() const
Get sensor sensitivity.
SensorInfo getSensorInfo() const
Get sensor information.
static const char * typeToString(SensorType type)
Convert sensor type enumeration to string representation.
void beginPower()
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.
void setup()
void loop()
Structure representing magnetometer data.
float heading_degrees
heading angle (degrees)
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.