3軸磁気センサーQMC5883Lのライブラリを作ってみた。
各軸を360度回転させると自動でキャリブレーションを行う。キャリブレーションの正確さは各軸センサーの最大値と最小値の正しさに依存し毎回キャリブレーションしなくても済むようにキャリブレーションデータの取得と設定が可能となっている。
【スケッチ】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <Wire.h> #include "qmc5883l.h" DECLARE_QMC5883L(Wire) qmc5883l; void setup(void) { Wire.begin(); Wire.setClock(qmc5883l.WIRE_CLOCK); qmc5883l.begin(); if (キャリブレーション保存データあり?) { 読込(qmc5883l.calibration); qmc5883l.setCalibration(qmc5883l.calibration); } qmc5883l.start(); } void loop(void) { qmc5883l.readHeading(qmc5883l.angle); Serial.printf("cal = %02X, yx = %d, zx = %d, zy = %d\n", qmc5883l.calibstatus, qmc5883l.angle.yx, qmc5883l.angle.zx, qmc5883l.angle.zy); if ((qmc5883l.calibstatus & 0x03) == 0x03) /* X&Y軸キャリブレーション完了 */ { /* qmc5883l.calibstatus: */ /* bit0: X軸キャリブレーション完了 */ /* bit1: Y軸キャリブレーション完了 */ /* bit2: Z軸キャリブレーション完了 */ qmc5883l.getCalibration(qmc5883l.calibration); 保存(qmc5883l.calibration); } delay(100); } |
【ライブラリ】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
/* qmc5883l.h - 3-Axis Magnetic Sensor Library for Arduino Copyright (c) 2024 Sasapea's Lab. All right reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ #pragma once #include <stdint.h> #include <stdbool.h> #include <string.h> #include <math.h> #if defined(JENNIC_CHIP_FAMILY_JN516x) #undef _BIT_FIELDS_HTOL #define _BIT_FIELDS_HTOL #endif #define DECLARE_QMC5883L(instance) QMC5883L<typeof(instance), instance> template<typename T, T& WIRE> class QMC5883L { public: static constexpr uint32_t WIRE_CLOCK = 400000; static constexpr uint8_t NUM_AXISES = 3; enum { ERR_NONE = 0, ERR_IO = 4, ERR_ARG = 8, }; typedef enum { MODE_STANDBY, MODE_CONTINUOUS, } MODE; typedef enum { ODR_10Hz, ODR_50Hz, ODR_100Hz, ODR_200Hz, } ODR; typedef enum { RNG_2G, RNG_8G, } RNG; typedef enum { OSR_512, OSR_256, OSR_128, OSR_64, } OSR; typedef struct __attribute__((packed)) { #if defined(_BIT_FIELDS_HTOL) uint8_t RSV : 5; uint8_t DOR : 1; /* Data Skip */ uint8_t OVL : 1; /* Overflow flag */ uint8_t DRDY: 1; /* Data Ready */ #else uint8_t DRDY: 1; /* Data Ready */ uint8_t OVL : 1; /* Overflow flag */ uint8_t DOR : 1; /* Data Skip */ uint8_t RSV : 5; #endif } status_t; typedef struct { struct { int16_t min; int16_t max; } axis[NUM_AXISES]; } calibration_t; typedef struct { int16_t axis[NUM_AXISES]; } output_t; typedef struct { int16_t yx; int16_t zx; int16_t zy; } angle_t; /* Free to use variables */ static inline output_t output; static inline status_t status; static inline int16_t temperature; static inline uint8_t chipid; static inline calibration_t calibration; static inline uint8_t calibstatus; static inline angle_t angle; static void begin(void) { /* Initialize Wire with setup() */ resetCalibration(); } static bool start(ODR odr = ODR_200Hz, RNG rng = RNG_8G, OSR osr = OSR_512) { bool rv = setPeriod(0x01); if (rv) { control1_t control1; rv = getControl1(control1); if (rv) { control1.mode = MODE_CONTINUOUS; control1.odr = odr; control1.rng = rng; control1.osr = osr; rv = setControl1(control1); } } return rv; } static bool stop(void) { control1_t control1; bool rv = getControl1(control1); if (rv) { control1.mode = MODE_STANDBY; rv = setControl1(control1); } return rv; } static bool readRaw(output_t &output) { return readRaw(output, status); } static bool readRaw(output_t &output, status_t &status) { bool rv = getStatus(status); if (rv && status.DRDY) rv = read(offsetof(regmap_t, Output), _regmap.Output, offsetof(regmap_t, Status) - offsetof(regmap_t, Output)); if (rv) { for (uint8_t i = 0; i < NUM_AXISES; ++i) output.axis[i] = (((int16_t)_regmap.Output[i].MSB) << 8) | _regmap.Output[i].LSB; } return rv; } static bool getTemperature(int16_t &value) { /* only relative temperature value is accurate (100 LSB/C) */ bool rv = read(offsetof(regmap_t, Temperature), &_regmap.Temperature, sizeof(_regmap.Temperature)); if (rv) value = (((int16_t)_regmap.Temperature.MSB << 8) | _regmap.Temperature.LSB); return rv; } static bool getChipID(uint8_t &value) { /* not supported ? */ bool rv = read(offsetof(regmap_t, ChipID), &_regmap.ChipID, sizeof(_regmap.ChipID)); if (rv) value = _regmap.ChipID; return rv; } static bool interrupt(bool enable) { control2_t control2; bool rv = getControl2(control2); if (rv) { control2.INT_ENB = !enable; rv = setControl2(control2); } return rv; } static bool rollOver(bool enable) { control2_t control2; bool rv = getControl2(control2); if (rv) { control2.ROL_PNT = enable; rv = setControl2(control2); } return rv; } static bool reset(void) { control2_t control2; bool rv = getControl2(control2); if (rv) { control2.SOFT_RST = 1; rv = setControl2(control2); } return rv; } static bool readHeading(angle_t &angle, uint8_t calibration = 0xFF) { return readHeading(angle, calibstatus, calibration); } static bool readHeading(angle_t &angle, uint8_t& status, uint8_t calibration = 0xFF) { float axis[2][NUM_AXISES]; bool rv = readRaw(output); if (rv) { uint8_t i; for (i = 0; i < NUM_AXISES; ++i) { int16_t raw = axis[0][i] = output.axis[i]; if (_calibration.axis[i].min > raw) _calibration.axis[i].min = raw; if (_calibration.axis[i].max < raw) _calibration.axis[i].max = raw; if ((_calibration.axis[i].max < THRESHOLD) || (-THRESHOLD < _calibration.axis[i].min)) calibration &= ~(1 << i); if (calibration & (1 << i)) axis[1][i] = (axis[0][i] - (_calibration.axis[i].max + _calibration.axis[i].min) / 2) / (_calibration.axis[i].max - _calibration.axis[i].min); } status = calibration & ((1 << NUM_AXISES) - 1); i = (calibration & 0b011) == 0b011; angle.yx = atan2(axis[i][1], axis[i][0]) * 180 / M_PI; i = (calibration & 0b101) == 0b101; angle.zx = atan2(axis[i][2], axis[i][0]) * 180 / M_PI; i = (calibration & 0b110) == 0b110; angle.zy = atan2(axis[i][2], axis[i][1]) * 180 / M_PI; if (angle.yx < 0) angle.yx += 360; if (angle.zx < 0) angle.zx += 360; if (angle.zy < 0) angle.zy += 360; } return rv; } static void resetCalibration(void) { memset(_regmap.Output, 0, sizeof(_regmap.Output)); memset(&_calibration, 0, sizeof(_calibration)); } static void getCalibration(calibration_t &value) { value = _calibration; } static void setCalibration(calibration_t &value) { _calibration = value; } static int errcode(void) { return _errcode; } private: static constexpr uint8_t DEVICE = 0x0D; static constexpr int16_t THRESHOLD = 500; /* [TODO] */ typedef struct __attribute__((packed)) { uint8_t LSB; int8_t MSB; } word_t; typedef struct __attribute__((packed)) { #if defined(_BIT_FIELDS_HTOL) OSR osr : 2; /* Over sample Rate */ RNG rng : 2; /* Range (Sensitivity of the magnetic sensor) */ ODR odr : 2; /* Output data rate */ MODE mode: 2; /* Transfer mode */ #else MODE mode: 2; /* Transfer mode */ ODR odr : 2; /* Output data rate */ RNG rng : 2; /* Range (Sensitivity of the magnetic sensor) */ OSR osr : 2; /* Over sample Rate */ #endif } control1_t; typedef struct __attribute__((packed)) { #if defined(_BIT_FIELDS_HTOL) uint8_t SOFT_RST: 1; /* Soft reset */ uint8_t ROL_PNT : 1; /* Enable pointer roll-over function */ uint8_t RSV : 5; uint8_t INT_ENB : 1; /* Interrupt enabling */ #else uint8_t INT_ENB : 1; /* Interrupt enabling */ uint8_t RSV : 5; uint8_t ROL_PNT : 1; /* Enable pointer roll-over function */ uint8_t SOFT_RST: 1; /* Soft reset */ #endif } control2_t; typedef struct __attribute__((packed)) { word_t Output[NUM_AXISES]; /* RO */ status_t Status; /* RO */ word_t Temperature; /* RO */ control1_t Control1; /* RW */ control2_t Control2; /* RW */ uint8_t Period; /* RW */ uint8_t Reserved; /* RO */ uint8_t ChipID; /* RO */ } regmap_t; /* static inline is C++17 and later */ static inline int _errcode; static inline regmap_t _regmap; static inline calibration_t _calibration; static bool read(uint8_t addr, void *buf, size_t len) { if (!write(addr, nullptr, 0, false)) return false; WIRE.requestFrom(DEVICE, len); return (_errcode = WIRE.readBytes((uint8_t *)buf, len) == len ? ERR_NONE : ERR_IO) == ERR_NONE; } static bool write(uint8_t addr, const void *buf, size_t len, uint8_t sendStop = true) { WIRE.beginTransmission(DEVICE); WIRE.write(&addr, sizeof(addr)); if (buf && len) WIRE.write((const uint8_t *)buf, len); return (_errcode = WIRE.endTransmission(sendStop)) == ERR_NONE; } static bool getStatus(status_t &value) { value = {}; return read(offsetof(regmap_t, Status), &value, sizeof(value)); } static bool getControl1(control1_t &value) { bool rv = read(offsetof(regmap_t, Control1), &_regmap.Control1, sizeof(_regmap.Control1)); if (rv) value = _regmap.Control1; return rv; } static bool setControl1(control1_t value) { bool rv = write(offsetof(regmap_t, Control1), &value, sizeof(value)); if (rv) _regmap.Control1 = value; return rv; } static bool getControl2(control2_t &value) { bool rv = read(offsetof(regmap_t, Control2), &_regmap.Control2, sizeof(_regmap.Control2)); if (rv) value = _regmap.Control2; return rv; } static bool setControl2(control2_t value) { bool rv = write(offsetof(regmap_t, Control2), &value, sizeof(value)); if (rv) { value.SOFT_RST = 0; _regmap.Control2 = value; } return rv; } static bool getPeriod(uint8_t &value) { bool rv = read(offsetof(regmap_t, Period), &_regmap.Period, sizeof(_regmap.Period)); if (rv) value = _regmap.Period; return rv; } static bool setPeriod(uint8_t value) { bool rv = write(offsetof(regmap_t, Period), &value, sizeof(value)); if (rv) _regmap.Period = value; return rv; } }; |