-
Notifications
You must be signed in to change notification settings - Fork 4
/
autopilot.nas
138 lines (103 loc) · 3.22 KB
/
autopilot.nas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
##
# An auto pilot system for the censa written in Nasal, the
# scripting language inside Flightgear. This is basically an
# event driven script much like the one I wrote in ATS, but it
# utilizes FlightGear's internal PID implementation.
#
# Pitch
# /autopilot/locks/altitude
# on: pitch-hold
# input: /autopilot/internal/target-pitch-deg
# Level Flight
# /autopilot/locks/wings-level on
# /autopilot/internal/target-roll-deg
# Heading
# /autopilot/locks/heading dg-heading-hold-(rudder|roll)
# Rudder: dg-heading-hold-rudder
# Roll: dg-heading-hold-roll
#/autopilot/settings/heading-bug-deg
# Vertical Speed
# /autopilot/locks/roc-lock (on|filter)
# /autopilot/internal/target-roc-fpm
# Altitude Hold
# /autopilot/locks/altitude-hold on
# Auto throttle
# /autopilot/locks/speed speed-with-throttle
# /autopilot/settings/target-speed-kt
var ap = 'autopilot';
var lks = 'locks';
var stg = 'settings';
var intr = 'internal';
control_law = func (test, next) {
print ("Running Control Law");
#By default, keep waiting until the
callback = func {
control_law (test, next);
};
if (test()) {
res = next();
callback = func {
control_law (res[0], res[1]);
};
}
print ("Checking for conditions.");
settimer (callback, 1);
}
#Set up the aircraft for flight.
var setup = func {
heading = getprop ('/orientation/heading-magnetic-deg');
print ("Selecting target heading: ", heading);
setprop (ap, stg, 'heading-bug-deg', heading);
#Clear the initial error
setprop (ap, intr, 'heading-offset-deg', 0.0);
setprop (ap, lks, 'heading', 'dg-heading-hold-rudder');
setprop (ap, 'internal', 'target-roll-deg', 0);
setprop (ap, lks, 'wings-level', 'on');
setprop (ap, lks, 'speed', 'speed-with-throttle');
setprop (ap, stg, 'target-speed-kt', 90);
setprop (ap, 'internal', 'target-pitch-deg', 2.0);
setprop (ap, lks, 'altitude', 'pitch-hold');
return [ready_to_tip_nose, tip_nose];
};
#First, we tip the nose slightly up
var ready_to_tip_nose = func {
var speed = getprop ('/velocities/airspeed-kt');
return speed >= 40.0;
};
var tip_nose = func {
setprop (ap, intr, 'target-pitch-deg', 4.5);
return [ready_to_rise, rise];
};
var ready_to_rise = func {
var speed = getprop ('/velocities/airspeed-kt');
var elevation = getprop ('/position/altitude-ft');
return (speed >= 70.0) and (elevation > 10.0);
};
var rise = func {
setprop (ap, intr, 'target-roc-fpm', 720.0);
setprop (ap, lks, 'roc-lock', 'on');
return [ready_to_turn, turn];
};
var ready_to_turn = func {
var height = getprop ('/position/altitude-ft');
return abs(800.0 - height) < 25.0;
};
var turn = func {
#Change our heading and adjust speed, use roll to adjust heading.
setprop (ap, stg, 'target-altitude-ft', 800.0);
setprop (ap, lks, 'altitude-hold', 'on');
setprop (ap, lks, 'heading', 'dg-heading-hold-roll');
setprop (ap, stg, 'target-speed-kt', 75);
setprop (ap, stg, 'heading-bug-deg', 100.0);
return [wait, 0];
};
var wait = func {
return 0;
};
setprop ('/autopilot/locks/takeoff', 'off');
setlistener('/autopilot/locks/takeoff', setup);
var ready_to_takeoff = func {
var start = getprop ('/autopilot/locks/takeoff');
return start == 'on';
};
control_law (ready_to_takeoff, setup);