Frankx  0.2.0
A High-Level Motion API for Franka
motion_joint_generator.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <franka/duration.h>
4 #include <franka/robot_state.h>
5 
9 #include <ruckig/ruckig.hpp>
10 
11 
12 namespace frankx {
13  using namespace movex;
14 
15 template<class RobotType>
17  ruckig::Ruckig<RobotType::degrees_of_freedoms> trajectory_generator {RobotType::control_rate};
18 
19  ruckig::InputParameter<RobotType::degrees_of_freedoms> input_para;
20  ruckig::OutputParameter<RobotType::degrees_of_freedoms> output_para;
21  ruckig::Result result;
22 
23  std::array<double, RobotType::degrees_of_freedoms> joint_positions;
24  double time {0.0};
25  RobotType* robot;
26 
29 
30  explicit JointMotionGenerator(RobotType* robot, JointMotion motion, MotionData& data): robot(robot), motion(motion), data(data) { }
31 
32  void init(const franka::RobotState& robot_state, franka::Duration period) {
33  input_para.current_position = robot_state.q_d;
34  input_para.current_velocity = toStd(Vector7d::Zero());
35  input_para.current_acceleration = toStd(Vector7d::Zero());
36 
37  input_para.target_position = toStd(motion.target);
38  input_para.target_velocity = toStd(Vector7d::Zero());
39  input_para.target_acceleration = toStd(Vector7d::Zero());
40 
41  for (size_t dof = 0; dof < RobotType::degrees_of_freedoms; dof += 1) {
42  input_para.max_velocity[dof] = RobotType::max_joint_velocity[dof] * robot->velocity_rel * data.velocity_rel;
43  input_para.max_acceleration[dof] = 0.3 * RobotType::max_joint_acceleration[dof] * robot->acceleration_rel * data.acceleration_rel;
44  input_para.max_jerk[dof] = 0.3 * RobotType::max_joint_jerk[dof] * robot->jerk_rel * data.jerk_rel;
45  }
46  }
47 
48  franka::JointPositions operator()(const franka::RobotState& robot_state, franka::Duration period) {
49  time += period.toSec();
50  if (time == 0.0) {
51  init(robot_state, period);
52  }
53 
54 #ifdef WITH_PYTHON
55  if (robot->stop_at_python_signal && Py_IsInitialized() && PyErr_CheckSignals() == -1) {
56  robot->stop();
57  }
58 #endif
59 
60  const int steps = std::max<int>(period.toMSec(), 1);
61  for (int i = 0; i < steps; i++) {
62  result = trajectory_generator.update(input_para, output_para);
63  joint_positions = output_para.new_position;
64 
65  if (result == ruckig::Result::Finished) {
66  joint_positions = input_para.target_position;
67  return franka::MotionFinished(franka::JointPositions(joint_positions));
68 
69  } else if (result == ruckig::Result::Error) {
70  std::cout << "[frankx robot] Invalid inputs:" << std::endl;
71  return franka::MotionFinished(franka::JointPositions(joint_positions));
72  }
73 
74  output_para.pass_to_input(input_para);
75  }
76 
77  return franka::JointPositions(joint_positions);
78  }
79 };
80 
81 } // namespace frankx
movex::MotionData::jerk_rel
double jerk_rel
Definition: motion_data.hpp:9
frankx::MotionGenerator
Definition: motion_generator.hpp:19
frankx
Definition: __init__.py:1
movex::JointMotion
Definition: motion_joint.hpp:11
movex::JointMotion::target
const Vector7d target
Definition: motion_joint.hpp:14
frankx::JointMotionGenerator::init
void init(const franka::RobotState &robot_state, franka::Duration period)
Definition: motion_joint_generator.hpp:32
robot_state.hpp
frankx::JointMotionGenerator::output_para
ruckig::OutputParameter< RobotType::degrees_of_freedoms > output_para
Definition: motion_joint_generator.hpp:20
frankx::JointMotionGenerator::JointMotionGenerator
JointMotionGenerator(RobotType *robot, JointMotion motion, MotionData &data)
Definition: motion_joint_generator.hpp:30
motion_data.hpp
frankx::JointMotionGenerator::joint_positions
std::array< double, RobotType::degrees_of_freedoms > joint_positions
Definition: motion_joint_generator.hpp:23
movex::MotionData
Definition: motion_data.hpp:8
frankx::JointMotionGenerator::operator()
franka::JointPositions operator()(const franka::RobotState &robot_state, franka::Duration period)
Definition: motion_joint_generator.hpp:48
frankx::JointMotionGenerator::motion
JointMotion motion
Definition: motion_joint_generator.hpp:27
movex::MotionData::acceleration_rel
double acceleration_rel
Definition: motion_data.hpp:9
frankx::JointMotionGenerator::input_para
ruckig::InputParameter< RobotType::degrees_of_freedoms > input_para
Definition: motion_joint_generator.hpp:19
motion_joint.hpp
movex::MotionData::velocity_rel
double velocity_rel
Definition: motion_data.hpp:9
frankx::JointMotionGenerator::data
MotionData & data
Definition: motion_joint_generator.hpp:28
movex
Definition: motion_impedance.hpp:13
frankx::JointMotionGenerator
Definition: motion_joint_generator.hpp:16
frankx::JointMotionGenerator::result
ruckig::Result result
Definition: motion_joint_generator.hpp:21
frankx::JointMotionGenerator::robot
RobotType * robot
Definition: motion_joint_generator.hpp:25