walking_control.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2017 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Author: SCH */
18 
19 #include <stdio.h>
21 
22 WalkingControl::WalkingControl(double control_cycle,
23  double dsp_ratio, double lipm_height, double foot_height_max, double zmp_offset_x, double zmp_offset_y,
24  std::vector<double_t> x_lipm, std::vector<double_t> y_lipm,
25  double foot_distance)
26  : walking_leg_(LEG_COUNT),
27  walking_phase_(PHASE_COUNT)
28 {
29  control_cycle_ = control_cycle;
30 
31  // Trajectory
32  init_time_ = 0.0;
33  fin_time_ = 1.0;
34 
35  // Foot Paramater
36  foot_step_size_ = 0;
37 
38  foot_size_x_ = 0.127;
39  foot_size_y_ = 0.0125;
41  foot_origin_shift_y_ = foot_distance; //0.09; //0.07;
42 
43  // Foot Trajectory Parameter
44  dsp_ratio_ = dsp_ratio; // default:
45  foot_tra_max_z_ = foot_height_max; // default:
46 
47  // Preview Control Parameter
48  preview_time_ = 1.6;
49  lipm_height_ = lipm_height; // default:
51 
52  // ZMP Offset Parameter
53  zmp_offset_x_ = zmp_offset_x; // default :
54  zmp_offset_y_ = zmp_offset_y; // default :
55 
56  // Initialization
57  init_body_pos_.resize(3, 0.0);
58  init_body_vel_.resize(3, 0.0);
59  init_body_accel_.resize(3, 0.0);
60  des_body_pos_.resize(3, 0.0);
61  des_body_vel_.resize(3, 0.0);
62  des_body_accel_.resize(3, 0.0);
63  goal_body_pos_.resize(3, 0.0);
64  goal_body_vel_.resize(3, 0.0);
65  goal_body_accel_.resize(3, 0.0);
66 
67  init_l_foot_pos_.resize(3, 0.0);
68  init_l_foot_vel_.resize(3, 0.0);
69  init_l_foot_accel_.resize(3, 0.0);
70  des_l_foot_pos_.resize(3, 0.0);
71  des_l_foot_vel_.resize(3, 0.0);
72  des_l_foot_accel_.resize(3, 0.0);
73  goal_l_foot_pos_.resize(3, 0.0);
74  goal_l_foot_vel_.resize(3, 0.0);
75  goal_l_foot_accel_.resize(3, 0.0);
76 
77  init_r_foot_pos_.resize(3, 0.0);
78  init_r_foot_vel_.resize(3, 0.0);
79  init_r_foot_accel_.resize(3, 0.0);
80  des_r_foot_pos_.resize(3, 0.0);
81  des_r_foot_vel_.resize(3, 0.0);
82  des_r_foot_accel_.resize(3, 0.0);
83  goal_r_foot_pos_.resize(3, 0.0);
84  goal_r_foot_vel_.resize(3, 0.0);
85  goal_r_foot_accel_.resize(3, 0.0);
86 
88 
89 // robot_ = new thormang3::KinematicsDynamics(thormang3::WholeBody);
90 
91  x_lipm_.resize(3,1);
92  y_lipm_.resize(3,1);
93 
94  for (int i=0; i<3; i++)
95  {
96  x_lipm_.coeffRef(i,0) = x_lipm[i];
97  y_lipm_.coeffRef(i,0) = y_lipm[i];
98  }
99 
100  preview_sum_zmp_x_ = 0.0;
101  preview_sum_zmp_y_ = 0.0;
102 
103 // ROS_INFO("x_lipm: %f", x_lipm[0]);
104 // ROS_INFO("y_lipm: %f", y_lipm[0]);
105 }
106 
108 {
109 
110 }
111 
112 void WalkingControl::initialize(op3_online_walking_module_msgs::FootStepCommand foot_step_command,
113  std::vector<double_t> init_body_pos, std::vector<double_t> init_body_Q,
114  std::vector<double_t> init_r_foot_pos, std::vector<double_t> init_r_foot_Q,
115  std::vector<double_t> init_l_foot_pos, std::vector<double_t> init_l_foot_Q)
116 {
117  init_body_pos_ = init_body_pos;
118  des_body_pos_ = init_body_pos;
119 
120  Eigen::Quaterniond body_Q(init_body_Q[3],init_body_Q[0],init_body_Q[1],init_body_Q[2]);
121  init_body_Q_ = body_Q;
122  des_body_Q_ = body_Q;
123 
124  Eigen::MatrixXd init_body_rpy = robotis_framework::convertQuaternionToRPY(init_body_Q_);
125  init_body_yaw_angle_ = init_body_rpy.coeff(2,0);
126 
127  init_r_foot_pos_ = init_r_foot_pos;
128  init_l_foot_pos_ = init_l_foot_pos;
129 
132 
133  Eigen::Quaterniond l_foot_Q(init_l_foot_Q[3],init_l_foot_Q[0],init_l_foot_Q[1],init_l_foot_Q[2]);
134  init_l_foot_Q_ = l_foot_Q;
135  des_l_foot_Q_ = l_foot_Q;
136 
137  Eigen::Quaterniond r_foot_Q(init_r_foot_Q[3],init_r_foot_Q[0],init_r_foot_Q[1],init_r_foot_Q[2]);
138  init_r_foot_Q_ = r_foot_Q;
139  des_r_foot_Q_ = r_foot_Q;
140 
141  // Calculation Foot Step
142  foot_step_command_ = foot_step_command;
144 
145  sum_of_zmp_x_ = 0.0;
146  sum_of_zmp_y_ = 0.0;
147  sum_of_cx_ = 0.0;
148  sum_of_cy_ = 0.0;
149 
150  u_x_.resize(1,1);
151  u_y_.resize(1,1);
152  u_x_.fill(0.0);
153  u_y_.fill(0.0);
154 }
155 
156 void WalkingControl::initialize(op3_online_walking_module_msgs::Step2DArray foot_step_2d,
157  std::vector<double_t> init_body_pos, std::vector<double_t> init_body_Q,
158  std::vector<double_t> init_r_foot_pos, std::vector<double_t> init_r_foot_Q,
159  std::vector<double_t> init_l_foot_pos, std::vector<double_t> init_l_foot_Q)
160 {
161  init_body_pos_ = init_body_pos;
162  des_body_pos_ = init_body_pos;
163 
164  Eigen::Quaterniond body_Q(init_body_Q[3],init_body_Q[0],init_body_Q[1],init_body_Q[2]);
165  init_body_Q_ = body_Q;
166  des_body_Q_ = body_Q;
167 
168  Eigen::MatrixXd init_body_rpy = robotis_framework::convertQuaternionToRPY(init_body_Q_);
169  init_body_yaw_angle_ = init_body_rpy.coeff(2,0);
170 
171  init_r_foot_pos_ = init_r_foot_pos;
172  init_l_foot_pos_ = init_l_foot_pos;
173 
176 
177  Eigen::Quaterniond l_foot_Q(init_l_foot_Q[3],init_l_foot_Q[0],init_l_foot_Q[1],init_l_foot_Q[2]);
178  init_l_foot_Q_ = l_foot_Q;
179  des_l_foot_Q_ = l_foot_Q;
180 
181  Eigen::Quaterniond r_foot_Q(init_r_foot_Q[3],init_r_foot_Q[0],init_r_foot_Q[1],init_r_foot_Q[2]);
182  init_r_foot_Q_ = r_foot_Q;
183  des_r_foot_Q_ = r_foot_Q;
184 
185  // Calculation Foot Step
186  foot_step_2d_ = foot_step_2d;
188 
189  sum_of_zmp_x_ = 0.0;
190  sum_of_zmp_y_ = 0.0;
191  sum_of_cx_ = 0.0;
192  sum_of_cy_ = 0.0;
193 
194  u_x_.resize(1,1);
195  u_y_.resize(1,1);
196  u_x_.fill(0.0);
197  u_y_.fill(0.0);
198 }
199 
201 {
205 
209 }
210 
212 {
213  return lipm_height_;
214 }
215 
217 {
218 
219 }
220 
221 void WalkingControl::set(double time, int step, bool foot_step_2d)
222 {
223  if (time == 0.0)
224  calcFootTrajectory(step);
225 
226  calcFootStepPose(time,step);
227  calcRefZMP(step);
228  calcPreviewControl(time,step);
229 
230 // if (walking_leg_ == LEFT_LEG)
231 // ROS_INFO("walking_leg_ : LEFT");
232 // else if (walking_leg_ == RIGHT_LEG)
233 // ROS_INFO("walking_leg_ : RIGHT");
234 
235  /* ----- Inverse Kinematics ---- */
236  double dsp_length = 0.5*(fin_time_ - init_time_)*dsp_ratio_;
237 
238  double init_time = init_time_ + dsp_length;
239  double fin_time = fin_time_ - dsp_length;
240 
241  int max_iter = 30;
242  double ik_tol = 1e-4;
243 
244  bool ik_success = false;
245 
246  // body
247  if (time < init_time)
248  {
251  }
252  else if (time > fin_time)
253  {
256  }
257  else
258  {
259  if (step == 0 ||
260  step == 1 ||
261  step == foot_step_size_ -1)
263  else
265 
266  double count = (time - init_time) / fin_time;
267  des_body_Q_ = init_body_Q_.slerp(count, goal_body_Q_);
268  }
269 
270 // if (walking_phase_ == DSP)
271 // ROS_INFO("walking_phase_ : DSP");
272 // else if (walking_phase_ == SSP)
273 // ROS_INFO("walking_phase_ : SSP");
274 
275  // right foot
276  Eigen::MatrixXd des_r_foot_pos = Eigen::MatrixXd::Zero(3,1);
277  des_r_foot_pos.coeffRef(0,0) = des_r_foot_pos_[0];
278  des_r_foot_pos.coeffRef(1,0) = des_r_foot_pos_[1];
279  des_r_foot_pos.coeffRef(2,0) = des_r_foot_pos_[2];
280 
281  if (time < init_time)
283  else if (time > fin_time)
285  else
286  {
287  double count = (time - init_time) / fin_time;
289  }
290 
291  // left foot
292  Eigen::MatrixXd des_l_foot_pos = Eigen::MatrixXd::Zero(3,1);
293  des_l_foot_pos.coeffRef(0,0) = des_l_foot_pos_[0];
294  des_l_foot_pos.coeffRef(1,0) = des_l_foot_pos_[1];
295  des_l_foot_pos.coeffRef(2,0) = des_l_foot_pos_[2];
296 
297  if (time < init_time)
299  else if (time > fin_time)
301  else
302  {
303  double count = (time - init_time) / fin_time;
305  }
306 }
307 
309 {
310  fin_time_ = foot_step_command_.step_time;
312 
313  int walking_start_leg;
314  if (foot_step_command_.start_leg == "left_leg")
315  walking_start_leg = LEFT_LEG;
316  else if (foot_step_command_.start_leg == "right_leg")
317  walking_start_leg = RIGHT_LEG;
318 
319  if (foot_step_command_.command == "right")
320  walking_start_leg = RIGHT_LEG;
321  else if (foot_step_command_.command == "left")
322  walking_start_leg = LEFT_LEG;
323  else if (foot_step_command_.command == "turn_right")
324  walking_start_leg = RIGHT_LEG;
325  else if (foot_step_command_.command == "turn_left")
326  walking_start_leg = LEFT_LEG;
327 
328  int walking_leg = walking_start_leg;
329  double foot_angle = init_body_yaw_angle_;
330 
331  for (int i=0; i<foot_step_size_; i++)
332  {
333  geometry_msgs::Pose2D msg;
334 
335  // Forward Step
336  msg.x = foot_step_command_.step_length;
337 
338  if (foot_step_command_.command == "stop")
339  msg.x *= 0.0;
340 
341  if (foot_step_command_.command == "backward")
342  msg.x *= -1.0;
343 
344  if (foot_step_command_.command == "left" ||
345  foot_step_command_.command == "right")
346  msg.x *= 0.0;
347 
348  if (foot_step_command_.command == "turn_left" ||
349  foot_step_command_.command == "turn_right")
350  msg.x *= 0.0;
351 
352  // Side Step
353  walking_leg = walking_start_leg++ % LEG_COUNT;
354  double lr = walking_leg;
355  if (foot_step_command_.command == "left")
356  {
357  lr += -1.0; lr *= -1.0;
358  }
359 
360  if ((foot_step_command_.command == "forward" || foot_step_command_.command == "backward") &&
361  foot_step_command_.start_leg == "left_leg")
362  {
363  lr += -1.0; lr *= -1.0;
364  }
365 
366  if (foot_step_command_.command == "turn_left" ||
367  foot_step_command_.command == "turn_right")
368  lr = 0.0;
369 
370  if (foot_step_command_.command == "stop")
371  lr *= 0.0;
372 
373  msg.y = foot_origin_shift_y_ + lr*foot_step_command_.side_length;
374 
375  // Theta
376  double theta;
377  theta = foot_step_command_.step_angle;
378 
379  if (foot_step_command_.command == "turn_right")
380  theta *= -1.0;
381 
382  if ((foot_step_command_.command == "forward" || foot_step_command_.command == "backward") &&
383  foot_step_command_.start_leg == "right_leg")
384  theta *= -1.0;
385 
386  if (foot_step_command_.command == "left" ||
387  foot_step_command_.command == "right")
388  theta *= 0.0;
389 
390  if (foot_step_command_.command == "stop")
391  theta *= 0.0;
392 
393  if (i == 0 ||
394  i == 1 ||
395  i == foot_step_size_-2 ||
396  i == foot_step_size_-1)
397  {
398  msg.x = 0.0;
399  msg.y = foot_origin_shift_y_;
400  theta = 0.0;
401  }
402 
403  foot_angle += theta;
404  msg.theta = foot_angle;
405 
406  foot_step_param_.moving_foot.push_back(walking_leg);
407  foot_step_param_.data.push_back(msg);
408  }
409 
411 
412 // ROS_INFO("--");
413 // for (int i=0; i<foot_step_size_; i++)
414 // {
415 // ROS_INFO("movint foot : %d", foot_step_param_.moving_foot[i]);
416 // ROS_INFO("foot position x: %f", foot_step_param_.data[i].x);
417 // ROS_INFO("foot position y: %f", foot_step_param_.data[i].y);
418 // ROS_INFO("foot position angle: %f", foot_step_param_.data[i].theta);
419 // }
420 }
421 
423 {
424  fin_time_ = foot_step_2d_.step_time;
425  foot_step_size_ = foot_step_2d_.footsteps_2d.size();
426 
427  goal_r_foot_pos_buffer_ = Eigen::MatrixXd::Zero(foot_step_size_,2);
428  goal_l_foot_pos_buffer_ = Eigen::MatrixXd::Zero(foot_step_size_,2);
429 
430  std::vector<double_t> init_r_foot_pos, init_l_foot_pos;
431  init_r_foot_pos.resize(2, 0.0);
432  init_r_foot_pos[0] = init_r_foot_pos_[0];
433  init_r_foot_pos[1] = init_r_foot_pos_[1];
434 
435  init_l_foot_pos.resize(2, 0.0);
436  init_l_foot_pos[0] = init_l_foot_pos_[0];
437  init_l_foot_pos[1] = init_l_foot_pos_[1];
438 
439  std::vector<double_t> goal_r_foot_pos, goal_l_foot_pos;
440  goal_r_foot_pos.resize(2, 0.0);
441  goal_l_foot_pos.resize(2, 0.0);
442 
443  op3_online_walking_module_msgs::FootStepArray foot_step_param;
444 
445  for (int step=0; step<foot_step_size_; step++)
446  {
447  op3_online_walking_module_msgs::Step2D msg = foot_step_2d_.footsteps_2d[step];
448 
449  foot_step_param.moving_foot.push_back(msg.moving_foot);
450  geometry_msgs::Pose2D foot_pose_2d;
451  foot_pose_2d.theta = msg.step2d.theta;
452  foot_step_param.data.push_back(foot_pose_2d);
453 
454  if (step == foot_step_size_ - 1)
455  {
456  goal_r_foot_pos = init_r_foot_pos;
457  goal_l_foot_pos = init_l_foot_pos;
458  }
459  else
460  {
461  if (msg.moving_foot == LEFT_LEG)
462  {
463 // ROS_INFO("L");
464 
465  goal_l_foot_pos[0] = msg.step2d.x;
466  goal_l_foot_pos[1] = msg.step2d.y;
467 
468  goal_r_foot_pos = init_r_foot_pos;
469  }
470  else if(msg.moving_foot == RIGHT_LEG)
471  {
472 // ROS_INFO("R");
473 
474  goal_r_foot_pos[0] = msg.step2d.x;
475  goal_r_foot_pos[1] = msg.step2d.y;
476 
477  goal_l_foot_pos = init_l_foot_pos;
478  }
479  }
480 
481  goal_r_foot_pos_buffer_.coeffRef(step,0) = goal_r_foot_pos[0];
482  goal_r_foot_pos_buffer_.coeffRef(step,1) = goal_r_foot_pos[1];
483  goal_l_foot_pos_buffer_.coeffRef(step,0) = goal_l_foot_pos[0];
484  goal_l_foot_pos_buffer_.coeffRef(step,1) = goal_l_foot_pos[1];
485 
486  init_r_foot_pos = goal_r_foot_pos;
487  init_l_foot_pos = goal_l_foot_pos;
488  }
489 
490  foot_step_param_ = foot_step_param;
491 
492  //PRINT_MAT(goal_r_foot_pos_buffer_);
493  //PRINT_MAT(goal_l_foot_pos_buffer_);
494 }
495 
497 {
499  Eigen::MatrixXd left_foot_rot = robotis_framework::convertQuaternionToRotation(des_l_foot_Q_);
501 
505 
506  if (foot_step_param_.moving_foot[step] == LEFT_LEG)
507  {
508  double angle = foot_step_param_.data[step].theta;
509 
510  // Goal
511 // goal_l_foot_pos_[0] = init_r_foot_pos_[0]
512 // + cos(angle) * foot_step_param_.data[step].x
513 // - sin(angle) * foot_step_param_.data[step].y;
514 // goal_l_foot_pos_[1] = init_r_foot_pos_[1]
515 // + sin(angle) * foot_step_param_.data[step].x
516 // + cos(angle) * foot_step_param_.data[step].y;
517 // goal_l_foot_pos_[2] = init_r_foot_pos_[2];
518 
519  goal_l_foot_pos_[0] = goal_l_foot_pos_buffer_.coeff(step,0);
520  goal_l_foot_pos_[1] = goal_l_foot_pos_buffer_.coeff(step,1);
522 
524 
527 
529 
530  // Via point
531  double via_time = 0.5*(init_time_ + fin_time_);
532 
533  std::vector<double_t> via_l_foot_pos, via_l_foot_vel, via_l_foot_accel;
534  via_l_foot_pos.resize(3, 0.0);
535  via_l_foot_vel.resize(3, 0.0);
536  via_l_foot_accel.resize(3, 0.0);
537 
538  via_l_foot_pos[0] = 0.5*(init_l_foot_pos_[0] + goal_l_foot_pos_[0]);
539  via_l_foot_pos[1] = 0.5*(init_l_foot_pos_[1] + goal_l_foot_pos_[1]);
540  via_l_foot_pos[2] = foot_tra_max_z_;
541 
542  if (step == 0 || step == 1)
543  via_l_foot_pos[2] = 0.0;
544 
545  if (step == foot_step_size_-1)
546  via_l_foot_pos[2] = 0.0;
547 
548  // Trajectory
552  via_l_foot_pos, via_l_foot_vel, via_l_foot_accel);
553 
554 // ROS_INFO("angle: %f", angle);
555  }
556  else if (foot_step_param_.moving_foot[step] == RIGHT_LEG)
557  {
558  double angle = foot_step_param_.data[step].theta;
559 
560  // Goal
561  goal_r_foot_pos_[0] = goal_r_foot_pos_buffer_.coeff(step,0);
562  goal_r_foot_pos_[1] = goal_r_foot_pos_buffer_.coeff(step,1);
563 // goal_r_foot_pos_[0] = init_l_foot_pos_[0]
564 // + cos(angle) * foot_step_param_.data[step].x
565 // + sin(angle) * foot_step_param_.data[step].y;
566 // goal_r_foot_pos_[1] = init_l_foot_pos_[1]
567 // + sin(angle) * foot_step_param_.data[step].x
568 // - cos(angle) * foot_step_param_.data[step].y;
569 // goal_r_foot_pos_[2] = init_l_foot_pos_[2];
570 
572 
575 
577 
578  // Via point
579  double via_time = 0.5*(init_time_ + fin_time_);
580 
581  std::vector<double_t> via_r_foot_pos, via_r_foot_vel, via_r_foot_accel;
582  via_r_foot_pos.resize(3, 0.0);
583  via_r_foot_vel.resize(3, 0.0);
584  via_r_foot_accel.resize(3, 0.0);
585 
586  via_r_foot_pos[0] = 0.5*(init_r_foot_pos_[0] + goal_r_foot_pos_[0]);
587  via_r_foot_pos[1] = 0.5*(init_r_foot_pos_[1] + goal_r_foot_pos_[1]);
588  via_r_foot_pos[2] = foot_tra_max_z_;
589 
590  if (step == 0 || step == 1)
591  via_r_foot_pos[2] = 0.0;
592 
593  if (step == foot_step_size_-1)
594  via_r_foot_pos[2] = 0.0;
595 
596  // Trajectory
600  via_r_foot_pos, via_r_foot_vel, via_r_foot_accel);
601  }
602 
603 // ROS_INFO("-----");
604 // ROS_INFO("goal_r_foot_pos_ x: %f , y: %f", goal_r_foot_pos_[0], goal_r_foot_pos_[1]);
605 // ROS_INFO("goal_l_foot_pos_ x: %f , y: %f", goal_l_foot_pos_[0], goal_l_foot_pos_[1]);
606 // ROS_INFO("-----");
607 }
608 
609 void WalkingControl::calcFootStepPose(double time, int step)
610 {
611  if (foot_step_param_.moving_foot[step] == LEFT_LEG)
612  {
616 
618  des_r_foot_vel_.resize(3, 0.0);
619  des_r_foot_accel_.resize(3, 0.0);
620 
622 
623 // ROS_INFO("left foot x: %f, y: %f, z: %f", desired_left_foot_pos_[0], desired_left_foot_pos_[1], desired_left_foot_pos_[2]);
624  }
625  else if (foot_step_param_.moving_foot[step] == RIGHT_LEG)
626  {
630 
632  des_l_foot_vel_.resize(3, 0.0);
633  des_l_foot_accel_.resize(3, 0.0);
634 
636 
637 // ROS_INFO("right foot x: %f, y: %f, z: %f", desired_right_foot_pos_[0], desired_right_foot_pos_[1], desired_right_foot_pos_[2]);
638  }
639 }
640 
642 {
643  if (step == 0 || step == 1)
644  {
645  ref_zmp_x_ = 0.5*(goal_r_foot_pos_[0] + goal_l_foot_pos_[0]); // + zmp_offset_x_;
647  }
648  else if (step == foot_step_size_-1)
649  {
650  ref_zmp_x_ = 0.5*(goal_r_foot_pos_[0] + goal_l_foot_pos_[0]); // + zmp_offset_x_;
652  }
653  else
654  {
655  if (foot_step_param_.moving_foot[step] == LEFT_LEG)
656  {
659  }
660  else if (foot_step_param_.moving_foot[step] == RIGHT_LEG)
661  {
664  }
665  }
666 
667 // ROS_INFO("ref zmp x: %f, y: %f", ref_zmp_x_, ref_zmp_y_);
668 }
669 
671 {
672  goal_r_foot_pos_buffer_ = Eigen::MatrixXd::Zero(foot_step_size_,2);
673  goal_l_foot_pos_buffer_ = Eigen::MatrixXd::Zero(foot_step_size_,2);
674 
675  std::vector<double_t> init_r_foot_pos, init_l_foot_pos;
676  init_r_foot_pos.resize(2, 0.0);
677  init_r_foot_pos[0] = init_r_foot_pos_[0];
678  init_r_foot_pos[1] = init_r_foot_pos_[1];
679 
680  init_l_foot_pos.resize(2, 0.0);
681  init_l_foot_pos[0] = init_l_foot_pos_[0];
682  init_l_foot_pos[1] = init_l_foot_pos_[1];
683 
684  std::vector<double_t> goal_r_foot_pos, goal_l_foot_pos;
685  goal_r_foot_pos.resize(2, 0.0);
686  goal_l_foot_pos.resize(2, 0.0);
687 
688  for (int step=0; step<foot_step_size_; step++)
689  {
690  double angle = foot_step_param_.data[step].theta;
691 
692  if (foot_step_param_.moving_foot[step] == LEFT_LEG)
693  {
694  //ROS_INFO("L");
695 
696  goal_l_foot_pos[0] = init_r_foot_pos[0]
697  + cos(angle) * foot_step_param_.data[step].x
698  - sin(angle) * foot_step_param_.data[step].y;
699  goal_l_foot_pos[1] = init_r_foot_pos[1]
700  + sin(angle) * foot_step_param_.data[step].x
701  + cos(angle) * foot_step_param_.data[step].y;
702 
703  goal_r_foot_pos = init_r_foot_pos;
704  }
705  else if(foot_step_param_.moving_foot[step] == RIGHT_LEG)
706  {
707  //ROS_INFO("R");
708 
709  goal_r_foot_pos[0] = init_l_foot_pos[0]
710  + cos(angle) * foot_step_param_.data[step].x
711  + sin(angle) * foot_step_param_.data[step].y;
712  goal_r_foot_pos[1] = init_l_foot_pos[1]
713  + sin(angle) * foot_step_param_.data[step].x
714  - cos(angle) * foot_step_param_.data[step].y;
715 
716  goal_l_foot_pos = init_l_foot_pos;
717  }
718 
719  goal_r_foot_pos_buffer_.coeffRef(step,0) = goal_r_foot_pos[0];
720  goal_r_foot_pos_buffer_.coeffRef(step,1) = goal_r_foot_pos[1];
721  goal_l_foot_pos_buffer_.coeffRef(step,0) = goal_l_foot_pos[0];
722  goal_l_foot_pos_buffer_.coeffRef(step,1) = goal_l_foot_pos[1];
723 
724  init_r_foot_pos = goal_r_foot_pos;
725  init_l_foot_pos = goal_l_foot_pos;
726  }
727 
728  //PRINT_MAT(goal_r_foot_pos_buffer_);
729  //PRINT_MAT(goal_l_foot_pos_buffer_);
730 }
731 
733 {
734  double ref_zmp_x;
735 
736  if (step == 0 || step == 1)
737  {
738  ref_zmp_x = 0.5 * (goal_r_foot_pos_buffer_.coeff(step,0) + goal_l_foot_pos_buffer_.coeff(step,0)); // + zmp_offset_x_;
739  }
740  else if (step >= foot_step_size_-1)
741  {
742  ref_zmp_x = 0.5 * (goal_r_foot_pos_buffer_.coeff(foot_step_size_-1,0) + goal_l_foot_pos_buffer_.coeff(foot_step_size_-1,0)); // + zmp_offset_x_;
743  }
744  else
745  {
746  if (foot_step_param_.moving_foot[step] == LEFT_LEG)
747  ref_zmp_x = goal_r_foot_pos_buffer_.coeff(step,0);
748  else if (foot_step_param_.moving_foot[step] == RIGHT_LEG)
749  ref_zmp_x = goal_l_foot_pos_buffer_.coeff(step,0);
750  }
751 
752  return ref_zmp_x;
753 }
754 
756 {
757  double ref_zmp_y;
758 
759  if (step == 0 || step == 1)
760  {
761  ref_zmp_y = 0.5 * (goal_r_foot_pos_buffer_.coeff(step,1) + goal_l_foot_pos_buffer_.coeff(step,1));
762  }
763  else if (step >= foot_step_size_-1)
764  {
765  ref_zmp_y = 0.5 * (goal_r_foot_pos_buffer_.coeff(foot_step_size_-1,1) + goal_l_foot_pos_buffer_.coeff(foot_step_size_-1,1));
766  }
767  else
768  {
769  if (foot_step_param_.moving_foot[step] == LEFT_LEG)
770  ref_zmp_y = goal_r_foot_pos_buffer_.coeff(step,1) - zmp_offset_y_;
771  else if (foot_step_param_.moving_foot[step] == RIGHT_LEG)
772  ref_zmp_y = goal_l_foot_pos_buffer_.coeff(step,1) + zmp_offset_y_;
773  }
774 
775  return ref_zmp_y;
776 }
777 
778 //void WalkingControl::calcPreviewParam(op3_online_walking_module_msgs::PreviewResponse msg)
779 void WalkingControl::calcPreviewParam(std::vector<double_t> K, int K_row, int K_col,
780  std::vector<double_t> P, int P_row, int P_col)
781 {
782  //
783 // ROS_INFO("lipm_height_ : %f", lipm_height_);
784 
785  double t = control_cycle_;
786 
787  A_.resize(3,3);
788  A_ << 1, t, t*t/2.0,
789  0, 1, t,
790  0, 0, 1;
791 
792  b_.resize(3,1);
793  b_ << t*t*t/6.0,
794  t*t/2.0,
795  t;
796 
797  c_.resize(1,3);
798  c_ << 1, 0, -lipm_height_/9.81;
799 
800  //
801 // preview_response_ = msg;
802 
803  int row_K = K_row;
804  int col_K = K_col;
805  std::vector<double_t> matrix_K = K;
806 
807  int row_P = P_row;
808  int col_P = P_col;
809  std::vector<double_t> matrix_P = P;
810 
811  K_.resize(row_K,col_K);
812  P_.resize(row_P,col_P);
813 
814  for(int j=0 ; j<row_K ; j++)
815  {
816  for(int i=0 ; i<col_K ; i++)
817  K_.coeffRef(j,i) = matrix_K[i*row_K+j];
818  }
819 
820  for(int j=0; j<row_P; j++)
821  {
822  for(int i=0 ; i<col_P; i++)
823  P_.coeffRef(j,i) = matrix_P[i*row_P+j];
824  }
825 
826  k_s_ = K_.coeff(0,0);
827  k_x_.resize(1,3);
828  k_x_ << K_.coeff(0,1), K_.coeff(0,2), K_.coeff(0,3);
829 
831 
833  lipm_height_,
834  K_, P_);
835 
836  delete preview_control_;
837 
838 // f_ = robot_->calcPreviewParam(preview_time_, control_cycle_,
839 // lipm_height_,
840 // K_, P_);
841 
842 // for (int i=0; i<preview_size_; i++)
843 // ROS_INFO("f: %f", f_.coeff(0,i));
844 }
845 
846 void WalkingControl::calcPreviewControl(double time, int step)
847 {
848  int index_new;
849 
850  preview_sum_zmp_x_ = 0.0;
851  preview_sum_zmp_y_ = 0.0;
852 
853  for (int i=0; i<preview_size_; i++)
854  {
855  index_new = time/control_cycle_ + (i+1) - 1;
856  if (index_new < 0)
857  index_new = 0;
858 
859  int fin_index = round(fin_time_/control_cycle_) +1;
860  int step_new = step + index_new/fin_index;
861 
862  double ref_zmp_x = calcRefZMPx(step_new);
863  double ref_zmp_y = calcRefZMPy(step_new);
864 
865  double preview_zmp_x = f_.coeff(0,i)*ref_zmp_x;
866  double preview_zmp_y = f_.coeff(0,i)*ref_zmp_y;
867 
868  preview_sum_zmp_x_ += preview_zmp_x;
869  preview_sum_zmp_y_ += preview_zmp_y;
870  }
871 
872 // ROS_INFO("preview_sum_zmp x: %f , y: %f", preview_sum_zmp_x_, preview_sum_zmp_y_);
873 
874  u_x_(0,0) =
876  -(k_x_(0,0)*x_lipm_(0,0) + k_x_(0,1)*x_lipm_(1,0) + k_x_(0,2)*x_lipm_(2,0))
878  u_y_(0,0) =
880  -(k_x_(0,0)*y_lipm_(0,0) + k_x_(0,1)*y_lipm_(1,0) + k_x_(0,2)*y_lipm_(2,0))
882 
883  x_lipm_ = A_*x_lipm_ + b_*u_x_;
884  y_lipm_ = A_*y_lipm_ + b_*u_y_;
885 
886  double cx = c_(0,0)*x_lipm_(0,0) + c_(0,1)*x_lipm_(1,0) + c_(0,2)*x_lipm_(2,0);
887  double cy = c_(0,0)*y_lipm_(0,0) + c_(0,1)*y_lipm_(1,0) + c_(0,2)*y_lipm_(2,0);
888 
889  sum_of_cx_ += cx;
890  sum_of_cy_ += cy;
891 
894 
895  des_body_pos_[0] = x_lipm_.coeff(0,0);
896  des_body_pos_[1] = y_lipm_.coeff(0,0);
897 
898 // ROS_INFO("x_lipm pos: %f, vel: %f, accel:%f", x_lipm_.coeff(0,0), x_lipm_.coeff(1,0), x_lipm_.coeff(2,0));
899 // ROS_INFO("y_lipm pos: %f, vel: %f, accel:%f", y_lipm_.coeff(0,0), y_lipm_.coeff(1,0), y_lipm_.coeff(2,0));
900 
901 // ROS_INFO("time: %f, desired_body_pos_ x: %f , y: %f", time, des_body_pos_[0], des_body_pos_[1]);
902 }
903 
904 void WalkingControl::getWalkingPosition(std::vector<double_t> &l_foot_pos,
905  std::vector<double_t> &r_foot_pos,
906  std::vector<double_t> &body_pos)
907 {
908  l_foot_pos = des_l_foot_pos_;
909  r_foot_pos = des_r_foot_pos_;
910  body_pos = des_body_pos_;
911 
912 // ROS_INFO("body x: %f , y: %f", body_pos[0], body_pos[1]);
913 // ROS_INFO("l_foot_pos x: %f , y: %f , z: %f", l_foot_pos[0], l_foot_pos[1], l_foot_pos[2]);
914 // ROS_INFO("r_foot_pos x: %f , y: %f , z: %f", r_foot_pos[0], r_foot_pos[1], r_foot_pos[2]);
915 }
916 
917 void WalkingControl::getWalkingVelocity(std::vector<double_t> &l_foot_vel,
918  std::vector<double_t> &r_foot_vel,
919  std::vector<double_t> &body_vel)
920 {
921  l_foot_vel = des_l_foot_vel_;
922  r_foot_vel = des_r_foot_vel_;
923 
924  // TODO
925  // body_vel =
926 }
927 
928 void WalkingControl::getWalkingAccleration(std::vector<double_t> &l_foot_accel,
929  std::vector<double_t> &r_foot_accel,
930  std::vector<double_t> &body_accel)
931 {
932  l_foot_accel = des_l_foot_accel_;
933  r_foot_accel = des_r_foot_accel_;
934 
935  // TODO
936  // body_accel =
937 }
938 
939 void WalkingControl::getWalkingOrientation(std::vector<double_t> &l_foot_Q,
940  std::vector<double_t> &r_foot_Q,
941  std::vector<double_t> &body_Q)
942 {
943  l_foot_Q[0] = des_l_foot_Q_.x();
944  l_foot_Q[1] = des_l_foot_Q_.y();
945  l_foot_Q[2] = des_l_foot_Q_.z();
946  l_foot_Q[3] = des_l_foot_Q_.w();
947 
948  r_foot_Q[0] = des_r_foot_Q_.x();
949  r_foot_Q[1] = des_r_foot_Q_.y();
950  r_foot_Q[2] = des_r_foot_Q_.z();
951  r_foot_Q[3] = des_r_foot_Q_.w();
952 
953  body_Q[0] = des_body_Q_.x();
954  body_Q[1] = des_body_Q_.y();
955  body_Q[2] = des_body_Q_.z();
956  body_Q[3] = des_body_Q_.w();
957 }
958 
959 void WalkingControl::getLIPM(std::vector<double_t> &x_lipm, std::vector<double_t> &y_lipm)
960 {
961  x_lipm.resize(3, 0.0);
962  y_lipm.resize(3, 0.0);
963 
964  for (int i=0; i<3; i++)
965  {
966  x_lipm[i] = x_lipm_.coeff(i,0);
967  y_lipm[i] = y_lipm_.coeff(i,0);
968  }
969 }
970 
971 void WalkingControl::getWalkingState(int &walking_leg, int &walking_phase)
972 {
973  walking_leg = walking_leg_;
974  walking_phase = walking_phase_;
975 }
void getWalkingPosition(std::vector< double_t > &l_foot_pos, std::vector< double_t > &r_foot_pos, std::vector< double_t > &body_pos)
std::vector< double_t > des_l_foot_pos_
std::vector< double_t > init_r_foot_pos_
op3_online_walking_module_msgs::FootStepCommand foot_step_command_
Eigen::Quaterniond des_l_foot_Q_
void calcRefZMP(int step)
std::vector< double_t > getVelocity(double time)
std::vector< double_t > goal_l_foot_accel_
void getWalkingState(int &walking_leg, int &walking_phase)
std::vector< double_t > init_l_foot_accel_
void initialize(op3_online_walking_module_msgs::FootStepCommand foot_step_command, std::vector< double_t > init_body_pos, std::vector< double_t > init_body_Q, std::vector< double_t > init_r_foot_pos, std::vector< double_t > init_r_foot_Q, std::vector< double_t > init_l_foot_pos, std::vector< double_t > init_l_foot_Q)
std::vector< double_t > des_body_vel_
WalkingControl(double control_cycle, double dsp_ratio, double lipm_height, double foot_height_max, double zmp_offset_x, double zmp_offset_y, std::vector< double_t > x_lipm, std::vector< double_t > y_lipm, double foot_distance)
double foot_origin_shift_x_
void calcFootTrajectory(int step)
robotis_framework::PreviewControl * preview_control_
std::vector< double_t > des_body_accel_
robotis_framework::MinimumJerkViaPoint * r_foot_tra_
std::vector< double_t > goal_r_foot_vel_
Eigen::Quaterniond convertRPYToQuaternion(double roll, double pitch, double yaw)
void getWalkingVelocity(std::vector< double_t > &l_foot_vel, std::vector< double_t > &r_foot_vel, std::vector< double_t > &body_vel)
double init_body_yaw_angle_
std::vector< double_t > goal_body_accel_
std::vector< double_t > des_r_foot_accel_
Eigen::MatrixXd goal_l_foot_pos_buffer_
Eigen::MatrixXd u_x_
virtual ~WalkingControl()
Eigen::Quaterniond convertRotationToQuaternion(const Eigen::Matrix3d &rotation)
Eigen::Quaterniond des_body_Q_
double preview_sum_zmp_y_
std::vector< double_t > init_r_foot_vel_
std::vector< double_t > des_l_foot_accel_
std::vector< double_t > init_body_pos_
Eigen::Vector3d convertQuaternionToRPY(const Eigen::Quaterniond &quaternion)
double calcRefZMPx(int step)
std::vector< double_t > goal_body_pos_
Eigen::Quaterniond init_r_foot_Q_
void calcPreviewParam(std::vector< double_t > K, int K_row, int K_col, std::vector< double_t > P, int P_row, int P_col)
Eigen::MatrixXd A_
Eigen::Matrix3d convertQuaternionToRotation(const Eigen::Quaterniond &quaternion)
double preview_sum_zmp_x_
void calcFootStepPose(double time, int step)
Eigen::MatrixXd y_lipm_
Eigen::Quaterniond goal_body_Q_
std::vector< double_t > goal_r_foot_pos_
std::vector< double_t > getAcceleration(double time)
Eigen::Quaterniond des_r_foot_Q_
Eigen::MatrixXd b_
Eigen::Quaterniond init_body_Q_
Eigen::MatrixXd u_y_
Eigen::MatrixXd calcPreviewParam(double preview_time, double control_cycle, double lipm_height, Eigen::MatrixXd K, Eigen::MatrixXd P)
void getWalkingAccleration(std::vector< double_t > &l_foot_accel, std::vector< double_t > &r_foot_accel, std::vector< double_t > &body_accel)
robotis_framework::MinimumJerkViaPoint * l_foot_tra_
Eigen::MatrixXd k_x_
double calcRefZMPy(int step)
std::vector< double_t > des_r_foot_vel_
Eigen::Quaterniond goal_r_foot_Q_
std::vector< double_t > des_body_pos_
op3_online_walking_module_msgs::Step2DArray foot_step_2d_
std::vector< double_t > des_r_foot_pos_
std::vector< double_t > getPosition(double time)
double foot_origin_shift_y_
std::vector< double_t > des_l_foot_vel_
std::vector< double_t > init_body_accel_
Eigen::Quaterniond init_l_foot_Q_
std::vector< double_t > init_body_vel_
std::vector< double_t > init_l_foot_pos_
Eigen::MatrixXd c_
std::vector< double_t > goal_l_foot_pos_
std::vector< double_t > init_l_foot_vel_
std::vector< double_t > goal_l_foot_vel_
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Eigen::MatrixXd x_lipm_
std::vector< double_t > init_r_foot_accel_
void getWalkingOrientation(std::vector< double_t > &l_foot_Q, std::vector< double_t > &r_foot_Q, std::vector< double_t > &body_Q)
void getLIPM(std::vector< double_t > &x_lipm, std::vector< double_t > &y_lipm)
op3_online_walking_module_msgs::FootStepArray foot_step_param_
Eigen::Quaterniond goal_l_foot_Q_
void set(double time, int step, bool foot_step_2d)
Eigen::MatrixXd P_
Eigen::MatrixXd f_
std::vector< double_t > goal_r_foot_accel_
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
std::vector< double_t > goal_body_vel_
void calcPreviewControl(double time, int step)
Eigen::MatrixXd goal_r_foot_pos_buffer_
Eigen::MatrixXd K_


op3_online_walking_module
Author(s): SCH
autogenerated on Mon Jun 10 2019 14:41:22