-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAIN.m
113 lines (89 loc) · 2.73 KB
/
MAIN.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
% Representation-Free Model Predictive Control for Dynamic Quadruped Panther
% Author: Yanran Ding
% Last modified: 2020/12/21
%
% Code accompanying the paper:
% Yanran Ding, Abhishek Pandala, Chuanzheng Li, Young-Ha Shin, Hae-Won Park
% "Representation-Free Model Predictive Control for Dynamic Motions in Quadrupeds"
% Transactions on Robotics
%
% preprint available at: https://arxiv.org/abs/2012.10002
% video available at: https://www.youtube.com/watch?v=iMacEwQisoQ&t=101s
%% initialization
clear all;close all;clc
addpath fcns fcns_MPC
%% --- parameters ---
% ---- gait ----
% 0-trot; 1-bound; 2-pacing 3-gallop; 4-trot run; 5-crawl
gait = 5;
p = get_params(gait);
p.playSpeed = 1;
p.flag_movie = 1; % 1 - make movie
use_qpSWIFT = 0; % 0 - quadprog, 1 - qpSWIFT (external)
dt_sim = p.simTimeStep;
SimTimeDuration = 1.5; % [sec]
MAX_ITER = floor(SimTimeDuration/p.simTimeStep);
% desired trajectory
p.acc_d = 1;
p.vel_d = [0.5;0];
p.yaw_d = 0.5;
%% Model Predictive Control
% --- initial condition ---
% Xt = [pc dpc vR wb pf]': [30,1]
if gait == 1
[p,Xt,Ut] = fcn_bound_ref_traj(p);
else
[Xt,Ut] = fcn_gen_XdUd(0,[],[1;1;1;1],p);
end
% --- logging ---
tstart = 0;
tend = dt_sim;
[tout,Xout,Uout,Xdout,Udout,Uext,FSMout] = deal([]);
% --- simulation ----
h_waitbar = waitbar(0,'Calculating...');
tic
for ii = 1:MAX_ITER
% --- time vector ---
t_ = dt_sim * (ii-1) + p.Tmpc * (0:p.predHorizon-1);
% --- FSM ---
if gait == 1
[FSM,Xd,Ud,Xt] = fcn_FSM_bound(t_,Xt,p);
else
[FSM,Xd,Ud,Xt] = fcn_FSM(t_,Xt,p);
end
% --- MPC ----
% form QP
[H,g,Aineq,bineq,Aeq,beq] = fcn_get_QP_form_eta(Xt,Ut,Xd,Ud,p);
if ~use_qpSWIFT
% solve QP using quadprog
[zval] = quadprog(H,g,Aineq,bineq,Aeq,beq,[],[]);
else
% interface with the QP solver qpSWIFT
end
Ut = Ut + zval(1:12);
% --- external disturbance ---
[u_ext,p_ext] = fcn_get_disturbance(tstart,p);
p.p_ext = p_ext; % position of external force
u_ext = 0*u_ext;
% --- simulate ---
[t,X] = ode45(@(t,X)dynamics_SRB(t,X,Ut,Xd,0*u_ext,p),[tstart,tend],Xt);
% --- update ---
Xt = X(end,:)';
tstart = tend;
tend = tstart + dt_sim;
% --- log ---
lent = length(t(2:end));
tout = [tout;t(2:end)];
Xout = [Xout;X(2:end,:)];
Uout = [Uout;repmat(Ut',[lent,1])];
Xdout = [Xdout;repmat(Xd(:,1)',[lent,1])];
Udout = [Udout;repmat(Ud(:,1)',[lent,1])];
Uext = [Uext;repmat(u_ext',[lent,1])];
FSMout = [FSMout;repmat(FSM',[lent,1])];
waitbar(ii/MAX_ITER,h_waitbar,'Calculating...');
end
close(h_waitbar)
fprintf('Calculation Complete!\n')
toc
%% Animation
[t,EA,EAd] = fig_animate(tout,Xout,Uout,Xdout,Udout,Uext,p);