Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
HMC5883L.h
Go to the documentation of this file.
1
5#ifndef HMC5883L_H
6#define HMC5883L_H
7
8#include "sys/i2c.h"
9#include <cmath>
10
18 enum ADDR {
19 DEFAULT = 0x1e,
20 };
21 enum MODE {
22 CONT = 0b00000000,
23 SINGLE = 0b00000001,
24 IDLE = 0b00000011,
25 };
26 enum SAMPLES {
27 S1 = 0b00000000,
28 S2 = 0b00100000,
29 S4 = 0b01000000,
30 S8 = 0b01100000,
31 };
32 enum SPEED {
33 Hz0_75 = 0b0000000,
34 Hz1_5 = 0b00000100,
35 Hz3 = 0b00001000,
36 Hz7_5 = 0b00001100,
37 Hz15 = 0b00010000,
38 Hz30 = 0b00010100,
39 Hz75 = 0b00011000
40 };
41 enum GAIN {
42 G0_88 = 0b00000000,
43 G1_3 = 0b00100000,
44 G1_9 = 0b01000000,
45 G2_5 = 0b01100000,
46 G4_0 = 0b10000000,
47 G4_7 = 0b10100000,
48 G5_6 = 0b11000000,
49 G8_1 = 0b11100000,
50 };
51 enum MEAS {
52 NORMAL = 0b00000000,
53 POS = 0b00000001,
54 NEG = 0b00000010,
55 };
59 HMC5883L(Sink<I2C::Request> &bus, enum ADDR addr=DEFAULT)
60 : I2C::Device{bus, addr} {
61 // Config register 1: 1 sample averages, 75 Hz output rate
62 writeReg(0x00, S1 | Hz75 | NORMAL);
63 // Config register 2: set 1.9 Gauss
64 writeReg(0x01, G1_3);
65 // Mode register: cont operation mode
66 writeReg(0x01, CONT);
67 }
68
72 void measure() {
73 bus.trypush({
74 .dev = this,
75 .data = 6,
76 .opts = {
77 .read = true,
78 .mem = true,
79 },
80 .mem = 0x3,
81 });
82 }
83
84 void writeReg(uint8_t reg, uint8_t val) {
85 bus.trypush({
86 .dev = this,
87 .data = {val},
88 .opts = {
89 .mem = true,
90 },
91 .mem = reg,
92 });
93 }
94
95 void callback(const I2C::Request &rq) override {
96 if (rq.data.size != 6) return;
97
98 this->Axes.x = rq.data.buf[1] | (rq.data.buf[0] << 8);
99 this->Axes.y = rq.data.buf[3] | (rq.data.buf[2] << 8);
100 this->Axes.z = rq.data.buf[5] | (rq.data.buf[4] << 8);
101
102 this->heading = atan2(this->Axes.y, this->Axes.x);
103
104 this->heading += this->declinationAngle;
105 if(this->heading < 0)
106 this->heading += 2 * M_PI;
107 if(this->heading > 2 * M_PI)
108 this->heading -= 2 * M_PI;
109 }
110
111 double getHeadingDegree() {
112 return heading * 180 / M_PI;
113 }
114
115 struct {
116 uint16_t x, y, z;
117 } Axes;
118
119 double heading = 0;
120 double declinationAngle = 0.0303; // http://www.magnetic-declination.com/
121};
122
123#endif //HMC5883L_H
Copyright (c) 2023 IACE.
size_t size
total capacity of buffer
Definition buffer.h:23
Implementation of the HMC5883L multi-chip three-axis magnetic sensor.
Definition HMC5883L.h:17
HMC5883L(Sink< I2C::Request > &bus, enum ADDR addr=DEFAULT)
initialize the sensor
Definition HMC5883L.h:59
void measure()
start async read of angle
Definition HMC5883L.h:72
void callback(const I2C::Request &rq) override
callback.
Definition HMC5883L.h:95
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
generic object sink, i.e.
Definition streams.h:15
void trypush(T &&t)
use this if you don't care if it's going to be successful. data gets discarded if sink is full.
Definition streams.h:28