]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/transponder.cxx
Bug 1122, transponder ident.
[flightgear.git] / src / Instrumentation / transponder.cxx
1 // transponder.cxx -- class to impliment a transponder
2 //
3 // Written by Roy Vegard Ovesen, started September 2004.
4 //
5 // Copyright (C) 2004  Roy Vegard Ovesen - rvovesen@tiscali.no
6 // Copyright (C) 2013  Clement de l'Hamaide - clemaez@hotmail.fr
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // Example invocation, in the instrumentation.xml file:
23 //      <transponder>
24 //        <name>encoder</name>
25 //        <number>0</number>
26 //        <mode>0</mode>  // Mode A = 0, Mode C = 1, Mode S = 2
27 //      </altimeter>
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include "transponder.hxx"
34
35 #include <simgear/compiler.h>
36 #include <simgear/sg_inlines.h>
37 #include <simgear/debug/logstream.hxx>
38
39 #include <Main/fg_props.hxx>
40
41 #include <cstdio>
42
43 using std::string;
44
45 const double IDENT_TIMEOUT = 18.0; // 18 seconds
46 const int INVALID_ALTITUDE = -9999;
47 const int INVALID_ID = -9999;
48
49 Transponder::Transponder(SGPropertyNode *node)
50     :
51     _name(node->getStringValue("name", "transponder")),
52     _num(node->getIntValue("number", 0)),
53     _mode((Mode) node->getIntValue("mode", 1)),
54     _listener_active(0)
55 {
56     _requiredBusVolts = node->getDoubleValue("bus-volts", 8.0);
57     _altitudeSourcePath = node->getStringValue("encoder-path", "/instrumentation/altimeter");
58     _kt70Compat = node->getBoolValue("kt70-compatability", false);
59 }
60
61
62 Transponder::~Transponder()
63 {
64 }
65
66
67 void Transponder::init()
68 {
69     SGPropertyNode *node = fgGetNode("/instrumentation/" + _name, _num, true );
70
71     // Inputs
72     _busPower_node = fgGetNode("/systems/electrical/outputs/transponder", true);
73     _pressureAltitude_node = fgGetNode(_altitudeSourcePath, true);
74
75     SGPropertyNode *in_node = node->getChild("inputs", 0, true);
76     for (int i=0; i<4;++i) {
77         _digit_node[i] = in_node->getChild("digit", i, true);
78         _digit_node[i]->addChangeListener(this);
79     }
80    
81     _knob_node = in_node->getChild("knob-mode", 0, true);
82     if (!_knob_node->hasValue()) {
83         _knob = KNOB_ON;
84         // default to, if aircraft wants to start dark, it can do this
85         // in its -set.xml
86         _knob_node->setIntValue(_knob);
87     } else {
88         _knob = static_cast<KnobPosition>(_knob_node->getIntValue());
89     }
90     
91     _knob_node->addChangeListener(this);
92     
93     _mode_node = in_node->getChild("mode", 0, true);
94     _mode_node->setIntValue(_mode);
95     _mode_node->addChangeListener(this);
96     
97     _identBtn_node = in_node->getChild("ident-btn", 0, true);
98     _identBtn_node->setBoolValue(false);
99     _identBtn_node->addChangeListener(this);
100     
101     _serviceable_node = node->getChild("serviceable", 0, true);
102     _serviceable_node->setBoolValue(true);
103     
104     _idCode_node = node->getChild("id-code", 0, true);
105     _idCode_node->addChangeListener(this);
106     // set default, but don't overwrite value from preferences.xml or -set.xml
107     if (!_idCode_node->hasValue()) { 
108         _idCode_node->setIntValue(1200);
109     }
110     
111     // Outputs
112     _altitude_node = node->getChild("altitude", 0, true);
113     _altitudeValid_node = node->getChild("altitude-valid", 0, true);
114     _ident_node = node->getChild("ident", 0, true);
115     _transmittedId_node = node->getChild("transmitted-id", 0, true);
116     
117     if (_kt70Compat) {
118         // alias the properties through
119         SGPropertyNode_ptr output = node->getChild("outputs", 0, true);
120         output->getChild("flight-level", 0, true)->alias(_altitude_node);
121         output->getChild("id-code", 0, true)->alias(_idCode_node);
122         in_node->getChild("func-knob", 0, true)->alias(_knob_node);
123     }
124 }
125
126 void Transponder::bind()
127 {
128     if (_kt70Compat) {
129         SGPropertyNode *node = fgGetNode("/instrumentation/" + _name, _num, true );
130         _tiedProperties.setRoot(node);
131         
132         _tiedProperties.Tie("annunciators/fl", this,
133                             &Transponder::getFLAnnunciator );
134         _tiedProperties.Tie("annunciators/alt", this,
135                             &Transponder::getAltAnnunciator );
136         _tiedProperties.Tie("annunciators/gnd", this,
137                             &Transponder::getGroundAnnuciator );
138         _tiedProperties.Tie("annunciators/on", this,
139                             &Transponder::getOnAnnunciator );
140         _tiedProperties.Tie("annunciators/sby", this,
141                             &Transponder::getStandbyAnnunciator );
142         _tiedProperties.Tie("annunciators/reply", this,
143                             &Transponder::getReplyAnnunciator );
144     } // of kt70 backwards compatability
145 }
146
147 void Transponder::unbind()
148 {
149     _tiedProperties.Untie();
150 }
151
152
153 void Transponder::update(double dt)
154 {
155     if (has_power() && _serviceable_node->getBoolValue())
156     {
157         // Mode C & S send also altitude
158         Mode effectiveMode = (_knob == KNOB_ALT) ? _mode : MODE_A;
159         SGPropertyNode* altitudeSource = NULL;
160         
161         switch (effectiveMode) {
162         case MODE_C:
163             altitudeSource = _pressureAltitude_node->getChild("mode-c-alt-ft");
164             break;
165         case MODE_S:
166             altitudeSource = _pressureAltitude_node->getChild("mode-s-alt-ft");
167             break;
168         default:
169             break;
170         }
171         
172         int alt = INVALID_ALTITUDE;
173         if (effectiveMode != MODE_A) {
174             if (altitudeSource) {
175                 alt = altitudeSource->getIntValue();
176             } else {
177                 // warn if altitude input is misconfigured
178                 SG_LOG(SG_INSTR, SG_INFO, "transponder altitude input for mode " << _mode << " is missing");
179             }
180         }
181         
182         _altitude_node->setIntValue(alt);
183         _altitudeValid_node->setBoolValue(alt != INVALID_ALTITUDE);
184
185         if ( _identMode ) {
186             _identTime += dt;
187             if ( _identTime > IDENT_TIMEOUT ) {
188                 // reset ident mode
189                 _ident_node->setBoolValue(false);
190                 _identMode = false;
191             }
192         }
193         
194         if (_knob >= KNOB_ON) {
195             _transmittedId_node->setIntValue(_idCode_node->getIntValue());
196         } else {
197             _transmittedId_node->setIntValue(INVALID_ID);
198         }
199     }
200     else
201     {
202       _altitude_node->setIntValue(INVALID_ALTITUDE);
203       _altitudeValid_node->setBoolValue(false);
204       _ident_node->setBoolValue(false);
205       _transmittedId_node->setIntValue(INVALID_ID);
206     }
207 }
208
209 static int powersOf10[4] = {1, 10, 100, 1000};
210
211 static int extractCodeDigit(int code, int index)
212 {
213     return (code / powersOf10[index]) % 10;
214 }
215
216 static int modifyCodeDigit(int code, int index, int digitValue)
217 {
218     assert(digitValue >= 0 && digitValue < 8);
219     int p = powersOf10[index];
220     int codeWithoutDigit = code - (extractCodeDigit(code, index) * p);
221     return codeWithoutDigit + (digitValue * p);
222 }
223
224 void Transponder::valueChanged(SGPropertyNode *prop)
225 {
226     // Ident button pressed
227     if (prop == _identBtn_node) {
228         if (prop->getBoolValue()) {
229             _identTime = 0.0;
230             _ident_node->setBoolValue(true);
231             _identMode = true;
232         } else {
233             // don't cancel state on release
234         }
235         return;
236     }
237     
238     if (prop == _mode_node) {
239         _mode = static_cast<Mode>(prop->getIntValue());
240         return;
241     }
242     
243     if (prop == _knob_node) {
244         _knob = static_cast<KnobPosition>(prop->getIntValue());
245         return;
246     }
247     
248     if (_listener_active)
249         return;
250
251     _listener_active++;
252
253     if (prop == _idCode_node) {
254         // keep the digits in-sync
255         for (int i=0; i<4; ++i) {
256             _digit_node[i]->setIntValue(extractCodeDigit(prop->getIntValue(), i));
257         }
258     } else {
259         // digit node
260         int index = prop->getIndex();
261         int digitValue = prop->getIntValue();
262         SG_CLAMP_RANGE<int>(digitValue, 0, 7);
263         _idCode_node->setIntValue(modifyCodeDigit(_idCode_node->getIntValue(), index, digitValue));
264         prop->setIntValue(digitValue);
265     }
266     
267     _listener_active--;
268 }
269
270 bool Transponder::has_power() const
271 {
272     return (_knob_node->getIntValue() > KNOB_STANDBY) && (_busPower_node->getDoubleValue() > _requiredBusVolts);
273 }
274
275 bool Transponder::getFLAnnunciator() const
276 {
277     return (_knob == KNOB_ALT) || (_knob == KNOB_GROUND) || (_knob == KNOB_TEST);
278 }
279
280 bool Transponder::getAltAnnunciator() const
281 {
282     return (_knob == KNOB_ALT) || (_knob == KNOB_TEST);
283 }
284
285 bool Transponder::getGroundAnnuciator() const
286 {
287     return (_knob == KNOB_GROUND) || (_knob == KNOB_TEST);
288 }
289
290 bool Transponder::getOnAnnunciator() const
291 {
292     return (_knob == KNOB_ON) || (_knob == KNOB_TEST);
293 }
294
295 bool Transponder::getStandbyAnnunciator() const
296 {
297     return (_knob == KNOB_STANDBY) || (_knob == KNOB_TEST);
298 }
299
300 bool Transponder::getReplyAnnunciator() const
301 {
302     return _identMode || (_knob == KNOB_TEST);
303 }
304