Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
AS5048b.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <cmath>
8#include <cstdint>
9#include "sys/i2c.h"
10
13public:
14
19 AS5048B(Sink<I2C::Request> &bus, bool A0, bool A1)
20 : I2C::Device{bus, (uint8_t)(0x40 | (A1 << 1) | A0)}
21 , bInit(false)
22 { }
23
27 void measure() {
28 bus.trypush({
29 .dev = this,
30 .data = 2,
31 .opts = {
32 .read = true,
33 .mem = true,
34 },
35 .mem = 0xfe,
36 });
37 }
38
39 void callback(const I2C::Request &rq) override {
40 if (rq.mem != 0xfe) return;
41 uint16_t iRawAngle = ((uint16_t) rq.data.buf[0] << 6);
42 iRawAngle += (rq.data.buf[1] & 0x3F);
43
44 if (!bInit) {
45 this->dLastAngle = ((double) iRawAngle);
46 bInit = true;
47 } else {
48 double dCurAngle = ((double) iRawAngle);
49 double dDiffAngle = this->dLastAngle - dCurAngle;
50 if (this->dLastAngle == -1 || (this->dLastAngle == 0 && (dDiffAngle > 1000 || dDiffAngle < -1000))) {
51 dDiffAngle = 0;
52 } else if (dDiffAngle < -10000) {
53 dDiffAngle += 16384;
54 } else if (dDiffAngle > 10000) {
55 dDiffAngle -= 16384;
56 }
57
58 this->dAngle += dDiffAngle;
59 this->dLastAngle = dCurAngle;
60 }
61 }
62
66 double get() {
67 return this->dAngle / 16384 * 2 * M_PI;
68 }
69
73 void reset() {
74 this->dAngle = 0;
75 this->dLastAngle = 0;
76 bInit = false;
77 }
78
79private:
81 bool bInit = false;
82
83 double dAngle = 0; // difference angle for rotational movement
84 double dLastAngle = 0; // difference angle for rotational movement
86};
14-bit rotary position sensor
Definition AS5048b.h:12
AS5048B(Sink< I2C::Request > &bus, bool A0, bool A1)
initialize the sensor.
Definition AS5048b.h:19
void callback(const I2C::Request &rq) override
callback.
Definition AS5048b.h:39
double get()
Returns the angle in radians.
Definition AS5048b.h:66
void measure()
start async read of angle
Definition AS5048b.h:27
void reset()
Resets the angles.
Definition AS5048b.h:73
Copyright (c) 2023 IACE.
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
uint8_t mem
memory address for memory transmissions
Definition i2c.h:61
generic object sink, i.e.
Definition streams.h:15