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 )
39 return SGMiscd::normalizePeriodic( minPeriod->get_value(), maxPeriod->get_value(), value );
42 InputValue::InputValue( SGPropertyNode_ptr node, double value, double offset, double scale) :
46 parse( node, value, offset, scale );
50 void InputValue::parse( SGPropertyNode_ptr node, double aValue, double aOffset, double aScale )
65 if( (n = node->getChild("condition")) != NULL ) {
66 _condition = sgReadCondition(fgGetNode("/"), n);
69 if( (n = node->getChild( "scale" )) != NULL ) {
70 _scale = new InputValue( n, aScale );
73 if( (n = node->getChild( "offset" )) != NULL ) {
74 _offset = new InputValue( n, aOffset );
77 if( (n = node->getChild( "max" )) != NULL ) {
78 _max = new InputValue( n );
81 if( (n = node->getChild( "min" )) != NULL ) {
82 _min = new InputValue( n );
85 if( (n = node->getChild( "abs" )) != NULL ) {
86 _abs = n->getBoolValue();
89 if( (n = node->getChild( "period" )) != NULL ) {
90 _periodical = new PeriodicalValue( n );
93 SGPropertyNode *valueNode = node->getChild( "value" );
94 if ( valueNode != NULL ) {
95 _value = valueNode->getDoubleValue();
98 if ((n = node->getChild("expression")) != NULL) {
99 _expression = SGReadDoubleExpression(fgGetNode("/"), n->getChild(0));
103 n = node->getChild( "property" );
104 // if no <property> element, check for <prop> element for backwards
107 n = node->getChild( "prop" );
110 _property = fgGetNode( n->getStringValue(), true );
111 if ( valueNode != NULL ) {
112 // initialize property with given value
113 // if both <prop> and <value> exist
114 double s = get_scale();
116 _property->setDoubleValue( (_value - get_offset())/s );
118 _property->setDoubleValue( 0 ); // if scale is zero, value*scale is zero
122 } // of have a <property> or <prop>
125 if (valueNode == NULL) {
126 // no <value>, <prop> or <expression> element, use text node
127 const char * textnode = node->getStringValue();
129 // try to convert to a double value. If the textnode does not start with a number
130 // endp will point to the beginning of the string. We assume this should be
132 _value = strtod( textnode, &endp );
133 if( endp == textnode ) {
134 _property = fgGetNode( textnode, true );
139 void InputValue::set_value( double aValue )
144 double s = get_scale();
146 _property->setDoubleValue( (aValue - get_offset())/s );
148 _property->setDoubleValue( 0 ); // if scale is zero, value*scale is zero
151 double InputValue::get_value() const
153 double value = _value;
156 // compute the expression value
157 value = _expression->getValue(NULL);
158 } else if( _property != NULL ) {
159 value = _property->getDoubleValue();
163 value *= _scale->get_value();
166 value += _offset->get_value();
169 double m = _min->get_value();
175 double m = _max->get_value();
181 value = _periodical->normalize( value );
184 return _abs ? fabs(value) : value;