humoto
whole_body_controller.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4  @author Jan Michalczyk
5  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
6  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
7 
8  @brief
9 */
10 
11 #pragma once
12 
13 namespace humoto
14 {
15  namespace pepper_ik
16  {
17  /**
18  * @brief Whole body controller
19  *
20  * @tparam t_features features identifying the model
21  */
22  template <int t_features>
24  {
25  private:
28 
29 
30  public:
32  std::map<std::string, etools::Vector6> tags_velocity_;
33  std::map<std::string, etools::Vector6> tags_desired_pose_global_;
34 
35 
36  public:
37  /**
38  * @brief Get tag velocity in local frame (vx, vy, vz, wx, wy, wz)
39  *
40  * @param[in] tag_name
41  * @param[in] spatial_type
42  *
43  * @return velocity in local frame
44  */
45  Eigen::VectorXd getTagVelocityInLocal(const std::string& tag_name,
46  const rbdl::SpatialType::Type& spatial_type) const
47  {
48  Eigen::VectorXd velocity_in_local;
50 
51  if((tags_velocity_.find(tag_name) != tags_velocity_.end()))
52  {
53  velocity_in_local = tags_velocity_.at(tag_name);
54  }
55  else
56  {
57  velocity_in_local.setZero();
58  }
59 
60  const std::size_t part_size = 3;
61  switch(spatial_type)
62  {
64  return(velocity_in_local.tail(part_size));
66  return(velocity_in_local.head(part_size));
68  return(velocity_in_local);
69  default:
70  HUMOTO_THROW_MSG("Unsupported velocity type.");
71  }
72  }
73 
74 
75  /**
76  * @brief Set tag(s) velocity
77  *
78  * @param[in] tag_velocity
79  */
80  void setTagsVelocity(const std::map<std::string, etools::Vector6>& tag_velocity)
81  {
82  tags_velocity_ = tag_velocity;
83  }
84 
85 
86  /**
87  * @brief Get base velocity in global frame
88  *
89  * @param[in] model
90  * @return base_velocity
91  */
93  {
94  return(getTagVelocityInGlobal(model, "CameraTop_optical_frame",
95  getTagVelocityInLocal("CameraTop_optical_frame",
98  }
99 
100 
101  /**
102  * @brief Get velocity in global frame
103  *
104  * @param[in] model
105  * @param[in] tag_name
106  * @param[in] tag_velocity_local
107  * @param[in] spatial_type
108  * @return velocity in global frame
109  */
111  const std::string& tag_name,
112  const etools::Vector6& tag_velocity_local,
113  const rbdl::SpatialType::Type& spatial_type) const
114  {
115  HUMOTO_ASSERT(tag_velocity_local.size() == 6,
116  "Computing global tag velocity requires full local tag velocity vector.")
117 
118  rbdl::TagLinkPtr tag = model.getLinkTag(tag_name);
119 
120  Eigen::VectorXd velocity_in_global;
121  velocity_in_global.resize(rbdl::SpatialType::getNumberOfElements(spatial_type));
122 
123  const std::size_t part_size = 3;
124  switch(spatial_type)
125  {
127  velocity_in_global.head(part_size) = model.getTagOrientation(tag) *
128  tag_velocity_local.tail(part_size);
129  break;
130 
132  velocity_in_global.tail(part_size) = model.getTagPosition(tag).cross(model.getTagOrientation(tag)
133  * tag_velocity_local.tail(part_size))
134  + model.getTagOrientation(tag) *
135  tag_velocity_local.head(part_size);
136  break;
137 
139  velocity_in_global.head(part_size) = model.getTagOrientation(tag) *
140  tag_velocity_local.tail(part_size);
141 
142  velocity_in_global.tail(part_size) = model.getTagPosition(tag).cross(model.getTagOrientation(tag)
143  * tag_velocity_local.tail(part_size))
144  + model.getTagOrientation(tag) *
145  tag_velocity_local.head(part_size);
146  break;
147 
148  default:
149  HUMOTO_THROW_MSG("Unsupported velocity type.");
150  }
151 
152  return(velocity_in_global);
153  }
154 
155 
156  /**
157  * @brief Get desired tag(s) pose in global frame
158  *
159  * @param[in] model
160  */
162  {
163  std::map<std::string, etools::Vector6>::iterator i;
164  for(i = tags_velocity_.begin(); i != tags_velocity_.end(); ++i)
165  {
166  rbdl::TagLinkPtr tag = model.getLinkTag(i->first);
167 
168  const std::size_t part_size = 3;
169  etools::Vector6 tag_pose;
170 
171  tag_pose.head(part_size) = rigidbody::convertMatrixToEulerAngles(
173  getTagVelocityInLocal(i->first, rbdl::SpatialType::ROTATION),
174  wbc_parameters_.control_interval_),
176 
177  tag_pose.tail(part_size) = model.getTagPosition(tag) + wbc_parameters_.control_interval_ *
178  getTagVelocityInGlobal(model, i->first,
179  getTagVelocityInLocal(i->first,
182 
183  tags_desired_pose_global_[i->first] = tag_pose;
184  }
185  }
186 
187 
188  /**
189  * @brief Get tag pose error in global frame
190  *
191  * @param[in] model
192  * @param[in] tag_name
193  * @return error in pose in global frame
194  */
196  const std::string& tag_name) const
197  {
198  etools::Vector6 tag_pose_error;
199  tag_pose_error.setZero();
200 
201  if(tags_desired_pose_global_.find(tag_name) != tags_desired_pose_global_.end())
202  {
203  rbdl::TagLinkPtr tag = model.getLinkTag(tag_name);
204 
205  std::size_t part_size = 3;
206  tag_pose_error.head(part_size) = rigidbody::getRotationErrorAngleAxis(
207  model.getTagOrientation(tag),
209  tags_desired_pose_global_.at(tag_name).head(part_size), rigidbody::EulerAngles::RPY));
210 
211  tag_pose_error.tail(part_size) = tags_desired_pose_global_.at(tag_name).tail(part_size) - model.getTagPosition(tag);
212  }
213 
214  return(tag_pose_error);
215  }
216 
217 
218  /**
219  * @brief Constructor
220  */
222  {
223  solution_is_parsed_ = false;
224  }
225 
226 
227  /**
228  * @brief Constructor
229  *
230  * @param[in] wbc_parameters
231  */
232  explicit WholeBodyController(const humoto::pepper_ik::WBCParameters& wbc_parameters)
233  : wbc_parameters_(wbc_parameters)
234  {
235  solution_is_parsed_ = false;
236  }
237 
238 
239  /**
240  * @brief Update control problem
241  *
242  * @param[in] model model of the system
243  * @param[in] motion_parameters
244  *
245  * @return ControlProblemStatus::OK/ControlProblemStatus::STOPPED
246  */
249  const humoto::pepper_ik::MotionParameters &motion_parameters)
250  {
252  solution_is_parsed_ = false;
253 
254  // decision variables
255  sol_structure_.reset();
256  sol_structure_.addSolutionPart(ROOT_VARIABLES_ID , ModelDescription<t_features>::ROOT_DOF_NUMBER);
257  sol_structure_.addSolutionPart(JOINTS_VARIABLES_ID, ModelDescription<t_features>::JOINTS_DOF_NUMBER);
258 
259  motion_parameters_ = motion_parameters;
260 
261  return (control_status);
262  }
263 
264 
265  /**
266  * @brief Process solution
267  *
268  * @param[in] solution solution
269  */
270  void parseSolution(const humoto::Solution &solution)
271  {
272  solution_is_parsed_ = true;
273  }
274 
275 
276  /**
277  * @brief Get next model state.
278  *
279  * @param[in] solution solution
280  * @param[in] model model
281  *
282  * @return next model state.
283  */
286  const humoto::Solution &solution,
288  {
289  HUMOTO_ASSERT(solution_is_parsed_ == false, "The solution is parsed.");
290  parseSolution(solution);
291 
292  humoto::pepper_ik::GeneralizedCoordinates<t_features> next_generalized_coordinates;
293  next_generalized_coordinates = model.getState();
294  next_generalized_coordinates.root_pose_ += solution.getSolutionPart(ROOT_VARIABLES_ID);
295  next_generalized_coordinates.joint_angles_ += solution.getSolutionPart(JOINTS_VARIABLES_ID);
296 
297  return(next_generalized_coordinates);
298  }
299 
300 
301  /**
302  * @brief Get next model state.
303  *
304  * @param[in] model model
305  *
306  * @return next model state.
307  */
310  const humoto::pepper_ik::Model<t_features> &model) const
311  {
312  HUMOTO_ASSERT(solution_is_parsed_ == true, "This function can be called only after the solution is parsed.");
313  return(model.state_);
314  }
315 
316 
317  /**
318  * @brief Log
319  *
320  * @param[in,out] logger logger
321  * @param[in] parent parent
322  * @param[in] name name
323  */
325  const LogEntryName &parent = LogEntryName(),
326  const std::string & name = "wbc") const
327  {
328  }
329  };
330  }
331 }
332 
boost::shared_ptr< const TagLink > TagLinkPtr
Definition: tag.h:44
Abstract base class (for control problems)
static const char * ROOT_VARIABLES_ID
Root position and orientation.
Definition: common.h:17
std::map< std::string, etools::Vector6 > tags_velocity_
static std::size_t getNumberOfElements(const Type &spatial_type)
Returns number of elements (dimensionality) for given spatial data type.
WholeBodyController(const humoto::pepper_ik::WBCParameters &wbc_parameters)
Constructor.
static const char * JOINTS_VARIABLES_ID
Joint angles.
Definition: common.h:19
#define HUMOTO_LOCAL
Definition: export_import.h:26
Desired motion parameters.
Definition: common.h:78
Eigen::VectorBlock< Eigen::VectorXd > getSolutionPart(const std::string &id)
Get part of the solution by its id.
Definition: solution.h:281
etools::Vector3 getTagPosition(const rbdl::TagLinkPtr tag) const
Get tag position.
Definition: model.h:477
#define HUMOTO_GLOBAL_LOGGER_IF_DEFINED
Definition: logger.h:997
#define HUMOTO_ASSERT(condition, message)
Container of the solution.
Definition: solution.h:176
humoto::pepper_ik::GeneralizedCoordinates< t_features > getNextGeneralizedCoordinates(const humoto::Solution &solution, const humoto::pepper_ik::Model< t_features > &model)
Get next model state.
#define HUMOTO_THROW_MSG(s)
HUMOTO_THROW_MSG throws an error message concatenated with the name of the function (if supported)...
Represents log entry name.
Definition: logger.h:169
humoto::pepper_ik::GeneralizedCoordinates< t_features > getNextGeneralizedCoordinates(const humoto::pepper_ik::Model< t_features > &model) const
Get next model state.
etools::Matrix3 getTagOrientation(const rbdl::TagLinkPtr tag) const
Get tag orientation.
Definition: model.h:464
Eigen::VectorXd getTagVelocityInGlobal(const humoto::pepper_ik::Model< t_features > &model, const std::string &tag_name, const etools::Vector6 &tag_velocity_local, const rbdl::SpatialType::Type &spatial_type) const
Get velocity in global frame.
etools::Vector3 convertMatrixToEulerAngles(const etools::Matrix3 &rotation_matrix, const EulerAngles::Type euler_angles_type)
Convert rotation matrix to Euler angles.
Definition: rotary_state.h:41
void setTagsVelocity(const std::map< std::string, etools::Vector6 > &tag_velocity)
Set tag(s) velocity.
etools::Vector3 getRotationErrorAngleAxis(const etools::Matrix3 &current, const etools::Matrix3 &reference)
Definition: rotary_state.h:123
etools::Matrix3 integrateAngularVelocity(const etools::Matrix3 &orientation, const etools::Vector3 &angular_velocity, const double dt)
Integrate angular velocity to obtain orientation matrix. Simple version.
Definition: rotary_state.h:176
Threaded logger: any data sent to this logger is wrapped in a message and pushed to a queue...
Definition: logger.h:555
std::map< std::string, etools::Vector6 > tags_desired_pose_global_
void log(humoto::Logger &logger, const LogEntryName &parent=LogEntryName(), const std::string &name="wbc") const
Log.
etools::Vector6 getBaseVelocityInGlobal(const humoto::pepper_ik::Model< t_features > &model)
Get base velocity in global frame.
etools::Vector6 getTagPoseErrorInGlobal(const humoto::pepper_ik::Model< t_features > &model, const std::string &tag_name) const
Get tag pose error in global frame.
The root namespace of HuMoTo.
Definition: config.h:12
humoto::pepper_ik::WBCParameters wbc_parameters_
humoto::pepper_ik::MotionParameters motion_parameters_
rbdl::TagLinkPtr getLinkTag(const std::string &id) const
Get tag.
Definition: model.h:451
const GeneralizedCoordinates< t_features > & getState() const
Return current model state.
Definition: model.h:733
void parseSolution(const humoto::Solution &solution)
Process solution.
void computeTagsDesiredPoseInGlobal(const humoto::pepper_ik::Model< t_features > &model)
Get desired tag(s) pose in global frame.
Type
Type os spatial data.
ControlProblemStatus::Status update(humoto::pepper_ik::Model< t_features > &model, const humoto::pepper_ik::MotionParameters &motion_parameters)
Update control problem.
Parameters of the WBC problem.
Definition: common.h:25
etools::Matrix3 convertEulerAnglesToMatrix(const etools::Vector3 &euler_angles, const EulerAngles::Type euler_angles_type)
Convert rotation matrix to Euler angles.
Definition: rotary_state.h:71
Eigen::VectorXd getTagVelocityInLocal(const std::string &tag_name, const rbdl::SpatialType::Type &spatial_type) const
Get tag velocity in local frame (vx, vy, vz, wx, wy, wz)