-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.cpp
More file actions
347 lines (297 loc) · 8.65 KB
/
Copy pathrobot.cpp
File metadata and controls
347 lines (297 loc) · 8.65 KB
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
#include "robot.hpp"
#include "utils.hpp"
#include "ros.hpp"
#include "config.hpp"
#include "link_attacher.hpp"
#include "controllers.hpp"
#include <ros/ros.h>
#include <cmath>
using namespace vector_field_controller;
constexpr double Robot::d1;
constexpr double Robot::a2;
constexpr double Robot::a3;
constexpr double Robot::d4;
constexpr double Robot::d5;
constexpr double Robot::d6;
/**
* @brief: converts an opening diameter to an actual joint configuration.
*/
VEC3 gripperOpeningToJointConfig(double d);
Joints::Joints(VEC6 q, VEC3 gripper){
this->update(q, gripper);
}
VEC6 Joints::q(){
VEC6 q;
q << this->shoulder_pan, this->shoulder_lift, this->elbow,
this->wrist_1, this->wrist_2, this->wrist_3;
return q;
}
VEC3 Joints::q_gripper(){
VEC3 gripper;
gripper << this->hand_1, this->hand_2, this->hand_3;
return gripper;
}
void Joints::update(){
VEC9 joints = readJoints();
this->shoulder_pan = joints[0];
this->shoulder_lift = joints[1];
this->elbow = joints[2];
this->wrist_1 = joints[3];
this->wrist_2 = joints[4];
this->wrist_3 = joints[5];
this->hand_1 = joints[6];
this->hand_2 = joints[7];
this->hand_3 = joints[8];
}
void Joints::update(VEC6 q){
this->shoulder_pan = q[0];
this->shoulder_lift = q[1];
this->elbow = q[2];
this->wrist_1 = q[3];
this->wrist_2 = q[4];
this->wrist_3 = q[5];
}
void Joints::update(VEC3 gripper){
this->hand_1 = gripper[0];
this->hand_2 = gripper[1];
this->hand_3 = gripper[2];
}
void Joints::update(VEC6 q, VEC3 gripper){
this->shoulder_pan = q[0];
this->shoulder_lift = q[1];
this->elbow = q[2];
this->wrist_1 = q[3];
this->wrist_2 = q[4];
this->wrist_3 = q[5];
this->hand_1 = gripper[0];
this->hand_2 = gripper[1];
this->hand_3 = gripper[2];
}
Robot::Robot(VEC6 q)
{
VEC3 gripper;
q_home << -0.32, -0.78, -2.56, -1.63, -1.57, 3.49;
#if SOFT_GRIPPER
gripper << 0.0, 0.0, 0.0;
#else
gripper << 1.8, 1.8, 1.8;
#endif
this->joints = Joints(q, gripper);
this->pose = SE3Operations::tau(this->forwardKinematics(q));
}
/**
* @brief: Transformation matrixes
* @param: theta - the joint angle
*/
SE3 Robot::T01(double theta1)
{
SE3 tmp;
tmp << cos(theta1), -sin(theta1), 0, 0,
sin(theta1), cos(theta1), 0, 0,
0, 0, 1, Robot::d1,
0, 0, 0, 1;
return tmp;
}
SE3 Robot::T12(double theta2)
{
SE3 tmp;
tmp << cos(theta2), -sin(theta2), 0, 0,
0, 0, -1, 0,
sin(theta2), cos(theta2), 0, 0,
0, 0, 0, 1;
return tmp;
}
SE3 Robot::T23(double theta3)
{
SE3 tmp;
tmp << cos(theta3), -sin(theta3), 0, Robot::a2,
sin(theta3), cos(theta3), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;
return tmp;
}
SE3 Robot::T34(double theta4)
{
SE3 tmp;
tmp << cos(theta4), -sin(theta4), 0, Robot::a3,
sin(theta4), cos(theta4), 0, 0,
0, 0, 1, Robot::d4,
0, 0, 0, 1;
return tmp;
}
SE3 Robot::T45(double theta5)
{
SE3 tmp;
tmp << cos(theta5), -sin(theta5), 0, 0,
0, 0, -1, -Robot::d5,
sin(theta5), cos(theta5), 0, 0,
0, 0, 0, 1;
return tmp;
}
SE3 Robot::T56(double theta6)
{
SE3 tmp;
tmp << cos(theta6), -sin(theta6), 0, 0,
0, 0, 1, Robot::d6,
-sin(theta6), -cos(theta6), 0, 0,
0, 0, 0, 1;
return tmp;
}
/**
* @brief: Forward kinematics
* @param: q - the joint state vector
*/
SE3 Robot::forwardKinematics(VEC6 &q)
{
// Transformation matrixes
SE3 t01 = T01(q(0, 0));
SE3 t12 = T12(q(1, 0));
SE3 t23 = T23(q(2, 0));
SE3 t34 = T34(q(3, 0));
SE3 t45 = T45(q(4, 0));
SE3 t56 = T56(q(5, 0));
// Transformation matrixes from the base frame to the end-effector
SE3 t06 = t01 * t12 * t23 * t34 * t45 * t56;
return t06;
}
/**
* @brief: Jacobian of the end-effector transformation with respect to
* the current joints coordinates.
*/
MAT6 Robot::jacobian(VEC6 q)
{
Eigen::Matrix<double, 6, 6> J;
double s1 = sin(q(0, 0));
double c1 = cos(q(0, 0));
double c2 = cos(q(1, 0));
double s2 = sin(q(1, 0));
double s5 = sin(q(4, 0));
double c5 = cos(q(4, 0));
double s234 = sin(q(1, 0) + q(2, 0) + q(3, 0));
double c23 = cos(q(1, 0) + q(2, 0));
double s23 = sin(q(1, 0) + q(2, 0));
double c234 = cos(q(1, 0) + q(2, 0) + q(3, 0));
double r13 = -c1 * c234 * s5 + c5 * s1;
double r23 = -s1 * c234 * s5 - c1 * c5;
double r33 = -s5 * s234;
double px = r13 * d6 + c1 * (s234 * d5 + c23 * a3 + c2 * a2) + s1 * d4;
double py = r23 * d6 + s1 * (s234 * d5 + c23 * a3 + c2 * a2) - c1 * d4;
double pz = r33 * d6 - c234 * d5 + s23 * a3 + s2 * a2 + d1;
J << -py, -c1 * (pz - d1), c1 * (s234 * s5 * d6 + c234 * d5 - s23 * a3), c1 * (s234 * s5 * d6 + c234 * d5), -d6 * (s1 * s5 + c1 * c234 * c5), 0,
px, -s1 * (pz - d1), s1 * (s234 * s5 * d6 + c234 * d5 - s23 * a3), s1 * (s234 * s5 * d6 + c234 * d5), d6 * (c1 * s5 - c234 * c5 * s1), 0,
0, s1 * py + c1 * px, -c234 * s5 * d6 + s234 * d5 + c23 * a3, -c234 * s5 * d6 + s234 * d5, -c5 * s234 * d6, 0,
0, s1, s1, s1, c1 * s234, r13,
0, -c1, -c1, -c1, s1 * s234, r23,
1, 0, 0, 0, -c234, r33;
return J;
}
/**
* @brief Inverse kinematics implementation based on the LM algorithm.
*/
VEC6 Robot::inverseKinematics(SE3 &T_des)
{
int i = 0;
VEC6 q_k = this->joints.q();
while (i < LM::maxIterations)
{
// Current pose of the end-effector
SE3 T_k = forwardKinematics(q_k);
// Error vector
VEC6 e = LM::error(T_k, T_des);
// Error scalar
double e_norm = e.norm();
double E = e_norm * e_norm * 0.5;
if (E < LM::errTresh)
{
break;
}
Eigen::Matrix<double, 6, 6> Jk = jacobian(q_k);
Eigen::Matrix<double, 6, 6> Ak = LM::Ak(Jk, E);
VEC6 gk = LM::gk(Jk, e);
// Update the joint state vector
q_k = q_k + Ak.inverse() * gk;
i++;
}
normalize(q_k);
return q_k;
}
void Robot::move(SE3 &T_des){
ROS_INFO_STREAM("START: planar motion");
ros::Rate loop_rate(LOOP_FREQUENCY);
vectorFieldController(*this, T_des);
loop_rate.sleep();
this->joints.update();
ROS_INFO_STREAM("FINISH: planar motion");
}
void Robot::descent(SE3 &T_des, bool pick, ros::ServiceClient &gripperClient){
ros::Rate loop_rate(LOOP_FREQUENCY);
VEC6 q0 = this->joints.q();
VEC3 q_gripper = this->joints.q_gripper();
VEC6 q_des;
ROS_INFO_STREAM("START: descent");
SE3 T_curr = this->forwardKinematics(q0);
SE3 T_des1 = T_des;
T_des1(0, 3) = T_curr(0, 3);
T_des1(1, 3) = T_curr(1, 3);
T_des1(2, 3) = T_curr(2, 3);
q_des = this->inverseKinematics(T_des1);
velocityController(*this, DT, VELOCITY, q_des, false);
q_des = this->inverseKinematics(T_des);
velocityController(*this, DT, VELOCITY, q_des, false);
ROS_INFO_STREAM("FINISH: descent");
if(pick)
this->moveGripper(gripperClient, GRIPPER_OPENING, 10, 0.1);
else
this->moveGripper(gripperClient, 180, 10, 0.1);
ros::Duration(1).sleep();
ROS_INFO_STREAM("START: ascent");
velocityController(*this, DT, VELOCITY, q0, true);
ROS_INFO_STREAM("FINISH: ascent");
}
#if SIMULATION
void Robot::moveGripper(ros::ServiceClient &gripperClient, double d, int N, double dt) {
// gripper service is not used here
VEC3 q_des = gripperOpeningToJointConfig(d);
VEC3 q_gripper = this->joints.q_gripper();
VEC6 q = this->joints.q();
VEC3 incr = (q_des - q_gripper) / N;
ros::Rate loop_rate(1/dt);
VEC3 q_gripper_k = q_gripper + incr;
for (int i=0; i<N; ++i) {
publishJoints(pub_jstate, q, q_gripper_k);
q_gripper = q_gripper_k;
q_gripper_k += incr;
loop_rate.sleep();
}
this->joints.update();
}
#else
void Robot::moveGripper(ros::ServiceClient &gripperClient, double d, int N, double dt) {
// N and dt are not used in the real robot, gripper service is used instead
ros_impedance_controller::generic_float gripper_srv;
gripper_srv.request.data = d;
if (!gripperClient.call(gripper_srv) || !gripper_srv.response.ack) {
ROS_INFO_STREAM("Gripper service call failed");
exit(0);
}
}
#endif
#if SOFT_GRIPPER
VEC3 gripperOpeningToJointConfig(double d)
{
double D0 = 40;
double L = 60;
double opening = atan2(0.5*(d - D0), L);
VEC3 q;
q << opening, opening, 0;
return q;
}
#else
VEC3 gripperOpeningToJointConfig(double d)
{
double opening = (d - 22) / 108 * -M_PI + M_PI;
VEC3 q;
q << opening, opening, opening;
return q;
}
#endif