]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/autobrake.cxx
Merge branch 'jmt/gpswidget'
[flightgear.git] / src / Autopilot / autobrake.cxx
1 // autobrake.cxx - generic, configurable autobrake system
2 //
3 // Written by James Turner, started September 2009.
4 //
5 // Copyright (C) 2009  Curtis L. Olson
6 //
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.
11 //
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.
16 //
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #include "autobrake.hxx"
24
25 #include <Main/fg_props.hxx>
26
27 FGAutoBrake::FGAutoBrake() :
28   _lastGroundspeedKts(0.0),
29   _step(0),
30   _rto(false),
31   _armed(false),
32   _rtoArmed(false),
33   _engaged(false),
34   _targetDecel(0.0),
35   _fcsBrakeControl(0.0),
36   _leftBrakeOutput(0.0),
37   _rightBrakeOutput(0.0)
38 {
39   // default config, close to Boeing 747-400 / 777 values
40   _configNumSteps = 4;
41   _configRTOStep = -1;
42   _configDisengageStep = 0;
43   _configMaxDecel = 11; // ft-sec-sec
44   _configRTODecel = 11; // ft-sec-sec
45   _configRTOSpeed = 85; // kts
46 }
47
48 FGAutoBrake::~FGAutoBrake()
49 {
50 }
51
52 void FGAutoBrake::init()
53 {
54   _root = fgGetNode("/autopilot/autobrake", true);
55   
56   _root->tie("armed", SGRawValueMethods<FGAutoBrake, bool>
57     (*this, &FGAutoBrake::getArmed, &FGAutoBrake::setArmed));
58   
59   _root->tie("step", SGRawValueMethods<FGAutoBrake, int>
60     (*this, &FGAutoBrake::getStep, &FGAutoBrake::setStep));
61     
62   _root->tie("rto", SGRawValueMethods<FGAutoBrake, bool>
63     (*this, &FGAutoBrake::getRTO, &FGAutoBrake::setRTO));
64       
65   _engineControlsNode = fgGetNode("/controls/engines");
66   
67   // brake position nodes
68   _brakeInputs[0] = fgGetNode("/controls/gear/brake-left");
69   _brakeInputs[1] = fgGetNode("/controls/gear/brake-right");
70   _brakeInputs[2] = fgGetNode("/controls/gear/copilot-brake-left");
71   _brakeInputs[3] = fgGetNode("/controls/gear/copilot-brake-right");
72   
73     // config
74   SGPropertyNode_ptr config = _root->getChild("config", 0, true);
75   config->tie("num-steps", SGRawValuePointer<int>(&_configNumSteps));
76   config->tie("rto-step", SGRawValuePointer<int>(&_configRTOStep));
77   config->tie("disengage-step", SGRawValuePointer<int>(&_configDisengageStep));
78   config->tie("rto-decel-fts-sec", SGRawValuePointer<double>(&_configRTODecel));
79   config->tie("max-decel-fts-sec", SGRawValuePointer<double>(&_configMaxDecel));
80   config->tie("rto-engage-kts", SGRawValuePointer<double>(&_configRTOSpeed));
81   
82   
83   // outputs
84   _root->tie("target-decel-fts-sec", 
85     SGRawValueMethods<FGAutoBrake,double>(*this, &FGAutoBrake::getTargetDecel));
86   _root->tie("actual-decel-fts-sec", 
87     SGRawValueMethods<FGAutoBrake,double>(*this, &FGAutoBrake::getActualDecel));
88   _root->tie("fcs-brake", SGRawValuePointer<double>(&_fcsBrakeControl));
89   _root->tie("brake-left-output", SGRawValuePointer<double>(&_leftBrakeOutput));
90   _root->tie("brake-right-output", SGRawValuePointer<double>(&_rightBrakeOutput));
91   
92   _root->tie("engaged", SGRawValueMethods<FGAutoBrake, bool>(*this, &FGAutoBrake::getEngaged)); 
93 }
94
95 void FGAutoBrake::postinit()
96 {  
97   _weightOnWheelsNode = fgGetNode("/gear/gear/wow");
98   _groundspeedNode = fgGetNode("/velocities/groundspeed-kt");
99   if (!_weightOnWheelsNode) {
100     return; // don't crash if we're using an acft (UFO, ATC, with no WoW flag)
101   }
102   
103   _lastWoW = _weightOnWheelsNode->getBoolValue();
104 }
105
106
107 void FGAutoBrake::bind()
108 {
109 }
110
111 void FGAutoBrake::unbind()
112 {
113 }
114
115 void FGAutoBrake::update(double dt)
116 {
117   _leftBrakeInput = SGMiscd::max(_brakeInputs[0]->getDoubleValue(), 
118       _brakeInputs[2]->getDoubleValue());
119   _rightBrakeInput = SGMiscd::max(_brakeInputs[1]->getDoubleValue(), 
120       _brakeInputs[3]->getDoubleValue());
121
122   // various FDMs don't supply sufficent information for us to work,
123   // so just be passive in those cases.
124   if (!_weightOnWheelsNode || !_groundspeedNode || !_engineControlsNode) {
125     // pass the values straight through
126     _leftBrakeOutput = _leftBrakeInput; 
127     _rightBrakeOutput = _rightBrakeInput; 
128     return;
129   }
130
131   if (dt <= 0.0) {
132     return; // paused
133   }
134   
135   double gs = _groundspeedNode->getDoubleValue();
136   _actualDecel = (_lastGroundspeedKts - gs) / dt;
137   _lastGroundspeedKts = gs;
138   
139   if (!_armed || (_step == 0)) {
140     // even if we're off or disarmed, the physical valve system must pass
141     // pilot control values through.
142     _leftBrakeOutput = _leftBrakeInput; 
143     _rightBrakeOutput = _rightBrakeInput; 
144     return;
145   }
146   
147   if (_engaged) {
148     updateEngaged(dt);
149   } else {
150     bool e = shouldEngage();
151     if (e) {
152       engage();
153       updateEngaged(dt);
154     }
155   }
156   
157   // sum pilot inpiuts with auto-brake FCS input
158   _leftBrakeOutput = SGMiscd::max(_leftBrakeInput, _fcsBrakeControl);
159   _rightBrakeOutput = SGMiscd::max(_rightBrakeInput, _fcsBrakeControl);
160
161   _lastWoW = _weightOnWheelsNode->getBoolValue();
162 }
163
164 void FGAutoBrake::engage()
165 {
166   SG_LOG(SG_AUTOPILOT, SG_INFO, "FGAutoBrake: engaging");
167   _engaged = true;
168 }
169
170 void FGAutoBrake::updateEngaged(double dt)
171 {
172   // check for pilot braking input cancelling engage
173   const double BRAKE_INPUT_THRESHOLD = 0.02; // 2% application triggers
174   if ((_leftBrakeInput > BRAKE_INPUT_THRESHOLD) || (_rightBrakeInput > BRAKE_INPUT_THRESHOLD)) {
175     SG_LOG(SG_AUTOPILOT, SG_INFO, "auto-brake, detected pilot brake input, disengaing");
176     disengage();
177     return;
178   }
179
180   if (!throttlesAtIdle()) {
181     SG_LOG(SG_AUTOPILOT, SG_INFO, "auto-brake, throttles not at idle, disengaing");
182     disengage();
183     return;
184   }
185
186   // update the target deceleration; note step can be changed while engaged
187   if (_rto) {
188     _targetDecel = _configRTODecel;
189   } else {
190     double f = (double) _step / _configNumSteps;
191     _targetDecel = _configMaxDecel * f;
192   }
193 }
194
195 bool FGAutoBrake::shouldEngage()
196 {
197   if (_rto) {
198     return shouldEngageRTO();
199   }
200
201   if (_lastWoW || !_weightOnWheelsNode->getBoolValue()) {
202     return false;
203   }
204
205
206   if (!throttlesAtIdle()) {
207     return false;
208   }
209   
210   SG_LOG(SG_AUTOPILOT, SG_INFO, "Autobrake, should enage");
211   return true;
212 }
213
214 bool FGAutoBrake::shouldEngageRTO()
215 {
216   double speed = _groundspeedNode->getDoubleValue();
217
218   if (_rtoArmed) {
219     if (speed < _configRTOSpeed) {
220       SG_LOG(SG_AUTOPILOT, SG_INFO, "FGAutoBrake: RTO armed, but speed is now below arming speed");
221       _rtoArmed = false;
222       return false;
223     }
224     
225     if (!_weightOnWheelsNode->getBoolValue()) {
226       if (airborne()) {
227         SG_LOG(SG_AUTOPILOT, SG_INFO, "FGAutoBrake: RTO, airbone, disengaging");
228         _rtoArmed = false;
229         _armed = false;
230         _step = _configDisengageStep;
231       }
232             
233       return false;
234     }
235     
236     if (throttlesAtIdle()) {
237       SG_LOG(SG_AUTOPILOT, SG_INFO, "FGAutoBrake: RTO, throttles at idle, will engage");
238       return true;
239     } else {
240       return false;
241     }
242   } else {
243     // RTO arming case
244     if (speed > _configRTOSpeed) {
245       SG_LOG(SG_AUTOPILOT, SG_INFO, "Autobrake RTO: passed " << _configRTOSpeed << " knots, arming RTO mode");
246       _rtoArmed = true;
247     }
248   }
249   
250   return false;
251 }
252
253 void FGAutoBrake::disengage()
254 {
255   if (!_engaged) {
256     return;
257   }
258   
259   SG_LOG(SG_AUTOPILOT, SG_INFO, "FGAutoBrake: disengage");
260   _engaged = false;
261   _fcsBrakeControl = 0.0;
262   _armed = false;
263   _rtoArmed = false;
264   _targetDecel = 0.0;
265   _step = _configDisengageStep;
266 }
267
268 bool FGAutoBrake::airborne() const
269 {
270   return !_weightOnWheelsNode->getBoolValue();
271 }
272
273 bool FGAutoBrake::throttlesAtIdle() const
274 {
275   SGPropertyNode_ptr e;
276   const double IDLE_THROTTLE = 0.05; // 5% margin for idle setting
277   
278   for (int index=0; (e = _engineControlsNode->getChild("engine", index)) != NULL; ++index) {
279     if ((e->getDoubleValue("throttle") > IDLE_THROTTLE) && !e->getBoolValue("reverser")) {
280       return false;
281     }
282   } // of engines iteration
283   
284   return true;
285 }
286
287 void FGAutoBrake::setArmed(bool aArmed)
288 {
289   if (aArmed == _armed) {
290     return;
291   }
292     
293   disengage();
294   _armed = aArmed;
295 }
296
297 void FGAutoBrake::setRTO(bool aRTO)
298 {
299   if (aRTO) {
300     // ensure we meet RTO selection criteria: 
301     if (!_weightOnWheelsNode->getBoolValue()) {
302       // some systems combine RTO with a standard step, selecting RTO if on
303       // the ground. Handle that case by setting _rto = false, and letting
304       // setStep() do the rest of the work as normal.
305       if ((_configRTOStep > 0) && (_configRTOStep <= _configNumSteps)) {
306         // RTO is combined with a normal step, select that
307       } else {
308         SG_LOG(SG_AUTOPILOT, SG_WARN, "auto-brake: cannot select RTO mode, no weight on wheels");
309       }
310       
311       _rto = false;
312       return;
313     }
314     
315     _rtoArmed = false;
316     _rto = true;
317     _step = _configRTOStep;
318     setArmed(true);
319     SG_LOG(SG_AUTOPILOT, SG_INFO, "RTO mode set");
320   } else {
321     _rto = false;
322     _rtoArmed = false;
323     // and if we're enaged?
324     disengage(); 
325   }
326 }
327
328 void FGAutoBrake::setStep(int aStep)
329 {
330   if (aStep == _step) {
331     return;
332   }
333   
334   SG_LOG(SG_AUTOPILOT, SG_INFO, "Autobrake step now=" << aStep);
335   _step = aStep;
336   bool validStep = false;
337   
338   if (aStep == _configRTOStep) {
339     setRTO(true);
340     validStep = true;
341   } else {
342     _rto = false;
343     validStep = (_step > 0) && (_step <= _configNumSteps);
344   }
345   
346   setArmed(validStep); // selecting a valid step arms the system
347 }
348
349