abs.comp
1 // This is a component for EMC2 HAL 2 // Copyright 2006 John Kasunich <jmkasunich 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 abs "Compute the absolute value and sign of the input signal"; 17 18 pin in float in "Analog input value" ; 19 pin out float out "Analog output value, always positive"; 20 pin out bit sign "Sign of input, false for positive, true for negative" ; 21 pin out bit is_positive "TRUE if input is positive, FALSE if input is 0 or negative"; 22 pin out bit is_negative "TRUE if input is negative, FALSE if input is 0 or positive"; 23 24 function _; 25 license "GPL"; 26 ;; 27 FUNCTION(_) { 28 double tmp = in; 29 if ( tmp >= 0.0 ) { 30 sign = 0; 31 out = tmp; 32 } else { 33 sign = 1; 34 out = -tmp; 35 } 36 37 if (tmp > 0.000001) is_positive = 1; 38 else is_positive = 0; 39 40 if (tmp < -0.000001) is_negative = 1; 41 else is_negative = 0; 42 }