-
Notifications
You must be signed in to change notification settings - Fork 7
/
Simulation.m
192 lines (158 loc) · 8.4 KB
/
Simulation.m
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
classdef Simulation < handle
%Simulation Class to control all aspects (environment, aircraft)
% The simulation class contains the aircraft and the environment.
% It also keeps track of aircraft positions.Updating the simulation
% causes the aircraft positions to be updated according to their
% speed and heading angle. Heading angle is updated according to the
% turnrate commanded by the aircraft Flight Controler.
% The Dislay method plots thermals and aircraft positions and objectives
% onto the specified axis.
% The UpdateControlVariables method updates the control variables of
% the flight controllers of the aircraft in TheAircraft array.
properties
TheAircraft
axis
visualizeSimulation=false;
environment
AcPlotHandles;
ETPlotHandles
ETvPlotHandles
TCPlotHandles;
MapPlotHandles;
currenttime=0;
execution_frequency = 20; %Simulation execution frequency [Hz]
fastforwardfactor = 1.0; %Fast forward for simulation speed up (dt stays same, this is just to calculate faster!)
end
methods
function obj=Simulation(varargin)
fprintf('Initialising simulation...\n');
axis_to_use = [];
if (nargin == 1)
axis_to_use = varargin{1};
elseif(nargin >= 2)
axis_to_use = varargin{1};
obj.visualizeSimulation = varargin{2};
end
if nargin>=6
number_thermals = length(varargin{4});
X = varargin{3};
Y = varargin{4};
W = varargin{5};
R = varargin{6};
else
number_thermals = 5;
X = [-70, -20, 70, 0, 0];
Y = [-60, -20, 0, 50,-20];
W = [ 3, 3, 3, 3, 3];
R = [ 20, 20, 20, 20, 20];
end
if nargin>=7 && ~isempty(varargin{7})
nThml = varargin{7};
else
nThml = 100;
end
if(obj.visualizeSimulation) set(axis_to_use,'ButtonDownFcn',@obj.axis_clicked_fcn); end;
pathangle = 0;
V=9.3;
sinkrate=V/10;
%------------------------------%
%------Control variables-------%
%------------------------------%
variables.search_latch_threshhold= 0.5*sinkrate;
variables.cruise_latch_threshhold= 2.0*sinkrate;
variables.ceiling = 400;
variables.begin_search_altitude = -0;
variables.filter_rate = 5; %Kalman Filtering frequency [Hz]
variables.actual_noise = 0.0;
variables.actual_noise_z2 = 0.0;
variables.measurement_noise = 0.2; %This is the standard deviation
variables.measurement_noise_z2 = 1e6 ; %This is the standard deviation. Set to high value to disable.
variables.process_noise_q1 = 0.001; %This is the standard deviation for W
variables.process_noise_q2 = 0.1; %This is the standard deviation for R
variables.process_noise_q3 = 0.2; %This is the standard deviation for x,y
variables.kf_x_init = [1.5 80 30]; %Kalman filter initial state. Note that x_init(3) is just the distance from the current aircraft position
variables.kf_x_init_angle_offset= 0;
variables.kf_P_init = diag([2,10,20,20]);%diag([0.5^2 10^2 18^2 18^2]); %Kalman filter initial covariance
%variables.kf_P_init = diag([1^2 10^2 20^2 20^2]); %Kalman filter initial covariance
variables.ukf_alpha = 0.01; %Unscented Kalman Filter tuning parameter
variables.pf_K = 0.05; %Particle Filter tuning parameter
variables.thermalling_radius = 10;
variables.roll_param = 137.72; %Techpod at nominal airspeed: 20.95, AtlantikSolar at nominal airspeed: 137.72
variables.bSimulateSilently = false; %Set to true to avoid all output (drawing & text)
variables.SaveReducedHistory = true;
variables.acInitState = [-100,-100,200,0,0;...
-50, -100,200,0,0];
variables.acWPs = [-100,100,0; -100,-100,0];
% turnrate = (g/V)*tan(phi)
variables.search_pitch_angle = deg2rad(5.0);
variables.min_thermal_latch_time= 10;
variables.floor = 0;
variables.Waypointtol = 5;
variables.min_cruise_time = 5;
variables.min_search_time = 5;
variables.k_p = 2.0;
variables.k_d = 0.0;
variables.k_i = 0.01;
variables.KFtype = 1;
if nargin>=8 && ~isempty(varargin{8})
inputVars = varargin{8};
assert(isstruct(inputVars),'8th argument should be struct of variables');
% Allow specification of structure of variables.
vars = fieldnames(inputVars);
for iV=1:length(vars)
assert(isfield(variables,vars{iV}),'Unknown variable %s', vars{iV});
variables.(vars{iV}) = inputVars.(vars{iV});
end
end
if(obj.visualizeSimulation)
obj.axis=axis_to_use;
hold(obj.axis,'on');
set(obj.axis, 'CameraViewAngle', get(obj.axis,'CameraViewAngle'));
axis(obj.axis,'equal');
end
grid on;
% initial state covraiance
N=1000; % total dynamic steps
%xV = zeros(n,N); %estmate % allocate memory
%pV = zeros(3,N);
%vV=zeros(3,N);
obj.environment=Environment([-nThml,nThml],[-nThml,nThml],number_thermals,@GaussianThermal,obj.axis,X,Y,W,R);
%obj.environment=Environment_grid([-size,size],[-size,size],450,@FlightGearThermal,obj.axis,X,Y,W,R);
%obj.environment=Environment_random([-size,size],[-size,size],5,@FlightGearThermal,obj.axis,X,Y,W,R);
obj.environment.print;
st = variables.acInitState;
aircraft=Aircraft(st(1,1),st(1,2),st(1,3),st(1,4),st(1,5),variables,sinkrate,obj.environment,'Aircraft 1',obj.execution_frequency,variables.acWPs);
if size(st,1)>1
aircraft2=Aircraft(st(2,1),st(2,2),st(2,3),st(2,4),st(2,5),variables,sinkrate,obj.environment,'Aircraft 2',obj.execution_frequency,variables.acWPs);
aircraft = [aircraft, aircraft2];
end
obj.TheAircraft=aircraft;
end
function Update(obj,dt,bSimulateSilently)
if(nargin<3); bSimulateSilently=false; end
for i=1:length(obj.TheAircraft)
obj.TheAircraft(i).update(obj.currenttime+dt);
end
if(obj.visualizeSimulation && ~bSimulateSilently)
title(obj.axis,sprintf('Time: %4.1f seconds',obj.currenttime));
for i=1:length(obj.TheAircraft)
obj.TheAircraft(i).Display(obj.axis);
end
end
obj.currenttime = obj.currenttime+dt;
end
function UpdateControlVariables(obj,variables)
for i=1:length(obj.TheAircraft)
obj.TheAircraft(i).controller.variables = variables;
end
end
function axis_clicked_fcn(obj,h,~)
point = get(h,'CurrentPoint');
new_waypoint = [point(1,1),point(1,2)];
for i=1:length(obj.TheAircraft)
obj.TheAircraft(i).controller.Waypoints(end+1,:) = new_waypoint;
end
fprintf('New WP at [%3.1f,%3.1f]\n',point(1,1),point(1,2));
end
end
end