HEBI C++ API  3.9.0
grav_comp.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Eigen/Dense"
4 #include "group_feedback.hpp"
5 #include "robot_model.hpp"
6 
7 namespace hebi {
8 namespace util {
9 
13 inline Eigen::Vector3d gravityFromQuaternion(const hebi::Quaternionf& q) {
14  float xx = q.getX() * q.getX();
15  float xz = q.getX() * q.getZ();
16  float xw = q.getX() * q.getW();
17  float yy = q.getY() * q.getY();
18  float yz = q.getY() * q.getZ();
19  float yw = q.getY() * q.getW();
20 
21  Eigen::Vector3d res;
22  res[0] = -2.0f * (xz - yw);
23  res[1] = -2.0f * (yz + xw);
24  res[2] = -1.0f + 2.0f * (xx + yy);
25 
26  return res;
27 }
28 
33 [[deprecated(
34  "Use hebi::robot_model::RobotModel::getGravCompEfforts or hebi::Arm GravCompEfforts plug-in "
35  "instead.")]] static Eigen::VectorXd
36 getGravCompEfforts(const hebi::robot_model::RobotModel& model, const Eigen::VectorXd& masses,
37  const hebi::GroupFeedback& feedback) {
38  // Update gravity from base module:
39  // NOTE: consider using pose-filtered "orientation" feedback instead
40  auto base_accel = feedback[0].imu().accelerometer().get();
41  Vector3d gravity(-base_accel.getX(), -base_accel.getY(), -base_accel.getZ());
42 
43  // Normalize gravity vector (to 1g, or 9.8 m/s^2)
44  Eigen::Vector3d normed_gravity = gravity;
45  normed_gravity /= normed_gravity.norm();
46  normed_gravity *= 9.81;
47 
48  size_t num_dof = model.getDoFCount();
49  Eigen::VectorXd comp_torque(num_dof);
50  comp_torque.setZero();
51  model.getGravCompEfforts(feedback.getPosition(), normed_gravity, comp_torque);
52  return comp_torque;
53 }
54 
55 } // namespace util
56 } // namespace hebi
Eigen::Vector3d gravityFromQuaternion(const hebi::Quaternionf &q)
Definition: grav_comp.hpp:13
A list of Feedback objects that can be received from a Group of modules; the size() must match the nu...
Definition: group_feedback.hpp:18
float getY() const
Returns the Y component of the quaternion.
Definition: quaternion_f.hpp:25
float getW() const
Returns the W component of the quaternion.
Definition: quaternion_f.hpp:21
static Eigen::VectorXd getGravCompEfforts(const hebi::robot_model::RobotModel &model, const Eigen::VectorXd &masses, const hebi::GroupFeedback &feedback)
Definition: grav_comp.hpp:36
Definition: arm.cpp:8
float getZ() const
Returns the Z component of the quaternion.
Definition: quaternion_f.hpp:27
Eigen::VectorXd getPosition() const
Convenience function for returning feedback position values.
Definition: group_feedback.cpp:150
void getGravCompEfforts(const Eigen::VectorXd &angles, const Eigen::Vector3d &gravity, Eigen::VectorXd &comp_torque) const
Definition: robot_model.cpp:389
Structure to hold a floating point quaternion (i.e., w/x/y/z components)
Definition: quaternion_f.hpp:8
Represents a chain or tree of robot elements (rigid bodies and joints).
Definition: robot_model.hpp:412
size_t getDoFCount() const
Returns the number of settable degrees of freedom in the kinematic tree. (This is equal to the number...
Definition: robot_model.cpp:168
float getX() const
Returns the X component of the quaternion.
Definition: quaternion_f.hpp:23