-
Notifications
You must be signed in to change notification settings - Fork 4
/
startsim_matchexpoffboard.m
239 lines (188 loc) · 9.04 KB
/
startsim_matchexpoffboard.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
%startsim_matchexpoffboard.m Main script for running quadrotor simulation to match
%experimental Crash Set VII
% Author: Fiona Chui ([email protected])
% Last Updated: December 12, 2016
% Description: Main script for running quadrotor simulation to match
% experimental Crash Set VII. Before the collision, attitude
% control is used. After the collision, control switches to
% controlleroffboard, which mimics the unstable/suboptimal
% PX4 offboard controller used during experiments.
%-------------------------------------------------------------------------%
clear all;
crash = 'VII-15';
global g
global timeImpact
global globalFlag
%% Initialize Fuzzy Logic Process
[FuzzyInfo, PREIMPACT_ATT_CALCSTEPFWD] = initfuzzyinput();
%% Initialize Simulation Parameters
ImpactParams = initparams_navi;
SimParams.recordContTime = 0;
SimParams.useFaesslerRecovery = 0;%Use Faessler recovery
SimParams.useRecovery = 1;
SimParams.timeFinal = 2;
tStep = 1/200;%1/200;
ImpactParams.wallLoc = 1.5;%1.5;
ImpactParams.wallPlane = 'YZ';
ImpactParams.timeDes = 0.5; %Desired time of impact. Does nothing
ImpactParams.frictionModel.muSliding = 0.3;
ImpactParams.frictionModel.velocitySliding = 1e-4; %m/s
timeImpact = 10000;
timeStabilized = 10000;
%% Initialize Structures
IC = initIC;
Control = initcontrol;
PropState = initpropstate;
Setpoint = initsetpoint;
[Contact, ImpactInfo] = initcontactstructs;
localFlag = initflags;
ImpactIdentification = initimpactidentification;
%% Set initial Conditions
[VxImpact, IC.attEuler,~,~, ~, ~] = matchexperimentIC(crash);
[~, Setpoint] = matchoffboardsetpoints(crash);
IC.posn(3) = Setpoint.posn(3);
%%%%%%%%%%%% ***** SET INITIAL CONDITIONS HERE ***** %%%%%%%%%%%%%%%%%%%%%%
Control.twist.posnDeriv(1) = VxImpact; %World X Velocity at impact %%%
% IC.attEuler = [deg2rad(0);deg2rad(pitchImpact);deg2rad(yawImpact)]; %%%
IC.posn(1) = ImpactParams.wallLoc-0.32; %%%
% Setpoint.posn(3) = IC.posn(3); %%%
xAcc = 0; %%%
%%%%%%%%%%% ***** END SET INITIAL CONDITIONS HERE ***** %%%%%%%%%%%%%%%%%%%
rotMat = quat2rotmat(angle2quat(-(IC.attEuler(1)+pi),IC.attEuler(2),IC.attEuler(3),'xyz')');
% [IC.posn(1), VxImpact, SimParams.timeInit, xAcc ] = getinitworldx( ImpactParams, Control.twist.posnDeriv(1),IC, xAcc);
SimParams.timeInit = 0; %% comment out if using getinitworldx()
% Setpoint.head = deg2rad(-9.19+180);
% Setpoint.time = 2; %not used
% Setpoint.posn(1:2) = [1.5;0];
Trajectory = Setpoint;
iTrajectory = 1;
Experiment.propCmds = [];
Experiment.manualCmds = [];
globalFlag.experiment.rpmChkpt = zeros(4,1);
globalFlag.experiment.rpmChkptIsPassed = zeros(1,4);
IC.linVel = rotMat*[VxImpact;0;0];
[IC.rpm, Control.u] = initrpm(rotMat, [xAcc;0;0]); %Start with hovering RPM
PropState.rpm = IC.rpm;
%% Initialize state and kinematics structs from ICs
[state, stateDeriv] = initstate(IC, xAcc);
[Pose, Twist] = updatekinematics(state, stateDeriv);
%% Initialize sensors
Sensor = initsensor(state, stateDeriv);
%% Initialize History Arrays
% Initialize history
Hist = inithist(SimParams.timeInit, state, stateDeriv, Pose, Twist, Control, PropState, Contact, localFlag, Sensor);
% Initialize Continuous History
if SimParams.recordContTime == 1
ContHist = initconthist(SimParams.timeInit, state, stateDeriv, Pose, Twist, Control, ...
PropState, Contact, globalFlag, Sensor);
end
%% Simulation Loop
for iSim = SimParams.timeInit:tStep:SimParams.timeFinal-tStep
% display(iSim)
%% Impact Detection
[ImpactInfo, ImpactIdentification] = detectimpact(iSim, tStep, ImpactInfo, ImpactIdentification,...
Hist.sensors,Hist.poses,PREIMPACT_ATT_CALCSTEPFWD, stateDeriv,state);
[FuzzyInfo] = fuzzylogicprocess(iSim, ImpactInfo, ImpactIdentification,...
Sensor, Hist.poses(end), SimParams, Control, FuzzyInfo);
% Calculate accelref in world frame based on FuzzyInfo.output, estWallNormal
if sum(FuzzyInfo.InputsCalculated) == 4 && Control.accelRefCalculated == 0;
Control.accelRef = calculaterefacceleration(FuzzyInfo.output, ImpactIdentification.wallNormalWorld);
Control.accelRefCalculated = 1;
end
%% Control
if Control.accelRefCalculated*SimParams.useRecovery == 1 %recovery control
if SimParams.useFaesslerRecovery == 1
Control = checkrecoverystage(Pose, Twist, Control, ImpactInfo);
[Control] = computedesiredacceleration(Control, Twist);
% Compute control outputs
[Control] = controllerrecovery(tStep, Pose, Twist, Control);
Control.type = 'recovery';
else %Setpoint recovery
% disp('Offboard Control Engaged');
Control.pose.posn = Trajectory(iTrajectory).posn;
Control.pose.attEuler(3) = Trajectory(iTrajectory).head;
Control.twist.linVel = [0;0;0];
Control.twist.worldAcc = [0;0;0];
Control.twist.angVel = [0;0;0];
Control.recoveryStage = 0;
Control = controlleroffboard(state,stateDeriv,iSim,SimParams.timeInit,tStep,Control);
% Control = controllerposn(state,iSim,SimParams.timeInit,tStep,Control.pose.attEuler(3),Control);
Control.type = 'offboard';
end
else %normal posn control
Control.recoveryStage = 0;
Control.desEuler = IC.attEuler;
Control.pose.posn(3) = Trajectory(end).posn(3);
Control = controlleratt(state,iSim,SimParams.timeInit,tStep,Control,Experiment.manualCmds);
Control.type = 'att';
end
% if iSim >= timeImpact + 1.085258
% disp('Disarmed');
% Control.rpm = [0;0;0;0];
% Control.type = 'disarmed';
% end
%% Propagate Dynamics
options = getOdeOptions();
[tODE,stateODE] = ode45(@(tODE, stateODE) dynamicsystem(tODE,stateODE, ...
tStep,Control.rpm,ImpactParams,PropState.rpm, ...
Experiment.propCmds),[iSim iSim+tStep],state,options);
% Reset contact flags for continuous time recording
globalFlag.contact = localFlag.contact;
%% Record History
if SimParams.recordContTime == 0
[stateDeriv, Contact, PropState] = dynamicsystem(tODE(end),stateODE(end,:), ...
tStep,Control.rpm,ImpactParams, PropState.rpm, ...
Experiment.propCmds);
if sum(globalFlag.contact.isContact)>0
Contact.hasOccured = 1;
if ImpactInfo.firstImpactOccured == 0
ImpactInfo.firstImpactOccured = 1;
end
end
else
% Continuous time recording
for j = 1:size(stateODE,1)
[stateDeriv, Contact, PropState] = dynamicsystem(tODE(j),stateODE(j,:), ...
tStep,Control.rpm,ImpactParams, PropState.rpm, ...
Experiment.propCmds);
if sum(globalFlag.contact.isContact)>0
Contact.hasOccured = 1;
if ImpactInfo.firstImpactOccured == 0
ImpactInfo.firstImpactOccured = 1;
end
end
ContHist = updateconthist(ContHist,stateDeriv, Pose, Twist, Control, PropState, Contact, globalFlag, Sensor);
end
ContHist.times = [ContHist.times;tODE];
ContHist.states = [ContHist.states,stateODE'];
end
localFlag.contact = globalFlag.contact;
state = stateODE(end,:)';
t = tODE(end);
[Pose, Twist] = updatekinematics(state, stateDeriv);
%% Update Sensors
Sensor = updatesensor( state, stateDeriv );
%Discrete Time recording @ 200 Hz
Hist = updatehist(Hist, t, state, stateDeriv, Pose, Twist, Control, PropState, Contact, localFlag, Sensor);
%% End loop conditions
% Navi has crashed:
if state(9) <= 0
display('Navi has hit the floor :(');
ImpactInfo.isStable = 0;
break;
end
% Navi has drifted very far away from wall:
if state(7) <= -10
display('Navi has left the building');
ImpactInfo.isStable = 1;
break;
end
% Recovery control has worked, altitude stabilized:
if Control.recoveryStage == 4
display('Altitude has been stabilized');
ImpactInfo.isStable = 1;
break;
end
end
%% Generate plottable arrays
Plot = hist2plot(Hist);