|
| 1 | +#ifndef DEOXYS_FRANKA_INTERFACE_INCLUDE_UTILS_TRAJ_INTERPOLATORS_POSE_TRAJ_INTERPOLATOR_H_ |
| 2 | +#define DEOXYS_FRANKA_INTERFACE_INCLUDE_UTILS_TRAJ_INTERPOLATORS_POSE_TRAJ_INTERPOLATOR_H_ |
| 3 | +#include <Eigen/Dense> |
| 4 | + |
| 5 | +namespace rcs { |
| 6 | +namespace common { |
| 7 | + |
| 8 | +class LinearPoseTrajInterpolator { |
| 9 | + private: |
| 10 | + Eigen::Vector3d p_start_; |
| 11 | + Eigen::Vector3d p_goal_; |
| 12 | + Eigen::Vector3d last_p_t_; |
| 13 | + Eigen::Vector3d prev_p_goal_; |
| 14 | + |
| 15 | + Eigen::Quaterniond q_start_; |
| 16 | + Eigen::Quaterniond q_goal_; |
| 17 | + Eigen::Quaterniond last_q_t_; |
| 18 | + Eigen::Quaterniond prev_q_goal_; |
| 19 | + |
| 20 | + double dt_; |
| 21 | + double last_time_; |
| 22 | + double max_time_; |
| 23 | + double start_time_; |
| 24 | + bool start_; |
| 25 | + bool first_goal_; |
| 26 | + |
| 27 | + public: |
| 28 | + inline LinearPoseTrajInterpolator() |
| 29 | + : dt_(0.), |
| 30 | + last_time_(0.), |
| 31 | + max_time_(1.), |
| 32 | + start_time_(0.), |
| 33 | + start_(false), |
| 34 | + first_goal_(true) {}; |
| 35 | + |
| 36 | + inline ~LinearPoseTrajInterpolator() {}; |
| 37 | + |
| 38 | + inline void Reset(const double &time_sec, const Eigen::Vector3d &p_start, |
| 39 | + const Eigen::Quaterniond &q_start, |
| 40 | + const Eigen::Vector3d &p_goal, |
| 41 | + const Eigen::Quaterniond &q_goal, const int &policy_rate, |
| 42 | + const int &rate, |
| 43 | + const double &traj_interpolator_time_fraction) { |
| 44 | + dt_ = 1. / static_cast<double>(rate); |
| 45 | + last_time_ = time_sec; |
| 46 | + max_time_ = |
| 47 | + 1. / static_cast<double>(policy_rate) * traj_interpolator_time_fraction; |
| 48 | + start_time_ = time_sec; |
| 49 | + |
| 50 | + start_ = false; |
| 51 | + |
| 52 | + if (first_goal_) { |
| 53 | + p_start_ = p_start; |
| 54 | + q_start_ = q_start; |
| 55 | + |
| 56 | + prev_p_goal_ = p_start; |
| 57 | + prev_q_goal_ = q_start; |
| 58 | + first_goal_ = false; |
| 59 | + } else { |
| 60 | + // If the goal is already set, use prev goal as the starting point of |
| 61 | + // interpolation. |
| 62 | + prev_p_goal_ = p_goal_; |
| 63 | + prev_q_goal_ = q_goal_; |
| 64 | + |
| 65 | + p_start_ = prev_p_goal_; |
| 66 | + q_start_ = prev_q_goal_; |
| 67 | + } |
| 68 | + |
| 69 | + p_goal_ = p_goal; |
| 70 | + q_goal_ = q_goal; |
| 71 | + |
| 72 | + // Flip the sign if the dot product of quaternions is negative |
| 73 | + if (q_goal_.coeffs().dot(q_start_.coeffs()) < 0.0) { |
| 74 | + q_start_.coeffs() << -q_start_.coeffs(); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + inline void GetNextStep(const double &time_sec, Eigen::Vector3d &p_t, |
| 79 | + Eigen::Quaterniond &q_t) { |
| 80 | + if (!start_) { |
| 81 | + start_time_ = time_sec; |
| 82 | + last_p_t_ = p_start_; |
| 83 | + last_q_t_ = q_start_; |
| 84 | + start_ = true; |
| 85 | + } |
| 86 | + |
| 87 | + if (last_time_ + dt_ <= time_sec) { |
| 88 | + double t = |
| 89 | + std::min(std::max((time_sec - start_time_) / max_time_, 0.), 1.); |
| 90 | + last_p_t_ = p_start_ + t * (p_goal_ - p_start_); |
| 91 | + last_q_t_ = q_start_.slerp(t, q_goal_); |
| 92 | + last_time_ = time_sec; |
| 93 | + } |
| 94 | + p_t = last_p_t_; |
| 95 | + q_t = last_q_t_; |
| 96 | + }; |
| 97 | +}; |
| 98 | +} // namespace common |
| 99 | +} // namespace rcs |
| 100 | +#endif // DEOXYS_FRANKA_INTERFACE_INCLUDE_UTILS_TRAJ_INTERPOLATORS_POSE_TRAJ_INTERPOLATOR_H_ |
0 commit comments