/ src / hal / components / ilowpass.comp
ilowpass.comp
 1  component ilowpass "Low-pass filter with integer inputs and outputs";
 2  description """While it may find other applications, this component was written
 3  to create smoother motion while jogging with an MPG.
 4  
 5  In a machine with high acceleration, a short jog can behave almost like a step
 6  function.  By putting the \\fBilowpass\\fR component between the MPG
 7  encoder \\fBcounts\\fR output and the axis \\fRjog-counts\\fR input,
 8  this can be smoothed.
 9  
10  Choose \\fBscale\\fR conservatively so that during a single session
11  there will never be more than about 2e9/\\fBscale\\fR pulses seen
12  on the MPG.  Choose \\fBgain\\fR according to the smoothing level
13  desired.  Divide the \\fRaxis.\\fIN\\fR.jog-scale\\fR values by
14  \\fBscale\\fR.""";
15  
16  pin in s32 in;
17  
18  pin out s32 out """\\fBout\\fR tracks \\fBin\\fR*\\fBscale\\fR through a low-pass
19  filter of \\fBgain\\fR per period.""";
20  
21  param rw float scale = 1024 """A scale factor applied to the output
22  value of the low-pass filter.""";
23  
24  param rw float gain = .5 """Together with the period, sets the rate at
25  which the output changes.  Useful range is between 0 and 1, with higher
26  values causing the input value to be tracked more quickly.  For
27  instance, a setting of 0.9 causes the output value to go 90% of the way
28  towards the input value in each period""";
29  
30  variable double value;
31  
32  function _ "Update the output based on the input and parameters";
33  
34  license "GPL";
35  author "Jeff Epler <jepler@unpythonic.net>";
36  ;;
37  #include <rtapi_math.h>
38  
39  FUNCTION(_) {
40      value += (in - value) * gain;
41      out = (int)(rtapi_s64)floor((value * scale) + 0.5);
42  }