3 #include <franka/duration.h>
4 #include <franka/robot_state.h>
9 #include <ruckig/ruckig.hpp>
13 using namespace movex;
15 template<
class RobotType>
17 ruckig::Ruckig<RobotType::degrees_of_freedoms> trajectory_generator {RobotType::control_rate};
19 ruckig::InputParameter<RobotType::degrees_of_freedoms>
input_para;
20 ruckig::OutputParameter<RobotType::degrees_of_freedoms>
output_para;
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());
37 input_para.target_position = toStd(motion.
target);
38 input_para.target_velocity = toStd(Vector7d::Zero());
39 input_para.target_acceleration = toStd(Vector7d::Zero());
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;
48 franka::JointPositions
operator()(
const franka::RobotState& robot_state, franka::Duration period) {
49 time += period.toSec();
51 init(robot_state, period);
55 if (robot->stop_at_python_signal && Py_IsInitialized() && PyErr_CheckSignals() == -1) {
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;
65 if (result == ruckig::Result::Finished) {
66 joint_positions = input_para.target_position;
67 return franka::MotionFinished(franka::JointPositions(joint_positions));
69 }
else if (result == ruckig::Result::Error) {
70 std::cout <<
"[frankx robot] Invalid inputs:" << std::endl;
71 return franka::MotionFinished(franka::JointPositions(joint_positions));
74 output_para.pass_to_input(input_para);
77 return franka::JointPositions(joint_positions);