Digital EMA (Low Pass) Filter For Encoder Count or RPM

11 years 9 months ago #29527393 by rknopf
Hello All,

I have just acquired a pair of SDC2130S motor controllers and am thus far extremely pleased with the closed loop speed control that is achievable. We bought these controllers in an effort to help with the cogging and overall friction of some rather cheap motors (that we are unfortunately constrained to for the time being). The issue however is that our incremental magnetic encoders output some significant noise (transmitted digitally of course). We don\'t have the ability to enable the on-board low-pass of the mag. enc. Can anyone outline a MicroBasic script which would allow me to apply a very simple exponential moving average to the encoder reading?

Psuedo Code for what I am suggesting:

while(1) {

c_old = 0;
alpha = 0.1;

c = getVal( enc_counts_raw );
c = alpha*(c-c_old) + c_old;

setVal( enc_counts, c );


Hopefully that is enough to get the gist of what I\'m asking. Any help is extremely appreciated, we will need to solve this within the next couple of days, one way or the other.

- Ryan

Please Log in or Create an account to join the conversation.

11 years 9 months ago #29527405 by roboteq
Microbasic only works with 32-bit integers and booleans.

To do a moving average, you need to buffer past count value and then average them.

Below is an untested example:

dim CountsBuffer[16] as integer
dim BufPointer as integer
dim CountsSum as integer
dim AverageCounts as integer
dim i as integer


CountsBuffer[BufPointer] = getvalue(_C, 1) \' read counter 1
BufPointer = (BufPointer + 1) and 15 \' Keep within 0-15

\' Compute average
CountsSum = 0

for i = 0 andwhile i < 15
CountsSum = CountSum + CountsBuffer

AverageCounts = CountsSum / 16

goto top

I am not sure how you intend the exponentiation to work but since only integers are possible, if you need to work with fractional values, then consider multiplying everything by 10, so that you are working with 10ths of counts.

Please Log in or Create an account to join the conversation.

Time to create page: 0.072 seconds