Endorob  1.4
A C++ Robotics library by P. Berthet-Rayne
main.cpp
Go to the documentation of this file.
1 //#include <QCoreApplication>
2 #include "endorob.h"
3 #include <iostream>
4 #include <Eigen/StdVector>
5 
6 
7 #ifndef M_PI
8 #define M_PI 3.1415926535897932384626433832795
9 #endif
10 
11 
12 int main(int argc, char *argv[])
13 {
14 // QCoreApplication app(argc, argv);
15 
16 
17  Endorob truc[12];
18 
19 
20 
21 
22  for(auto i = 0; i < 12; i++)
23  {
24  truc[i].add_DH_entry(0,0,0,0,Joint_type::Revolute);
25  truc[i].initialize();
26  }
27 
28  Mat4d Tkuka_global = Mat4d::Identity(4,4);
29  Tkuka_global << -1, 0, 0, 420,
30  0, 1, 0, 0,
31  0, 0, -1, 570,
32  0, 0, 0, 1;
33  std::cout << "T kuka global:\n" << Tkuka_global << std::endl << std::endl;
34 
35  Mat4d Tkuka_local = Mat4d::Identity(4,4);
36  std::cout << "T kuka local:\n" << Tkuka_local << std::endl << std::endl;
37 
38  Mat4d Tbowl_local = Mat4d::Identity(4,4);
39  Tbowl_local.block<3,3>(0,0) = rot_y(30*M_PI/180);
40  Tbowl_local.block<3,1>(0,3) << 25.10, 0, 100.73;
41  std::cout << "Tbowl local:\n" << Tbowl_local << std::endl << std::endl;
42 
43  Mat4d Tsnake_local = Mat4d::Identity(4,4);
44  Tsnake_local.block<3,1>(0,3) << 0, 0, 0;
45  Tsnake_local.block<3,3>(0,0) = rot_z(1*M_PI/180);
46  std::cout << "Tsnake:\n" << Tsnake_local << std::endl << std::endl;
47 
48 
49  Mat4d Tbowl_global = Tkuka_global * Tbowl_local ;
50  std::cout << "bowl global:\n" << Tbowl_global << std::endl << std::endl;
51 
52  Mat4d goback = Tbowl_global * invert_transform( Tbowl_local );
53  std::cout << "back to kuka:\n" << goback << std::endl << std::endl;
54 
55  Mat4d bowl_motion = Mat4d::Identity(4,4);
56  bowl_motion.block<3,1>(0,3) << 0, 0, 0;
57  bowl_motion.block<3,3>(0,0) = rot_z(1*M_PI/180);
58 
59  goback = Tbowl_global * invert_transform( Tbowl_local * bowl_motion );
60  std::cout << "kuka motion:\n" << goback << std::endl << std::endl;
61  std::cout << "kuka motion:\n" << goback << std::endl << std::endl;
62 
63 
64 
65 
66 
67 
68 
69 
70 // Mat4d kukamotion = Mat4d::Identity(4,4);
71 // kukamotion.block<3,1>(0,3) = rot_y(-30*M_PI/180) * snakemotion.block<3,1>(0,3) + Tkuka.block<3,1>(0,3);//R * vec + T;
72 // kukamotion.block<3,3>(0,0) = snakemotion.block<3,3>(0,0) * Tkuka.block<3,3>(0,0);
73 // std::cout << "transformedinkukaframe:\n" << kukamotion << std::endl << std::endl;
74 
75 
76 
77 // Mat4d kukamotionbis = Mat4d::Identity(4,4);
78 // Mat4d ttt = get_transform_increment(start_point_in_bowl_frame,goal_point_in_bowl_frame);
79 
80 
81 // kukamotionbis = Tbowl_to_kuka * ttt;
82 // std::cout << "kukamotionbis:\n" << kukamotionbis << std::endl << std::endl;
83 
84 // kukamotionbis = invert_transform(Tkuka_to_bowl) * ttt;
85 // std::cout << "kukamotionbis:\n" << kukamotionbis << std::endl << std::endl;
86 
87 
88  Mat4d tmpp = Mat4d::Identity(4,4);
89  tmpp.block<3,1>(0,3) << 12,23,56;
90  tmpp.block<3,3>(0,0) << rot_z(M_PI);
91 
92  Mat4d start = Mat4d::Identity(4,4), goal = Mat4d::Identity(4,4), tmp = Mat4d::Identity(4,4);
93  start.block<3,1>(0,3) << -13,9,23;
94  start.block<3,3>(0,0) << rot_y(M_PI/2);
95 
96  tmp = tmpp * start;
97 
98  goal = invert_transform(tmpp) * tmp;
99 
100  std::cout << start << std::endl << std::endl;
101  std::cout << tmp << std::endl << std::endl;
102  std::cout << goal << std::endl << std::endl;
103 
104  std::cout << tmpp << std::endl << std::endl;
105  std::cout << invert_transform(tmpp) << std::endl << std::endl;
106 
107  std::cout << std::endl;
108 
109 
110  // Endorob machin;
111 
112  // //machin.set_DH_convention(DH_type::DH_modified);
113  // machin.set_DH_convention(DH_type::DH_standard);
114  // double a = 0.00, l = 0.05, a_end = 0.03, a_bis = 0.011;
115 
116  // machin.add_DH_entry(0,M_PI/2,0,0, Joint_type::Prismatic);
117  // machin.add_DH_entry(0,M_PI/2,0,0, Joint_type::Revolute);
118  // machin.add_DH_entry(0,M_PI/2,0.1,0, Joint_type::Revolute);
119  // machin.add_DH_entry(0,M_PI/2,0,0, Joint_type::Prismatic);
120  // machin.add_DH_entry(0,M_PI/2,0.3,0, Joint_type::Revolute);
133 
134  // // Set solver type
135  // machin.set_solver_type(Solver_type::Jacobian_DLS);
136 
137 
138  // // Set joint coupling matrix
139  // //Vec coup(14);
140  // //coup << 0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7;
141 
142  // // machin.set_joint_coupling(coup);
143 
144  // // Start solver
145  // // DO NOT FORGET THIS LINE!!!
146  // machin.initialize();
147  // Vec qv(5);
148  // qv << 0.8, -0.9, -0.1, 0.4, 0.9;
149  // machin.set_q_vec(qv);
150 
151  // Mat full = machin.get_full_kinematics();
152  // std::cout << full << std::endl << std::endl;
153 
154  // Mat result = machin.get_forward_kinematics();
155  // std::cout << result << std::endl << std::endl;
156 
157  // Mat j = machin.get_Jacobian();
158  // std::cout << j << std::endl << std::endl;
159 
160 
161  // //machin.set_solver_weight(0,5);
162  // //Motion_RW truc(&machin);
163  // //truc.set_write_frequency(250);
164  // //truc.start_recording();
165  // //truc.set_read_frequency(1000);
166  // //truc.load_file("output_data.csv");
167 
168  // Vec tq = machin.get_q_vec();
169  // std::cout << tq << std::endl << std::endl;
170 
171  // // get random vector
172  // Vec qr = machin.get_random_q_vec();
173  // std::cout << qr;
174 
175 
176 
177 
178  // std::vector<Mat4d, Eigen::aligned_allocator<Mat4d>>fwdK;
179  // //fwdK = machin.calculate_full_forward_kinematics();
180 
181 
182 
183  // Mat4d Te, Td,Ttest, Tcheck;
184  // Vec q;
185 
186  // Tcheck << 0,-1, 0,0,
187  // 0, 0,-1,0,
188  // 1, 0, 0,0.08,
189  // 0, 0, 0,1;
190  // q.resize(machin.dof());
191  // q.resize(machin.joints());
192  // q = Mat::Zero(q.rows(),1);
193 
202 
205 
207 
210 
213 
218 
221 
222 
224 
227 
230 
235 
238 
242 
244 
247 
250 
251  // q.resize(machin.dof());
252  // q = machin.get_random_q_vec();
253  // std::cout << "\n\nrandom q:\n"<< q ;
254 
255  // machin.set_q_vec(q);
256  // Td = machin.get_tip_pose();
257  // std::cout << "\n\ncorresponding tip pose:\n"<< Td ;
258 
259  // q.resize(machin.dof());
260  // q = Mat::Zero(q.rows(),1);
261 
262  // machin.set_q_vec(q);
263  // //std::cout << "\n\nnew q:\n"<< q ;
264  // Ttest = machin.get_tip_pose();
265  // std::cout << "\n\ncorresponding tip pose:\n"<< Ttest ;
266 
267  // machin.set_solver_target(Td);
268 
269  // q = machin.get_IK_solution();
270  // std::cout << "\n\n solution q:\n"<< q ;
271 
272  // Td = machin.get_tip_pose();
273  // std::cout << "\n\ncorresponding tip pose:\n"<< Td ;
274 
275 
276 
277 
278 
279  // //q = machin.get_inversekinematics(Te);
280 
281  // // // Mask debug
282  // // Vec mask(6);
283  // // mask << 1, 1, 1, 0, 0, 0;
284  // // machin.set_mask(mask);
285 
286 
287  // //machin.set_solver_target(Td);
288  // // std::cout << "Asking with mask...\n";
289  // // q = machin.get_IK_solution();
290  // // std::cout << "Got it with mask...\n";
291  // // std::cout << q;
292 
297 
298 
304 
305 
306 
307  // // std::cout << "Asking final...\n";
308 
309 
312 
313 
314 
316 
317 
318 
319 
320 
321 
322  // Create IK
323  Endorob inverse_kinematics; // todo: Endorob as _p
324  inverse_kinematics.set_solver_type(Solver_type::Jacobian_DLS);
325  inverse_kinematics.add_DH_entry(0, M_PI / 2, 0, M_PI / 2, Joint_type::Revolute);
326  inverse_kinematics.set_joint_limits(-0.8, 0.8);
327  inverse_kinematics.add_DH_entry(3.25, M_PI / 2, 0, 0, Joint_type::Revolute);
328  inverse_kinematics.set_joint_limits(-0.8, 0.8);
329  inverse_kinematics.add_DH_entry(8.75, 0, 0, 0, Joint_type::Revolute);
330  inverse_kinematics.set_joint_limits(-0.8, 0.8);
331  inverse_kinematics.add_DH_entry(6.75, 0, 0, 0, Joint_type::Revolute);
332  inverse_kinematics.set_joint_limits(-0.8, 0.8);
333  inverse_kinematics.add_DH_entry(3.25, 0, 0, 0, Joint_type::Revolute);
334  inverse_kinematics.set_joint_limits(-0.8, 0.8);
335  inverse_kinematics.add_DH_entry(4.42, 0, 0, 0, Joint_type::Revolute);
336  inverse_kinematics.set_joint_limits(-0.8, 0.8);
337  inverse_kinematics.add_DH_entry(0, M_PI / 2, 0, M_PI / 2, Joint_type::Revolute);
338  inverse_kinematics.set_joint_limits(-0.8, 0.8);
339  inverse_kinematics.add_DH_entry(0, M_PI / 2, 0, 0, Joint_type::Revolute);
340  inverse_kinematics.set_joint_limits(-0.8, 0.8);
341  Mat4d tool;
342  tool << 1, 0, 0, 0,
343  0, 1, 0, 0,
344  0, 0, 1, 10.95,
345  0, 0, 0, 1;
346  inverse_kinematics.set_tool_frame(tool);
347  inverse_kinematics.initialize();
348 
349  // Get Random target point
350  Vec q = inverse_kinematics.get_random_q_vec();
351  Mat4d Tt = inverse_kinematics.get_forward_kinematics(q);
352  inverse_kinematics.set_q_vec(q);
353  Eigen::Matrix4d target = inverse_kinematics.get_tip_pose();
354  std::cout << "Wanted result qvec:" << std::endl << std::endl << inverse_kinematics.get_q_vec() << std::endl << std::endl;
355  std::cout << "q:" << std::endl << std::endl << q << std::endl << std::endl;
356 
357 
358  inverse_kinematics.reset_q_vec();
359  inverse_kinematics.set_solver_target(target);
360  //Sleep(2000);
361  //inverse_kinematics.get_instant_IK_solution();
362  inverse_kinematics.get_IK_solution();
363  std::cout << "Target:" << std::endl << std::endl << target << std::endl << std::endl;
364  std::cout << "Tt:" << std::endl << std::endl << Tt << std::endl << std::endl;
365  std::cout << "Result:" << std::endl << std::endl << inverse_kinematics.get_tip_pose() << std::endl << std::endl;
366  std::cout << "qvec:" << std::endl << std::endl << inverse_kinematics.get_q_vec() << std::endl << std::endl;
367  std::cout << "Error:" << std::endl << std::endl << inverse_kinematics.get_error_norm() << std::endl << std::endl;
368 
369  std::cout << "Continue with ENTER" << std::endl;
370  //std::cin.get();
371 
372 
373 // Endorob *machin;
374 // machin = new Endorob;
375 // machin->add_DH_entry(0, M_PI / 2, 0, M_PI / 2, Joint_type::Revolute);
376 // machin->set_joint_limits(-0.8, 0.8);
377 // machin->add_DH_entry(3.25, M_PI / 2, 0, 0, Joint_type::Revolute);
378 // machin->set_joint_limits(-0.8, 0.8);
379 // machin->add_DH_entry(8.75, 0, 0, 0, Joint_type::Revolute);
380 // machin->set_joint_limits(-0.8, 0.8);
381 // machin->initialize();
382 // machin->set_solver_target(target);
383 // machin->get_IK_solution();
384 // delete machin;
385 
386 
387 
388  std::this_thread::sleep_for(std::chrono::seconds(10));
389 
390 
391  return 1;
392 }
Vec
Eigen::VectorXd Vec
Definition: endorob.h:165
Endorob::add_DH_entry
void add_DH_entry(const double &a, const double &alpha, const double &d, const double &theta, const Joint_type &type, const double &a_bis=0, const double &theta_bis=0, const uint &flex=0)
Setup robot DH table one entry at a time (Attention, modified DH convention by default)
Definition: endorob.cpp:283
Endorob::get_forward_kinematics
Mat4d get_forward_kinematics()
Get the tip pose / homogeneous matrix (forward kinematics)
Definition: endorob.cpp:561
endorob.h
M_PI
#define M_PI
Definition: endorob.h:131
Endorob::reset_q_vec
void reset_q_vec()
Reset robot to startup state.
Definition: endorob.cpp:1927
Endorob::get_IK_solution
Vec get_IK_solution()
Request for solver's converged solution, whether target is reached or not.
Definition: endorob.cpp:1890
Endorob
This is the main Class: EndoRob that you can use to control your own weird looking robot....
Definition: endorob.h:420
Endorob::set_solver_type
void set_solver_type(Solver_type solver, const double &lambda=1)
Set iterative solver type.
Definition: endorob.cpp:342
Endorob::rot_y
Mat3d rot_y(const double &angle)
Get the desired rotation matrix around the Y axis rotated by a desired angle:
Definition: endorob.cpp:1994
Endorob::set_q_vec
void set_q_vec(const Vec &q)
Set robot's joint vector.
Definition: endorob.cpp:1906
Endorob::initialize
void initialize()
Once all parameters set, initialize robot and start solver.
Definition: endorob.cpp:368
Endorob::get_random_q_vec
Vec get_random_q_vec()
Get random feasible joint vector.
Definition: endorob.cpp:1944
Endorob::set_joint_limits
void set_joint_limits(const double &min, const double &max)
Set joint limits for current joint, radians for revolute or rolling joints, meters for prismatic.
Definition: endorob.cpp:320
Endorob::set_solver_target
void set_solver_target(const Mat4d &T)
Set solver's target 4x4 homegenous matrix.
Definition: endorob.cpp:2159
Endorob::get_q_vec
Vec get_q_vec()
Get joint vector.
Definition: endorob.cpp:1861
Endorob::invert_transform
Mat4d invert_transform(const Mat4d &mat)
Invert homogeneous matrix for opposite transformation.
Definition: endorob.cpp:4832
Mat4d
Eigen::Matrix4d Mat4d
Definition: endorob.h:161
Endorob::get_error_norm
double get_error_norm()
Get the error norm between the desired and current position.
Definition: endorob.cpp:2119
Solver_type::Jacobian_DLS
Damped least square approach.
Endorob::get_tip_pose
Mat4d get_tip_pose()
Get the tip pose / homogeneous matrix (forward kinematics)
Definition: endorob.cpp:952
Endorob::set_tool_frame
void set_tool_frame(const Mat4d &T)
Set tool frame.
Definition: endorob.cpp:665
Endorob::rot_z
Mat3d rot_z(const double &angle)
Get the desired rotation matrix around the Z axis rotated by a desired angle:
Definition: endorob.cpp:1999
main
int main(int argc, char *argv[])
Definition: main.cpp:12
Joint_type::Revolute
Standard revolute joint.