@@ -5,23 +5,32 @@ MagneticSensorI2CConfig_s AS5600_I2C = {
5
5
.chip_address = 0x36 ,
6
6
.bit_resolution = 12 ,
7
7
.angle_register = 0x0C ,
8
- .data_start_bit = 11
8
+ .msb_mask = 0x0F ,
9
+ .msb_shift = 8 ,
10
+ .lsb_mask = 0xFF ,
11
+ .lsb_shift = 0
9
12
};
10
13
11
14
/* * Typical configuration for the 12bit AMS AS5048 magnetic sensor over I2C interface */
12
15
MagneticSensorI2CConfig_s AS5048_I2C = {
13
16
.chip_address = 0x40 , // highly configurable. if A1 and A2 are held low, this is probable value
14
17
.bit_resolution = 14 ,
15
18
.angle_register = 0xFE ,
16
- .data_start_bit = 15
19
+ .msb_mask = 0xFF ,
20
+ .msb_shift = 6 ,
21
+ .lsb_mask = 0x3F ,
22
+ .lsb_shift = 0
17
23
};
18
24
19
25
/* * Typical configuration for the 12bit MT6701 magnetic sensor over I2C interface */
20
26
MagneticSensorI2CConfig_s MT6701_I2C = {
21
27
.chip_address = 0x06 ,
22
28
.bit_resolution = 14 ,
23
29
.angle_register = 0x03 ,
24
- .data_start_bit = 15
30
+ .msb_mask = 0xFF ,
31
+ .msb_shift = 6 ,
32
+ .lsb_mask = 0xFC ,
33
+ .lsb_shift = 2
25
34
};
26
35
27
36
@@ -30,57 +39,49 @@ MagneticSensorI2CConfig_s MT6701_I2C = {
30
39
// @param _bit_resolution bit resolution of the sensor
31
40
// @param _angle_register_msb angle read register
32
41
// @param _bits_used_msb number of used bits in msb
33
- MagneticSensorI2C::MagneticSensorI2C (uint8_t _chip_address, int _bit_resolution, uint8_t _angle_register_msb, int _bits_used_msb){
34
- // chip I2C address
35
- chip_address = _chip_address;
36
- // angle read register of the magnetic sensor
37
- angle_register_msb = _angle_register_msb;
38
- // register maximum value (counts per revolution)
42
+ MagneticSensorI2C::MagneticSensorI2C (uint8_t _chip_address, int _bit_resolution, uint8_t _angle_register_msb, int _bits_used_msb, bool lsb_right_aligned){
43
+ _conf.chip_address = _chip_address;
44
+ _conf.bit_resolution = _bit_resolution;
45
+ _conf.angle_register = _angle_register_msb;
46
+ _conf.msb_mask = (uint8_t )( (1 << _bits_used_msb) - 1 );
47
+
48
+ uint8_t lsb_used = _bit_resolution - _bits_used_msb; // used bits in LSB
49
+ _conf.lsb_mask = (uint8_t )( (1 << (lsb_used)) - 1 );
50
+ if (!lsb_right_aligned)
51
+ _conf.lsb_shift = 8 -lsb_used;
52
+ else
53
+ _conf.lsb_shift = 0 ;
54
+ _conf.msb_shift = lsb_used;
55
+
39
56
cpr = _powtwo (_bit_resolution);
40
57
41
- // depending on the sensor architecture there are different combinations of
42
- // LSB and MSB register used bits
43
- // AS5600 uses 0..7 LSB and 8..11 MSB
44
- // AS5048 uses 0..5 LSB and 6..13 MSB
45
- // MT6701 uses 0..5 LSB and 9..15 MSB
46
- // used bits in LSB
47
- lsb_used = _bit_resolution - _bits_used_msb;
48
- // extraction masks
49
- lsb_mask = (uint8_t )( (2 << lsb_used) - 1 );
50
- msb_mask = (uint8_t )( (2 << _bits_used_msb) - 1 );
51
58
wire = &Wire;
52
59
}
53
60
54
- MagneticSensorI2C::MagneticSensorI2C (MagneticSensorI2CConfig_s config){
55
- chip_address = config.chip_address ;
56
61
57
- // angle read register of the magnetic sensor
58
- angle_register_msb = config.angle_register ;
59
- // register maximum value (counts per revolution)
60
- cpr = _powtwo (config.bit_resolution );
61
62
62
- int bits_used_msb = config.data_start_bit - 7 ;
63
- lsb_used = config.bit_resolution - bits_used_msb;
64
- // extraction masks
65
- lsb_mask = (uint8_t )( (2 << lsb_used) - 1 );
66
- msb_mask = (uint8_t )( (2 << bits_used_msb) - 1 );
63
+ MagneticSensorI2C::MagneticSensorI2C (MagneticSensorI2CConfig_s config){
64
+ _conf = config;
65
+ cpr = _powtwo (config.bit_resolution );
67
66
wire = &Wire;
68
67
}
69
68
69
+
70
+
70
71
MagneticSensorI2C MagneticSensorI2C::AS5600 () {
71
72
return {AS5600_I2C};
72
73
}
73
74
74
- void MagneticSensorI2C::init (TwoWire* _wire){
75
-
76
- wire = _wire;
77
75
78
- // I2C communication begin
79
- wire->begin ();
80
76
77
+ void MagneticSensorI2C::init (TwoWire* _wire){
78
+ wire = _wire;
79
+ wire->begin (); // I2C communication begin
81
80
this ->Sensor ::init (); // call base class init
82
81
}
83
82
83
+
84
+
84
85
// Shaft angle calculation
85
86
// angle is in radians [rad]
86
87
float MagneticSensorI2C::getSensorAngle (){
@@ -90,39 +91,25 @@ float MagneticSensorI2C::getSensorAngle(){
90
91
91
92
92
93
93
- // function reading the raw counter of the magnetic sensor
94
- int MagneticSensorI2C::getRawCount (){
95
- return (int )MagneticSensorI2C::read (angle_register_msb);
96
- }
97
-
98
94
// I2C functions
99
95
/*
100
- * Read a register from the sensor
101
- * Takes the address of the register as a uint8_t
102
- * Returns the value of the register
96
+ * Read an angle from the angle register of the sensor
103
97
*/
104
- int MagneticSensorI2C::read ( uint8_t angle_reg_msb ) {
98
+ int MagneticSensorI2C::getRawCount ( ) {
105
99
// read the angle register first MSB then LSB
106
100
byte readArray[2 ];
107
101
uint16_t readValue = 0 ;
108
102
// notify the device that is aboout to be read
109
- wire->beginTransmission (chip_address);
110
- wire->write (angle_reg_msb );
103
+ wire->beginTransmission (_conf. chip_address );
104
+ wire->write (_conf. angle_register );
111
105
currWireError = wire->endTransmission (false );
112
-
113
106
// read the data msb and lsb
114
- wire->requestFrom (chip_address, ( uint8_t ) 2 );
107
+ wire->requestFrom (_conf. chip_address , 2 );
115
108
for (byte i=0 ; i < 2 ; i++) {
116
109
readArray[i] = wire->read ();
117
110
}
118
-
119
- // depending on the sensor architecture there are different combinations of
120
- // LSB and MSB register used bits
121
- // AS5600 uses 0..7 LSB and 8..11 MSB
122
- // AS5048 uses 0..5 LSB and 6..13 MSB
123
- // MT6701 uses 0..5 LSB and 6..13 MSB
124
- readValue = ( readArray[1 ] & lsb_mask );
125
- readValue += ( ( readArray[0 ] & msb_mask ) << lsb_used );
111
+ readValue = (readArray[0 ] & _conf.msb_mask ) << _conf.msb_shift ;
112
+ readValue |= (readArray[1 ] & _conf.lsb_mask ) >> _conf.lsb_shift ;
126
113
return readValue;
127
114
}
128
115
0 commit comments