@@ -10,7 +10,7 @@ MagneticSensorPWM::MagneticSensorPWM(uint8_t _pinPWM, int _min_raw_count, int _m
10
10
11
11
pinPWM = _pinPWM;
12
12
13
- cpr = _max_raw_count - _min_raw_count;
13
+ cpr = _max_raw_count - _min_raw_count + 1 ;
14
14
min_raw_count = _min_raw_count;
15
15
max_raw_count = _max_raw_count;
16
16
@@ -22,6 +22,35 @@ MagneticSensorPWM::MagneticSensorPWM(uint8_t _pinPWM, int _min_raw_count, int _m
22
22
}
23
23
24
24
25
+ /* * MagneticSensorPWM(uint8_t _pinPWM, int freqHz, int _total_pwm_clocks, int _min_pwm_clocks, int _max_pwm_clocks)
26
+ *
27
+ * Constructor that computes the min and max raw counts based on the PWM frequency and the number of PWM clocks in one period
28
+ *
29
+ * @param _pinPWM the pin that is reading the pwm from magnetic sensor
30
+ * @param freqHz the frequency of the PWM signal, in Hz, e.g. 115, 230, 460 or 920 for the AS5600, depending on the PWM frequency setting
31
+ * @param _total_pwm_clocks the total number of PWM clocks in one period, e.g. 4351 for the AS5600
32
+ * @param _min_pwm_clocks the 0 value returned by the sensor, in PWM clocks, e.g. 128 for the AS5600
33
+ * @param _max_pwm_clocks the largest value returned by the sensor, in PWM clocks, e.g. 4223 for the AS5600
34
+ */
35
+ MagneticSensorPWM::MagneticSensorPWM (uint8_t _pinPWM, int freqHz, int _total_pwm_clocks, int _min_pwm_clocks, int _max_pwm_clocks){
36
+
37
+ pinPWM = _pinPWM;
38
+
39
+ min_raw_count = lroundf (1000000 .0f /freqHz/_total_pwm_clocks*_min_pwm_clocks);
40
+ max_raw_count = lroundf (1000000 .0f /freqHz/_total_pwm_clocks*_max_pwm_clocks);
41
+ cpr = max_raw_count - min_raw_count + 1 ;
42
+
43
+ // define if the sensor uses interrupts
44
+ is_interrupt_based = false ;
45
+
46
+ min_elapsed_time = 1 .0f /freqHz; // set the minimum time between two readings
47
+
48
+ // define as not set
49
+ last_call_us = _micros ();
50
+ }
51
+
52
+
53
+
25
54
void MagneticSensorPWM::init (){
26
55
27
56
// initial hardware
@@ -36,6 +65,7 @@ float MagneticSensorPWM::getSensorAngle(){
36
65
// raw data from sensor
37
66
raw_count = getRawCount ();
38
67
if (raw_count > max_raw_count) raw_count = max_raw_count;
68
+ if (raw_count < min_raw_count) raw_count = min_raw_count;
39
69
return ( (float ) (raw_count - min_raw_count) / (float )cpr) * _2PI;
40
70
}
41
71
0 commit comments