GaitGenerator.cpp
Go to the documentation of this file.
1 /* -*- coding:utf-8-unix; mode:c++; -*- */
2 
3 #include "GaitGenerator.h"
4 #include <numeric>
5 
6 namespace rats
7 {
8 #ifndef rad2deg
9 #define rad2deg(rad) (rad * 180 / M_PI)
10 #endif
11 #ifndef deg2rad
12 #define deg2rad(deg) (deg * M_PI / 180)
13 #endif
15  const double ratio, const hrp::Vector3& start,
16  const hrp::Vector3& goal, const double height,
17  const double default_top_ratio)
18  {
19  hrp::Vector3 u ( goal - start );
20  hrp::Vector3 uz (0,0, ratio * u(2));
21  u(2) = 0.0;
22  double pth = 2 * M_PI * ratio, norm_u = u.norm();
23  if ( !eps_eq(norm_u, 0.0,1e-3*0.01) )
24  u = u.normalized();
25  /* check ratio vs 0.5 for default_top_ratio blending */
26  hrp::Vector3 cycloid_point( ((0.5 > ratio) ? ( 2 * default_top_ratio * norm_u ) : ( 2 * (1 - default_top_ratio) * norm_u )) * ( pth - sin(pth) ) / (2 * M_PI) -
27  ((0.5 > ratio) ? 0.0 : (norm_u * (1 - 2 * default_top_ratio)) ), // local x
28  0, // local y
29  ( 0.5 * height * ( 1 - cos(pth) )) ); // local z
30  hrp::Vector3 v(hrp::Vector3(0,0,1).cross(u));
31  hrp::Matrix33 dvm;
32  dvm << u(0), v(0), 0,
33  u(1), v(1), 0,
34  u(2), v(2), 1;
35  ret = dvm * cycloid_point + start + uz;
36  };
37  void multi_mid_coords (coordinates& ret, const std::vector<coordinates>& cs, const double eps)
38  {
39  if (cs.size() == 1) {
40  ret = cs.front();
41  } else {
42  std::vector<coordinates> tmp_mid_coords;
43  double ratio = (1.0 - 1.0 / cs.size());
44  for (size_t i = 1; i < cs.size(); i++) {
45  coordinates tmp;
46  mid_coords(tmp, ratio, cs.front(), cs.at(i), eps);
47  tmp_mid_coords.push_back(tmp);
48  }
49  multi_mid_coords(ret, tmp_mid_coords, eps);
50  }
51  return;
52  };
53 
54  std::string leg_type_to_leg_type_string (const leg_type l_r)
55  {
56  return ((l_r==LLEG)?std::string("lleg"):
57  (l_r==RARM)?std::string("rarm"):
58  (l_r==LARM)?std::string("larm"):
59  std::string("rleg"));
60  };
61 
62  double set_value_according_to_toe_heel_type (const toe_heel_type tht, const double toe_value, const double heel_value, const double default_value)
63  {
64  if (tht == TOE) {
65  return toe_value;
66  } else if (tht == HEEL) {
67  return heel_value;
68  } else {
69  return default_value;
70  }
71  };
72 
73  /* member function implementation for refzmp_generator */
74  void refzmp_generator::push_refzmp_from_footstep_nodes_for_dual (const std::vector<step_node>& fns,
75  const std::vector<step_node>& _support_leg_steps,
76  const std::vector<step_node>& _swing_leg_steps)
77  {
78  hrp::Vector3 rzmp;
79  std::vector<hrp::Vector3> dzl;
80  hrp::Vector3 ret_zmp;
81  hrp::Vector3 tmp_zero = hrp::Vector3::Zero();
82  std::vector<hrp::Vector3> foot_x_axises;
83  double sum_of_weight = 0.0;
84  for (std::vector<step_node>::const_iterator it = _support_leg_steps.begin(); it != _support_leg_steps.end(); it++) {
85  dzl.push_back((it->worldcoords.rot * default_zmp_offsets[it->l_r] + it->worldcoords.pos) * zmp_weight_map[it->l_r]);
86  sum_of_weight += zmp_weight_map[it->l_r];
87  }
88  for (std::vector<step_node>::const_iterator it = _swing_leg_steps.begin(); it != _swing_leg_steps.end(); it++) {
89  dzl.push_back((it->worldcoords.rot * default_zmp_offsets[it->l_r] + it->worldcoords.pos) * zmp_weight_map[it->l_r]);
90  sum_of_weight += zmp_weight_map[it->l_r];
91  foot_x_axises.push_back( hrp::Vector3(it->worldcoords.rot * hrp::Vector3::UnitX()) );
92  }
93  foot_x_axises_list.push_back(foot_x_axises);
94  rzmp = std::accumulate(dzl.begin(), dzl.end(), tmp_zero) / sum_of_weight;
95  refzmp_cur_list.push_back( rzmp );
96  std::vector<leg_type> swing_leg_types;
97  for (size_t i = 0; i < fns.size(); i++) {
98  swing_leg_types.push_back(fns.at(i).l_r);
99  }
100  swing_leg_types_list.push_back( swing_leg_types );
101  step_count_list.push_back(static_cast<size_t>(fns.front().step_time/dt));
103  //std::cerr << "double " << (fns[fs_index].l_r==RLEG?LLEG:RLEG) << " [" << refzmp_cur_list.back()(0) << " " << refzmp_cur_list.back()(1) << " " << refzmp_cur_list.back()(2) << "]" << std::endl;
104  };
105 
106  void refzmp_generator::push_refzmp_from_footstep_nodes_for_single (const std::vector<step_node>& fns, const std::vector<step_node>& _support_leg_steps, const toe_heel_types& tht)
107  {
108  // support leg = prev fns l_r
109  // swing leg = fns l_r
110  hrp::Vector3 rzmp, tmp_zero=hrp::Vector3::Zero();
111  std::vector<hrp::Vector3> dzl;
112  std::vector<hrp::Vector3> foot_x_axises;
113  double sum_of_weight = 0.0;
114 
115  for (std::vector<step_node>::const_iterator it = _support_leg_steps.begin(); it != _support_leg_steps.end(); it++) {
116  dzl.push_back((it->worldcoords.rot * default_zmp_offsets[it->l_r] + it->worldcoords.pos) * zmp_weight_map[it->l_r]);
117  sum_of_weight += zmp_weight_map[it->l_r];
118  foot_x_axises.push_back( hrp::Vector3(it->worldcoords.rot * hrp::Vector3::UnitX()) );
119  }
120  rzmp = std::accumulate(dzl.begin(), dzl.end(), tmp_zero) / sum_of_weight;
121  refzmp_cur_list.push_back( rzmp );
122  foot_x_axises_list.push_back(foot_x_axises);
123  std::vector<leg_type> swing_leg_types;
124  for (size_t i = 0; i< fns.size(); i++) {
125  swing_leg_types.push_back(fns.at(i).l_r);
126  }
127  swing_leg_types_list.push_back( swing_leg_types );
128  step_count_list.push_back(static_cast<size_t>(fns.front().step_time/dt));
129  toe_heel_types_list.push_back(tht);
130  //std::cerr << "single " << fns[fs_index-1].l_r << " [" << refzmp_cur_list.back()(0) << " " << refzmp_cur_list.back()(1) << " " << refzmp_cur_list.back()(2) << "]" << std::endl;
131  };
132 
133  void refzmp_generator::calc_current_refzmp (hrp::Vector3& ret, std::vector<hrp::Vector3>& swing_foot_zmp_offsets, const double default_double_support_ratio_before, const double default_double_support_ratio_after, const double default_double_support_static_ratio_before, const double default_double_support_static_ratio_after)
134  {
135  size_t cnt = one_step_count - refzmp_count; // current counter (0 -> one_step_count)
136  size_t double_support_count_half_before = default_double_support_ratio_before * one_step_count;
137  size_t double_support_count_half_after = default_double_support_ratio_after * one_step_count;
138  size_t double_support_static_count_half_before = default_double_support_static_ratio_before * one_step_count;
139  size_t double_support_static_count_half_after = default_double_support_static_ratio_after * one_step_count;
140  for (size_t i = 0; i < swing_leg_types_list[refzmp_index].size(); i++) {
141  swing_foot_zmp_offsets.push_back(default_zmp_offsets[swing_leg_types_list[refzmp_index].at(i)]);
142  }
143  double zmp_diff = 0.0; // difference between total swing_foot_zmp_offset and default_zmp_offset
144  //if (cnt==0) std::cerr << "z " << refzmp_index << " " << refzmp_cur_list.size() << " " << fs_index << " " << (refzmp_index == refzmp_cur_list.size()-2) << " " << is_final_double_support_set << std::endl;
145 
146  // Calculate swing foot zmp offset for toe heel zmp transition
148  !(is_start_double_support_phase() || is_end_double_support_phase())) { // Do not use toe heel zmp transition during start and end double support period because there is no swing foot
149  double first_zmp_offset_x, second_zmp_offset_x;
150  if (use_toe_heel_auto_set) {
151  first_zmp_offset_x = set_value_according_to_toe_heel_type(toe_heel_types_list[refzmp_index].src_type, toe_zmp_offset_x, heel_zmp_offset_x, swing_foot_zmp_offsets.front()(0));
152  second_zmp_offset_x = set_value_according_to_toe_heel_type(toe_heel_types_list[refzmp_index].dst_type, toe_zmp_offset_x, heel_zmp_offset_x, swing_foot_zmp_offsets.front()(0));
153  } else {
154  first_zmp_offset_x = toe_zmp_offset_x;
155  second_zmp_offset_x = heel_zmp_offset_x;
156  }
157  if (thp.is_between_phases(cnt, SOLE0)) {
158  double ratio = thp.calc_phase_ratio(cnt+1, SOLE0);
159  swing_foot_zmp_offsets.front()(0) = (1-ratio)*swing_foot_zmp_offsets.front()(0) + ratio*first_zmp_offset_x;
160  } else if (thp.is_between_phases(cnt, HEEL2SOLE, SOLE2)) {
161  double ratio = thp.calc_phase_ratio(cnt, HEEL2SOLE, SOLE2);
162  swing_foot_zmp_offsets.front()(0) = ratio*swing_foot_zmp_offsets.front()(0) + (1-ratio)*second_zmp_offset_x;
163  } else if (thp.is_between_phases(cnt, SOLE0, SOLE2TOE)) {
164  swing_foot_zmp_offsets.front()(0) = first_zmp_offset_x;
165  } else if (thp.is_between_phases(cnt, SOLE2HEEL, HEEL2SOLE)) {
166  swing_foot_zmp_offsets.front()(0) = second_zmp_offset_x;
167  } else if (thp.is_between_phases(cnt, SOLE2TOE, SOLE2HEEL)) {
168  double ratio = thp.calc_phase_ratio(cnt, SOLE2TOE, SOLE2HEEL);
169  swing_foot_zmp_offsets.front()(0) = ratio * second_zmp_offset_x + (1-ratio) * first_zmp_offset_x;
170  }
171  zmp_diff = swing_foot_zmp_offsets.front()(0)-default_zmp_offsets[swing_leg_types_list[refzmp_index].front()](0);
172  if ((is_second_phase() && ( cnt < double_support_count_half_before )) ||
173  (is_second_last_phase() && ( cnt > one_step_count - double_support_count_half_after ))) {
174  // "* 0.5" is for double supprot period
175  zmp_diff *= 0.5;
176  }
177  }
178 
179  // Calculate total reference ZMP
182  } else if ( cnt < double_support_static_count_half_before ) { // Start double support static period
183  hrp::Vector3 current_support_zmp = refzmp_cur_list[refzmp_index];
184  hrp::Vector3 prev_support_zmp = refzmp_cur_list[refzmp_index-1] + zmp_diff * foot_x_axises_list[refzmp_index-1].front();
185  double ratio = (is_second_phase()?1.0:0.5);
186  ret = (1 - ratio) * current_support_zmp + ratio * prev_support_zmp;
187  } else if ( cnt > one_step_count - double_support_static_count_half_after ) { // End double support static period
188  hrp::Vector3 current_support_zmp = refzmp_cur_list[refzmp_index+1] + zmp_diff * foot_x_axises_list[refzmp_index+1].front();
189  hrp::Vector3 prev_support_zmp = refzmp_cur_list[refzmp_index];
190  double ratio = (is_second_last_phase()?1.0:0.5);
191  ret = (1 - ratio) * prev_support_zmp + ratio * current_support_zmp;
192  } else if ( cnt < double_support_count_half_before ) { // Start double support period
193  hrp::Vector3 current_support_zmp = refzmp_cur_list[refzmp_index];
194  hrp::Vector3 prev_support_zmp = refzmp_cur_list[refzmp_index-1] + zmp_diff * foot_x_axises_list[refzmp_index-1].front();
195  double ratio = ((is_second_phase()?1.0:0.5) / (double_support_count_half_before-double_support_static_count_half_before)) * (double_support_count_half_before-cnt);
196  ret = (1 - ratio) * current_support_zmp + ratio * prev_support_zmp;
197  } else if ( cnt > one_step_count - double_support_count_half_after ) { // End double support period
198  hrp::Vector3 current_support_zmp = refzmp_cur_list[refzmp_index+1] + zmp_diff * foot_x_axises_list[refzmp_index+1].front();
199  hrp::Vector3 prev_support_zmp = refzmp_cur_list[refzmp_index];
200  double ratio = ((is_second_last_phase()?1.0:0.5) / (double_support_count_half_after-double_support_static_count_half_after)) * (cnt - 1 - (one_step_count - double_support_count_half_after));
201  ret = (1 - ratio) * prev_support_zmp + ratio * current_support_zmp;
202  } else {
204  }
205  };
206 
208  {
209  if ( 1 <= refzmp_count ) {
210  refzmp_count--;
211  } else {
212  refzmp_index++;
213  // Check length of step_count_list and refzmp_index
214  // The case if !(refzmp_index <= step_count_list.size()-1) is finalizing of gait_generator.
215  // If finalizing, this can be neglected.
216  if (refzmp_index <= step_count_list.size()-1) {
219  }
220  //std::cerr << "fs " << fs_index << "/" << fnl.size() << " rf " << refzmp_index << "/" << refzmp_cur_list.size() << " flg " << std::endl;
221  }
222  };
223 
224  void leg_coords_generator::calc_current_swing_foot_rot (std::map<leg_type, hrp::Vector3>& tmp_swing_foot_rot, const double _default_double_support_ratio_before, const double _default_double_support_ratio_after)
225  {
226  // interpolation
227  int support_len_before = one_step_count * _default_double_support_ratio_before;
228  int support_len_after = one_step_count * _default_double_support_ratio_after;
229  int current_swing_count = (one_step_count - lcg_count); // 0->one_step_count
230  // swing foot rot interpolator interpolates difference from src to dst.
231  if (current_swing_count == support_len_before) {
232  for (std::vector<step_node>::iterator it = swing_leg_src_steps.begin(); it != swing_leg_src_steps.end(); it++) {
233  swing_foot_rot_interpolator[it->l_r]->clear();
234  double tmp[3] = {};
235  swing_foot_rot_interpolator[it->l_r]->set(tmp);
236  }
237  int swing_len = one_step_count - support_len_before - support_len_after;
238  for (size_t ii = 0; ii < swing_leg_dst_steps.size(); ii++) {
239  leg_type lt = swing_leg_dst_steps[ii].l_r;
240  swing_foot_rot_interpolator[lt]->setGoal(hrp::rpyFromRot(swing_leg_src_steps[ii].worldcoords.rot.transpose() * swing_leg_dst_steps[ii].worldcoords.rot).data(),
241  dt * swing_len);
242  swing_foot_rot_interpolator[lt]->sync();
243  }
244  } else if ( (current_swing_count > support_len_before) && (current_swing_count < (one_step_count-support_len_after) ) ) {
245  int tmp_len = (lcg_count - support_len_after);
246  for (size_t ii = 0; ii < swing_leg_dst_steps.size(); ii++) {
247  leg_type lt = swing_leg_dst_steps[ii].l_r;
248  swing_foot_rot_interpolator[lt]->setGoal(hrp::rpyFromRot(swing_leg_src_steps[ii].worldcoords.rot.transpose() * swing_leg_dst_steps[ii].worldcoords.rot).data(),
249  dt * tmp_len);
250  swing_foot_rot_interpolator[lt]->sync();
251  }
252  }
253  for (size_t ii = 0; ii < swing_leg_dst_steps.size(); ii++) {
254  hrp::Vector3 tmpv;
255  if ( !swing_foot_rot_interpolator[swing_leg_dst_steps[ii].l_r]->isEmpty() ) {
256  swing_foot_rot_interpolator[swing_leg_dst_steps[ii].l_r]->get(tmpv.data(), true);
257  } else {
258  if ( (current_swing_count < support_len_before) ) {
259  tmpv = hrp::Vector3::Zero();
260  } else if (current_swing_count >= (one_step_count-support_len_after)) {
261  tmpv = hrp::rpyFromRot(swing_leg_src_steps[ii].worldcoords.rot.transpose() * swing_leg_dst_steps[ii].worldcoords.rot);
262  }
263  }
264  tmp_swing_foot_rot.insert(std::pair<leg_type, hrp::Vector3>(swing_leg_dst_steps[ii].l_r, tmpv));
265  }
266  };
267 
268  /* member function implementation for leg_coords_generator */
269  void leg_coords_generator::calc_current_swing_leg_steps (std::vector<step_node>& rets, const double step_height, const double _current_toe_angle, const double _current_heel_angle, const double _default_double_support_ratio_before, const double _default_double_support_ratio_after)
270  {
271  /* match the src step order and the dst step order */
272  std::sort(swing_leg_src_steps.begin(), swing_leg_src_steps.end(),
273  ((&boost::lambda::_1->* &step_node::l_r) < (&boost::lambda::_2->* &step_node::l_r)));
274  std::sort(swing_leg_dst_steps.begin(), swing_leg_dst_steps.end(),
275  ((&boost::lambda::_1->* &step_node::l_r) < (&boost::lambda::_2->* &step_node::l_r)));
276  std::map<leg_type, hrp::Vector3> tmp_swing_foot_rot;
277  calc_current_swing_foot_rot(tmp_swing_foot_rot, _default_double_support_ratio_before, _default_double_support_ratio_after);
278  size_t swing_trajectory_generator_idx = 0;
279  for (std::vector<step_node>::iterator it1 = swing_leg_src_steps.begin(), it2 = swing_leg_dst_steps.begin();
280  it1 != swing_leg_src_steps.end() && it2 != swing_leg_dst_steps.end();
281  it1++, it2++) {
283  ret.rot = it1->worldcoords.rot * hrp::rotFromRpy(tmp_swing_foot_rot[it2->l_r]);
284  switch (default_orbit_type) {
285  case SHUFFLING:
286  ret.pos = swing_ratio*it1->worldcoords.pos + (1-swing_ratio)*it2->worldcoords.pos;
287  break;
288  case CYCLOID:
289  cycloid_midcoords(ret, it1->worldcoords, it2->worldcoords, step_height);
290  break;
291  case RECTANGLE:
292  rectangle_midcoords(ret, it1->worldcoords, it2->worldcoords, step_height, swing_trajectory_generator_idx);
293  break;
294  case STAIR:
295  stair_midcoords(ret, it1->worldcoords, it2->worldcoords, step_height);
296  break;
297  case CYCLOIDDELAY:
298  cycloid_delay_midcoords(ret, it1->worldcoords, it2->worldcoords, step_height, swing_trajectory_generator_idx);
299  break;
300  case CYCLOIDDELAYKICK:
301  cycloid_delay_kick_midcoords(ret, it1->worldcoords, it2->worldcoords, step_height);
302  break;
303  case CROSS:
304  cross_delay_midcoords(ret, it1->worldcoords, it2->worldcoords, step_height, it1->l_r);
305  break;
306  default: break;
307  }
308  swing_trajectory_generator_idx++;
309  if (std::fabs(step_height) > 1e-3*10) {
310  if (swing_leg_src_steps.size() == 1) /* only biped or crawl because there is only one toe_heel_interpolator */
311  modif_foot_coords_for_toe_heel_phase(ret, _current_toe_angle, _current_heel_angle);
312  }
313  rets.push_back(step_node(it1->l_r, ret, 0, 0, 0, 0));
314  }
315  };
316 
317  void leg_coords_generator::calc_ratio_from_double_support_ratio (const double default_double_support_ratio_before, const double default_double_support_ratio_after)
318  {
319  int support_len_before = one_step_count * default_double_support_ratio_before;
320  int support_len_after = one_step_count * default_double_support_ratio_after;
321  // int support_len = 2*static_cast<int>(one_step_count * default_double_support_ratio * 0.5);
322  int swing_len = one_step_count - support_len_before - support_len_after;
323  int current_swing_len = lcg_count - support_len_before;
324  double tmp_current_swing_time;
325  int current_swing_count = (one_step_count - lcg_count); // 0->one_step_count
326  if ( current_swing_count < support_len_before ) { // First double support period
327  swing_ratio = 0.0;
328  tmp_current_swing_time = current_swing_len * dt - swing_len * dt;
329  is_swing_phase = false;
330  } else if ( current_swing_count >= support_len_before+swing_len ) { // Last double support period
331  swing_ratio = 1.0;
332  tmp_current_swing_time = current_swing_len * dt + (support_len_before + support_len_after + next_one_step_count) * dt;
333  is_swing_phase = false;
334  } else {
335  tmp_current_swing_time = current_swing_len * dt;
336  swing_ratio = static_cast<double>(current_swing_count-support_len_before)/swing_len;
337  //std::cerr << "gp " << swing_ratio << " " << swing_rot_ratio << std::endl;
338  if (current_step_height > 0.0) is_swing_phase = true;
339  else is_swing_phase = false;
340  }
341  for (std::vector<leg_type>::const_iterator it = support_leg_types.begin(); it != support_leg_types.end(); it++) {
342  current_swing_time.at(*it) = (lcg_count + default_double_support_ratio_before * next_one_step_count) * dt;
343  }
344  for (std::vector<leg_type>::const_iterator it = swing_leg_types.begin(); it != swing_leg_types.end(); it++) {
345  if (current_step_height > 0.0) {
346  current_swing_time.at(*it) = tmp_current_swing_time;
347  } else {
348  current_swing_time.at(*it) = (lcg_count + default_double_support_ratio_before * next_one_step_count) * dt;
349  }
350  }
351  //std::cerr << "sl " << support_leg << " " << current_swing_time[support_leg==RLEG?0:1] << " " << current_swing_time[support_leg==RLEG?1:0] << " " << tmp_current_swing_time << " " << lcg_count << std::endl;
352  };
353 
354  double leg_coords_generator::calc_interpolated_toe_heel_angle (const toe_heel_phase start_phase, const toe_heel_phase goal_phase, const double start, const double goal)
355  {
356  double tmp_ip_ratio;
357  size_t current_count = one_step_count - lcg_count;
358  if (thp.is_phase_starting(current_count, start_phase)) {
359  toe_heel_interpolator->clear();
360  toe_heel_interpolator->set(&start);
361  //toe_heel_interpolator->go(&goal, thp.calc_phase_period(start_phase, goal_phase, dt));
362  toe_heel_interpolator->setGoal(&goal, thp.calc_phase_period(start_phase, goal_phase, dt));
363  toe_heel_interpolator->sync();
364  }
365  if (!toe_heel_interpolator->isEmpty()) {
366  toe_heel_interpolator->get(&tmp_ip_ratio, true);
367  } else {
368  toe_heel_interpolator->get(&tmp_ip_ratio, false);
369  }
370  return tmp_ip_ratio;
371  };
372 
373  void leg_coords_generator::modif_foot_coords_for_toe_heel_phase (coordinates& org_coords, const double _current_toe_angle, const double _current_heel_angle)
374  {
375  coordinates new_coords;
376  size_t current_count = one_step_count - lcg_count;
377  double dif_angle = 0.0;
378  hrp::Vector3 ee_local_pivot_pos(hrp::Vector3(0,0,0));
379  double first_goal_angle, second_goal_angle, first_pos_offset_x, second_pos_offset_x;
380  if (use_toe_heel_auto_set) {
381  first_goal_angle = set_value_according_to_toe_heel_type(current_src_toe_heel_type, _current_toe_angle, -1 * _current_heel_angle, 0);
382  second_goal_angle = set_value_according_to_toe_heel_type(current_dst_toe_heel_type, _current_toe_angle, -1 * _current_heel_angle, 0);
383  first_pos_offset_x = set_value_according_to_toe_heel_type(current_src_toe_heel_type, toe_pos_offset_x, heel_pos_offset_x, 0);
384  second_pos_offset_x = set_value_according_to_toe_heel_type(current_dst_toe_heel_type, toe_pos_offset_x, heel_pos_offset_x, 0);
385  } else {
386  first_goal_angle = _current_toe_angle;
387  second_goal_angle = -1 * _current_heel_angle;
388  first_pos_offset_x = toe_pos_offset_x;
389  second_pos_offset_x = heel_pos_offset_x;
390  }
391  if ( thp.is_between_phases(current_count, SOLE0, SOLE2TOE) ) {
392  dif_angle = calc_interpolated_toe_heel_angle(SOLE0, SOLE2TOE, 0.0, first_goal_angle);
393  ee_local_pivot_pos(0) = first_pos_offset_x;
394  } else if ( thp.is_between_phases(current_count, SOLE2HEEL, HEEL2SOLE) ) {
395  dif_angle = calc_interpolated_toe_heel_angle(SOLE2HEEL, HEEL2SOLE, second_goal_angle, 0.0);
396  ee_local_pivot_pos(0) = second_pos_offset_x;
397  } else if ( thp.is_between_phases(current_count, SOLE2TOE, SOLE2HEEL) ) {
398  // If SOLE1 phase does not exist, interpolate toe => heel smoothly, without 0 velocity phase.
399  if ( thp.is_no_SOLE1_phase() ) {
400  dif_angle = calc_interpolated_toe_heel_angle(SOLE2TOE, SOLE2HEEL, first_goal_angle, second_goal_angle);
401  double tmpd = (second_goal_angle-first_goal_angle);
402  if (std::fabs(tmpd) > 1e-5) {
403  ee_local_pivot_pos(0) = (second_pos_offset_x - first_pos_offset_x) * (dif_angle - first_goal_angle) / tmpd + first_pos_offset_x;
404  } else {
405  ee_local_pivot_pos(0) = first_pos_offset_x;
406  }
407  } else {
408  if ( thp.is_between_phases(current_count, SOLE2TOE, TOE2SOLE) ) {
409  dif_angle = calc_interpolated_toe_heel_angle(SOLE2TOE, TOE2SOLE, first_goal_angle, 0.0);
410  ee_local_pivot_pos(0) = first_pos_offset_x;
411  } else if ( thp.is_between_phases(current_count, SOLE1, SOLE2HEEL) ) {
412  dif_angle = calc_interpolated_toe_heel_angle(SOLE1, SOLE2HEEL, 0.0, second_goal_angle);
413  ee_local_pivot_pos(0) = second_pos_offset_x;
414  }
415  }
416  }
417  foot_dif_rot_angle = (dif_angle > 0.0 ? deg2rad(dif_angle) : 0.0);
418  if (use_toe_joint && dif_angle > 0.0) dif_angle = 0.0;
419  toe_heel_dif_angle = dif_angle;
420  Eigen::AngleAxis<double> tmpr(deg2rad(dif_angle), hrp::Vector3::UnitY());
421  rotm3times(new_coords.rot, org_coords.rot, tmpr.toRotationMatrix());
422  new_coords.pos = org_coords.pos + org_coords.rot * ee_local_pivot_pos - new_coords.rot * ee_local_pivot_pos;
423  org_coords = new_coords;
424  };
425 
427  const coordinates& goal, const double height) const
428  {
429  cycloid_midpoint (ret.pos, swing_ratio, start.pos, goal.pos, height, default_top_ratio);
430  };
431 
433  const coordinates& goal, const double height, const size_t swing_trajectory_generator_idx)
434  {
435  rdtg[swing_trajectory_generator_idx].get_trajectory_point(ret.pos, hrp::Vector3(start.pos), hrp::Vector3(goal.pos), height);
436  };
437 
439  const coordinates& goal, const double height)
440  {
441  sdtg.get_trajectory_point(ret.pos, hrp::Vector3(start.pos), hrp::Vector3(goal.pos), height);
442  };
443 
445  const coordinates& goal, const double height, const size_t swing_trajectory_generator_idx)
446  {
447  cdtg[swing_trajectory_generator_idx].get_trajectory_point(ret.pos, hrp::Vector3(start.pos), hrp::Vector3(goal.pos), height);
448  };
449 
451  const coordinates& goal, const double height)
452  {
453  cdktg.set_start_rot(hrp::Matrix33(start.rot));
454  cdktg.get_trajectory_point(ret.pos, hrp::Vector3(start.pos), hrp::Vector3(goal.pos), height);
455  };
456 
458  const coordinates& goal, const double height, leg_type lr)
459  {
460  crdtg.set_swing_leg(lr);
461  crdtg.get_trajectory_point(ret.pos, hrp::Vector3(start.pos), hrp::Vector3(goal.pos), height);
462  };
463 
464  bool leg_coords_generator::is_same_footstep_nodes(const std::vector<step_node>& fns_1, const std::vector<step_node>& fns_2) const
465  {
466  bool matching_flag = true;
467  if (fns_1.size() == fns_2.size()) {
468  for (std::vector<step_node>::const_iterator it1 = fns_1.begin(); it1 != fns_1.end(); it1++) {
469  std::vector<step_node>::const_iterator it2 = std::find_if(fns_2.begin(), fns_2.end(), (&boost::lambda::_1->* &step_node::l_r == it1->l_r));
470  if (it2 == fns_2.end()) {
471  matching_flag = false;
472  break;
473  }
474  }
475  } else {
476  matching_flag = false;
477  }
478  return matching_flag;
479  };
480 
482  {
483  std::vector<coordinates> swg_src_coords, swg_dst_coords,sup_coords;
484  for (std::vector<step_node>::const_iterator it = swing_leg_src_steps.begin(); it != swing_leg_src_steps.end(); it++) {
485  if (it->l_r == RLEG or it->l_r == LLEG) swg_src_coords.push_back(it->worldcoords);
486  }
487  for (std::vector<step_node>::const_iterator it = swing_leg_dst_steps.begin(); it != swing_leg_dst_steps.end(); it++) {
488  if (it->l_r == RLEG or it->l_r == LLEG) swg_dst_coords.push_back(it->worldcoords);
489  }
490  for (std::vector<step_node>::const_iterator it = support_leg_steps.begin(); it != support_leg_steps.end(); it++) {
491  if (it->l_r == RLEG or it->l_r == LLEG) sup_coords.push_back(it->worldcoords);
492  }
493  coordinates tmp_swg_src_mid, tmp_swg_dst_mid, tmp_swg_mid, tmp_sup_mid;
494  const double rot_eps = 1e-5; // eps for mid_rot calculation
495  if (swg_src_coords.size() > 0) multi_mid_coords(tmp_swg_src_mid, swg_src_coords, rot_eps);
496  if (swg_dst_coords.size() > 0) multi_mid_coords(tmp_swg_dst_mid, swg_dst_coords, rot_eps);
497  if (sup_coords.size() > 0) multi_mid_coords(tmp_sup_mid, sup_coords, rot_eps);
498  if (lcg_count == one_step_count) {
499  foot_midcoords_interpolator->clear();
500  double tmp[foot_midcoords_interpolator->dimension()];
501  for (size_t ii = 0; ii < 3; ii++) {
502  tmp[ii] = tmp_swg_src_mid.pos(ii);
503  tmp[ii+3] = 0;
504  }
505  foot_midcoords_interpolator->set(tmp);
506  // set dst
507  hrp::Matrix33 difrot(tmp_swg_src_mid.rot.transpose() * tmp_swg_dst_mid.rot);
508  hrp::Vector3 tmpr = hrp::rpyFromRot(difrot);
509  for (size_t ii = 0; ii < 3; ii++) {
510  tmp[ii] = tmp_swg_dst_mid.pos(ii);
511  tmp[ii+3] = tmpr(ii);
512  }
513  foot_midcoords_interpolator->setGoal(tmp, dt*one_step_count, true);
514  foot_midcoords_interpolator->sync();
515  } else {
516  double tmp[foot_midcoords_interpolator->dimension()];
517  hrp::Matrix33 difrot(tmp_swg_src_mid.rot.transpose() * tmp_swg_dst_mid.rot);
518  hrp::Vector3 tmpr = hrp::rpyFromRot(difrot);
519  for (size_t ii = 0; ii < 3; ii++) {
520  tmp[ii] = tmp_swg_dst_mid.pos(ii);
521  tmp[ii+3] = tmpr(ii);
522  }
523  foot_midcoords_interpolator->setGoal(tmp, dt*lcg_count, true);
524  foot_midcoords_interpolator->sync();
525  }
526  if (!foot_midcoords_interpolator->isEmpty()) {
527  double tmp[foot_midcoords_interpolator->dimension()];
528  foot_midcoords_interpolator->get(tmp, true);
529  hrp::Vector3 tmpr;
530  for (size_t ii = 0; ii < 3; ii++) {
531  tmp_swg_mid.pos(ii) = tmp[ii];
532  tmpr(ii) = tmp[ii+3];
533  }
534  tmp_swg_mid.rot = tmp_swg_src_mid.rot * hrp::rotFromRpy(tmpr);
535  } else {
536  tmp_swg_mid = tmp_swg_dst_mid;
537  }
538  mid_coords(swing_support_midcoords, static_cast<double>(sup_coords.size()) / (swg_src_coords.size() + sup_coords.size()), tmp_swg_mid, tmp_sup_mid, rot_eps);
539  };
540 
541  void leg_coords_generator::update_leg_steps (const std::vector< std::vector<step_node> >& fnsl, const double default_double_support_ratio_before, const double default_double_support_ratio_after, const toe_heel_type_checker& thtc)
542  {
543  // Get current swing coords, support coords, and support leg parameters
544  calc_swing_support_params_from_footstep_nodes_list(fnsl);
545  current_src_toe_heel_type = thtc.check_toe_heel_type_from_swing_support_coords(swing_leg_src_steps.front().worldcoords, support_leg_steps.front().worldcoords, toe_pos_offset_x, heel_pos_offset_x);
546  current_dst_toe_heel_type = thtc.check_toe_heel_type_from_swing_support_coords(swing_leg_dst_steps.front().worldcoords, support_leg_steps.front().worldcoords, toe_pos_offset_x, heel_pos_offset_x);
547  calc_swing_support_mid_coords ();
548 
549  calc_ratio_from_double_support_ratio(default_double_support_ratio_before, default_double_support_ratio_after);
550  swing_leg_steps.clear();
551  calc_current_swing_leg_steps(swing_leg_steps, current_step_height, current_toe_angle, current_heel_angle, default_double_support_ratio_before, default_double_support_ratio_after);
552  if ( 1 <= lcg_count ) {
553  lcg_count--;
554  } else {
555  //std::cerr << "gp " << footstep_index << std::endl;
556  if (footstep_index < fnsl.size() - 1) {
557  footstep_index++;
558  }
559  if (footstep_index < fnsl.size() - 1) {
560  current_step_height = fnsl[footstep_index].front().step_height;
561  current_toe_angle = fnsl[footstep_index].front().toe_angle;
562  current_heel_angle = fnsl[footstep_index].front().heel_angle;
563  } else {
564  current_step_height = current_toe_angle = current_heel_angle = 0.0;
565  }
566  if (footstep_index < fnsl.size()) {
567  one_step_count = static_cast<size_t>(fnsl[footstep_index].front().step_time/dt);
569  }
570  if (footstep_index + 1 < fnsl.size()) {
571  next_one_step_count = static_cast<size_t>(fnsl[footstep_index+1].front().step_time/dt);
572  }
573  lcg_count = one_step_count;
574  switch (default_orbit_type) {
575  case RECTANGLE:
576  for (size_t i = 0; i < rdtg.size(); i++)
577  rdtg[i].reset(one_step_count, default_double_support_ratio_before, default_double_support_ratio_after);
578  break;
579  case STAIR:
580  sdtg.reset(one_step_count, default_double_support_ratio_before, default_double_support_ratio_after);
581  break;
582  case CYCLOIDDELAY:
583  for (size_t i = 0; i < cdtg.size(); i++)
584  cdtg[i].reset(one_step_count, default_double_support_ratio_before, default_double_support_ratio_after);
585  break;
586  case CYCLOIDDELAYKICK:
587  cdktg.reset(one_step_count, default_double_support_ratio_before, default_double_support_ratio_after);
588  break;
589  case CROSS:
590  crdtg.reset(one_step_count, default_double_support_ratio_before, default_double_support_ratio_after);
591  break;
592  default:
593  break;
594  }
595  }
596  };
597 
598  /* member function implementation for gait_generator */
600  const std::vector<step_node>& initial_support_leg_steps,
601  const std::vector<step_node>& initial_swing_leg_dst_steps,
602  const double delay)
603  {
604  /* clear all gait_parameter */
605  size_t one_step_len = footstep_nodes_list.front().front().step_time / dt;
606  finalize_count = 0;
607  for (std::vector<step_node>::iterator it_fns = footstep_nodes_list.front().begin(); it_fns != footstep_nodes_list.front().end(); it_fns++) {
608  for (std::vector<step_node>::const_iterator it_init = initial_swing_leg_dst_steps.begin(); it_init != initial_swing_leg_dst_steps.end(); it_init++) {
609  if (it_fns->l_r == it_init->l_r) {
610  /* initial_swing_leg_dst_steps has dummy step_height, step_time, toe_angle and heel_angle. */
611  it_fns->worldcoords = it_init->worldcoords;
612  break;
613  }
614  }
615  }
616  // get initial_foot_mid_coords
617  std::vector<coordinates> cv;
618  for (size_t i = 0; i < initial_support_leg_steps.size(); i++) {
619  cv.push_back(initial_support_leg_steps[i].worldcoords);
620  }
621  for (size_t i = 0; i < initial_swing_leg_dst_steps.size(); i++) {
622  cv.push_back(initial_swing_leg_dst_steps[i].worldcoords);
623  }
624  multi_mid_coords(initial_foot_mid_coords, cv);
625  // rg+lcg initialization
626  rg.reset(one_step_len);
627  rg.push_refzmp_from_footstep_nodes_for_dual(footstep_nodes_list.front(), initial_support_leg_steps, initial_swing_leg_dst_steps);
628  if ( preview_controller_ptr != NULL ) {
629  delete preview_controller_ptr;
630  preview_controller_ptr = NULL;
631  }
632  //preview_controller_ptr = new preview_dynamics_filter<preview_control>(dt, cog(2) - refzmp_cur_list[0](2), refzmp_cur_list[0]);
633  preview_controller_ptr = new preview_dynamics_filter<extended_preview_control>(dt, cog(2) - rg.get_refzmp_cur()(2), rg.get_refzmp_cur(), gravitational_acceleration);
634  lcg.reset(one_step_len, footstep_nodes_list.at(1).front().step_time/dt, initial_swing_leg_dst_steps, initial_swing_leg_dst_steps, initial_support_leg_steps, default_double_support_ratio_swing_before, default_double_support_ratio_swing_after);
635  /* make another */
636  lcg.set_swing_support_steps_list(footstep_nodes_list);
637  for (size_t i = 1; i < footstep_nodes_list.size()-1; i++) {
638  std::vector<step_node> tmp_swing_leg_src_steps;
639  lcg.calc_swing_leg_src_steps(tmp_swing_leg_src_steps, footstep_nodes_list, i);
640  toe_heel_types tht(thtc.check_toe_heel_type_from_swing_support_coords(tmp_swing_leg_src_steps.front().worldcoords, lcg.get_support_leg_steps_idx(i).front().worldcoords, lcg.get_toe_pos_offset_x(), lcg.get_heel_pos_offset_x()),
641  thtc.check_toe_heel_type_from_swing_support_coords(lcg.get_swing_leg_dst_steps_idx(i).front().worldcoords, lcg.get_support_leg_steps_idx(i).front().worldcoords, lcg.get_toe_pos_offset_x(), lcg.get_heel_pos_offset_x()));
642  rg.push_refzmp_from_footstep_nodes_for_single(footstep_nodes_list.at(i), lcg.get_support_leg_steps_idx(i), tht);
643  }
644  rg.push_refzmp_from_footstep_nodes_for_dual(footstep_nodes_list.back(),
645  lcg.get_support_leg_steps_idx(footstep_nodes_list.size()-1),
646  lcg.get_swing_leg_dst_steps_idx(footstep_nodes_list.size()-1));
647  emergency_flg = IDLING;
648  };
649 
651  {
652  solved = false;
653  /* update refzmp */
654  if (emergency_flg == EMERGENCY_STOP && lcg.get_footstep_index() > 0) {
655  leg_type cur_leg = footstep_nodes_list[lcg.get_footstep_index()].front().l_r;
656  leg_type first_step = overwritable_footstep_index_offset % 2 == 0 ? cur_leg : (cur_leg == RLEG ? LLEG : RLEG);
657 
658  overwrite_footstep_nodes_list.push_back(boost::assign::list_of(step_node(first_step, footstep_nodes_list[get_overwritable_index() - 2].front().worldcoords, 0, default_step_time, 0, 0)));
659  overwrite_footstep_nodes_list.push_back(boost::assign::list_of(step_node(first_step==RLEG?LLEG:RLEG, footstep_nodes_list[get_overwritable_index() - 1].front().worldcoords, 0, default_step_time, 0, 0)));
660  overwrite_footstep_nodes_list.push_back(boost::assign::list_of(step_node(first_step, footstep_nodes_list[get_overwritable_index() - 2].front().worldcoords, 0, default_step_time, 0, 0)));
661 
662  overwrite_refzmp_queue(overwrite_footstep_nodes_list);
663  overwrite_footstep_nodes_list.clear();
664  emergency_flg = STOPPING;
665  } else if ( lcg.get_lcg_count() == get_overwrite_check_timing() ) {
666  if (velocity_mode_flg != VEL_IDLING && lcg.get_footstep_index() > 0) {
667  std::vector< std::vector<step_node> > cv;
668  calc_next_coords_velocity_mode(cv, get_overwritable_index(),
669  (overwritable_footstep_index_offset == 0 ? 4 : 3) // Why?
670  );
671  if (velocity_mode_flg == VEL_ENDING) velocity_mode_flg = VEL_IDLING;
672  std::vector<leg_type> first_overwrite_leg;
673  for (size_t i = 0; i < footstep_nodes_list[get_overwritable_index()].size(); i++) {
674  first_overwrite_leg.push_back(footstep_nodes_list[get_overwritable_index()].at(i).l_r);
675  }
676  for (size_t i = 0; i < cv.size(); i++) {
677  std::vector<step_node> tmp_fsn;
678  for (size_t j = 0; j < cv.at(i).size(); j++) {
679  cv.at(i).at(j).worldcoords.pos += modified_d_footstep;
680  tmp_fsn.push_back(step_node(cv.at(i).at(j).l_r, cv.at(i).at(j).worldcoords,
681  lcg.get_default_step_height(), default_step_time, lcg.get_toe_angle(), lcg.get_heel_angle()));
682  }
683  overwrite_footstep_nodes_list.push_back(tmp_fsn);
684  }
685  overwrite_refzmp_queue(overwrite_footstep_nodes_list);
686  overwrite_footstep_nodes_list.clear();
687  } else if ( !overwrite_footstep_nodes_list.empty() && // If overwrite_footstep_node_list exists
688  (lcg.get_footstep_index() < footstep_nodes_list.size()-1) && // If overwrite_footstep_node_list is specified and current footstep is not last footstep.
689  get_overwritable_index() == overwrite_footstep_index ) {
690  overwrite_refzmp_queue(overwrite_footstep_nodes_list);
691  overwrite_footstep_nodes_list.clear();
692  }
693  }
694  // limit stride
695  if (use_stride_limitation && lcg.get_footstep_index() > 0 && lcg.get_footstep_index() < footstep_nodes_list.size()-overwritable_footstep_index_offset-2 &&
696  (overwritable_footstep_index_offset == 0 || lcg.get_lcg_count() == get_overwrite_check_timing())) {
697  if (lcg.get_footstep_index() == footstep_nodes_list.size()-overwritable_footstep_index_offset-3) {
698  hrp::Vector3 orig_footstep_pos = footstep_nodes_list[get_overwritable_index()].front().worldcoords.pos;
699  limit_stride(footstep_nodes_list[get_overwritable_index()].front(), footstep_nodes_list[get_overwritable_index()-1].front(), overwritable_stride_limitation);
700  for (size_t i = get_overwritable_index() + 1; i < footstep_nodes_list.size(); i++) {
701  footstep_nodes_list[i].front().worldcoords.pos -= orig_footstep_pos - footstep_nodes_list[get_overwritable_index()].front().worldcoords.pos;
702  }
703  } else {
704  limit_stride(footstep_nodes_list[get_overwritable_index()].front(), footstep_nodes_list[get_overwritable_index()-1].front(), overwritable_stride_limitation);
705  }
706  overwrite_footstep_nodes_list.insert(overwrite_footstep_nodes_list.end(), footstep_nodes_list.begin()+get_overwritable_index(), footstep_nodes_list.end());
707  overwrite_refzmp_queue(overwrite_footstep_nodes_list);
708  overwrite_footstep_nodes_list.clear();
709  }
710  // modify footsteps based on diff_cp
711  if(modify_footsteps) modify_footsteps_for_recovery();
712 
713  if ( !solved ) {
714  hrp::Vector3 rzmp;
715  std::vector<hrp::Vector3> sfzos;
716  bool refzmp_exist_p = rg.get_current_refzmp(rzmp, sfzos, default_double_support_ratio_before, default_double_support_ratio_after, default_double_support_static_ratio_before, default_double_support_static_ratio_after);
717  if (!refzmp_exist_p) {
718  finalize_count++;
719  rzmp = prev_que_rzmp;
720  sfzos = prev_que_sfzos;
721  } else {
722  prev_que_rzmp = rzmp;
723  prev_que_sfzos = sfzos;
724  }
725  solved = preview_controller_ptr->update(refzmp, cog, swing_foot_zmp_offsets, rzmp, sfzos, (refzmp_exist_p || finalize_count < preview_controller_ptr->get_delay()-default_step_time/dt));
726  }
727 
728  rg.update_refzmp();
729  // { // debug
730  // double cart_zmp[3];
731  // preview_controller_ptr->get_cart_zmp(cart_zmp);
732  // std::cerr << "(list " << std::endl;
733  // std::cerr << ":cog "; print_vector(std::cerr, cog);
734  // std::cerr << ":refzmp "; print_vector(std::cerr, refzmp);
735  // std::cerr << ":cart-zmp "; print_vector(std::cerr, cart_zmp, 3);
736  // std::cerr << ")" << std::endl;
737  // }
738 
739  /* update swing_leg_coords, support_leg_coords */
740  if ( solved ) {
741  lcg.update_leg_steps(footstep_nodes_list, default_double_support_ratio_swing_before, default_double_support_ratio_swing_after, thtc);
742  } else if (finalize_count>0) {
743  lcg.clear_interpolators();
744  }
745  return solved;
746  };
747 
748  void gait_generator::limit_stride (step_node& cur_fs, const step_node& prev_fs, const double (&limit)[5]) const
749  {
750  // limit[5] = {forward, outside, theta, backward, inside}
751  leg_type cur_leg = cur_fs.l_r;
752  // prev_fs frame
753  cur_fs.worldcoords.pos = prev_fs.worldcoords.rot.transpose() * (cur_fs.worldcoords.pos - prev_fs.worldcoords.pos);
754  double stride_r = std::pow(cur_fs.worldcoords.pos(0), 2.0) + std::pow(cur_fs.worldcoords.pos(1) + footstep_param.leg_default_translate_pos[cur_leg == LLEG ? RLEG : LLEG](1) - footstep_param.leg_default_translate_pos[cur_leg](1), 2.0);
755  // front, rear, outside limitation
756  double stride_r_limit = std::pow(std::max(limit[cur_fs.worldcoords.pos(0) >= 0 ? 0 : 3], limit[1] - limit[4]), 2.0);
757  if (stride_r > stride_r_limit && (cur_leg == LLEG ? 1 : -1) * cur_fs.worldcoords.pos(1) > footstep_param.leg_default_translate_pos[LLEG](1) - footstep_param.leg_default_translate_pos[RLEG](1)) {
758  cur_fs.worldcoords.pos(0) *= sqrt(stride_r_limit / stride_r);
759  cur_fs.worldcoords.pos(1) = footstep_param.leg_default_translate_pos[cur_leg](1) - footstep_param.leg_default_translate_pos[cur_leg == LLEG ? RLEG : LLEG](1) +
760  sqrt(stride_r_limit / stride_r) * (cur_fs.worldcoords.pos(1) + footstep_param.leg_default_translate_pos[cur_leg == LLEG ? RLEG : LLEG](1) - footstep_param.leg_default_translate_pos[cur_leg](1));
761  }
762  if (cur_fs.worldcoords.pos(0) > limit[0]) cur_fs.worldcoords.pos(0) = limit[0];
763  if (cur_fs.worldcoords.pos(0) < -1 * limit[0]) cur_fs.worldcoords.pos(0) = -1 * limit[3];
764  if ((cur_leg == LLEG ? 1 : -1) * cur_fs.worldcoords.pos(1) > limit[1]) cur_fs.worldcoords.pos(1) = (cur_leg == LLEG ? 1 : -1) * limit[1];
765  // inside limitation
766  std::vector<double> cur_leg_vertices_y;
767  cur_leg_vertices_y.reserve(4);
768  cur_leg_vertices_y.push_back((cur_fs.worldcoords.pos + prev_fs.worldcoords.rot.transpose() * cur_fs.worldcoords.rot * hrp::Vector3(leg_margin[0], (cur_leg == LLEG ? 1 : -1) * leg_margin[2], 0.0))(1));
769  cur_leg_vertices_y.push_back((cur_fs.worldcoords.pos + prev_fs.worldcoords.rot.transpose() * cur_fs.worldcoords.rot * hrp::Vector3(leg_margin[0], (cur_leg == LLEG ? -1 : 1) * leg_margin[3], 0.0))(1));
770  cur_leg_vertices_y.push_back((cur_fs.worldcoords.pos + prev_fs.worldcoords.rot.transpose() * cur_fs.worldcoords.rot * hrp::Vector3(-1 * leg_margin[1], (cur_leg == LLEG ? 1 : -1) * leg_margin[2], 0.0))(1));
771  cur_leg_vertices_y.push_back((cur_fs.worldcoords.pos + prev_fs.worldcoords.rot.transpose() * cur_fs.worldcoords.rot * hrp::Vector3(-1 * leg_margin[1], (cur_leg == LLEG ? -1 : 1) * leg_margin[3], 0.0))(1));
772  if (cur_leg == LLEG) {
773  if (*std::min_element(cur_leg_vertices_y.begin(), cur_leg_vertices_y.end()) < limit[4]) cur_fs.worldcoords.pos(1) += limit[4] - *std::min_element(cur_leg_vertices_y.begin(), cur_leg_vertices_y.end());
774  } else {
775  if (*std::max_element(cur_leg_vertices_y.begin(), cur_leg_vertices_y.end()) > -1 * limit[4]) cur_fs.worldcoords.pos(1) += -1 * limit[4] - *std::max_element(cur_leg_vertices_y.begin(), cur_leg_vertices_y.end());
776  }
777  // world frame
778  cur_fs.worldcoords.pos = prev_fs.worldcoords.pos + prev_fs.worldcoords.rot * cur_fs.worldcoords.pos;
779  };
780 
782  {
783  if (isfinite(diff_cp(0)) && isfinite(diff_cp(1))) {
784  // calculate diff_cp
785  hrp::Vector3 tmp_diff_cp;
786  for (size_t i = 0; i < 2; i++) {
787  if (std::fabs(diff_cp(i)) > cp_check_margin[i]) {
788  is_emergency_walking[i] = true;
789  tmp_diff_cp(i) = diff_cp(i) - cp_check_margin[i] * diff_cp(i)/std::fabs(diff_cp(i));
790  } else {
791  is_emergency_walking[i] = false;
792  }
793  }
794  if (lcg.get_footstep_index() > 0 && lcg.get_footstep_index() < footstep_nodes_list.size()-2) {
795  // calculate sum of preview_f
796  static double preview_f_sum;
797  if (lcg.get_lcg_count() == static_cast<size_t>(footstep_nodes_list[lcg.get_footstep_index()][0].step_time/dt * 1.0) - 1) {
798  preview_f_sum = preview_controller_ptr->get_preview_f(preview_controller_ptr->get_delay());
799  for (size_t i = preview_controller_ptr->get_delay()-1; i >= lcg.get_lcg_count()+1; i--) {
800  preview_f_sum += preview_controller_ptr->get_preview_f(i);
801  }
802  modified_d_footstep = hrp::Vector3::Zero();
803  }
804  if (lcg.get_lcg_count() <= preview_controller_ptr->get_delay()) {
805  preview_f_sum += preview_controller_ptr->get_preview_f(lcg.get_lcg_count());
806  }
807  // calculate modified footstep position
808  double preview_db = 1/6.0 * dt * dt * dt + 1/2.0 * dt * dt * 1/std::sqrt(gravitational_acceleration / (cog(2) - refzmp(2)));
809  hrp::Vector3 d_footstep = -1/preview_f_sum * 1/preview_db * footstep_modification_gain * tmp_diff_cp;
810  d_footstep(2) = 0.0;
811  // overwrite footsteps
812  if (lcg.get_lcg_count() <= static_cast<size_t>(footstep_nodes_list[lcg.get_footstep_index()][0].step_time/dt * 1.0) - 1 &&
813  lcg.get_lcg_count() >= static_cast<size_t>(footstep_nodes_list[lcg.get_footstep_index()][0].step_time/dt * (default_double_support_ratio_after + margin_time_ratio)) - 1 &&
814  !(lcg.get_lcg_count() <= static_cast<size_t>(footstep_nodes_list[lcg.get_footstep_index()][0].step_time/dt * 0.5) - 1 && act_contact_states[0] && act_contact_states[1])) {
815  // stride limitation check
816  hrp::Vector3 orig_footstep_pos = footstep_nodes_list[get_overwritable_index()].front().worldcoords.pos;
817  for (size_t i = 0; i < 2; i++) {
818  if (is_emergency_walking[i]) footstep_nodes_list[get_overwritable_index()].front().worldcoords.pos(i) += d_footstep(i);
819  }
820  limit_stride(footstep_nodes_list[get_overwritable_index()].front(), footstep_nodes_list[get_overwritable_index()-1].front(), overwritable_stride_limitation);
821  d_footstep = footstep_nodes_list[get_overwritable_index()].front().worldcoords.pos - orig_footstep_pos;
822  for (size_t i = lcg.get_footstep_index()+1; i < footstep_nodes_list.size(); i++) {
823  footstep_nodes_list[i].front().worldcoords.pos += d_footstep;
824  }
825  if (is_emergency_walking[0] || is_emergency_walking[1]) {
826  overwrite_footstep_nodes_list.insert(overwrite_footstep_nodes_list.end(), footstep_nodes_list.begin()+lcg.get_footstep_index(), footstep_nodes_list.end());
827  // overwrite zmp
828  overwrite_refzmp_queue(overwrite_footstep_nodes_list);
829  overwrite_footstep_nodes_list.clear();
830  modified_d_footstep += d_footstep;
831  }
832  }
833  } else {
834  modified_d_footstep = hrp::Vector3::Zero();
835  }
836  }
837  }
838 
839  /* generate vector of step_node from :go-pos params
840  * x, y and theta are simply divided by using stride params
841  * unit system -> x [mm], y [mm], theta [deg]
842  */
843  bool gait_generator::go_pos_param_2_footstep_nodes_list (const double goal_x, const double goal_y, const double goal_theta, /* [mm] [mm] [deg] */
844  const std::vector<coordinates>& initial_support_legs_coords, coordinates start_ref_coords,
845  const std::vector<leg_type>& initial_support_legs,
846  std::vector< std::vector<step_node> >& new_footstep_nodes_list,
847  const bool is_initialize)
848  {
849  // Get overwrite footstep index
850  size_t overwritable_fs_index = 0;
851  if (!is_initialize) {
852  if (lcg.get_footstep_index() <= get_overwrite_check_timing()) { // ending half
853  overwritable_fs_index = get_overwritable_index()+1;
854  } else { // starting half
855  overwritable_fs_index = get_overwritable_index();
856  }
857  }
858  // Check overwritable_fs_index
859  if (overwritable_fs_index > footstep_nodes_list.size()-1) return false;
860  go_pos_param_2_footstep_nodes_list_core (goal_x, goal_y, goal_theta,
861  initial_support_legs_coords, start_ref_coords, initial_support_legs,
862  new_footstep_nodes_list, is_initialize, overwritable_fs_index);
863  // For Last double support period
864  if (is_initialize) {
865  clear_footstep_nodes_list();
866  footstep_nodes_list = new_footstep_nodes_list;
867  } else {
868  set_overwrite_foot_steps_list(new_footstep_nodes_list);
869  set_overwrite_foot_step_index(overwritable_fs_index);
870  }
871  print_footstep_nodes_list();
872  return true;
873  };
874 
875  void gait_generator::go_pos_param_2_footstep_nodes_list_core (const double goal_x, const double goal_y, const double goal_theta, /* [mm] [mm] [deg] */
876  const std::vector<coordinates>& initial_support_legs_coords, coordinates start_ref_coords,
877  const std::vector<leg_type>& initial_support_legs,
878  std::vector< std::vector<step_node> >& new_footstep_nodes_list,
879  const bool is_initialize, const size_t overwritable_fs_index) const
880  {
881  // Calc goal ref
882  coordinates goal_ref_coords;
883  if (is_initialize) {
884  goal_ref_coords = start_ref_coords;
885  } else {
886  goal_ref_coords = initial_foot_mid_coords;
887  step_node tmpfs = footstep_nodes_list[overwritable_fs_index-1].front();
888  start_ref_coords = tmpfs.worldcoords;
889  start_ref_coords.pos += start_ref_coords.rot * hrp::Vector3(-1*footstep_param.leg_default_translate_pos[tmpfs.l_r]);
890  }
891  goal_ref_coords.pos += goal_ref_coords.rot * hrp::Vector3(goal_x, goal_y, 0.0);
892  goal_ref_coords.rotate(deg2rad(goal_theta), hrp::Vector3(0,0,1));
893  std::cerr << "start ref coords" << std::endl;
894  std::cerr << " pos =" << std::endl;
895  std::cerr << start_ref_coords.pos.format(Eigen::IOFormat(Eigen::StreamPrecision, 0, ", ", ", ", "", "", " [", "]")) << std::endl;
896  std::cerr << " rot =" << std::endl;
897  std::cerr << start_ref_coords.rot.format(Eigen::IOFormat(Eigen::StreamPrecision, 0, ", ", "\n", " [", "]")) << std::endl;
898  std::cerr << "goal ref midcoords" << std::endl;
899  std::cerr << " pos =" << std::endl;
900  std::cerr << goal_ref_coords.pos.format(Eigen::IOFormat(Eigen::StreamPrecision, 0, ", ", ", ", "", "", " [", "]")) << std::endl;
901  std::cerr << " rot =" << std::endl;
902  std::cerr << goal_ref_coords.rot.format(Eigen::IOFormat(Eigen::StreamPrecision, 0, ", ", "\n", " [", "]")) << std::endl;
903 
904  /* initialize */
905  if (is_initialize) {
906  // For initial double support period
907  std::vector<step_node> initial_footstep_nodes;
908  for (size_t i = 0; i < initial_support_legs.size(); i++) {
909  initial_footstep_nodes.push_back(step_node(initial_support_legs.at(i), initial_support_legs_coords.at(i), 0, default_step_time, 0, 0));
910  }
911  new_footstep_nodes_list.push_back(initial_footstep_nodes);
912  } else {
913  new_footstep_nodes_list.push_back(footstep_nodes_list[overwritable_fs_index]);
914  }
915 
916  /* footstep generation loop */
917  hrp::Vector3 dp, dr;
918  start_ref_coords.difference(dp, dr, goal_ref_coords);
919  dp = start_ref_coords.rot.transpose() * dp;
920  dr = start_ref_coords.rot.transpose() * dr;
921  while ( !(eps_eq(std::sqrt(dp(0)*dp(0)+dp(1)*dp(1)), 0.0, 1e-3*0.1) && eps_eq(dr(2), 0.0, deg2rad(0.5))) ) {
922  velocity_mode_parameter cur_vel_param;
923  cur_vel_param.set(dp(0)/default_step_time, dp(1)/default_step_time, rad2deg(dr(2))/default_step_time);
924  append_footstep_list_velocity_mode(new_footstep_nodes_list, cur_vel_param);
925  start_ref_coords = new_footstep_nodes_list.back().front().worldcoords;
926  start_ref_coords.pos += start_ref_coords.rot * hrp::Vector3(footstep_param.leg_default_translate_pos[new_footstep_nodes_list.back().front().l_r] * -1.0);
927  start_ref_coords.difference(dp, dr, goal_ref_coords);
928  dp = start_ref_coords.rot.transpose() * dp;
929  dr = start_ref_coords.rot.transpose() * dr;
930  }
931  for (size_t i = 0; i < optional_go_pos_finalize_footstep_num; i++) {
932  append_go_pos_step_nodes(start_ref_coords, calc_counter_leg_types_from_footstep_nodes(new_footstep_nodes_list.back(), all_limbs), new_footstep_nodes_list);
933  }
934 
935  /* finalize */
936  // Align last foot
937  append_go_pos_step_nodes(start_ref_coords, calc_counter_leg_types_from_footstep_nodes(new_footstep_nodes_list.back(), all_limbs), new_footstep_nodes_list);
938  // Check align
939  coordinates final_step_coords1 = new_footstep_nodes_list[new_footstep_nodes_list.size()-2].front().worldcoords; // Final coords in footstep_node_list
940  coordinates final_step_coords2 = start_ref_coords; // Final coords calculated from start_ref_coords + translate pos
941  final_step_coords2.pos += final_step_coords2.rot * hrp::Vector3(footstep_param.leg_default_translate_pos[new_footstep_nodes_list[new_footstep_nodes_list.size()-2].front().l_r]);
942  final_step_coords1.difference(dp, dr, final_step_coords2);
943  if ( !(eps_eq(dp.norm(), 0.0, 1e-3*0.1) && eps_eq(dr.norm(), 0.0, deg2rad(0.5))) ) { // If final_step_coords1 != final_step_coords2, add steps to match final_step_coords1 and final_step_coords2
944  append_go_pos_step_nodes(start_ref_coords, calc_counter_leg_types_from_footstep_nodes(new_footstep_nodes_list.back(), all_limbs), new_footstep_nodes_list);
945  }
946  // For Last double support period
947  if (is_initialize) {
948  append_finalize_footstep(new_footstep_nodes_list);
949  }
950  return;
951  };
952 
953  void gait_generator::go_single_step_param_2_footstep_nodes_list (const double goal_x, const double goal_y, const double goal_z, const double goal_theta,
954  const std::string& tmp_swing_leg,
955  const coordinates& _support_leg_coords)
956  {
957  leg_type _swing_leg = (tmp_swing_leg == "rleg") ? RLEG : LLEG;
958  step_node sn0((_swing_leg == RLEG) ? LLEG : RLEG, _support_leg_coords, lcg.get_default_step_height(), default_step_time, lcg.get_toe_angle(), lcg.get_heel_angle());
959  footstep_nodes_list.push_back(boost::assign::list_of(sn0));
960  step_node sn1(_swing_leg, _support_leg_coords, lcg.get_default_step_height(), default_step_time, lcg.get_toe_angle(), lcg.get_heel_angle());
961  hrp::Vector3 trs(2.0 * footstep_param.leg_default_translate_pos[_swing_leg] + hrp::Vector3(goal_x, goal_y, goal_z));
962  sn1.worldcoords.pos += sn1.worldcoords.rot * trs;
963  sn1.worldcoords.rotate(deg2rad(goal_theta), hrp::Vector3(0,0,1));
964  footstep_nodes_list.push_back(boost::assign::list_of(sn1));
965  footstep_nodes_list.push_back(boost::assign::list_of(sn0));
966  };
967 
969  const double vel_x, const double vel_y, const double vel_theta,
970  const std::vector<leg_type>& current_legs)
971  {
972  velocity_mode_flg = VEL_DOING;
973  /* initialize */
974  clear_footstep_nodes_list();
975  set_velocity_param (vel_x, vel_y, vel_theta);
976  append_go_pos_step_nodes(_ref_coords, current_legs);
977  append_footstep_list_velocity_mode();
978  append_footstep_list_velocity_mode();
979  append_footstep_list_velocity_mode();
980  };
981 
983  {
984  if (velocity_mode_flg == VEL_DOING) velocity_mode_flg = VEL_ENDING;
985  };
986 
987  void gait_generator::calc_ref_coords_trans_vector_velocity_mode (coordinates& ref_coords, hrp::Vector3& trans, double& dth, const std::vector<step_node>& sup_fns, const velocity_mode_parameter& cur_vel_param) const
988  {
989  ref_coords = sup_fns.front().worldcoords;
990  hrp::Vector3 tmpv(footstep_param.leg_default_translate_pos[sup_fns.front().l_r] * -1.0); /* not fair to every support legs */
991  ref_coords.pos += ref_coords.rot * tmpv;
992  double dx = cur_vel_param.velocity_x + offset_vel_param.velocity_x, dy = cur_vel_param.velocity_y + offset_vel_param.velocity_y;
993  dth = cur_vel_param.velocity_theta + offset_vel_param.velocity_theta;
994  //std::cerr << "Before limit dx " << dx << " dy " << dy << " dth " << dth << std::endl;
995  /* velocity limitation by stride parameters <- this should be based on footstep candidates */
996  if (default_stride_limitation_type == SQUARE) {
997  dth = std::max(-1 * footstep_param.stride_outside_theta / default_step_time, std::min(footstep_param.stride_outside_theta / default_step_time, dth));
998  } else if (default_stride_limitation_type == CIRCLE) {
999  dth = std::max(-1 * stride_limitation_for_circle_type[2] / default_step_time, std::min(stride_limitation_for_circle_type[2] / default_step_time, dth));
1000  }
1001  if (default_stride_limitation_type == SQUARE) {
1002  dx = std::max(-1 * footstep_param.stride_bwd_x / default_step_time, std::min(footstep_param.stride_fwd_x / default_step_time, dx ));
1003  dy = std::max(-1 * footstep_param.stride_outside_y / default_step_time, std::min(footstep_param.stride_outside_y / default_step_time, dy ));
1004  /* inside step limitation */
1005  if (use_inside_step_limitation) {
1006  if (dy > 0) {
1007  // If dy>0 (== leftward step) and LLEG/LARM support, do inside limitation
1008  if (std::count_if(sup_fns.begin(), sup_fns.end(), (&boost::lambda::_1->* &step_node::l_r == LLEG || &boost::lambda::_1->* &step_node::l_r == LARM)) > 0) {
1009  dy = std::min(footstep_param.stride_inside_y / default_step_time, dy);
1010  }
1011  } else {
1012  // If dy<=0 (== rightward step) and RLEG/RARM support, do inside limitation
1013  if (std::count_if(sup_fns.begin(), sup_fns.end(), (&boost::lambda::_1->* &step_node::l_r == RLEG || &boost::lambda::_1->* &step_node::l_r == RARM)) > 0) {
1014  dy = std::max(-1 * footstep_param.stride_inside_y / default_step_time, dy);
1015  }
1016  }
1017  if (dth > 0) {
1018  // If dth>0 (== leftward turn step) and LLEG/LARM support, do inside limitation
1019  if (std::count_if(sup_fns.begin(), sup_fns.end(), (&boost::lambda::_1->* &step_node::l_r == LLEG || &boost::lambda::_1->* &step_node::l_r == LARM)) > 0) {
1020  dth = std::min(footstep_param.stride_inside_theta / default_step_time, dth);
1021  }
1022  } else {
1023  // If dth<=0 (== rightward turn step) and RLEG/RARM support, do inside limitation
1024  if (std::count_if(sup_fns.begin(), sup_fns.end(), (&boost::lambda::_1->* &step_node::l_r == RLEG || &boost::lambda::_1->* &step_node::l_r == RARM)) > 0) {
1025  dth = std::max(-1 * footstep_param.stride_inside_theta / default_step_time, dth);
1026  }
1027  }
1028  }
1029  }
1030  //std::cerr << "After Limit dx " << dx << " dy " << dy << " dth " << dth << std::endl;
1031  trans = hrp::Vector3(dx * default_step_time, dy * default_step_time, 0);
1032  dth = deg2rad(dth * default_step_time);
1033  };
1034 
1036  {
1037  append_footstep_list_velocity_mode(footstep_nodes_list, vel_param);
1038  };
1039 
1040  void gait_generator::append_footstep_list_velocity_mode (std::vector< std::vector<step_node> >& _footstep_nodes_list, const velocity_mode_parameter& cur_vel_param) const
1041  {
1042  coordinates ref_coords;
1044  double dth;
1045  calc_ref_coords_trans_vector_velocity_mode(ref_coords, trans, dth, _footstep_nodes_list.back(), cur_vel_param);
1046 
1047  ref_coords.pos += ref_coords.rot * trans;
1048  ref_coords.rotate(dth, hrp::Vector3(0,0,1));
1049  append_go_pos_step_nodes(ref_coords, calc_counter_leg_types_from_footstep_nodes(_footstep_nodes_list.back(), all_limbs), _footstep_nodes_list);
1050  if (default_stride_limitation_type == CIRCLE) limit_stride(_footstep_nodes_list[_footstep_nodes_list.size()-1].front(), _footstep_nodes_list[_footstep_nodes_list.size()-2].front(), stride_limitation_for_circle_type);
1051  };
1052 
1053  void gait_generator::calc_next_coords_velocity_mode (std::vector< std::vector<step_node> >& ret_list, const size_t idx, const size_t future_step_num)
1054  {
1055  coordinates ref_coords;
1057  double dth;
1058  calc_ref_coords_trans_vector_velocity_mode(ref_coords, trans, dth, footstep_nodes_list[idx-1], vel_param);
1059 
1060  std::vector<leg_type> cur_sup_legs, next_sup_legs;
1061  for (size_t i = 0; i < footstep_nodes_list[idx-1].size(); i++) cur_sup_legs.push_back(footstep_nodes_list[idx-1].at(i).l_r);
1062  next_sup_legs = calc_counter_leg_types_from_footstep_nodes(footstep_nodes_list[idx-1], all_limbs);
1063 
1064  for (size_t i = 0; i < future_step_num; i++) {
1065  std::vector<step_node> ret;
1066  std::vector<leg_type> forcused_sup_legs;
1067  switch( i % 2) {
1068  case 0: forcused_sup_legs = next_sup_legs; break;
1069  case 1: forcused_sup_legs = cur_sup_legs; break;
1070  }
1071  if ( velocity_mode_flg != VEL_ENDING ) {
1072  ref_coords.pos += ref_coords.rot * trans;
1073  ref_coords.rotate(dth, hrp::Vector3(0,0,1));
1074  }
1075  for (size_t j = 0; j < forcused_sup_legs.size(); j++) {
1076  ret.push_back(step_node(forcused_sup_legs.at(j), ref_coords, 0, 0, 0, 0));
1077  ret[j].worldcoords.pos += ret[j].worldcoords.rot * footstep_param.leg_default_translate_pos[forcused_sup_legs.at(j)];
1078  if (default_stride_limitation_type == CIRCLE) limit_stride(ret[j], (i == 0 ? footstep_nodes_list[idx-1].at(j) : ret_list[i -1].at(j)), stride_limitation_for_circle_type);
1079  }
1080  ret_list.push_back(ret);
1081  }
1082  };
1083 
1084  void gait_generator::overwrite_refzmp_queue(const std::vector< std::vector<step_node> >& fnsl)
1085  {
1086  size_t idx = get_overwritable_index();
1087  footstep_nodes_list.erase(footstep_nodes_list.begin()+idx, footstep_nodes_list.end());
1088 
1089  /* add new next steps ;; the number of next steps is fnsl.size() */
1090  footstep_nodes_list.insert(footstep_nodes_list.end(), fnsl.begin(), fnsl.end());
1091 
1092  /* Update lcg */
1093  lcg.set_swing_support_steps_list(footstep_nodes_list);
1094 
1095  /* Update refzmp_generator */
1096  /* Remove refzmp after idx for allocation of new refzmp by push_refzmp_from_footstep_nodes */
1097  rg.remove_refzmp_cur_list_over_length(idx);
1098  /* reset index and counter */
1099  rg.set_indices(idx);
1100  if (overwritable_footstep_index_offset == 0) {
1101  rg.set_refzmp_count(lcg.get_lcg_count()); // Start refzmp_count from current remaining footstep count of swinging.
1102  } else {
1103  rg.set_refzmp_count(static_cast<size_t>(fnsl[0][0].step_time/dt)); // Start refzmp_count from step length of first overwrite step
1104  }
1105  /* reset refzmp */
1106  for (size_t i = 0; i < fnsl.size(); i++) {
1107  if (emergency_flg == EMERGENCY_STOP)
1108  rg.push_refzmp_from_footstep_nodes_for_dual(footstep_nodes_list[idx+i],
1109  lcg.get_swing_leg_dst_steps_idx(footstep_nodes_list.size()-1),
1110  lcg.get_support_leg_steps_idx(footstep_nodes_list.size()-1));
1111  else {
1112  if (i==fnsl.size()-1) {
1113  rg.push_refzmp_from_footstep_nodes_for_dual(footstep_nodes_list[fnsl.size()-1],
1114  lcg.get_swing_leg_dst_steps_idx(footstep_nodes_list.size()-1),
1115  lcg.get_support_leg_steps_idx(footstep_nodes_list.size()-1));
1116  } else {
1117  std::vector<step_node> tmp_swing_leg_src_steps;
1118  lcg.calc_swing_leg_src_steps(tmp_swing_leg_src_steps, footstep_nodes_list, idx+i);
1119  toe_heel_types tht(thtc.check_toe_heel_type_from_swing_support_coords(tmp_swing_leg_src_steps.front().worldcoords, lcg.get_support_leg_steps_idx(idx+i).front().worldcoords, lcg.get_toe_pos_offset_x(), lcg.get_heel_pos_offset_x()),
1120  thtc.check_toe_heel_type_from_swing_support_coords(lcg.get_swing_leg_dst_steps_idx(idx+i).front().worldcoords, lcg.get_support_leg_steps_idx(idx+i).front().worldcoords, lcg.get_toe_pos_offset_x(), lcg.get_heel_pos_offset_x()));
1121  rg.push_refzmp_from_footstep_nodes_for_single(footstep_nodes_list[idx+i], lcg.get_support_leg_steps_idx(idx+i), tht);
1122  }
1123  }
1124  }
1125  /* Overwrite refzmp index in preview contoroller queue */
1126  size_t queue_size = preview_controller_ptr->get_preview_queue_size();
1127  size_t overwrite_idx;
1128  if (overwritable_footstep_index_offset == 0) {
1129  overwrite_idx = 0; // Overwrite all queue
1130  } else {
1131  overwrite_idx = lcg.get_lcg_count(); // Overwrite queue except current footstep
1132  }
1133  /* fill preview controller queue by new refzmp */
1134  hrp::Vector3 rzmp;
1135  bool refzmp_exist_p;
1136  std::vector<hrp::Vector3> sfzos;
1137  for (size_t i = overwrite_idx; i < queue_size - 1; i++) {
1138  refzmp_exist_p = rg.get_current_refzmp(rzmp, sfzos, default_double_support_ratio_before, default_double_support_ratio_after, default_double_support_static_ratio_before, default_double_support_static_ratio_after);
1139  preview_controller_ptr->set_preview_queue(rzmp, sfzos, i+1);
1140  if (refzmp_exist_p) {
1141  prev_que_rzmp = rzmp;
1142  prev_que_sfzos = sfzos;
1143  }
1144  rg.update_refzmp();
1145  sfzos.clear();
1146  }
1147  finalize_count = 0;
1148  refzmp_exist_p = rg.get_current_refzmp(rzmp, sfzos, default_double_support_ratio_before, default_double_support_ratio_after, default_double_support_static_ratio_before, default_double_support_static_ratio_after);
1149  if (!refzmp_exist_p) {
1150  finalize_count++;
1151  rzmp = prev_que_rzmp;
1152  sfzos = prev_que_sfzos;
1153  } else {
1154  prev_que_rzmp = rzmp;
1155  prev_que_sfzos = sfzos;
1156  }
1157  solved = preview_controller_ptr->update(refzmp, cog, swing_foot_zmp_offsets, rzmp, sfzos, (refzmp_exist_p || finalize_count < preview_controller_ptr->get_delay()-default_step_time/dt));
1158  rg.update_refzmp();
1159  };
1160 
1161  const std::vector<leg_type> gait_generator::calc_counter_leg_types_from_footstep_nodes(const std::vector<step_node>& fns, std::vector<std::string> _all_limbs) const {
1162  std::vector<std::string> fns_names, cntr_legs_names;
1163  for (std::vector<step_node>::const_iterator it = fns.begin(); it != fns.end(); it++) {
1164  fns_names.push_back(leg_type_map.find(it->l_r)->second);
1165  }
1166  std::sort(_all_limbs.begin(), _all_limbs.end());
1167  std::sort(fns_names.begin(), fns_names.end());
1168  std::set_difference(_all_limbs.begin(), _all_limbs.end(), /* all candidates for legs */
1169  fns_names.begin(), fns_names.end(), /* support legs */
1170  std::back_inserter(cntr_legs_names)); /* swing legs */
1171  std::vector<leg_type> ret;
1172  for (std::vector<std::string>::const_iterator it = cntr_legs_names.begin(); it != cntr_legs_names.end(); it++) {
1173  std::map<leg_type, std::string>::const_iterator dst = std::find_if(leg_type_map.begin(), leg_type_map.end(), (&boost::lambda::_1->* &std::map<leg_type, std::string>::value_type::second == *it));
1174  ret.push_back(dst->first);
1175  }
1176  return ret;
1177  };
1178 }
void modif_foot_coords_for_toe_heel_phase(coordinates &org_coords, const double _current_toe_angle, const double _current_heel_angle)
bool go_pos_param_2_footstep_nodes_list(const double goal_x, const double goal_y, const double goal_theta, const std::vector< coordinates > &initial_support_legs_coords, coordinates start_ref_coords, const std::vector< leg_type > &initial_support_legs, const bool is_initialize=true)
#define max(a, b)
void calc_current_swing_leg_steps(std::vector< step_node > &rets, const double step_height, const double _current_toe_angle, const double _current_heel_angle, const double _default_double_support_ratio_before, const double _default_double_support_ratio_after)
SequenceElement & front(CorbaSequence &seq)
void rectangle_midcoords(coordinates &ret, const coordinates &start, const coordinates &goal, const double height, const size_t swing_trajectory_generator_idx)
const std::vector< leg_type > calc_counter_leg_types_from_footstep_nodes(const std::vector< step_node > &fns, std::vector< std::string > _all_limbs) const
double calc_phase_ratio(const size_t current_count, const toe_heel_phase start_phase, const toe_heel_phase goal_phase) const
const bool is_second_phase() const
void multi_mid_coords(coordinates &ret, const std::vector< coordinates > &cs, const double eps)
void overwrite_refzmp_queue(const std::vector< std::vector< step_node > > &fnsl)
hrp::Matrix33 rot
Definition: RatsMatrix.h:20
void cross_delay_midcoords(coordinates &ret, const coordinates &start, const coordinates &goal, const double height, leg_type lr)
bool is_between_phases(const size_t current_count, const toe_heel_phase phase0, const toe_heel_phase phase1) const
bool is_same_footstep_nodes(const std::vector< step_node > &fns_1, const std::vector< step_node > &fns_2) const
toe_heel_phase
void set_one_step_count(const size_t _count)
const bool is_second_last_phase() const
void calc_ref_coords_trans_vector_velocity_mode(coordinates &ref_coords, hrp::Vector3 &trans, double &dth, const std::vector< step_node > &sup_fns, const velocity_mode_parameter &cur_vel_param) const
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
std::string leg_type_to_leg_type_string(const leg_type l_r)
void reset(const size_t _refzmp_count)
std::vector< size_t > step_count_list
void modify_footsteps_for_recovery()
#define rad2deg(rad)
png_uint_32 i
void cycloid_midcoords(coordinates &ret, const coordinates &start, const coordinates &goal, const double height) const
png_infop png_bytep * trans
coordinates worldcoords
Definition: GaitGenerator.h:33
void calc_next_coords_velocity_mode(std::vector< std::vector< step_node > > &ret_list, const size_t idx, const size_t future_step_num=3)
Eigen::Vector3d Vector3
void initialize_gait_parameter(const hrp::Vector3 &cog, const std::vector< step_node > &initial_support_leg_steps, const std::vector< step_node > &initial_swing_leg_dst_steps, const double delay=1.6)
Matrix33 rotFromRpy(const Vector3 &rpy)
void stair_midcoords(coordinates &ret, const coordinates &start, const coordinates &goal, const double height)
void append_footstep_list_velocity_mode()
Eigen::Matrix3d Matrix33
#define min(a, b)
std::vector< toe_heel_types > toe_heel_types_list
const bool is_start_double_support_phase() const
void cycloid_delay_midcoords(coordinates &ret, const coordinates &start, const coordinates &goal, const double height, const size_t swing_trajectory_generator_idx)
bool is_phase_starting(const size_t current_count, const toe_heel_phase _phase) const
void rotm3times(hrp::Matrix33 &m12, const hrp::Matrix33 &m1, const hrp::Matrix33 &m2)
Definition: RatsMatrix.cpp:31
double calc_phase_period(const toe_heel_phase start_phase, const toe_heel_phase goal_phase, const double _dt) const
def j(str, encoding="cp932")
std::vector< hrp::Vector3 > refzmp_cur_list
double calc_interpolated_toe_heel_angle(const toe_heel_phase start_phase, const toe_heel_phase goal_phase, const double start, const double goal)
const bool is_end_double_support_phase() const
std::vector< std::vector< hrp::Vector3 > > foot_x_axises_list
void initialize_velocity_mode(const coordinates &_ref_coords, const double vel_x, const double vel_y, const double vel_theta, const std::vector< leg_type > &current_legs)
HRP_UTIL_EXPORT Vector3 rpyFromRot(const Matrix33 &m)
std::vector< hrp::Vector3 > default_zmp_offsets
Vector3 cross(const Vector3 &v1, const Vector3 &v2)
#define M_PI
bool eps_eq(const double a, const double b, const double eps=0.001)
Definition: RatsMatrix.h:8
void cycloid_delay_kick_midcoords(coordinates &ret, const coordinates &start, const coordinates &goal, const double height)
void go_single_step_param_2_footstep_nodes_list(const double goal_x, const double goal_y, const double goal_z, const double goal_theta, const std::string &tmp_swing_leg, const coordinates &_support_leg_coords)
void cycloid_midpoint(hrp::Vector3 &ret, const double ratio, const hrp::Vector3 &start, const hrp::Vector3 &goal, const double height, const double default_top_ratio)
void push_refzmp_from_footstep_nodes_for_single(const std::vector< step_node > &fns, const std::vector< step_node > &_support_leg_steps, const toe_heel_types &tht)
void calc_ratio_from_double_support_ratio(const double default_double_support_ratio_before, const double default_double_support_ratio_after)
void limit_stride(step_node &cur_fs, const step_node &prev_fs, const double(&limit)[5]) const
void calc_current_refzmp(hrp::Vector3 &ret, std::vector< hrp::Vector3 > &swing_foot_zmp_offsets, const double default_double_support_ratio_before, const double default_double_support_ratio_after, const double default_double_support_static_ratio_before, const double default_double_support_static_ratio_after)
void difference(hrp::Vector3 &dif_pos, hrp::Vector3 &dif_rot, const coordinates &c) const
Definition: RatsMatrix.h:54
#define deg2rad(deg)
double set_value_according_to_toe_heel_type(const toe_heel_type tht, const double toe_value, const double heel_value, const double default_value)
void calc_current_swing_foot_rot(std::map< leg_type, hrp::Vector3 > &tmp_swing_foot_rot, const double _default_double_support_ratio_before, const double _default_double_support_ratio_after)
hrp::Vector3 pos
Definition: RatsMatrix.h:19
void go_pos_param_2_footstep_nodes_list_core(const double goal_x, const double goal_y, const double goal_theta, const std::vector< coordinates > &initial_support_legs_coords, coordinates start_ref_coords, const std::vector< leg_type > &initial_support_legs, std::vector< std::vector< step_node > > &new_footstep_nodes_list, const bool is_initialize, const size_t overwritable_fs_index) const
JSAMPIMAGE data
toe_heel_phase_counter thp
void rotate(const double theta, const hrp::Vector3 &axis, const std::string &wrt=":local")
Definition: RatsMatrix.h:47
void update_leg_steps(const std::vector< std::vector< step_node > > &fnsl, const double default_double_support_ratio_before, const double default_double_support_ratio_after, const toe_heel_type_checker &thtc)
toe_heel_type check_toe_heel_type_from_swing_support_coords(const coordinates &swing_coords, const coordinates &support_coords, const double toe_pos_offset_x, const double heel_pos_offset_x) const
void push_refzmp_from_footstep_nodes_for_dual(const std::vector< step_node > &fns, const std::vector< step_node > &_support_leg_steps, const std::vector< step_node > &_swing_leg_steps)
std::map< leg_type, double > zmp_weight_map
std::vector< std::vector< leg_type > > swing_leg_types_list
void mid_coords(coordinates &mid_coords, const double p, const coordinates &c1, const coordinates &c2, const double eps)
Definition: RatsMatrix.cpp:58
void set(const double _vx, const double _vy, const double _vth)
Definition: GaitGenerator.h:92


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:49