X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FNetwork%2FATC-Inputs.cxx;h=369a2d3bc15d0b3e990a10b1fc6199d7bcf7787a;hb=80bada55cd20de9e8bb8fd2557edc08afd534853;hp=3209827e0dc488760eba0df36b49c45d9a4416e9;hpb=61ed285d06ace1840aac7d100bac85d762866fe4;p=flightgear.git diff --git a/src/Network/ATC-Inputs.cxx b/src/Network/ATC-Inputs.cxx index 3209827e0..369a2d3bc 100644 --- a/src/Network/ATC-Inputs.cxx +++ b/src/Network/ATC-Inputs.cxx @@ -16,7 +16,7 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id$ @@ -31,6 +31,8 @@ # include # include # include +# include +# include #endif #include @@ -222,7 +224,8 @@ bool FGATCInput::open() { ///////////////////////////////////////////////////////////////////// // scale a number between min and max (with center defined) to a scale -// from -1.0 to 1.0 +// from -1.0 to 1.0. The deadband value is symmetric, so specifying +// '1' will give you a deadband of +/-1 static double scale( int center, int deadband, int min, int max, int value ) { // cout << center << " " << min << " " << max << " " << value << " "; double result; @@ -231,9 +234,11 @@ static double scale( int center, int deadband, int min, int max, int value ) { if ( value <= (center - deadband) ) { range = (center - deadband) - min; result = (value - (center - deadband)) / range; - } else { + } else if ( value >= (center + deadband) ) { range = max - (center + deadband); result = (value - (center + deadband)) / range; + } else { + result = 0.0; } if ( result < -1.0 ) result = -1.0; @@ -263,6 +268,18 @@ static double scale( int min, int max, int value ) { } +static double clamp( double min, double max, double value ) { + double result = value; + + if ( result < min ) result = min; + if ( result > max ) result = max; + + // cout << result << endl; + + return result; +} + + static int tony_magic( int raw, int obs[3] ) { int result = 0; @@ -397,11 +414,12 @@ bool FGATCInput::do_analog_in() { string name = ""; string type = ""; string subtype = ""; - vector output_nodes; output_nodes.clear(); + vector output_nodes; int center = -1; int min = 0; int max = 1023; int deadband = 0; + float offset = 0.0; float factor = 1.0; if ( cname == "channel" ) { SGPropertyNode *prop; @@ -440,6 +458,10 @@ bool FGATCInput::do_analog_in() { if ( prop != NULL ) { deadband = prop->getIntValue(); } + prop = child->getChild( "offset" ); + if ( prop != NULL ) { + offset = prop->getFloatValue(); + } prop = child->getChild( "factor" ); if ( prop != NULL ) { factor = prop->getFloatValue(); @@ -467,6 +489,14 @@ bool FGATCInput::do_analog_in() { scaled_value = scale( min, max, raw_value ); } scaled_value *= factor; + scaled_value += offset; + + // final sanity clamp + if ( center >= 0 ) { + scaled_value = clamp( -1.0, 1.0, scaled_value ); + } else { + scaled_value = clamp( 0.0, 1.0, scaled_value ); + } // update the property tree values for ( j = 0; j < (int)output_nodes.size(); ++j ) { @@ -483,6 +513,14 @@ bool FGATCInput::do_analog_in() { scaled_value = scale( min, max, raw_value ); } scaled_value *= factor; + scaled_value += offset; + + // final sanity clamp + if ( center >= 0 ) { + scaled_value = clamp( -1.0, 1.0, scaled_value ); + } else { + scaled_value = clamp( 0.0, 1.0, scaled_value ); + } // update the property tree values for ( j = 0; j < (int)output_nodes.size(); ++j ) { @@ -622,7 +660,7 @@ bool FGATCInput::do_switches() { string cname = child->getName(); string name = ""; string type = ""; - vector output_nodes; output_nodes.clear(); + vector output_nodes; int row = -1; int col = -1; float factor = 1.0; @@ -825,7 +863,7 @@ bool FGATCInput::do_radio_switches() { if ( cname == "switch" ) { string name = ""; string type = ""; - vector output_nodes; output_nodes.clear(); + vector output_nodes; int byte_num = -1; int right_shift = 0; int mask = 0xff;