satnogs-rotator-firmware
Loading...
Searching...
No Matches
as5601.h
Go to the documentation of this file.
1
12#ifndef AS5601_H_
13#define AS5601_H_
14
15#include <Arduino.h>
16#include <Wire.h>
17
18#define I2C_FREQ 100000
19
20#define AS5601_ID 0x36
21#define RAW_ANG_HIGH 0x0C
22#define RAW_ANG_LOW 0x0D
23#define STATUS_REG 0x0B
24#define AGC 0x1A
25#define MAGNITUDE_HIGH 0x1B
26#define MAGNITUDE_LOW 0x1C
27#define CONF_HIGH 0x07
28#define CONF_LOW 0x08
29
30/**************************************************************************/
35/**************************************************************************/
36class AS5601 {
37public:
38
39 /**************************************************************************/
43 /**************************************************************************/
44 void Begin() {
45 Wire.begin();
46 Wire.setClock(I2C_FREQ);
47 }
48
49 /**************************************************************************/
59 /**************************************************************************/
60 uint8_t get_pos(double *new_pos) {
61 uint16_t raw_angle;
62 uint8_t status_val;
63 float raw_pos = 0;
64 float delta_raw_pos = 0;
65 float real_pos = 0;
66
68 // Read Status Bits
70 // Check the status register
71 if ((status_val & 0x20) && !(status_val & 0x10)
72 && !(status_val & 0x08)) {
73 // Convert raw value to angle in deg
74 raw_pos = (float) raw_angle * 0.0879;
75 // Unwrap the angle
76 delta_raw_pos = _raw_prev_pos - raw_pos;
77 if (delta_raw_pos > 180)
78 _n++;
79 else if (delta_raw_pos < -180)
80 _n--;
81 // Calculate the real angle
82 real_pos = - ((raw_pos + 360 * _n) / _enc_ratio) - _angle_offset;
83 _raw_prev_pos = raw_pos;
84 }
85 *new_pos = (double)real_pos;
86 return status_val;
87 }
88
89 /**************************************************************************/
96 /**************************************************************************/
97 uint8_t get_agc() {
99 }
100
101 /**************************************************************************/
106 /**************************************************************************/
110
111 /**************************************************************************/
116 /**************************************************************************/
117 uint16_t get_conf() {
119 }
120
121 /**************************************************************************/
129 /**************************************************************************/
130 uint8_t set_zero() {
131 double current_pos;
132 uint8_t status_val = get_pos(&current_pos);
133 _angle_offset = current_pos;
134 return status_val;
135 }
136
137 /**************************************************************************/
141 /**************************************************************************/
142 void init_zero() {
143 _angle_offset = 0.0;
144 }
145
146 /**************************************************************************/
152 /**************************************************************************/
153 void set_gear_ratio(uint8_t enc_ratio) {
154 _enc_ratio = enc_ratio;
155 }
156
157private:
158 double _angle_offset = 0;
159 int32_t _n = 0;
160 float _raw_prev_pos = 0;
161 uint8_t _enc_ratio = 0;
162
163 uint8_t i2c_byte_transaction(uint8_t i2c_address, uint8_t i2c_register) {
164 Wire.beginTransmission(i2c_address);
165 Wire.write(i2c_register);
166 Wire.endTransmission();
167 Wire.requestFrom(i2c_address, (uint8_t) 1);
168 while (Wire.available() == 0)
169 ;
170 return Wire.read();
171 }
172
173 uint16_t i2c_word_transaction(uint8_t i2c_address,uint8_t i2c_register) {
174 uint8_t word_high = i2c_byte_transaction(i2c_address, i2c_register);
175 uint8_t word_low = i2c_byte_transaction(i2c_address, i2c_register + 1);
176 return ((word_high << 8) | word_low);
177 }
178};
179
180#endif /* AS5601_H_ */
181
#define RAW_ANG_HIGH
Definition as5601.h:21
#define STATUS_REG
Definition as5601.h:23
#define AS5601_ID
Definition as5601.h:20
#define MAGNITUDE_HIGH
Definition as5601.h:25
#define CONF_HIGH
Definition as5601.h:27
#define AGC
Definition as5601.h:24
#define I2C_FREQ
Definition as5601.h:18
Class that functions for interacting with AS5601 magnetic rotary position sensor.
Definition as5601.h:36
float _raw_prev_pos
Definition as5601.h:160
uint16_t i2c_word_transaction(uint8_t i2c_address, uint8_t i2c_register)
Definition as5601.h:173
void Begin()
Initialize the I2C bus.
Definition as5601.h:44
uint8_t _enc_ratio
Definition as5601.h:161
int32_t _n
Definition as5601.h:159
uint16_t get_magnitude()
Get the magnitude value of the internal CORDIC output.
Definition as5601.h:107
void init_zero()
Reset zero position set the offset to zero.
Definition as5601.h:142
double _angle_offset
Definition as5601.h:158
uint8_t get_pos(double *new_pos)
Calculate an unwrap the position.
Definition as5601.h:60
uint16_t get_conf()
Get the configuration register.
Definition as5601.h:117
uint8_t get_agc()
Calculate the automatic gain control (AGC)
Definition as5601.h:97
void set_gear_ratio(uint8_t enc_ratio)
Set the gear ratio between encoder and measure axis.
Definition as5601.h:153
uint8_t set_zero()
Set zero by setting offset angle.
Definition as5601.h:130
uint8_t i2c_byte_transaction(uint8_t i2c_address, uint8_t i2c_register)
Definition as5601.h:163