Frankx  0.2.0
A High-Level Motion API for Franka
time_parametrization.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <ruckig/ruckig.hpp>
5 
6 
7 namespace movex {
8 
10  std::unique_ptr<ruckig::Ruckig<1>> otg;
11 
13  const double delta_time;
14 
15  double intersection(double s1, double ds1, double s2, double ds2, double max_ds, double max_dds, double max_ddds) {
16 
17  }
18 
19  std::tuple<double, double> max_dynamics(double s_start, double ds_start, double max_ds, double max_dds, double max_ddds) {
20 
21  }
22 
23 public:
24  TimeParametrization(double delta_time): delta_time(delta_time) {
25  otg = std::make_unique<ruckig::Ruckig<1>>(delta_time);
26  }
27 
29  Trajectory parametrize(const Path& path, const std::array<double, 7>& max_velocity, const std::array<double, 7>& max_acceleration, const std::array<double, 7>& max_jerk) {
30  Trajectory trajectory {path};
31 
32  // For linear segments: accelerate as fast as possible
33  // For blend segments: Constant path velocity ds
34  // Check continuous, and go back to zero velocity otherwise
35 
36  Vector7d max_velocity_v = Eigen::Map<const Vector7d>(max_velocity.data(), max_velocity.size());
37  Vector7d max_accleration_v = Eigen::Map<const Vector7d>(max_acceleration.data(), max_acceleration.size());
38  Vector7d max_jerk_v = Eigen::Map<const Vector7d>(max_jerk.data(), max_jerk.size());
39 
40  std::vector<std::tuple<double, double, double>> max_path_dynamics;
41  for (auto segment: path.segments) {
42  auto max_pddq = segment->max_pddq();
43  auto max_pdddq = segment->max_pdddq();
44 
45  double max_ds, max_dds, max_ddds;
46 
47  // Linear segments
48  if ((max_pddq.array().abs() < 1e-16).any() && (max_pdddq.array().abs() < 1e-16).any()) {
49  auto constant_pdq = segment->pdq(0.0);
50 
51  max_ds = (max_velocity_v.array() / constant_pdq.array().abs()).minCoeff();
52  max_dds = (max_accleration_v.array() / constant_pdq.array().abs()).minCoeff();
53  max_ddds = (max_jerk_v.array() / constant_pdq.array().abs()).minCoeff();
54 
55  // Other segments
56  } else {
57  // ds = max_velocity_v.array() / pdq(s) // pdq will always be between two linear segments...
58  double ds_acc = (max_accleration_v.array() / max_pddq.array().abs()).sqrt().minCoeff();
59  double ds_jerk = (max_jerk_v.array() / max_pdddq.array().abs()).pow(1./3).minCoeff();
60  max_ds = std::min(ds_acc, ds_jerk);
61  max_dds = 0.0;
62  max_ddds = 0.0;
63  }
64 
65  max_path_dynamics.push_back({max_ds, max_dds, max_ddds});
66  }
67 
68  double current_s, current_ds, current_dds;
69  // Integrate forward and (if necessary) backward
70  // Get maximal possible velocity, acceleration
71 
72  // Calculate path at time steps
73  ruckig::InputParameter<1> input;
74  ruckig::OutputParameter<1> output;
75  ruckig::Result otg_result {ruckig::Result::Working};
76 
77  double time {0.0};
78  double s_new {0.0}, ds_new {0.0}, dds_new {0.0};
79  size_t index_current = path.get_index(s_new);
80  // auto [segment_new, s_local_new] = path.get_local(s_new);
81 
82  Trajectory::State current_state {time, s_new, ds_new, dds_new, 0.0};
83  trajectory.states.push_back(current_state);
84 
85  input.current_position[0] = s_new;
86  input.current_velocity[0] = ds_new;
87  input.current_acceleration[0] = dds_new;
88  input.target_position[0] = path.get_length();
89  input.target_velocity[0] = 0.0;
90  std::tie(input.max_velocity[0], input.max_acceleration[0], input.max_jerk[0]) = max_path_dynamics[index_current];
91 
92  while (otg_result == ruckig::Result::Working) {
93  time += delta_time;
94  otg_result = otg->update(input, output);
95 
96  s_new = output.new_position[0];
97  ds_new = output.new_velocity[0];
98  dds_new = output.new_acceleration[0];
99 
100  size_t index_new = path.get_index(s_new);
101 
102  // New segment
103  if (index_new > index_current) {
104  index_current = index_new;
105 
106  // std::tie(segment_new, s_local_new) = path.get_local(s_new);
107  // std::tie(input.max_velocity(0), input.max_acceleration(0), input.max_jerk(0)) = max_path_dynamics[index_current];
108  }
109 
110  current_state = {time, s_new, ds_new, dds_new, 0.0};
111  trajectory.states.push_back(current_state);
112 
113  input.current_position = output.new_position;
114  input.current_velocity = output.new_velocity;
115  input.current_acceleration = output.new_acceleration;
116  }
117 
118  return trajectory;
119  }
120 };
121 
122 } // namespace movex
movex::Path
Definition: path.hpp:15
movex::Trajectory::State
Definition: trajectory.hpp:9
movex::Trajectory
Definition: trajectory.hpp:8
movex::TimeParametrization
Definition: time_parametrization.hpp:9
movex::Path::segments
std::vector< std::shared_ptr< Segment > > segments
Definition: path.hpp:27
movex::Path::get_length
double get_length() const
Definition: path.cpp:94
trajectory.hpp
movex::Vector7d
Eigen::Matrix< double, 7, 1 > Vector7d
Definition: segment.hpp:10
movex::Path::get_index
size_t get_index(double s) const
Definition: path.cpp:6
movex
Definition: motion_impedance.hpp:13
movex::TimeParametrization::parametrize
Trajectory parametrize(const Path &path, const std::array< double, 7 > &max_velocity, const std::array< double, 7 > &max_acceleration, const std::array< double, 7 > &max_jerk)
Returns list of path positions s at delta time.
Definition: time_parametrization.hpp:29
movex::TimeParametrization::TimeParametrization
TimeParametrization(double delta_time)
Definition: time_parametrization.hpp:24