Frankx  0.2.0
A High-Level Motion API for Franka
motion_path_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 <movex/path/path.hpp>
12 
13 
14 namespace frankx {
15  using namespace movex;
16 
17 template<class RobotType>
19  size_t trajectory_index {0};
20  double s_current {0.0};
21  const bool use_elbow {false};
22  double time {0.0};
23 
24  Trajectory trajectory {};
25 
26  RobotType* robot;
30 
31  explicit PathMotionGenerator(RobotType* robot, const Affine& frame, PathMotion motion, MotionData& data): robot(robot), frame(frame), motion(motion), data(data) {
32  // Insert current pose into beginning of path
33  auto initial_state = robot->readOnce();
34  franka::CartesianPose initial_cartesian_pose(initial_state.O_T_EE_c, initial_state.elbow_c);
35  Affine initial_pose(initial_cartesian_pose.O_T_EE);
36 
37  Waypoint start_waypoint {initial_pose * frame, initial_cartesian_pose.elbow[0]};
38  auto all_waypoints = motion.waypoints;
39  all_waypoints.insert(all_waypoints.begin(), start_waypoint);
40 
41  // Create path
42  const Path path {all_waypoints};
43 
44  // Get time parametrization
45  TimeParametrization time_parametrization {RobotType::control_rate};
46  const auto [max_velocity, max_acceleration, max_jerk] = getInputLimits(robot, data);
47  trajectory = time_parametrization.parametrize(path, max_velocity, max_acceleration, max_jerk);
48  }
49 
50  franka::CartesianPose operator()(const franka::RobotState& robot_state, franka::Duration period) {
51  time += period.toSec();
52 
53 #ifdef WITH_PYTHON
54  if (robot->stop_at_python_signal && Py_IsInitialized() && PyErr_CheckSignals() == -1) {
55  robot->stop();
56  }
57 #endif
58 
59  const int steps = std::max<int>(period.toMSec(), 1);
60  trajectory_index += steps;
61  if (trajectory_index >= trajectory.states.size()) {
62  s_current = trajectory.path.get_length();
63  return franka::MotionFinished(CartesianPose(trajectory.path.q(s_current, frame), use_elbow));
64  }
65 
66  s_current = trajectory.states[trajectory_index].s;
67  return CartesianPose(trajectory.path.q(s_current, frame), use_elbow);
68  }
69 };
70 
71 } // namespace frankx
movex::Path
Definition: path.hpp:15
frankx::MotionGenerator
Definition: motion_generator.hpp:19
frankx
Definition: __init__.py:1
movex::Waypoint::elbow
std::optional< double > elbow
Definition: waypoint.hpp:23
movex::Trajectory
Definition: trajectory.hpp:8
movex::TimeParametrization
Definition: time_parametrization.hpp:9
robot_state.hpp
frankx::PathMotionGenerator::PathMotionGenerator
PathMotionGenerator(RobotType *robot, const Affine &frame, PathMotion motion, MotionData &data)
Definition: motion_path_generator.hpp:31
movex::PathMotion
Definition: motion_path.hpp:15
movex::PathMotion::waypoints
std::vector< Waypoint > waypoints
Definition: motion_path.hpp:18
time_parametrization.hpp
movex::Waypoint
Definition: waypoint.hpp:12
frankx::PathMotionGenerator::motion
PathMotion motion
Definition: motion_path_generator.hpp:28
frankx::PathMotionGenerator::frame
Affine frame
Definition: motion_path_generator.hpp:27
motion_data.hpp
motion_path.hpp
trajectory.hpp
movex::MotionData
Definition: motion_data.hpp:8
frankx::Affine
affx::Affine Affine
Definition: motion_generator.hpp:15
frankx::PathMotionGenerator::robot
RobotType * robot
Definition: motion_path_generator.hpp:26
frankx::PathMotionGenerator
Definition: motion_path_generator.hpp:18
movex
Definition: motion_impedance.hpp:13
frankx::PathMotionGenerator::data
MotionData & data
Definition: motion_path_generator.hpp:29
frankx::PathMotionGenerator::operator()
franka::CartesianPose operator()(const franka::RobotState &robot_state, franka::Duration period)
Definition: motion_path_generator.hpp:50
path.hpp