-
Notifications
You must be signed in to change notification settings - Fork 0
/
bipedal_Robot_simulator.m
419 lines (353 loc) · 14.6 KB
/
bipedal_Robot_simulator.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
function varargout = simulator(varargin)
% Biped simulator m-file for simulator.fig
% Uses: simulator.fig, animatef.m, simclosereq.m
%
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @simulator_OpeningFcn, ...
'gui_OutputFcn', @simulator_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before simulator is made visible.
function simulator_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to simulator (see VARARGIN)
% animation state stopped
set(handles.animation_axes, 'UserData', 0);
% slow down disabled
handles.slow = 1;
% timer object
handles.T = timer('TimerFcn', '%');
% initial sample time
handles.st = 0.01;
% converted data for animation
handles.converteddata = [];
% Choose default command line output for simulator
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = simulator_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in simbutton.
function simbutton_Callback(hObject, eventdata, handles)
% hObject handle to simbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% evaluate biped parameter m-file
s = get(handles.bipedparamedit, 'String');
if length(s)>2
if strcmp(s([end-1, end]), '.m')
s = s(1:end-2);
end;
end;
eval(s);
% evaluate additional parameters m-file
s = get(handles.additionalparamedit, 'String');
if length(s)>2
if strcmp(s([end-1, end]), '.m')
s = s(1:end-2);
end;
end;
eval(s);
% disable all controls
set(gcf, 'Pointer', 'watch');
set(handles.simmodeledit, 'Enable', 'off');
set(handles.bipedparamedit, 'Enable', 'off');
set(handles.additionalparamedit, 'Enable', 'off');
set(handles.playbutton, 'Enable', 'off');
set(handles.pausebutton, 'Enable', 'off');
set(handles.stopbutton, 'Enable', 'off');
set(handles.animationslider, 'Enable', 'off');
set(handles.simtime, 'Enable', 'off');
set(handles.slowmenu, 'Enable', 'off');
set(handles.animationtime, 'Enable', 'off');
set(handles.simbutton, 'Enable', 'off');
set(handles.savebutton, 'Enable', 'off');
set(handles.loadbutton, 'Enable', 'off');
set(handles.savestatebutton, 'Enable', 'off');
% get the simulation file name
s = get(handles.simmodeledit, 'String');
if length(s)>4
if strcmp(s([end-3: end]), '.mdl')
s = s(1:end-4);
end;
end;
simoptions = simset('SrcWorkspace', 'current');
tic;
%%% simulate the model %%%
sim(s, str2double(get(handles.simtime,'String')), simoptions);
%%%%%%%%%%%%%%%%%%%%%%%%%%
toc;
% get the simulation data
handles.data.state = qout;
handles.data.control = uout;
handles.gcstate = gcstateout;
handles.robot = eval(get_param([s, '/Biped model'], 'robot'));
handles.groundp = eval(get_param([s, '/Biped model'], 'groundp'));
handles.st = eval(get_param([s, '/Biped model'], 'st'));
% Update handles structure
guidata(hObject, handles);
% update playback
update_playback(handles);
set(handles.loadbutton, 'Enable', 'on');
set(handles.savestatebutton, 'Enable', 'on');
% done
beep;
function update_playback(handles)
% Updates playback buttons, simulation time, animation slider and
% draws the initial position.
% animation length
n = size(handles.data.state,1);
% update playbutton
set(handles.playbutton, 'Enable', 'on');
% update slider
set(handles.animationslider, 'Min', 0);
set(handles.animationslider, 'Max', (n-1)*handles.st);
set(handles.animationslider, 'SliderStep',...
[handles.st,10*handles.st]/(n*handles.st));
set(handles.animationslider, 'Value', 0);
set(handles.animationslider, 'Enable', 'on');
% update time
s=['0.00/', num2str(get(handles.animationslider, 'Max'), '%.2f')];
set(handles.animationtime, 'String', s);
set(handles.animationtime, 'Enable', 'on');
% enable controls
set(handles.simtime, 'Enable', 'on');
set(handles.slowmenu, 'Enable', 'on');
set(handles.simmodeledit, 'Enable', 'on');
set(handles.bipedparamedit, 'Enable', 'on');
set(handles.additionalparamedit, 'Enable', 'on');
set(handles.simbutton, 'Enable', 'on');
set(handles.savebutton, 'Enable', 'on');
set(handles.figure1, 'Pointer', 'arrow');
handles.converteddata = [];
% update picture
set(handles.animation_axes, 'UserData', 3);
handles.converteddata = animatef(handles);
set(handles.animation_axes, 'UserData', 0);
% Update handles structure
guidata(handles.figure1, handles);
% --- Executes during object creation, after setting all properties.
function simtime_CreateFcn(hObject, eventdata, handles)
% hObject handle to simtime (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
function simtime_Callback(hObject, eventdata, handles)
% hObject handle to simtime (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in playbutton.
function playbutton_Callback(hObject, eventdata, handles)
% hObject handle to playbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% get slow down value
contents = get(handles.slowmenu,'String');
switch contents{get(handles.slowmenu,'Value')}
case 'x 2'
handles.slow=2;
case 'x 5'
handles.slow=5;
case 'x 10'
handles.slow=10;
otherwise
handles.slow=1;
end;
% Update handles structure
guidata(gcbo, handles);
% start new animation
set(handles.slowmenu, 'Enable', 'off');
set(handles.playbutton, 'Enable', 'off');
set(handles.stopbutton, 'Enable', 'on');
set(handles.pausebutton, 'Enable', 'on');
set(handles.simbutton, 'Enable', 'off');
handles.converteddata = animatef(handles);
% Update handles structure
guidata(hObject, handles);
% animation stopped or paused
set(handles.slowmenu, 'Enable', 'on');
set(handles.simbutton, 'Enable', 'on');
if get(handles.animation_axes, 'UserData')==0
% stopped
set(handles.playbutton, 'Enable', 'on');
set(handles.stopbutton, 'Enable', 'off');
set(handles.pausebutton, 'Enable', 'off');
else
% paused
set(handles.playbutton, 'Enable', 'on');
set(handles.stopbutton, 'Enable', 'on');
set(handles.pausebutton, 'Enable', 'off');
end;
% --- Executes on button press in pausebutton.
function pausebutton_Callback(hObject, eventdata, handles)
% hObject handle to pausebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.playbutton, 'Enable', 'on');
set(handles.pausebutton, 'Enable', 'off');
set(handles.animation_axes, 'UserData', 2);
% --- Executes on button press in stopbutton.
function stopbutton_Callback(hObject, eventdata, handles)
% hObject handle to stopbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.playbutton, 'Enable', 'on');
set(handles.pausebutton, 'Enable', 'off');
set(handles.stopbutton, 'Enable', 'off');
set(handles.animation_axes, 'UserData', 0);
% --- Executes during object creation, after setting all properties.
function animationslider_CreateFcn(hObject, eventdata, handles)
% hObject handle to animationslider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
usewhitebg = 0;
if usewhitebg
set(hObject,'BackgroundColor',[.9 .9 .9]);
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end;
% --- Executes on slider movement.
function animationslider_Callback(hObject, eventdata, handles)
% hObject handle to animationslider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% round the value
val = get(hObject,'Value');
val = handles.st*round(val/handles.st);
set(hObject, 'Value', val);
% update time
s = [num2str(val, '%.2f'), '/', num2str(get(hObject, 'Max'), '%.2f')];
set(handles.animationtime, 'String', s);
% update picture
set(handles.animation_axes, 'UserData', 3);
set(handles.playbutton, 'Enable', 'on');
set(handles.pausebutton, 'Enable', 'off');
set(handles.stopbutton, 'Enable', 'on');
handles.converteddata = animatef(handles);
% Update handles structure
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function slowmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to slowmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- Executes on selection change in slowmenu.
function slowmenu_Callback(hObject, eventdata, handles)
% hObject handle to slowmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function bipedparamedit_CreateFcn(hObject, eventdata, handles)
% hObject handle to bipedparamedit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
function bipedparamedit_Callback(hObject, eventdata, handles)
% hObject handle to bipedparamedit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function additionalparamedit_CreateFcn(hObject, eventdata, handles)
% hObject handle to additionalparamedit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
function additionalparamedit_Callback(hObject, eventdata, handles)
% hObject handle to additionalparamedit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function simmodeledit_CreateFcn(hObject, eventdata, handles)
% hObject handle to simmodeledit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
function simmodeledit_Callback(hObject, eventdata, handles)
% hObject handle to simmodeledit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in savebutton.
function savebutton_Callback(hObject, eventdata, handles)
% hObject handle to savebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
data = handles.data;
data.st = handles.st;
data.robot = handles.robot;
data.groundp = handles.groundp;
% move data to the base workspace
assignin('base', 'data', data);
% --- Executes on button press in loadbutton.
function loadbutton_Callback(hObject, eventdata, handles)
% hObject handle to loadbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if evalin('base', 'exist(''data'')')
handles.data.state = evalin('base','data.state');
handles.data.control = evalin('base','data.control');
handles.st = evalin('base','data.st');
handles.robot = evalin('base', 'data.robot');
handles.groundp = evalin('base', 'data.groundp');
set(handles.savestatebutton, 'Enable', 'off');
% Update handles structure
guidata(hObject, handles);
% update playback controls
update_playback(handles);
else
disp('No variable named ''data'' found.');
end;
% --- Executes on button press in savestatebutton.
function savestatebutton_Callback(hObject, eventdata, handles)
% hObject handle to savestatebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% get time
time = round(get(handles.animationslider, 'Value')/handles.st+1);
% get current state
currentstate.coordinates = handles.data.state(time, 1:7);
currentstate.speeds = handles.data.state(time, 8:14);
currentstate.gcstate = handles.gcstate(time, :);
% move to the base workspace
assignin('base', 'currentstate', currentstate);