-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance.cpp
325 lines (266 loc) · 8.69 KB
/
balance.cpp
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
/*
This file is part of quadruped_ctrl_ros - learning material for quadruped control
quadruped_ctrl_ros is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
quadruped_ctrl_ros is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with quadruped_ctrl_ros. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file ctrl_trot.cpp
* \date 25/10/2024
* \author pattylo
* \copyright (c) AIRO-LAB, RCUAS of Hong Kong Polytechnic University
* \brief classes for quadruped_ctrl_ros_uav using airo_control_interface
*/
#include "quadruped_ctrl_ros/ctrl_server.h"
void ctrl_server::balance_ctrl()
{
if (!balance_track_start)
{
set_balance_ctrl();
set_balance_ctrl_gain();
balance_track_start = true;
}
if (balance_fsm == "X")
posi_delta_B.x() = x_mag * sin(ctrl_param);
else if (balance_fsm == "Y")
posi_delta_B.y() = y_mag * sin(ctrl_param);
else if (balance_fsm == "Z")
posi_delta_B.z() = z_mag * sin(ctrl_param);
else
yaw_base = yaw_mag * sin(ctrl_param);
ctrl_param = ctrl_param + 2 * M_PI / 3.0 * 1 / ctrl_freq;
if (ctrl_param > 2 * M_PI)
{
ctrl_param = 0;
posi_delta_B.setZero();
if (balance_fsm == "X")
balance_fsm = "Y";
else if (balance_fsm == "Y")
balance_fsm = "Z";
else if (balance_fsm == "Z")
balance_fsm = "YAW";
else
balance_fsm = "X";
}
// in inertial frame
Eigen::Vector3d acc_p =
Kp_p * (
posi_base_I + pose_SE3_robot_base.rotationMatrix() * posi_delta_B -
pose_SE3_robot_base.translation()
)
+
Kd_p * (
Eigen::Vector3d::Zero()
-
// pose_SE3_robot_base.rotationMatrix() *
twist_robot_base.head(3)
);
Eigen::Matrix3d dR = rpy2q(Eigen::Vector3d(0.0,0.0,yaw_base)).normalized().toRotationMatrix() * pose_SE3_robot_base.rotationMatrix().inverse();
Eigen::JacobiSVD<Eigen::Matrix3d> svd(dR, Eigen::ComputeFullU | Eigen::ComputeFullV);
dR = svd.matrixU() * svd.matrixV().transpose();
if (dR.determinant() < 0)
dR = -dR;
Eigen::Vector3d acc_w = // Eigen::Vector3d::Zero();
Kp_w * rotMatToExp(dR)
+
Kd_w * (Eigen::Vector3d::Zero() - twist_robot_base.tail(3));
std::vector<Eigen::Vector3d> feet_posi_I;
for (int leg_i = 0; leg_i < leg_no; leg_i ++)
feet_posi_I.emplace_back(pose_SE3_robot_base.rotationMatrix() * get_foot_p_B(leg_i));
Sophus::Vector6d acc;
acc.head(3) = acc_p;
acc.tail(3) = acc_w;
f_now = (-1) * get_f(feet_posi_I, acc, contact_gait);
f_prev = f_now;
set_tau(f_now);
for(int i = 0; i < DoF; i++)
cmdSet.motorCmd[i].tau = balance_tau[i];
}
void ctrl_server::set_balance_ctrl()
{
x_mag = 0.04;
y_mag = 0.04;
z_mag = 0.04;
yaw_mag = 20 * M_PI / 180;
Kp_p = Eigen::Vector3d(150,150,150).asDiagonal();
Kd_p = Eigen::Vector3d(25, 25, 25).asDiagonal();
Kp_w = 200;
Kd_w = Eigen::Vector3d(30, 30, 30).asDiagonal();
posi_base_I = pose_SE3_robot_base.translation();
posi_delta_B.setZero();
yaw_base = q2rpy(pose_SE3_robot_base.unit_quaternion())(2);
m = 12.0;
mI = Eigen::Vector3d(0.0792, 0.2085, 0.2265).asDiagonal();
contact_gait.setConstant(1);
f_prev.resize(12);
f_prev.setZero();
Eigen::Matrix<double, 6, 1> s;
s << 20, 20, 50, 450, 450, 450;
S_w = s.asDiagonal();
Eigen::Matrix<double, 12, 1> w;
w << 10, 10, 4, 10, 10, 4, 10, 10, 4, 10, 10, 4;
W_w = w.asDiagonal();
Eigen::Matrix<double, 12, 1> u;
u << 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3;
U_w = u.asDiagonal();
alpha = 0.001;
beta = 0.1;
ctrl_param = 0;
balance_fsm = "X";
}
void ctrl_server::set_balance_ctrl_gain()
{
for(int leg_i = 0; leg_i < leg_no; leg_i++)
{
cmdSet.motorCmd[leg_i*3+0].mode = 10;
cmdSet.motorCmd[leg_i*3+0].dq = 0;
cmdSet.motorCmd[leg_i*3+0].Kp = 0.8;
cmdSet.motorCmd[leg_i*3+0].Kd = 0.8;
cmdSet.motorCmd[leg_i*3+1].mode = 10;
cmdSet.motorCmd[leg_i*3+1].dq = 0;
cmdSet.motorCmd[leg_i*3+1].Kp = 0.8;
cmdSet.motorCmd[leg_i*3+1].Kd = 0.8;
cmdSet.motorCmd[leg_i*3+2].mode = 10;
cmdSet.motorCmd[leg_i*3+2].dq = 0;
cmdSet.motorCmd[leg_i*3+2].Kp = 0.8;
cmdSet.motorCmd[leg_i*3+2].Kd = 0.8;
}
}
void ctrl_server::balance_ctrl_reset()
{
balance_track_start = false;
}
Eigen::VectorXd ctrl_server::get_f(
std::vector<Eigen::Vector3d> feet_posi,
Sophus::Vector6d acc,
Eigen::Vector4i contact_i
)
{
set_HMat(feet_posi);
set_fVec(acc);
set_constraints();
qp_opt(HMat, fVec, AMat, lbVec, ubVec);
// Q, g, A, lb, ub
return getQpsol();
}
void ctrl_server::set_HMat(
const std::vector<Eigen::Vector3d> feet_posi
)
{
// A
A_dyn.setZero();
A_dyn.block<3,3>(0,0).setIdentity();
A_dyn.block<3,3>(0,3).setIdentity();
A_dyn.block<3,3>(0,6).setIdentity();
A_dyn.block<3,3>(0,9).setIdentity();
A_dyn.block<3,3>(3,0) = Sophus::SO3d::hat(feet_posi[0]);
A_dyn.block<3,3>(3,3) = Sophus::SO3d::hat(feet_posi[1]);
A_dyn.block<3,3>(3,6) = Sophus::SO3d::hat(feet_posi[2]);
A_dyn.block<3,3>(3,9) = Sophus::SO3d::hat(feet_posi[3]);
// HMat
HMat = A_dyn.transpose() * S_w * A_dyn + alpha * W_w + beta * U_w;
}
void ctrl_server::set_fVec(const Sophus::Vector6d acc)
{
// b
Eigen::Matrix<double, 6, 1> b;
b.head(3) = m * (acc.head(3) - Eigen::Vector3d(0,0,-9.81));
b.tail(3) = pose_SE3_robot_base.rotationMatrix() * mI * pose_SE3_robot_base.rotationMatrix().inverse() * acc.tail(3);
// fVec
fVec = -1 * b.transpose() * S_w * A_dyn - f_prev.transpose() * beta * U_w;
}
void ctrl_server::set_constraints()
{
// std::cout<<"here in set constraints"<<std::endl;
// std::cout<<contact_gait<<std::endl<<std::endl;
int leg_no_in_air = 0;
std::vector<int> which_legs_in_air, which_legs_on_ground;
for (int leg_i = 0; leg_i < leg_no; leg_i++)
{
if(contact_gait(leg_i) == 0)
{
leg_no_in_air++;
which_legs_in_air.emplace_back(leg_i);
}
else
{
which_legs_on_ground.emplace_back(leg_i);
}
}
int leg_no_on_ground = (leg_no - leg_no_in_air);
int row_no = leg_no_on_ground * 5 + leg_no_in_air * 3;
int col_no = 12;
AMat.resize(row_no, col_no);
AMat.setZero();
ubVec.resize(row_no, 1);
ubVec.setZero();
int inf_size = (leg_no - leg_no_in_air) * 5;
ubVec.block(0, 0, inf_size, 1).setConstant(INFINITY);
lbVec.resize(row_no, 1);
lbVec.setZero();
Eigen::Matrix<double, 5, 3> oneblock;
oneblock <<
1, 0, mu,
-1, 0 ,mu,
0, 1, mu,
0, -1, mu,
0, 0, 1;
for (int i = 0; i < which_legs_on_ground.size(); i++)
{
AMat.block<5,3>(i*5,which_legs_on_ground[i]*3) = oneblock;
}
for (int i = 0; i < which_legs_in_air.size(); i++)
{
AMat.block<3,3>(leg_no_on_ground * 5 + i*3,which_legs_in_air[i]*3) = Eigen::Matrix3d::Identity();
}
// std::cout<<contact_gait<<std::endl;
// std::cout<<AMat<<std::endl<<std::endl;
// std::cout<<ubVec<<std::endl<<std::endl;;
// std::cout<<lbVec<<std::endl<<std::endl;;
}
void ctrl_server::set_tau(
Eigen::Matrix<double, 12, 1> f_I
)
{
for (int leg_i = 0; leg_i < leg_no; leg_i++)
{
balance_tau.segment(leg_i * 3, 3) =
get_Jacobian(leg_i).transpose() *
pose_SE3_robot_base.rotationMatrix().inverse() *
f_I.segment(leg_i * 3, 3);
}
}
Eigen::Vector3d ctrl_server::rotMatToExp(const Eigen::Matrix3d& rm)
{
double cosValue = rm.trace()/2.0-1/2.0;
if(cosValue > 1.0f)
{
cosValue = 1.0f;
}
else if(cosValue < -1.0f)
{
cosValue = -1.0f;
}
double angle = acos(cosValue);
Eigen::Vector3d exp;
if (fabs(angle) < 1e-5)
{
exp=Eigen::Vector3d(0,0,0);
}
else if (fabs(angle - M_PI) < 1e-5)
{
exp = angle * Eigen::Vector3d(rm(0,0)+1, rm(0,1), rm(0,2)) / sqrt(2*(1+rm(0, 0)));
}
else
{
exp=angle/(2.0f*sin(angle))*Eigen::Vector3d(rm(2,1)-rm(1,2),rm(0,2)-rm(2,0),rm(1,0)-rm(0,1));
}
return exp;
}