1 // inputvalue.hxx - provide input to autopilot components
3 // Written by Torsten Dreyer
4 // Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "inputvalue.hxx"
22 #include <Main/fg_props.hxx>
23 using namespace FGXMLAutopilot;
25 PeriodicalValue::PeriodicalValue( SGPropertyNode_ptr root )
27 SGPropertyNode_ptr minNode = root->getChild( "min" );
28 SGPropertyNode_ptr maxNode = root->getChild( "max" );
29 if( minNode == NULL || maxNode == NULL ) {
30 SG_LOG(SG_AUTOPILOT, SG_ALERT, "periodical defined, but no <min> and/or <max> tag. Period ignored." );
32 minPeriod = new InputValue( minNode );
33 maxPeriod = new InputValue( maxNode );
37 double PeriodicalValue::normalize( double value ) const
39 return SGMiscd::normalizePeriodic( minPeriod->get_value(), maxPeriod->get_value(), value );
42 double PeriodicalValue::normalizeSymmetric( double value ) const
44 value = SGMiscd::normalizePeriodic( minPeriod->get_value(), maxPeriod->get_value(), value );
45 double width_2 = (maxPeriod->get_value() - minPeriod->get_value())/2;
46 return value > width_2 ? width_2 - value : value;
49 InputValue::InputValue( SGPropertyNode_ptr node, double value, double offset, double scale) :
53 parse( node, value, offset, scale );
57 void InputValue::parse( SGPropertyNode_ptr node, double aValue, double aOffset, double aScale )
72 if( (n = node->getChild("condition")) != NULL ) {
73 _condition = sgReadCondition(fgGetNode("/"), n);
76 if( (n = node->getChild( "scale" )) != NULL ) {
77 _scale = new InputValue( n, aScale );
80 if( (n = node->getChild( "offset" )) != NULL ) {
81 _offset = new InputValue( n, aOffset );
84 if( (n = node->getChild( "max" )) != NULL ) {
85 _max = new InputValue( n );
88 if( (n = node->getChild( "min" )) != NULL ) {
89 _min = new InputValue( n );
92 if( (n = node->getChild( "abs" )) != NULL ) {
93 _abs = n->getBoolValue();
96 if( (n = node->getChild( "period" )) != NULL ) {
97 _periodical = new PeriodicalValue( n );
100 SGPropertyNode *valueNode = node->getChild( "value" );
101 if ( valueNode != NULL ) {
102 _value = valueNode->getDoubleValue();
105 if ((n = node->getChild("expression")) != NULL) {
106 _expression = SGReadDoubleExpression(fgGetNode("/"), n->getChild(0));
110 n = node->getChild( "property" );
111 // if no <property> element, check for <prop> element for backwards
114 n = node->getChild( "prop" );
117 _property = fgGetNode( n->getStringValue(), true );
118 if ( valueNode != NULL ) {
119 // initialize property with given value
120 // if both <prop> and <value> exist
121 double s = get_scale();
123 _property->setDoubleValue( (_value - get_offset())/s );
125 _property->setDoubleValue( 0 ); // if scale is zero, value*scale is zero
129 } // of have a <property> or <prop>
132 if (valueNode == NULL) {
133 // no <value>, <prop> or <expression> element, use text node
134 const char * textnode = node->getStringValue();
136 // try to convert to a double value. If the textnode does not start with a number
137 // endp will point to the beginning of the string. We assume this should be
139 _value = strtod( textnode, &endp );
140 if( endp == textnode ) {
141 _property = fgGetNode( textnode, true );
146 void InputValue::set_value( double aValue )
151 double s = get_scale();
153 _property->setDoubleValue( (aValue - get_offset())/s );
155 _property->setDoubleValue( 0 ); // if scale is zero, value*scale is zero
158 double InputValue::get_value() const
160 double value = _value;
163 // compute the expression value
164 value = _expression->getValue(NULL);
165 } else if( _property != NULL ) {
166 value = _property->getDoubleValue();
170 value *= _scale->get_value();
173 value += _offset->get_value();
176 double m = _min->get_value();
182 double m = _max->get_value();
188 value = _periodical->normalize( value );
191 return _abs ? fabs(value) : value;