| 1 |
# Copyright 2019 Stuart Buchanan |
| 2 |
# This file is part of FlightGear. |
| 3 |
# |
| 4 |
# FlightGear is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 2 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# FlightGear is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with FlightGear. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
# |
| 17 |
# Emesary interface to set autopilot configuration. |
| 18 |
# |
| 19 |
|
| 20 |
var GFC700Interface = { |
| 21 |
|
| 22 |
new : func () |
| 23 |
{ |
| 24 |
var obj = { parents : [ GFC700Interface ] }; |
| 25 |
|
| 26 |
# Emesary |
| 27 |
obj._recipient = nil; |
| 28 |
obj._transmitter = emesary.GlobalTransmitter; |
| 29 |
obj._registered = 0; |
| 30 |
|
| 31 |
obj._vertical_mode = globals.props.getNode("/autopilot/annunciator/vertical-mode", 1); |
| 32 |
obj._pitch_setting = globals.props.getNode("/autopilot/settings/target-pitch-deg", 1); |
| 33 |
obj._climb_setting = globals.props.getNode("/autopilot/settings/vertical-speed-fpm", 1); |
| 34 |
obj._speed_setting = globals.props.getNode("/autopilot/settings/target-speed-kt", 1); |
| 35 |
|
| 36 |
# State variables |
| 37 |
obj._vertical_mode_button = globals.props.getNode("/autopilot/vertical-mode-button", 1); |
| 38 |
obj._lateral_mode_button = globals.props.getNode("/autopilot/lateral-mode-button", 1); |
| 39 |
obj._ap_mode_button = globals.props.getNode("/autopilot/AP-mode-button", 1); |
| 40 |
obj._ap_enabled = globals.props.getNode("/autopilot/annunciator/autopilot-enabled", 1);; |
| 41 |
obj._fd_enabled = globals.props.getNode("/autopilot/annunciator/flight-director-enabled", 1);; |
| 42 |
|
| 43 |
return obj; |
| 44 |
}, |
| 45 |
|
| 46 |
# Under the covers there are 3 FSMs, two of which run in parallel, and need |
| 47 |
# separate input channels. |
| 48 |
sendModeChange : func(value) { |
| 49 |
me._vertical_mode_button.setValue(value); |
| 50 |
me._lateral_mode_button.setValue(value); |
| 51 |
if (value == "AP") { |
| 52 |
me._ap_mode_button.setValue("AP"); |
| 53 |
} |
| 54 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 55 |
}, |
| 56 |
|
| 57 |
handleNoseUpDown : func(value) { |
| 58 |
var vertical_mode = me._vertical_mode.getValue(); |
| 59 |
|
| 60 |
if (vertical_mode == "PIT") { |
| 61 |
me._pitch_setting.setValue(me._pitch_setting.getValue() + (value * 1)); |
| 62 |
} |
| 63 |
|
| 64 |
if (vertical_mode == "VS") { |
| 65 |
me._climb_setting.setValue(me._climb_setting.getValue() + (value * 100)); |
| 66 |
setprop("/autopilot/annunciator/vertical-mode-target", |
| 67 |
sprintf("%+ifpm", me._climb_setting.getValue()) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
if (vertical_mode == "FLC") { |
| 72 |
# Note that the button is NOSE UP / NOSE DN, so pressing NOSE DN _increases_ |
| 73 |
# speed, while NOSE UP _decreases_ speed. So the speed setting is reversed |
| 74 |
# in comparison with setting direct pitch. |
| 75 |
me._speed_setting.setValue(me._speed_setting.getValue() - (value * 1)); |
| 76 |
setprop("/autopilot/annunciator/vertical-mode-target", |
| 77 |
sprintf("%i kt", me._speed_setting.getValue()) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
return emesary.Transmitter.ReceiptStatus_Finished; |
| 82 |
}, |
| 83 |
|
| 84 |
setAPNavSource : func(src) { |
| 85 |
setprop("/autopilot/settings/nav-mode-source", src); |
| 86 |
# Also need to do something to trigger a NAV change if we're in NAV mode already. |
| 87 |
return emesary.Transmitter.ReceiptStatus_OK; |
| 88 |
}, |
| 89 |
|
| 90 |
RegisterWithEmesary : func() |
| 91 |
{ |
| 92 |
if (me._recipient == nil){ |
| 93 |
me._recipient = emesary.Recipient.new("AutopilotInterface"); |
| 94 |
var controller = me; |
| 95 |
|
| 96 |
# Note that unlike the various keys, this data isn't specific to a particular |
| 97 |
# Device - it's shared by all. Hence we don't check for the notificaiton |
| 98 |
# Device_Id. |
| 99 |
me._recipient.Receive = func(notification) |
| 100 |
{ |
| 101 |
|
| 102 |
if (notification.NotificationType == notifications.PFDEventNotification.DefaultType and |
| 103 |
notification.Event_Id == notifications.PFDEventNotification.HardKeyPushed and |
| 104 |
notification.EventParameter != nil) |
| 105 |
{ |
| 106 |
var id = notification.EventParameter.Id; |
| 107 |
var value = notification.EventParameter.Value; |
| 108 |
|
| 109 |
if (id == fg1000.FASCIA.AP ) return controller.sendModeChange("AP"); |
| 110 |
if (id == fg1000.FASCIA.HDG) return controller.sendModeChange("HDG"); |
| 111 |
if (id == fg1000.FASCIA.NAV) return controller.sendModeChange("NAV"); |
| 112 |
if (id == fg1000.FASCIA.APR) return controller.sendModeChange("APR"); |
| 113 |
if (id == fg1000.FASCIA.VS ) return controller.sendModeChange("VS"); |
| 114 |
if (id == fg1000.FASCIA.FLC) return controller.sendModeChange("FLC"); |
| 115 |
if (id == fg1000.FASCIA.FD ) return controller.sendModeChange("FD"); |
| 116 |
if (id == fg1000.FASCIA.ALT) return controller.sendModeChange("ALT"); |
| 117 |
if (id == fg1000.FASCIA.VNV) return controller.sendModeChange("VNV"); |
| 118 |
if (id == fg1000.FASCIA.BC ) controller.sendModeChange("BC"); |
| 119 |
if (id == fg1000.FASCIA.NOSE_UP) return controller.handleNoseUpDown(1); |
| 120 |
if (id == fg1000.FASCIA.NOSE_DOWN) return controller.handleNoseUpDown(-1); |
| 121 |
} |
| 122 |
|
| 123 |
if (notification.NotificationType == notifications.PFDEventNotification.DefaultType and |
| 124 |
notification.Event_Id == notifications.PFDEventNotification.FMSData and |
| 125 |
notification.EventParameter != nil) |
| 126 |
{ |
| 127 |
foreach(var key; keys(notification.EventParameter)) { |
| 128 |
var val = notification.EventParameter[key]; |
| 129 |
if (key == "AutopilotNAVSource") return controller.setAPNavSource(val); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 134 |
}; |
| 135 |
} |
| 136 |
|
| 137 |
me._transmitter.Register(me._recipient); |
| 138 |
me._registered = 1; |
| 139 |
}, |
| 140 |
|
| 141 |
DeRegisterWithEmesary : func() |
| 142 |
{ |
| 143 |
# remove registration from transmitter; but keep the recipient once it is created. |
| 144 |
if (me._registered == 1) me._transmitter.DeRegister(me._recipient); |
| 145 |
me._registered = 0; |
| 146 |
}, |
| 147 |
|
| 148 |
|
| 149 |
start : func() { |
| 150 |
me.RegisterWithEmesary(); |
| 151 |
}, |
| 152 |
stop : func() { |
| 153 |
me.DeRegisterWithEmesary(); |
| 154 |
}, |
| 155 |
|
| 156 |
}; |