PicoLowLevel
Loading...
Searching...
No Matches
ams_as5048b.h
Go to the documentation of this file.
1/**************************************************************************/
43/**************************************************************************/
44
45#if ARDUINO >= 100
46 #include <Arduino.h>
47#else
48 #include <WProgram.h>
49#endif
50
51#include <math.h>
52#include <Wire.h>
53
54// custom debug class
55#include "Debug.h"
56
57
58#ifndef _AMS_AS5048B_H_
59#define _AMS_AS5048B_H_
60
61
62#define ANGLE_READ_ERROR -1000
63
64
65// OPERATIONS
66//#define SERIAL_DEBUG_ENABLED - modified to use custom Debug class
67//#define USE_WIREBEGIN_ENABLED // to comment if Wire.begin() function is called in Setup() for instance. Usefull to manage one or several I2C devices in the same sketch
68
69// Default addresses for AS5048B
70#define AS5048_ADDRESS 0x40 // 0b10000 + ( A1 & A2 to GND)
71#define AS5048B_PROG_REG 0x03
72#define AS5048B_ADDR_REG 0x15
73#define AS5048B_ZEROMSB_REG 0x16 //bits 0..7
74#define AS5048B_ZEROLSB_REG 0x17 //bits 0..5
75#define AS5048B_GAIN_REG 0xFA
76#define AS5048B_DIAG_REG 0xFB
77#define AS5048B_MAGNMSB_REG 0xFC //bits 0..7
78#define AS5048B_MAGNLSB_REG 0xFD //bits 0..5
79#define AS5048B_ANGLMSB_REG 0xFE //bits 0..7
80#define AS5048B_ANGLLSB_REG 0xFF //bits 0..5
81#define AS5048B_RESOLUTION 16384.0 //14 bits
82
83
84// Moving Exponential Average on angle - beware heavy calculation for some Arduino boards
85// This is a 1st order low pass filter
86// Moving average is calculated on Sine et Cosine values of the angle to provide an extrapolated accurate angle value.
87#define EXP_MOVAVG_N 5 //history length impact on moving average impact - keep in mind the moving average will be impacted by the measurement frequency too
88#define EXP_MOVAVG_LOOP 1 //number of measurements before starting mobile Average - starting with a simple average - 1 allows a quick start. Value must be 1 minimum
89
90//unit consts - just to make the units more readable
91#define U_RAW 1
92#define U_TRN 2
93#define U_DEG 3
94#define U_RAD 4
95#define U_GRAD 5
96#define U_MOA 6
97#define U_SOA 7
98#define U_MILNATO 8
99#define U_MILSE 9
100#define U_MILRU 10
101
102
103
104
105
107 public:
108 AMS_AS5048B(void);
109 AMS_AS5048B(uint8_t chipAddress);
110
111 void begin(void); // to init the object, must be called in the setup loop
112 void toggleDebug(void); // start / stop debug through serial at anytime
113 void setClockWise(boolean cw = true); //set clockwise counting, default is false (native sensor)
114 void progRegister(uint8_t regVal); //nothing so far - manipulate the OTP register
115 void doProg(void); //progress programming slave address OTP
116 void doProgZero(void); //progress programming zero position OTP
117 void addressRegW(uint8_t regVal); //change the chip address
118 uint8_t addressRegR(void); //read chip address
119 void setZeroReg(void); //set Zero to current angle position
120 void zeroRegW(uint16_t regVal); //write Zero register value
121 uint16_t zeroRegR(void); //read Zero register value
122 uint16_t angleRegR(void); //read raw value of the angle register
123 uint8_t diagR(void); //read diagnostic register
124 uint16_t magnitudeR(void); //read current magnitude
125 double angleR(int unit = U_RAW, boolean newVal = true); //Read current angle or get last measure with unit conversion : RAW, TRN, DEG, RAD, GRAD, MOA, SOA, MILNATO, MILSE, MILRU
126 uint8_t getAutoGain(void);
127 uint8_t getDiagReg(void);
128
129 void updateMovingAvgExp(void); //measure the current angle and feed the Exponential Moving Average calculation
130 double getMovingAvgExp(int unit = U_RAW); //get Exponential Moving Average calculation
131 void resetMovingAvgExp(void); //reset Exponential Moving Average calculation values
132
133 private:
134 //variables
135 boolean _debugFlag;
136 boolean _clockWise;
137 uint8_t _chipAddress;
138 uint8_t _addressRegVal;
139 uint16_t _zeroRegVal;
140 double _lastAngleRaw;
141 double _movingAvgExpAngle;
142 double _movingAvgExpSin;
143 double _movingAvgExpCos;
144 double _movingAvgExpAlpha;
145 int _movingAvgCountLoop;
146 boolean _i2cerror;
147
148 //methods
149 uint8_t readReg8(uint8_t address);
150 uint16_t readReg16(uint8_t address); //16 bit value got from 2x8bits registers (7..0 MSB + 5..0 LSB) => 14 bits value
151 void writeReg(uint8_t address, uint8_t value);
152 double convertAngle(int unit, double angle); //RAW, TRN, DEG, RAD, GRAD, MOA, SOA, MILNATO, MILSE, MILRU
153 double getExpAvgRawAngle(void);
154 void printDebug(void);
155};
156
157#endif
#define U_RAW
Definition ams_as5048b.h:91
Definition ams_as5048b.h:106
uint8_t addressRegR(void)
reads I2C address register value
Definition ams_as5048b.cpp:223
void updateMovingAvgExp(void)
Performs an exponential moving average on the angle. Works on Sine and Cosine of the angle to avoid i...
Definition ams_as5048b.cpp:373
double angleR(int unit=U_RAW, boolean newVal=true)
reads current angle value and converts it into the desired unit
Definition ams_as5048b.cpp:340
void setClockWise(boolean cw=true)
Set / unset clock wise counting - sensor counts CCW natively.
Definition ams_as5048b.cpp:106
uint16_t angleRegR(void)
Definition ams_as5048b.cpp:293
uint8_t getAutoGain(void)
reads the 1 bytes auto gain register value
Definition ams_as5048b.cpp:308
double getMovingAvgExp(int unit=U_RAW)
sent back the exponential moving averaged angle in the desired unit
Definition ams_as5048b.cpp:409
uint8_t diagR(void)
void zeroRegW(uint16_t regVal)
writes the 2 bytes Zero position register value
Definition ams_as5048b.cpp:256
void doProgZero(void)
Burn values to the zero position OTP register.
Definition ams_as5048b.cpp:167
void progRegister(uint8_t regVal)
writes OTP control register
Definition ams_as5048b.cpp:124
uint16_t zeroRegR(void)
reads the 2 bytes Zero position register value
Definition ams_as5048b.cpp:273
void addressRegW(uint8_t regVal)
write I2C address value (5 bits) into the address register
Definition ams_as5048b.cpp:202
void doProg(void)
Burn values to the slave address OTP register.
Definition ams_as5048b.cpp:140
void begin(void)
init values and overall behaviors for AS5948B use
Definition ams_as5048b.cpp:57
void toggleDebug(void)
Toggle debug output to serial.
Definition ams_as5048b.cpp:90
AMS_AS5048B(void)
Definition ams_as5048b.cpp:32
void resetMovingAvgExp(void)
Definition ams_as5048b.cpp:414
uint16_t magnitudeR(void)
reads the 2 bytes magnitude register value
Definition ams_as5048b.cpp:288
void setZeroReg(void)
sets current angle as the zero position
Definition ams_as5048b.cpp:238
uint8_t getDiagReg(void)
reads the 1 bytes diagnostic register value
Definition ams_as5048b.cpp:323