/ src / hal / components / invert.comp
invert.comp
 1  //   This is a component for EMC2 HAL
 2  //   Copyright 2008 Stephen Wille Padnos <swpadnos at sourceforge dot net>
 3  //
 4  //   This program is free software; you can redistribute it and/or
 5  //   modify it under the terms of version 2 of the GNU General
 6  //   Public License as published by the Free Software Foundation.
 7  //
 8  //   This program is distributed in the hope that it will be useful,
 9  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  //   GNU General Public License for more details.
12  //
13  //   You should have received a copy of the GNU General Public License
14  //   along with this program; if not, write to the Free Software
15  //   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  component invert """Compute the inverse of the input signal
17  The output will be the mathematical inverse of the input, ie \\fBout\\fR = 1/\\fBin\\fR.
18  The parameter \\fBdeadband\\fR can be used to control how close to 0 the denominator can be
19  before the output is clamped to 0.  \\fBdeadband\\fR must be at least 1e-8, and must be positive.""";
20  
21  pin in float in "Analog input value" ;
22  pin out float out "Analog output value";
23  param rw float deadband "The \\fBout\\fR will be zero if \\fBin\\fR is between -\\fBdeadband\\fR and +\\fBdeadband\\fR" ;
24  
25  function _;
26  license "GPL";
27  ;;
28  
29  #include <rtapi_math.h>
30  
31  FUNCTION(_) {
32      double tmp = in;
33      if (deadband < 1e-12) deadband = 1e-12;
34      if ( tmp > -deadband && tmp <0)
35  	out = -1/deadband;
36      else if (tmp >= 0 && tmp <deadband)
37  	out = 1/deadband;
38      else
39  	out = 1/tmp;
40  }