Skip to content

Commit 9b0c2b6

Browse files
committed
Adding noise reduction
1 parent 9052f7a commit 9b0c2b6

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

ESP32/AS5311-PWM-LIB-QUEUE/AS5311.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
}

ESP32/AS5311-PWM-LIB-QUEUE/AS5311.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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;

0 commit comments

Comments
 (0)