Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
QMC5883L.h
Go to the documentation of this file.
1
5#ifndef QMC5883L_H
6#define QMC5883L_H
7
8#include "sys/i2c.h"
9#include <cmath>
10
19 enum ADDR {
20 DEFAULT = 0b0001101,
21 ALT = 0b0001111,
22 };
23 enum MODE {
24 STANDBY = 0b00000000,
25 CONT = 0b00000001,
26 };
27 enum ODR {
28 Hz10 = 0b00000000,
29 Hz50 = 0b00000100,
30 Hz100 = 0b00001000,
31 Hz200 = 0b00001100,
32 };
33 enum RANGE {
34 G2 = 0b00000000,
35 G8 = 0b00010000,
36 };
37 enum OSR {
38 S512 = 0b00000000,
39 S256 = 0b01000000,
40 S128 = 0b10000000,
41 S64 = 0b11000000,
42 };
43
47 QMC5883L(Sink<I2C::Request> &bus, enum ADDR addr=DEFAULT)
48 : I2C::Device{bus, addr} {
49 // set/reset period
50 writeReg(0x0B,0x01);
51 // set mode
52 writeReg(0x09, CONT | Hz200 | G8 | S512);
53 }
54
58 void measure() {
59 bus.push({
60 .dev = this,
61 .data = 9,
62 .opts = {
63 .read = true,
64 .mem = true,
65 },
66 .mem = 0x00,
67 });
68 }
69
70 void writeReg(uint8_t reg, uint8_t val) {
71 bus.push({
72 .dev = this,
73 .data = {val},
74 .opts = {
75 .mem = true,
76 },
77 .mem = reg,
78 });
79 }
80
81 void callback(const I2C::Request &rq) override {
82 if (rq.data.size != 9) return;
83
84 // TODO Check status register
85 this->Axes.x = rq.data.buf[0] | (rq.data.buf[1] << 8);
86 this->Axes.y = rq.data.buf[2] | (rq.data.buf[3] << 8);
87 this->Axes.z = rq.data.buf[4] | (rq.data.buf[5] << 8);
88 this->Temperature = ((double) (rq.data.buf[7] | (rq.data.buf[8] << 8)));
89
90 this->heading = atan2(this->Axes.y, this->Axes.x);
91
92 this->heading += this->declinationAngle;
93 if(this->heading < 0)
94 this->heading += 2 * M_PI;
95 if(this->heading > 2 * M_PI)
96 this->heading -= 2 * M_PI;
97 }
98
99 double getHeadingDegree() {
100 return heading * 180 / M_PI;
101 }
102
103private:
104 double Temperature = 0; // current temperature
105 struct {
106 uint16_t x, y, z;
107 } Axes;
108
109 double heading = 0;
110 double declinationAngle = 0.0303; // http://www.magnetic-declination.com/
111};
112
113#endif //QMC5883L_H
Copyright (c) 2023 IACE.
size_t size
total capacity of buffer
Definition buffer.h:23
generic I2C device wrapper.
Definition i2c.h:21
Device(Sink< Request > &bus, uint8_t address)
construct device on bus with given address
Definition i2c.h:26
struct defining an I2C request.
Definition i2c.h:37
Buffer< uint8_t > data
transfer data
Definition i2c.h:39
Implementation of the QMC5883L multi-chip three-axis magnetic sensor.
Definition QMC5883L.h:18
QMC5883L(Sink< I2C::Request > &bus, enum ADDR addr=DEFAULT)
initialize the sensor
Definition QMC5883L.h:47
void callback(const I2C::Request &rq) override
callback.
Definition QMC5883L.h:81
void measure()
start async read of angle
Definition QMC5883L.h:58
generic object sink, i.e.
Definition streams.h:15
virtual void push(const T &t)
copy semantics does not check for space. guard by using 'if (!full) { ... }'
Definition streams.h:20