]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/autobrake.cxx
0eae0fe1b185358866d8336bdd0b6b020526a4ea
[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   _configMaxDecel = 11; // ft-sec-sec
43   _configRTODecel = 11; // ft-sec-sec
44   _configRTOSpeed = 85; // kts
45 }
46
47 FGAutoBrake::~FGAutoBrake()
48 {
49 }
50
51 void FGAutoBrake::init()
52 {
53   _root = fgGetNode("/autopilot/autobrake", true);
54   
55   _root->tie("armed", SGRawValueMethods<FGAutoBrake, bool>
56     (*this, &FGAutoBrake::getArmed, &FGAutoBrake::setArmed));
57   
58   _root->tie("step", SGRawValueMethods<FGAutoBrake, int>
59     (*this, &FGAutoBrake::getStep, &FGAutoBrake::setStep));
60     
61   _root->tie("rto", SGRawValueMethods<FGAutoBrake, bool>
62     (*this, &FGAutoBrake::getRTO, &FGAutoBrake::setRTO));
63       
64   _engineControlsNode = fgGetNode("/controls/engines");
65   
66   // brake position nodes
67   _brakeInputs[0] = fgGetNode("/controls/gear/brake-left");
68   _brakeInputs[1] = fgGetNode("/controls/gear/brake-right");
69   _brakeInputs[2] = fgGetNode("/controls/gear/copilot-brake-left");
70   _brakeInputs[3] = fgGetNode("/controls/gear/copilot-brake-right");
71   
72     // config
73   SGPropertyNode_ptr config = _root->getChild("config", 0, true);
74   config->tie("num-steps", SGRawValuePointer<int>(&_configNumSteps));
75   config->tie("rto-step", SGRawValuePointer<int>(&_configRTOStep));
76   config->tie("rto-decel-fts-sec", SGRawValuePointer<double>(&_configRTODecel));
77   config->tie("max-decel-fts-sec", SGRawValuePointer<double>(&_configMaxDecel));
78   config->tie("rto-engage-kts", SGRawValuePointer<double>(&_configRTOSpeed));
79   
80   // outputs
81   _root->tie("target-decel-fts-sec", 
82     SGRawValueMethods<FGAutoBrake,double>(*this, &FGAutoBrake::getTargetDecel));
83   _root->tie("actual-decel-fts-sec", 
84     SGRawValueMethods<FGAutoBrake,double>(*this, &FGAutoBrake::getActualDecel));
85   _root->tie("fcs-brake", SGRawValuePointer<double>(&_fcsBrakeControl));
86   _root->tie("brake-left-output", SGRawValuePointer<double>(&_leftBrakeOutput));
87   _root->tie("brake-right-output", SGRawValuePointer<double>(&_rightBrakeOutput));
88   
89   _root->tie("engaged", SGRawValueMethods<FGAutoBrake, bool>(*this, &FGAutoBrake::getEngaged)); 
90 }
91
92 void FGAutoBrake::postinit()
93 {  
94   _weightOnWheelsNode = fgGetNode("/gear/gear/wow");
95   _groundspeedNode = fgGetNode("/velocities/groundspeed-kt");
96   _lastWoW = _weightOnWheelsNode->getBoolValue();
97 }
98
99
100 void FGAutoBrake::bind()
101 {
102 }
103
104 void FGAutoBrake::unbind()
105 {
106 }
107
108 void FGAutoBrake::update(double dt)
109 {
110   _leftBrakeInput = SGMiscd::max(_brakeInputs[0]->getDoubleValue(), 
111       _brakeInputs[2]->getDoubleValue());
112   _rightBrakeInput = SGMiscd::max(_brakeInputs[1]->getDoubleValue(), 
113       _brakeInputs[3]->getDoubleValue());
114
115   // various FDMs don't supply sufficent information for us to work,
116   // so just be passive in those cases.
117   if (!_weightOnWheelsNode || !_groundspeedNode || !_engineControlsNode) {
118     // pass the values straight through
119     _leftBrakeOutput = _leftBrakeInput; 
120     _rightBrakeOutput = _rightBrakeInput; 
121     return;
122   }
123
124   if (dt <= 0.0) {
125     return; // paused
126   }
127   
128   double gs = _groundspeedNode->getDoubleValue();
129   _actualDecel = (_lastGroundspeedKts - gs) / dt;
130   _lastGroundspeedKts = gs;
131   
132   if (!_armed || (_step == 0)) {
133     // even if we're off or disarmed, the physical valve system must pass
134     // pilot control values through.
135     _leftBrakeOutput = _leftBrakeInput; 
136     _rightBrakeOutput = _rightBrakeInput; 
137     return;
138   }
139   
140   if (_engaged) {
141     updateEngaged(dt);
142   } else {
143     bool e = shouldEngage();
144     if (e) {
145       engage();
146       updateEngaged(dt);
147     }
148   }
149   
150   // sum pilot inpiuts with auto-brake FCS input
151   _leftBrakeOutput = SGMiscd::max(_leftBrakeInput, _fcsBrakeControl);
152   _rightBrakeOutput = SGMiscd::max(_rightBrakeInput, _fcsBrakeControl);
153
154   _lastWoW = _weightOnWheelsNode->getBoolValue();
155 }
156
157 void FGAutoBrake::engage()
158 {
159   SG_LOG(SG_AUTOPILOT, SG_INFO, "FGAutoBrake: engaging");
160   _engaged = true;
161 }
162
163 void FGAutoBrake::updateEngaged(double dt)
164 {
165   // check for pilot braking input cancelling engage
166   const double BRAKE_INPUT_THRESHOLD = 0.02; // 2% application triggers
167   if ((_leftBrakeInput > BRAKE_INPUT_THRESHOLD) || (_rightBrakeInput > BRAKE_INPUT_THRESHOLD)) {
168     SG_LOG(SG_AUTOPILOT, SG_INFO, "auto-brake, detected pilot brake input, disengaing");
169     disengage();
170     return;
171   }
172
173   // update the target deceleration; note step can be changed while engaged
174   if (_rto) {
175     _targetDecel = _configRTODecel;
176   } else {
177     double f = (double) _step / _configNumSteps;
178     _targetDecel = _configMaxDecel * f;
179   }
180 }
181
182 bool FGAutoBrake::shouldEngage()
183 {
184   if (_rto) {
185     return shouldEngageRTO();
186   }
187
188   if (_lastWoW || !_weightOnWheelsNode->getBoolValue()) {
189     return false;
190   }
191
192   SG_LOG(SG_AUTOPILOT, SG_INFO, "Autobrake, should enage");
193   return true;
194 }
195
196 bool FGAutoBrake::shouldEngageRTO()
197 {
198   double speed = _groundspeedNode->getDoubleValue();
199
200   if (_rtoArmed) {
201     if (speed < _configRTOSpeed) {
202       SG_LOG(SG_AUTOPILOT, SG_INFO, "FGAutoBrake: RTO armed, but speed is now below arming speed");
203       _rtoArmed = false;
204       return false;
205     }
206     
207     if (!_weightOnWheelsNode->getBoolValue()) {
208       return false;
209     }
210     
211     return throttlesAtIdle();
212   } else {
213     // RTO arming case
214     if (speed > _configRTOSpeed) {
215       SG_LOG(SG_AUTOPILOT, SG_INFO, "Autobrake RTO: passed " << _configRTOSpeed << " knots, arming RTO mode");
216       _rtoArmed = true;
217     }
218   }
219   
220   return false;
221 }
222
223 void FGAutoBrake::disengage()
224 {
225   if (!_engaged) {
226     return;
227   }
228   
229   SG_LOG(SG_AUTOPILOT, SG_INFO, "FGAutoBrake: disengage");
230   _engaged = false;
231   _armed = false;
232   _rtoArmed = false;
233   _targetDecel = 0.0;
234 }
235
236 bool FGAutoBrake::throttlesAtIdle()
237 {
238   SGPropertyNode_ptr e;
239   const double IDLE_THROTTLE = 0.05; // 5% margin for idle setting
240   
241   for (int index=0; (e = _engineControlsNode->getChild("engine", index)) != NULL; ++index) {
242     if ((e->getDoubleValue("throttle") > IDLE_THROTTLE) && !e->getBoolValue("reverser")) {
243       return false;
244     }
245   } // of engines iteration
246   
247   SG_LOG(SG_AUTOPILOT, SG_INFO, "FGAutoBrake: throttles at idle");
248   return true;
249 }
250
251 void FGAutoBrake::setArmed(bool aArmed)
252 {
253   if (aArmed == _armed) {
254     return;
255   }
256     
257   disengage();
258   _armed = aArmed;
259 }
260
261 void FGAutoBrake::setRTO(bool aRTO)
262 {
263   if (aRTO) {
264     // ensure we meet RTO selection criteria: 
265     if (!_weightOnWheelsNode->getBoolValue()) {
266       SG_LOG(SG_AUTOPILOT, SG_WARN, "auto-brake: cannot select RTO mode, no weight on wheels");
267       return;
268     }
269     
270     _rtoArmed = false;
271     _rto = true;
272     _step = _configRTOStep;
273     setArmed(true);
274     SG_LOG(SG_AUTOPILOT, SG_INFO, "RTO mode set");
275   } else {
276     _rto = false;
277     _rtoArmed = false;
278     // and if we're enaged?
279     disengage(); 
280   }
281 }
282
283 void FGAutoBrake::setStep(int aStep)
284 {
285   if (aStep == _step) {
286     return;
287   }
288   
289   SG_LOG(SG_AUTOPILOT, SG_INFO, "Autobrake step now=" << aStep);
290   _step = aStep;
291   bool validStep = false;
292   
293   if (aStep == _configRTOStep) {
294     setRTO(true);
295     validStep = true;
296   } else {
297     _rto = false;
298     validStep = (_step > 0) && (_step <= _configNumSteps);
299   }
300   
301   setArmed(validStep); // selecting a valid step arms the system
302 }
303
304