1 // encoder.cxx -- class to impliment an altitude encoder
3 // Written by Roy Vegard Ovesen, started September 2004.
5 // Copyright (C) 2004 Roy Vegard Ovesen - rvovesen@tiscali.no
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "encoder.hxx"
28 // Altitude based on pressure difference from sea level.
29 // pressure difference inHG, altitude ft
30 static double altitude_data[][2] = {
54 { 29.62, 100000.00 }, // just to fill it in
58 int round (double value, int nearest=1)
60 return ((int) (value/nearest + 0.5)) * nearest;
63 Encoder::Encoder(SGPropertyNode *node)
65 altitudeTable(new SGInterpTable),
68 staticPort("/systems/static")
71 for ( i = 0; altitude_data[i][0] != -1; i++ )
72 altitudeTable->addEntry(altitude_data[i][0], altitude_data[i][1]);
74 for ( i = 0; i < node->nChildren(); ++i ) {
75 SGPropertyNode *child = node->getChild(i);
76 string cname = child->getName();
77 string cval = child->getStringValue();
78 if ( cname == "name" ) {
80 } else if ( cname == "number" ) {
81 num = child->getIntValue();
82 } else if ( cname == "static-port" ) {
85 SG_LOG( SG_INSTR, SG_WARN,
86 "Error in encoder config logic" );
87 if ( name.length() ) {
88 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
104 branch = "/instrumentation/" + name;
105 staticPort += "/pressure-inhg";
107 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
109 staticPressureNode = fgGetNode(staticPort.c_str(), true);
110 busPowerNode = fgGetNode("/systems/electrical/outputs/encoder", true);
111 serviceableNode = node->getChild("serviceable", 0, true);
113 pressureAltitudeNode = node->getChild("pressure-alt-ft", 0, true);
114 modeCAltitudeNode = node->getChild("mode-c-alt-ft", 0, true);
118 void Encoder::update(double dt)
120 if (serviceableNode->getBoolValue())
122 double staticPressure = staticPressureNode->getDoubleValue();
123 double pressureAltitude =
124 altitudeTable->interpolate(29.92 - staticPressure);
125 int pressureAlt = round(pressureAltitude, 10);
126 int modeCAltitude = round(pressureAltitude, 100);
128 pressureAltitudeNode->setIntValue(pressureAlt);
129 modeCAltitudeNode->setIntValue(modeCAltitude);