File tree Expand file tree Collapse file tree
ESP32/AS5311-PWM-LIB-QUEUE Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -111,9 +111,24 @@ void AS5311::handleDataTask(void *parameter) {
111111
112112 // Calculate position
113113 if (localData.period !=0 and localData.duty_cycle <= localData.period ){
114- _position = (float ) localData.duty_cycle / (float ) localData.period ;
114+ // At the 0/1 change the duty cycle gets noisy, so we need to average it over multiple senses
115+ _position = calculateRollingAverage ((float ) localData.duty_cycle / (float ) localData.period );
115116 }
116117
117118 }
118119 }
119120}
121+
122+ float AS5311::calculateRollingAverage (float newVal) {
123+ static float values[3 ] = {0.0 , 0.0 , 0.0 };
124+ static int insertIndex = 0 ;
125+ static float sum = 0.0 ;
126+
127+ sum -= values[insertIndex];
128+ values[insertIndex] = newVal;
129+ sum += newVal;
130+
131+ insertIndex = (insertIndex + 1 ) % 3 ;
132+
133+ return sum / 3.0 ;
134+ }
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ class AS5311 {
1919 static volatile bool _time_2_print;
2020 static QueueHandle_t dataQueue; // Define the Queue handle globally.
2121 static void handleDataTask (void *parameter);
22-
22+ static float calculateRollingAverage ( float newVal);
2323 typedef struct {
2424 uint32_t period;
2525 uint32_t duty_cycle;
You can’t perform that action at this time.
0 commit comments