Debian Codegraph @

Aircraft/Generic/helicopter-fcs.nas

raw
1 #
2 # Flight Control System for Helicopters by Tatsuhiro Nishioka
3 # $Id$
4 #
5
6 var enableDebug = func() {
7 setprop("/controls/flight/fcs/switches/debug", 1);
8 }
9
10 var debugEnabled = func() {
11 var debugStatus = getprop("/controls/flight/fcs/switches/debug");
12 if (debugStatus == 1) {
13 return 1;
14 } else {
15 return 0;
16 }
17 }
18
19 var dumpParameters = func() {
20 debug.dump(props.globals.getNode("/controls/flight/fcs/gains").getValues());
21 }
22
23 #
24 # FCSFilter - base class for FCS components like CAS and SAS
25 #
26 var FCSFilter = {
27 #
28 # new - constructor
29 # input_path: a property path for a filter input
30 # nil is equivalent to "/controls/flight/"
31 # output_path: a property path for a filter output
32 #
33 new : func(input_path, output_path) {
34 var obj = { parents : [FCSFilter],
35 input_path : input_path,
36 output_path : output_path };
37 obj.axis_conv = {'roll' : 'aileron', 'pitch' : 'elevator', 'yaw' : 'rudder' };
38 obj.body_conv = {'roll' : 'v', 'pitch' : 'u' };
39 obj.last_body_fps = {'roll' : 0.0, 'pitch' : 0.0 };
40 obj.last_pos = {'roll' : 0.0, 'pitch' : 0.0, 'yaw' : 0.0};
41 return obj;
42 },
43
44 #
45 # updateSensitivities: read sensitivitiy values for all axis from the property
46 #
47 updateSensitivities : func() {
48 me.sensitivities = props.globals.getNode("/controls/flight/fcs/gains/sensitivities").getValues();
49 },
50
51 #
52 # read - gets input command for a given axis from input_path
53 #
54 read : func(axis) {
55 if (me.input_path == nil or me.input_path == "") {
56 return getprop("/controls/flight/" ~ me.axis_conv[axis]);
57 } else {
58 var value = getprop(me.input_path ~ "/" ~ axis);
59 value = int(value * 1000) / 1000.0;
60 }
61 },
62
63 #
64 # write - outputs command for a given axis into output_path
65 # this will be the output of an next command filter (like SAS)
66 #
67 write : func(axis, value) {
68 if (me.output_path == nil or me.output_path == '') {
69 setprop("/controls/flight/fcs/" ~ axis, me.limit(value, 1.0));
70 } else {
71 setprop(me.output_path ~ "/" ~ axis, me.limit(value, 1.0));
72 }
73 },
74
75 #
76 # toggleFilterStatus - toggles engage/disengage FCS function
77 # name: FCS filter name; one of /controls/flight/fcs/switches/*
78 #
79 toggleFilterStatus : func(name) {
80 var messages = ["disengaged", "engaged"];
81 var path = "/controls/flight/fcs/switches/" ~ name;
82 var status = getprop(path);
83 setprop(path, 1 - status);
84 screen.log.write(name ~ " " ~ messages[1 - status]);
85 },
86
87 #
88 # getStatus - returns 1 if a given function is engaged
89 # name: FCS filter name; one of /controls/flight/fcs/switches/*
90 #
91 getStatus : func(name) {
92 var path = "/controls/flight/fcs/switches/" ~ name;
93 return getprop(path);
94 },
95
96 #
97 # limit - cut out a given value between +range to -range
98 # value: number to be adjusted
99 # range: absolute number for specifying the range
100 limit : func(value, range) {
101 if (value > range) {
102 return range;
103 } elsif (value < -range) {
104 return - range;
105 }
106 return value;
107 },
108
109 max : func(val1, val2) {
110 return (val1 > val2) ? val1 : val2;
111 },
112
113 min : func(val1, val2) {
114 return (val1 > val2) ? val2 : val1;
115 },
116
117 #
118 # calcCounterBodyFPS - calculates counter-force command to kill movement in each axis
119 # axis: one of 'roll', 'pitch', or 'yaw'
120 # input: input (0.0 - 1.0) for a given axis
121 # offset_deg:
122 #
123 calcCounterBodyFPS : func(axis, input, offset_deg) {
124 var position = getprop("/orientation/" ~ axis ~ "-deg");
125 var body_fps = 0;
126 var last_body_fps = me.last_body_fps[axis];
127 var reaction_gain = 0;
128 var heading = getprop("/orientation/heading-deg");
129 var wind_speed_fps = getprop("/environment/wind-speed-kt") * 1.6878099;
130 var wind_direction = getprop("/environment/wind-from-heading-deg");
131 var wind_direction -= heading;
132 var rate = getprop("/orientation/" ~ axis ~ "-rate-degps");
133 var gear_pos = getprop("/gear/gear[0]/compression-norm") + getprop("/gear/gear[1]/compression-norm");
134 var counter_fps = 0;
135 var fps_axis = me.body_conv[axis]; # convert from {roll, pitch} to {u, v}
136 var target_pos = offset_deg;
137 var brake_deg = 0;
138
139 body_fps = getprop("/velocities/" ~ fps_axis ~ "Body-fps");
140 if (axis == 'roll') {
141 var wind_fps = math.sin(wind_direction / 180 * math.pi) * wind_speed_fps;
142 } else {
143 var wind_fps = math.cos(wind_direction / 180 * math.pi) * wind_speed_fps;
144 }
145 var brake_freq = getprop("/controls/flight/fcs/gains/afcs/fps-" ~ axis ~ "-brake-freq");
146 var brake_gain = getprop("/controls/flight/fcs/gains/afcs/fps-brake-gain-" ~ axis);
147 body_fps -= wind_fps;
148 var dfps = body_fps - me.last_body_fps[axis];
149 var fps_coeff = getprop("/controls/flight/fcs/gains/afcs/fps-" ~ axis ~ "-coeff");
150 target_pos -= int(body_fps * 100) / 100 * fps_coeff;
151 if (axis == 'roll' and gear_pos > 0.0 and position > 0) {
152 target_pos -= position * gear_pos / 5;
153 }
154 reaction_gain = getprop("/controls/flight/fcs/gains/afcs/fps-reaction-gain-" ~ axis);
155 var brake_sensitivity = (axis == 'roll') ? 1 : 1;
156 if (math.abs(position + rate / brake_freq * brake_sensitivity) > math.abs(target_pos)) {
157 if (math.abs(dfps) > 1) {
158 dfps = 1;
159 }
160 var error_deg = target_pos - position;
161 brake_deg = (error_deg - rate / brake_freq) * math.abs(dfps * 10) * brake_gain;
162 if (target_pos > 0) {
163 brake_deg = me.min(brake_deg, 0);
164 } else {
165 brake_deg = me.max(brake_deg, 0);
166 }
167 }
168 counter_fps = me.limit((target_pos + brake_deg) * reaction_gain, 1.0);
169 if (debugEnabled() == 1) {
170 setprop("/controls/flight/fcs/afcs/status/ah-" ~ fps_axis ~ "body-fps", body_fps);
171 setprop("/controls/flight/fcs/afcs/status/ah-" ~ fps_axis ~ "body-wind-fps", wind_fps);
172 setprop("/controls/flight/fcs/afcs/status/ah-" ~ axis ~ "-target-deg", target_pos);
173 setprop("/controls/flight/fcs/afcs/status/ah-" ~ axis ~ "-rate", rate);
174 setprop("/controls/flight/fcs/afcs/status/ah-delta-" ~ fps_axis ~ "body-fps", dfps);
175 setprop("/controls/flight/fcs/afcs/status/ah-" ~ axis ~ "-brake-deg", brake_deg);
176 setprop("/controls/flight/fcs/afcs/status/counter-fps-" ~ axis, counter_fps);
177 }
178
179 me.last_pos[axis] = position;
180 me.last_body_fps[axis] = body_fps;
181 return me.limit(counter_fps + input * 0.2, 1.0);
182 },
183
184 };
185
186 #
187 # AFCS - Automatic Flight Control System
188 #
189 var AFCS = {
190 new : func(input_path, output_path) {
191 var obj = FCSFilter.new(input_path, output_path);
192 obj.parents = [FCSFilter, AFCS];
193 return obj;
194 },
195
196 #
197 # toggle* - I/F methods for Instruments
198 #
199 toggleAutoHover : func() {
200 me.toggleFilterStatus("auto-hover");
201 },
202
203 toggleAirSpeedLock : func() {
204 me.toggleFilterStatus("air-speed-lock");
205 },
206
207 toggleHeadingLock : func() {
208 me.toggleFilterStatus("heading-lock");
209 },
210
211 toggleAltitudeLock : func() {
212 me.toggleFilterStatus("altitude-lock");
213 },
214
215 #
216 # auto hover - locks vBody_fps and uBody_fps regardless of wind speed/direction
217 #
218 autoHover : func(axis, input) {
219 if (axis == 'yaw') {
220 return input;
221 } else {
222 var offset_deg = getprop("/controls/flight/fcs/gains/afcs/fps-" ~ axis ~ "-offset-deg");
223 return me.calcCounterBodyFPS(axis, input, offset_deg);
224 }
225 },
226
227 altitudeLock : func(axis, input) {
228 # not implemented yet
229 return input;
230 },
231
232 headingLock : func(axis, input) {
233 # not implementet yet
234 return input;
235 },
236
237 #
238 # applying all AFCS functions
239 # only auto hover is available at this moment
240 #
241 apply : func(axis) {
242 var input = me.read(axis);
243 var hover_status = me.getStatus("auto-hover");
244 if (hover_status == 0) {
245 me.write(axis, input);
246 return;
247 }
248 me.write(axis, me.autoHover(axis, input));
249 }
250 };
251
252 #
253 # SAS : Stability Augmentation System - a rate damper
254 #
255 var SAS = {
256 #
257 # new
258 # input_path: is a base path to input axis; nil for using raw input from KB/JS
259 # output_path: is a base path to output axis; nis for using /controls/flight/fcs
260 # with input_path / output_path, you can connect SAS, CAS, and more control filters
261 #
262 new : func(input_path, output_path) {
263 var obj = FCSFilter.new(input_path, output_path);
264 obj.parents = [FCSFilter, SAS];
265 return obj;
266 },
267
268 toggleEnable : func() {
269 me.toggleFilterStatus("sas");
270 },
271
272 #
273 # calcGain - get gain for each axis based on air speed and dynamic pressure
274 # axis: one of 'roll', 'pitch', or 'yaw'
275 #
276 calcGain : func(axis) {
277 var mach = getprop("/velocities/mach");
278 var initial_gain = getprop("/controls/flight/fcs/gains/sas/" ~ axis);
279 var gain = initial_gain - 0.1 * mach * mach;
280 if (math.abs(gain) < math.abs(initial_gain) * 0.01 or gain * initial_gain < 0) {
281 gain = initial_gain * 0.01;
282 }
283 return gain;
284 },
285
286 #
287 # calcAuthorityLimit - returns SAS authority limit using a given limit and mach number
288 #
289 calcAuthorityLimit : func() {
290 var mach = getprop("/velocities/mach");
291 var min_mach = 0.038;
292 me.authority_limit = getprop("/controls/flight/fcs/gains/sas/authority-limit");
293 var limit = me.authority_limit;
294 if (math.abs(mach < min_mach)) {
295 limit += (min_mach - math.abs(mach)) / min_mach * (1 - me.authority_limit) * 0.95;
296 }
297 if (debugEnabled() == 1) {
298 setprop("/controls/flight/fcs/sas/status/authority-limit", limit);
299 }
300 return limit;
301 },
302
303 #
304 # apply - apply SAS damper to a given input axis
305 # axis: one of 'roll', 'pitch', or 'yaw'
306 #
307 apply : func(axis) {
308 me.updateSensitivities();
309 var status = me.getStatus("sas");
310 var input = me.read(axis);
311 if (status == 0) {
312 me.write(axis, input);
313 return;
314 }
315 var mach = getprop("/velocities/mach");
316 var value = 0;
317 var rate = getprop("/orientation/" ~ axis ~ "-rate-degps");
318 var gain = me.calcGain(axis);
319 var limit = me.calcAuthorityLimit();
320 if (math.abs(rate) >= me.sensitivities[axis]) {
321 value = - gain * rate;
322 if (value > limit) {
323 value = limit;
324 } elsif (value < - limit) {
325 value = - limit;
326 }
327 }
328 me.write(axis, value + input);
329 }
330 };
331
332 #
333 # CAS : Control Augmentation System - makes your aircraft more meneuverable
334 #
335 var CAS = {
336 new : func(input_path, output_path) {
337 var obj = FCSFilter.new(input_path, output_path);
338 obj.parents = [FCSFilter, CAS];
339 setprop("/autopilot/locks/altitude", '');
340 setprop("/autopilot/locks/heading", '');
341 obj.setCASControlThresholds();
342
343 return obj;
344 },
345
346 calcRollRateAdjustment : func {
347 var position = getprop("/orientation/roll-deg");
348 return math.abs(math.sin(position / 180 * math.pi)) / 6;
349 },
350
351 #
352 # calcHeadingAdjustment - returns roll axis output for stabilizing heading
353 #
354 calcHeadingAdjustment : func {
355 if (getprop("/controls/flight/fcs/switches/heading-adjuster") == 1) {
356 var gain = getprop("/controls/flight/fcs/gains/cas/output/heading-adjuster-gain");
357 var yaw_rate = getprop("/orientation/yaw-rate-degps");
358 var limit = getprop("/controls/flight/fcs/gains/cas/output/heading-adjuster-limit");
359 var adjuster = yaw_rate * gain;
360 return me.limit(adjuster, limit);
361 } else {
362 return 0;
363 }
364 },
365
366 #
367 # calcSideSlipAdjustment - returns yaw axis output for preventing side slip
368 #
369 calcSideSlipAdjustment : func {
370 if (getprop("/controls/flight/fcs/switches/sideslip-adjuster") == 0) {
371 return 0;
372 }
373 var mach = getprop("/velocities/mach");
374 var slip = -getprop("/orientation/side-slip-deg"); # inverted after a change in side-slip sign (bug #901)
375 var min_speed_threshold = getprop("/controls/flight/fcs/gains/cas/input/anti-side-slip-min-speed");
376 if (mach < min_speed_threshold) { # works only if air speed > min_speed_threshold
377 slip = 0;
378 }
379 var anti_slip_gain = getprop("/controls/flight/fcs/gains/cas/output/anti-side-slip-gain");
380 var roll_deg = getprop("/orientation/roll-deg");
381 var gain_adjuster = me.min(math.abs(mach) / 0.060, 1) * me.limit(0.2 + math.sqrt(math.abs(roll_deg)/10), 3);
382 anti_slip_gain *= gain_adjuster;
383 if (debugEnabled() == 1) {
384 setprop("/controls/flight/fcs/cas/status/anti-side-slip", slip * anti_slip_gain);
385 }
386 return slip * anti_slip_gain;
387 },
388
389 #
390 # isInverted - returns 1 if aircraft is inverted (roll > 90 or roll < -90)
391 #
392 isInverted : func() {
393 var roll_deg = getprop("/orientation/roll-deg");
394 if (roll_deg > 90 or roll_deg < -90)
395 return 1;
396 else
397 return 0;
398 },
399
400 # FIXME: command for CAS is just a temporal one
401 #
402 # calcCommand - returns CAS output for each axis
403 #
404 calcCommand: func (axis, input) {
405 var output = 0;
406 var mach = getprop("/velocities/mach");
407 var input_gain = me.calcGain(axis);
408 var output_gain = getprop("/controls/flight/fcs/gains/cas/output/" ~ axis);
409 var target_rate = input * input_gain;
410 var rate = getprop("/orientation/" ~ axis ~ "-rate-degps");
411 var drate = target_rate - rate;
412 if (axis == 'pitch' and me.isInverted() == 1) {
413 drate = - drate;
414 }
415 var attitudeControlThreshold = getprop("/controls/flight/fcs/gains/cas/input/attitude-control-threshold");
416 var rateControlThreshold = getprop("/controls/flight/fcs/gains/cas/input/rate-control-threshold");
417 var locks = {'pitch' : getprop("/autopilot/locks/altitude"),
418 'roll' : getprop("/autopilot/locks/heading")};
419 setprop("/controls/flight/fcs/cas/target_" ~ axis ~ "rate", target_rate);
420 setprop("/controls/flight/fcs/cas/delta_" ~ axis, drate);
421
422 if (axis == 'roll' or axis == 'pitch') {
423 if (math.abs(input) > rateControlThreshold) {
424 return input;
425 } elsif (math.abs(input) > attitudeControlThreshold or locks[axis] != '') {
426 output = drate * output_gain;
427 } else {
428 output = me.calcAttitudeCommand(axis);
429 }
430 if (axis == 'roll' and math.abs(mach) < 0.035) {
431 # FIXME: I don't know if OH-1 has this one
432 output += me.calcCounterBodyFPS(axis, input, -0.8);
433 }
434 } elsif (axis == 'yaw') {
435 if (getprop("/controls/flight/fcs/switches/tail-rotor-adjuster") == 0) {
436 output = input;
437 } else {
438 output = drate * output_gain + me.calcSideSlipAdjustment();
439 }
440 } else {
441 output = drate * output_gain;
442 }
443 return output;
444 },
445
446 toggleEnable : func() {
447 me.toggleFilterStatus("cas");
448 },
449
450 #
451 # toggle enable / disable attitude control
452 # you can make similar function that changes parameters
453 # in attitude-control-limit and rate-control-limit
454 # at controls/flight/fcs/gains/cas/input
455 # CAS changes its behavior when roll/pitch axis inputs reaches each limit.
456 # e.g. when attitude-control-limit is 0.7 and rate-control-limit is 0.9,
457 # giving 0.6 for roll holds bank angle, 0.8 keeps roll rate,
458 # and 1.0 makes roll at maximum roll rate.
459 # Sets of initial values for these limits are stored at
460 # controls/fcs/gains/cas/{attitude,rate}
461 #
462 toggleAttitudeControl : func() {
463 me.toggleFilterStatus("attitude-control");
464 me.setCASControlThresholds();
465 },
466
467 setCASControlThresholds : func()
468 {
469 if (me.getStatus("attitude-control") == 1) {
470 var params = props.globals.getNode("controls/flight/fcs/gains/cas/control/attitude").getValues();
471 props.globals.getNode("controls/flight/fcs/gains/cas/input").setValues(params);
472 } else {
473 var params = props.globals.getNode("controls/flight/fcs/gains/cas/control/rate").getValues();
474 props.globals.getNode("controls/flight/fcs/gains/cas/input").setValues(params);
475 }
476 },
477
478 #
479 # calcAttitudeCommand - Attitude base Augmentation output for roll and pitch axis
480 # axis: either 'roll' or 'pitch'
481 #
482 calcAttitudeCommand : func(axis) {
483 var input_gain = getprop("/controls/flight/fcs/gains/cas/input/attitude-" ~ axis);
484 var output_gain = getprop("/controls/flight/fcs/gains/cas/output/" ~ axis);
485 var brake_freq = getprop("/controls/flight/fcs/gains/cas/output/" ~ axis ~ "-brake-freq");
486 var brake_gain = getprop("/controls/flight/fcs/gains/cas/output/" ~ axis ~ "-brake");
487 var trim = getprop("/controls/flight/" ~ me.axis_conv[axis] ~ "-trim");
488
489 var current_deg = getprop("/orientation/" ~ axis ~ "-deg");
490 var rate = getprop("/orientation/" ~ axis ~ "-rate-degps");
491 var target_deg = (me.read(axis) + trim) * input_gain;
492 if (axis == 'roll' and math.abs(target_deg) < 0.1) {
493 # rolls a bit to counteract the heading changes only if target roll rate = 0
494 target_deg += me.calcHeadingAdjustment();
495 }
496 var command_deg = 0;
497 if (target_deg != 0) {
498 command_deg = (0.094 * math.ln(math.abs(target_deg)) + 0.53) * target_deg;
499 }
500
501 var error_deg = command_deg - current_deg;
502 if (axis == 'pitch' and me.isInverted() == 1) {
503 error_deg = - error_deg;
504 }
505 var brake_deg = (error_deg - rate / brake_freq) * math.abs(error_deg) * brake_gain;
506
507 if (command_deg > 0) {
508 brake_deg = me.min(brake_deg, 0);
509 } else {
510 brake_deg = me.max(brake_deg, 0);
511 }
512
513 if (debugEnabled() == 1) {
514 var monitor_prefix = me.output_path ~ "/status/" ~ axis;
515 setprop(monitor_prefix ~ "-target_deg", target_deg);
516 setprop(monitor_prefix ~ "-error_deg", error_deg);
517 setprop(monitor_prefix ~ "-brake_deg", brake_deg);
518 setprop(monitor_prefix ~ "-deg", current_deg);
519 setprop(monitor_prefix ~ "-rate", -rate);
520 }
521
522 return (error_deg + brake_deg) * output_gain;
523 },
524
525 #
526 # calcGain - returns gain for a given axis using a given gain and speed
527 # FixMe: gain should be calculated using both speed and dynamic pressure
528 #
529 calcGain : func(axis) {
530 var mach = getprop("/velocities/mach");
531 var input_gain = getprop("/controls/flight/fcs/gains/cas/input/" ~ axis);
532 var gain = input_gain;
533 if (axis == 'pitch') {
534 gain += 0.1 * mach * mach;
535 } elsif (axis== 'yaw') {
536 gain *= ((1 - mach) * (1 - mach));
537 }
538 if (gain * input_gain < 0.0 ) {
539 gain = 0;
540 }
541 if (debugEnabled() == 1) {
542 setprop("/controls/flight/fcs/cas/gain-" ~ axis, gain);
543 }
544 return gain;
545 },
546
547 #
548 # apply - public method that outputs CAS command for a given axis to output_path
549 # input is read from input_path
550 # axis: one of 'roll', 'pitch', or 'yaw'
551 #
552 apply : func(axis) {
553 me.updateSensitivities();
554 var input = me.read(axis);
555 var status = me.getStatus("cas");
556 var cas_command = 0;
557 # FIXME : hmm, a bit nasty. CAS should be enabled even with auto-hover....
558 if (status == 0 or (me.getStatus("auto-hover") == 1 and axis != 'yaw')) {
559 me.write(axis, input);
560 return;
561 }
562 cas_command = me.calcCommand(axis, input);
563 me.write(axis, cas_command);
564 }
565 };
566
567 #
568 # Tail hstab, "stabilator," for stabilize the nose
569 #
570 var Stabilator = {
571 new : func() {
572 var obj = { parents : [Stabilator] };
573 me.gainTable = props.globals.getNode("/controls/flight/fcs/gains/stabilator").getChildren('gain-table');
574 return obj;
575 },
576
577 toggleEnable : func {
578 var status = getprop("/controls/flight/fcs/switches/auto-stabilator");
579 getprop("/controls/flight/fcs/switches/auto-stabilator", 1 - status);
580 },
581
582 #
583 # calcPosition - returns stabilator position (output) depending on
584 # predefined gain table and mach number
585 #
586 calcPosition : func() {
587 var speed = getprop("/velocities/mach") / 0.001497219; # in knot
588 var index = int(math.abs(speed) / 10);
589 if (index >= size(me.gainTable) - 1) {
590 index = size(me.gainTable) - 2;
591 }
592 var gain = me.gainTable[index].getValue();
593 var gainAmb = me.gainTable[index-1].getValue();
594 var mod = math.mod(int(math.abs(speed)), 10);
595 var position = gain * ((10 - mod) / 10) + gainAmb * mod / 10;
596 if (speed < -20) {
597 position = - position;
598 }
599 return position;
600 },
601
602 #
603 # apply - public method for Stabilator control
604 # no axis is required since it is only for hstab
605 #
606 apply : func() {
607 var status = getprop("/controls/flight/fcs/switches/auto-stabilator");
608 if (status == 0) {
609 return;
610 }
611 var gain = getprop("/controls/flight/fcs/gains/stabilator/stabilator-gain");
612 var mach = getprop("/velocities/mach");
613 var throttle = getprop("/controls/flight/throttle");
614 var stabilator_norm = 0;
615
616 stabilator_norm = me.calcPosition();
617 setprop("/controls/flight/fcs/stabilator", stabilator_norm);
618 }
619 };
620
621 #
622 # Automatic tail rotor adjuster depending on collective/throttle status
623 #
624 var TailRotorCollective = {
625 new : func() {
626 var obj = FCSFilter.new("/controls/engines/engine[1]", "/controls/flight/fcs/tail-rotor");
627 obj.parents = [FCSFilter, TailRotorCollective];
628 obj.adjuster = 0.0;
629 return obj;
630 },
631
632 #
633 # apply - public method for tail rotor adjuster
634 # no axis is required
635 #
636 apply : func() {
637 var throttle = me.read("throttle");
638 var pedal_pos_deg = getprop("/controls/flight/fcs/yaw");
639 var cas_input = cas.read('yaw');
640 var cas_input_gain = cas.calcGain('yaw');
641 var target_rate = cas_input * cas_input_gain;
642 var rate = getprop("/orientation/yaw-rate-degps");
643 var error_rate = getprop("/controls/flight/fcs/cas/delta_yaw");
644 var error_adjuster_gain = getprop("/controls/flight/fcs/gains/tail-rotor/error-adjuster-gain");
645
646 var minimum = getprop("/controls/flight/fcs/gains/tail-rotor/src-minimum");
647 var maximum = getprop("/controls/flight/fcs/gains/tail-rotor/src-maximum");
648 var low_limit = getprop("/controls/flight/fcs/gains/tail-rotor/low-limit");
649 var high_limit = getprop("/controls/flight/fcs/gains/tail-rotor/high-limit");
650 var authority_limit = getprop("/controls/flight/fcs/gains/tail-rotor/authority-limit");
651 var output = 0;
652 var range = maximum - minimum;
653
654 if (throttle < minimum) {
655 output = low_limit;
656 } elsif (throttle > maximum) {
657 output = high_limit;
658 } else {
659 output = low_limit + (throttle - minimum) / range * (high_limit - low_limit);
660 }
661
662 # CAS driven tail rotor thrust adjuster
663 me.adjuster = error_rate * error_adjuster_gain;
664 me.adjuster = me.limit(me.adjuster, authority_limit);
665 output += me.adjuster;
666
667 setprop("/controls/flight/fcs/tail-rotor/error-rate", error_rate);
668 setprop("/controls/flight/fcs/tail-rotor/adjuster", me.adjuster);
669
670 me.write("throttle", output);
671 }
672 };
673
674 # Back-up FCS
675 # It automatically disable CAS and shifts to
676 # the backup mode (e.g. SAS only or direct link mode)
677 #
678 var BackupFCS = {
679 new : func() {
680 var obj = { parents : [BackupFCS] };
681 obj.switches = {'cas' : 0, 'sas' : 1, 'attitude-control' : 0 }; # default backup switches
682 obj.normalSwitches = props.globals.getNode("/controls/flight/fcs/switches").getValues();
683 setprop("/controls/flight/fcs/failures/manual-backup-mode", 0);
684 setprop("/controls/flight/fcs/switches/backup-mode", 0);
685 return obj;
686 },
687
688 # checkFCSFailures - detects FCS failures
689 # returns 1 if failure (or manual backup mode) is detected, 0 otherwise
690 #
691 checkFCSFailures : func()
692 {
693 # not fully implemented yet
694 if (getprop("/controls/flight/fcs/failures/manual-backup-mode") == 1) {
695 return 1;
696 } else {
697 return 0;
698 }
699 },
700
701 #
702 # shiftToBackupMode - overwrites switches for force entering backup mode
703 #
704 shiftToBackupMode : func() {
705 if (me.switches != nil) {
706 var switchNode = props.globals.getNode("/controls/flight/fcs/switches");
707 switchNode.setValues(me.switches);
708 setprop("/controls/flight/fcs/switches/backup-mode", 1);
709 }
710 },
711
712 #
713 # shiftToNormalMode - bring switches back to normal mode
714 # switches for normalMode are captured at BackupFCS.new
715 shiftToNormalMode : func() {
716 if (me.normalSwitches != nil) {
717 props.globals.getNode("/controls/flight/fcs/switches").setValues(me.normalSwitches);
718 setprop("/controls/flight/fcs/switches/backup-mode", 0);
719 }
720 },
721
722 #
723 # setBackupMode - specifies set of values on FCS switches
724 # switches: hash of FCS switch values that will be set to
725 # controls/flight/fcs/switches on backup mode
726 # only values to be overwritten must be specified
727 # e.g. {'cas' : 0, 'sas' : 1}
728 #
729 setBackupMode : func(switches) {
730 me.switches = switches
731 },
732
733 #
734 # update - main I/F for BackupFCS
735 #
736 update : func() {
737 if (me.checkFCSFailures() == 1) {
738 me.shiftToBackupMode();
739 } elsif (getprop("/controls/flight/fcs/switches/backup-mode") == 1) {
740 me.shiftToNormalMode();
741 }
742 },
743
744 #
745 # toggleBackupMode - I/F for Cockpit Panel
746 #
747 toggleBackupMode : func() {
748 var mode = getprop("/controls/flight/fcs/failures/manual-backup-mode");
749 setprop("/controls/flight/fcs/failures/manual-backup-mode", 1 - mode);
750 }
751 };
752
753 var sas = nil;
754 var cas = nil;
755 var afcs = nil;
756 var stabilator = nil;
757 var tail = nil;
758 var backup = nil;
759 var count = 0;
760
761 #
762 # AFCS main loop
763 # This runs at 60Hz (on every other update of /rotors/main/cone-deg)
764 #
765 var update = func {
766 count += 1;
767 # AFCS, CAS, and SAS run at 60Hz
768 rpm = getprop("/rotors/main/rpm");
769 # AFCS, CAS, and SAS run at 60Hz only when engine rpm >= 10
770 # this rpm filter prevents CAS/SAS work when engine is not running,
771 # which may cause Nasal runtime error
772 if (math.mod(count, 2) == 0 or rpm < 10) {
773 return;
774 }
775
776 cas.apply('roll');
777 cas.apply('pitch');
778 cas.apply('yaw');
779
780 afcs.apply('roll');
781 afcs.apply('pitch');
782 afcs.apply('yaw');
783
784 sas.apply('roll');
785 sas.apply('pitch');
786 sas.apply('yaw');
787 stabilator.apply();
788 tail.apply();
789 backup.update();
790 }
791
792 # Factory default configuration values
793 # DO NOT CHANGE THESE VALUES.!
794 # You can change some of these values in per-aircraft nasal file.
795 # See Aircraft/OH-1/Nasal/OH1.nas for more detail
796 #
797 var default_fcs_params = {
798 'gains' : {
799 'afcs' : {
800 # Auto Hover parameters
801 'fps-brake-gain-pitch' : 1.8,
802 'fps-brake-gain-roll' : 0.8,
803 'fps-pitch-brake-freq' : 3,
804 'fps-pitch-coeff' : -0.95,
805 'fps-pitch-offset-deg' : 0.9,
806 'fps-reaction-gain-pitch' : -0.8,
807 'fps-reaction-gain-roll' : 0.3436,
808 'fps-roll-brake-freq' : 8,
809 'fps-roll-coeff' : 0.8,
810 'fps-roll-offset-deg' : -0.8
811 },
812 'cas' : {
813 'input' : { # Input gains for CAS
814 'roll' : 30,
815 'pitch' : -60,
816 'yaw' : 30,
817 'attitude-roll' : 80,
818 'attitude-pitch' : -80,
819 'attitude-control-threshold' : 0.0, # input threshold that CAS changes attitude-base control to rate-base control
820 'rate-control-threshold' : 0.95, # input threshold that CAS changes rate-base control to doing nothing
821 'anti-side-slip-min-speed' : 0.015
822 },
823 'output' : { # Output gains for CAS
824 'roll' : 0.06,
825 'pitch' : -0.1,
826 'yaw' : 0.5,
827 'roll-brake-freq' : 10,
828 'pitch-brake-freq' : 3,
829 'roll-brake' : 0.4,
830 'pitch-brake' : 6,
831 'anti-side-slip-gain' : -4.5,
832 'heading-adjuster-gain' : -5,
833 'heading-adjuster-limit' : 5,
834 },
835 'control' : { # configuration for CAS augumentation modes
836 'attitude' : { # Attitude control augmentation mode (e.g. ATTDAGMT button on OH-1)
837 # Note: attitude-control-threshold must be smaller than rate-control-threshold in any mode
838 'attitude-control-threshold' : 0.95, # Roll / Pitch attitude(angle) hold mode when 0 < input <= 0.95
839 'rate-control-threshold' : 1.0 # Rate hold mode when 0.95 < input <= 1.0
840 },
841 'rate' : { # Rate control augmentation mode
842 'attitude-control-threshold' : 0.0,
843 'rate-control-threshold' : 0.95
844 },
845 }
846 },
847 'sas' : { # gains for SAS
848 'roll' : 0.02,
849 'pitch' : -0.10,
850 'yaw' : 0.04,
851 'authority-limit' : 0.15 # How much SAS will take over pilot's control. 0.15 means 15%
852 },
853 'sensitivities' : {
854 'roll' : 1.0,
855 'pitch' : 1.0,
856 'yaw' : 3.0
857 },
858 'tail-rotor' : { # parameters for tail rotor control based on throttle / collective
859 'src-minimum' : 0.10, # throttle value that outputs low-limit
860 'src-maximum' : 1.00, # throttle value that outputs high-limit
861 'low-limit' : 0.00011,
862 'high-limit' : 0.0035,
863 'error-adjuster-gain' : -0.5, # gain that how much CAS adjust yaw rate
864 'authority-limit' : 0.3
865 },
866 'stabilator' : { # gain tables for adjusting either incidence or flap angle of hstab
867 # index is the speed (Kt) devided by 10
868 # 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160, 170, 180, .....
869 'gain-table' : [-0.9, -0.8, 0.1, -0.5, 0.0, 0.7, 0.8, 0.9, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.9, 0.8, 0.6, 0.4, 0.2, -1.0]
870 }
871 },
872 'switches' : { # master switches for AFCS, can be controlled by cockpit panel or keys
873 'auto-hover' : 0,
874 'cas' : 1,
875 'sas' : 1,
876 'attitude-control' : 0,
877 'auto-stabilator' : 1,
878 'sideslip-adjuster' : 1,
879 'tail-rotor-adjuster' : 1,
880 'heading-adjuster' : 0,
881 'air-speed-lock' : 0,
882 'heading-lock' : 0,
883 'altitude-lock' : 0,
884 }
885 };
886
887 #
888 # initialize - creates AFCS components and invokes AFCS main loop
889 #
890 var initialize = func {
891 cas = CAS.new(nil, "/controls/flight/fcs/cas");
892 afcs = AFCS.new("/controls/flight/fcs/cas", "/controls/flight/fcs/afcs");
893 sas = SAS.new("/controls/flight/fcs/afcs", "/controls/flight/fcs");
894 stabilator = Stabilator.new();
895 tail = TailRotorCollective.new();
896 backup = BackupFCS.new();
897 setlistener("/rotors/main/cone-deg", update);
898 }
899
900 #
901 # Stores default AFCS parameters at startup
902 #
903 var confNode = props.globals.getNode("/controls/flight/fcs", 1);
904 confNode.setValues(default_fcs_params);
905
906 #
907 # fcs-initialized signal must be set by per-aircraft nasal script
908 # to show that FCS configuration parameters are set
909 #
910 setlistener("/sim/signals/fcs-initialized", initialize);
911