Frankx  0.2.0
A High-Level Motion API for Franka
motion_generator.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Eigen/Core>
4 #include <franka/duration.h>
5 #include <franka/robot_state.h>
6 
7 #include <ruckig/input_parameter.hpp>
10 #include <movex/waypoint.hpp>
11 
12 
13 namespace frankx {
14  using namespace movex;
16  using Vector6d = Eigen::Matrix<double, 6, 1>;
17  using Vector7d = Eigen::Matrix<double, 7, 1>;
18 
20  static inline franka::CartesianPose CartesianPose(const Vector7d& vector, bool include_elbow = true) {
21  auto affine = Affine(vector);
22  if (include_elbow) {
23  return franka::CartesianPose(affine.array(), {vector[6], -1});
24  }
25  return franka::CartesianPose(affine.array());
26  }
27 
28  static inline franka::CartesianPose CartesianPose(const std::array<double, 7>& vector, bool include_elbow = true) {
29  auto affine = Affine(vector);
30  if (include_elbow) {
31  return franka::CartesianPose(affine.array(), {vector[6], -1});
32  }
33  return franka::CartesianPose(affine.array());
34  }
35 
36  template <class T = double>
37  static inline std::array<T, 7> VectorCartRotElbow(T cart, T rot, T elbow) {
38  return {cart, cart, cart, rot, rot, rot, elbow};
39  }
40 
41  static inline void setCartRotElbowVector(Vector7d& vector, double cart, double rot, double elbow) {
42  const std::array<double, 7> data = VectorCartRotElbow(cart, rot, elbow);
43  vector = Eigen::Map<const Vector7d>(data.data(), data.size());
44  }
45 
46  static inline std::array<double, 7> toStd(const Vector7d& vector) {
47  std::array<double, 7> result;
48  Vector7d::Map(result.data()) = vector;
49  return result;
50  }
51 
52  static inline movex::RobotState<7> convertState(const franka::RobotState& franka) {
54  movex.q = franka.q;
55  movex.q_d = franka.q_d;
56  movex.dq = franka.dq;
57 
58  movex.O_T_EE = franka.O_T_EE;
59  movex.O_T_EE_c = franka.O_T_EE_c;
60  movex.O_dP_EE_c = franka.O_dP_EE_c;
61 
62  movex.elbow = franka.elbow;
63  movex.elbow_c = franka.elbow_c;
64  movex.elbow_d = franka.elbow_d;
65 
66  movex.O_F_ext_hat_K = franka.O_F_ext_hat_K;
67  return movex;
68  }
69 
70  template<class RobotType>
71  static std::tuple<std::array<double, 7>, std::array<double, 7>, std::array<double, 7>> getInputLimits(RobotType* robot, const MotionData& data) {
72  return getInputLimits(robot, Waypoint(), data);
73  }
74 
75  template<class RobotType>
76  static std::tuple<std::array<double, 7>, std::array<double, 7>, std::array<double, 7>> getInputLimits(RobotType* robot, const Waypoint& waypoint, const MotionData& data) {
77  constexpr double translation_factor {0.4};
78  constexpr double derivative_factor {0.4};
79 
80  if (waypoint.max_dynamics || data.max_dynamics) {
81  auto max_velocity = MotionGenerator::VectorCartRotElbow(
82  0.8 * translation_factor * robot->max_translation_velocity,
83  0.8 * robot->max_rotation_velocity,
84  0.8 * robot->max_elbow_velocity
85  );
86  auto max_acceleration = MotionGenerator::VectorCartRotElbow(
87  0.8 * translation_factor * derivative_factor * robot->max_translation_acceleration,
88  0.8 * derivative_factor * robot->max_rotation_acceleration,
89  0.8 * derivative_factor * robot->max_elbow_acceleration
90  );
92  0.8 * translation_factor * std::pow(derivative_factor, 2) * robot->max_translation_jerk,
93  0.8 * std::pow(derivative_factor, 2) * robot->max_rotation_jerk,
94  0.8 * std::pow(derivative_factor, 2) * robot->max_elbow_jerk
95  );
96  return {max_velocity, max_acceleration, max_jerk};
97  }
98 
99  auto max_velocity = MotionGenerator::VectorCartRotElbow(
100  translation_factor * waypoint.velocity_rel * data.velocity_rel * robot->velocity_rel * robot->max_translation_velocity,
101  waypoint.velocity_rel * data.velocity_rel * robot->velocity_rel * robot->max_rotation_velocity,
102  waypoint.velocity_rel * data.velocity_rel * robot->velocity_rel * robot->max_elbow_velocity
103  );
104  auto max_acceleration = MotionGenerator::VectorCartRotElbow(
105  translation_factor * derivative_factor * data.acceleration_rel * robot->acceleration_rel * robot->max_translation_acceleration,
106  derivative_factor * data.acceleration_rel * robot->acceleration_rel * robot->max_rotation_acceleration,
107  derivative_factor * data.acceleration_rel * robot->acceleration_rel * robot->max_elbow_acceleration
108  );
109  auto max_jerk = MotionGenerator::VectorCartRotElbow(
110  translation_factor * std::pow(derivative_factor, 2) * data.jerk_rel * robot->jerk_rel * robot->max_translation_jerk,
111  std::pow(derivative_factor, 2) * data.jerk_rel * robot->jerk_rel * robot->max_rotation_jerk,
112  std::pow(derivative_factor, 2) * data.jerk_rel * robot->jerk_rel * robot->max_elbow_jerk
113  );
114  return {max_velocity, max_acceleration, max_jerk};
115  }
116 
117  template<class RobotType>
118  static void setInputLimits(ruckig::InputParameter<7>& input_parameters, RobotType* robot, const MotionData& data) {
119  setInputLimits(input_parameters, robot, Waypoint(), data);
120  }
121 
122  template<class RobotType>
123  static void setInputLimits(ruckig::InputParameter<7>& input_parameters, RobotType* robot, const Waypoint& waypoint, const MotionData& data) {
124  std::tie(input_parameters.max_velocity, input_parameters.max_acceleration, input_parameters.max_jerk) = getInputLimits(robot, waypoint, data);
125 
126  if (!(waypoint.max_dynamics || data.max_dynamics) && waypoint.minimum_time.has_value()) {
127  input_parameters.minimum_duration = waypoint.minimum_time.value();
128  }
129 
130  if (waypoint.max_dynamics) {
131  input_parameters.synchronization = ruckig::Synchronization::TimeIfNecessary;
132  } else {
133  input_parameters.synchronization = ruckig::Synchronization::Time;
134  }
135  }
136 };
137 
138 
139 // Stateful functor to get values after the control function
140 template <typename OriginalFunctor, typename RType>
142  OriginalFunctor &fun;
143 
144 public:
145  StatefulFunctor() = delete;
146  StatefulFunctor(OriginalFunctor &orig) : fun(orig) {}
147  StatefulFunctor(StatefulFunctor const &other) : fun(other.fun) {}
148  StatefulFunctor(StatefulFunctor &&other) : fun(other.fun) {}
149 
150  template <typename... Args>
151  RType operator() (Args&&... args) {
152  return fun(std::forward<Args>(args)...);
153  }
154 };
155 
156 
157 template <typename RT, typename OF>
159  return StatefulFunctor<OF, RT>(fun);
160 }
161 
162 } // namespace frankx
movex::MotionData::jerk_rel
double jerk_rel
Definition: motion_data.hpp:9
frankx::MotionGenerator
Definition: motion_generator.hpp:19
frankx::MotionGenerator::convertState
static movex::RobotState< 7 > convertState(const franka::RobotState &franka)
Definition: motion_generator.hpp:52
frankx::MotionGenerator::CartesianPose
static franka::CartesianPose CartesianPose(const std::array< double, 7 > &vector, bool include_elbow=true)
Definition: motion_generator.hpp:28
movex::Waypoint::max_dynamics
bool max_dynamics
Dynamic Waypoint: Use maximal dynamics of the robot independent on other parameters.
Definition: waypoint.hpp:31
frankx
Definition: __init__.py:1
frankx::StatefulFunctor::StatefulFunctor
StatefulFunctor(StatefulFunctor &&other)
Definition: motion_generator.hpp:148
movex::Waypoint::minimum_time
std::optional< double > minimum_time
Dynamic Waypoint: Minimum time to get to next waypoint.
Definition: waypoint.hpp:37
robot_state.hpp
frankx::MotionGenerator::setCartRotElbowVector
static void setCartRotElbowVector(Vector7d &vector, double cart, double rot, double elbow)
Definition: motion_generator.hpp:41
frankx::StatefulFunctor
Definition: motion_generator.hpp:141
movex::Waypoint
Definition: waypoint.hpp:12
motion_data.hpp
movex::Waypoint::velocity_rel
double velocity_rel
Dynamic Waypoint: Relative velocity factor.
Definition: waypoint.hpp:28
frankx::Vector6d
Eigen::Matrix< double, 6, 1 > Vector6d
Definition: motion_generator.hpp:16
movex::RobotState
The overall state of the robot.
Definition: robot_state.hpp:8
frankx::stateful
StatefulFunctor< OF, RT > stateful(OF &fun)
Definition: motion_generator.hpp:158
frankx::MotionGenerator::setInputLimits
static void setInputLimits(ruckig::InputParameter< 7 > &input_parameters, RobotType *robot, const MotionData &data)
Definition: motion_generator.hpp:118
movex::MotionData
Definition: motion_data.hpp:8
frankx::MotionGenerator::getInputLimits
static std::tuple< std::array< double, 7 >, std::array< double, 7 >, std::array< double, 7 > > getInputLimits(RobotType *robot, const MotionData &data)
Definition: motion_generator.hpp:71
frankx::Affine
affx::Affine Affine
Definition: motion_generator.hpp:15
frankx::MotionGenerator::setInputLimits
static void setInputLimits(ruckig::InputParameter< 7 > &input_parameters, RobotType *robot, const Waypoint &waypoint, const MotionData &data)
Definition: motion_generator.hpp:123
movex::MotionData::max_dynamics
bool max_dynamics
Definition: motion_data.hpp:10
movex::MotionData::acceleration_rel
double acceleration_rel
Definition: motion_data.hpp:9
movex::MotionData::velocity_rel
double velocity_rel
Definition: motion_data.hpp:9
frankx::StatefulFunctor::StatefulFunctor
StatefulFunctor(OriginalFunctor &orig)
Definition: motion_generator.hpp:146
frankx::MotionGenerator::toStd
static std::array< double, 7 > toStd(const Vector7d &vector)
Definition: motion_generator.hpp:46
movex
Definition: motion_impedance.hpp:13
frankx::StatefulFunctor::StatefulFunctor
StatefulFunctor(StatefulFunctor const &other)
Definition: motion_generator.hpp:147
waypoint.hpp
frankx::MotionGenerator::getInputLimits
static std::tuple< std::array< double, 7 >, std::array< double, 7 >, std::array< double, 7 > > getInputLimits(RobotType *robot, const Waypoint &waypoint, const MotionData &data)
Definition: motion_generator.hpp:76
frankx::MotionGenerator::CartesianPose
static franka::CartesianPose CartesianPose(const Vector7d &vector, bool include_elbow=true)
Definition: motion_generator.hpp:20
frankx::Vector7d
Eigen::Matrix< double, 7, 1 > Vector7d
Definition: motion_generator.hpp:17
frankx::MotionGenerator::VectorCartRotElbow
static std::array< T, 7 > VectorCartRotElbow(T cart, T rot, T elbow)
Definition: motion_generator.hpp:37