Franky  0.9.1
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>
4 
6 #include "franky/types.hpp"
7 
8 namespace franky {
9 
11  private:
12  const std::unique_ptr<ruckig::Ruckig<1>> otg;
13 
15  const double delta_time;
16 
17  public:
18  explicit TimeParametrization(double delta_time)
19  : delta_time(delta_time), otg(std::make_unique<ruckig::Ruckig<1>>(delta_time)) {}
20 
22  template<size_t state_dimensions>
25  const std::array<double, state_dimensions> &max_velocity,
26  const std::array<double, state_dimensions> &max_acceleration,
27  const std::array<double, state_dimensions> &max_jerk) {
29 
30  // For linear segments: accelerate as fast as possible
31  // For blend segments: Constant path velocity ds
32  // Check continuous, and go back to zero velocity otherwise
33 
34  Eigen::Vector<double, state_dimensions> max_velocity_v = \
35  Eigen::Map<const Eigen::Vector<double, state_dimensions>>(max_velocity.data(), max_velocity.size());
36  Eigen::Vector<double, state_dimensions> max_accleration_v = \
37  Eigen::Map<const Eigen::Vector<double, state_dimensions>>(max_acceleration.data(), max_acceleration.size());
38  Eigen::Vector<double, state_dimensions> max_jerk_v = \
39  Eigen::Map<const Eigen::Vector<double, state_dimensions>>(max_jerk.data(), max_jerk.size());
40 
41  std::vector<std::tuple<double, double, double>> max_path_dynamics;
42  for (auto segment : path.segments()) {
43  auto max_pddq = segment->max_ddq();
44  auto max_pdddq = segment->max_dddq();
45 
46  double max_ds, max_dds, max_ddds;
47 
48  // Linear segments
49  if ((max_pddq.array().abs() < 1e-16).any() && (max_pdddq.array().abs() < 1e-16).any()) {
50  auto constant_pdq = (*segment)(0.0).dq;
51 
52  max_ds = (max_velocity_v.array() / constant_pdq.array().abs()).minCoeff();
53  max_dds = (max_accleration_v.array() / constant_pdq.array().abs()).minCoeff();
54  max_ddds = (max_jerk_v.array() / constant_pdq.array().abs()).minCoeff();
55 
56  // Other segments
57  } else {
58  // ds = max_velocity_v.array() / pdq(s) // pdq will always be between two linear segments...
59  double ds_acc = (max_accleration_v.array() / max_pddq.array().abs()).sqrt().minCoeff();
60  double ds_jerk = (max_jerk_v.array() / max_pdddq.array().abs()).pow(1. / 3).minCoeff();
61  max_ds = std::min(ds_acc, ds_jerk);
62  max_dds = 0.0;
63  max_ddds = 0.0;
64  }
65 
66  max_path_dynamics.emplace_back(max_ds, max_dds, max_ddds);
67  }
68 
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 
81  trajectory.states.push_back(TrajectoryState{time, s_new, ds_new, dds_new, 0.0});
82 
83  input.current_position[0] = s_new;
84  input.current_velocity[0] = ds_new;
85  input.current_acceleration[0] = dds_new;
86  input.target_position[0] = path.length();
87  input.target_velocity[0] = 0.0;
88  std::tie(input.max_velocity[0], input.max_acceleration[0], input.max_jerk[0]) = max_path_dynamics[index_current];
89 
90  while (otg_result == ruckig::Result::Working) {
91  time += delta_time;
92  otg_result = otg->update(input, output);
93 
94  s_new = output.new_position[0];
95  ds_new = output.new_velocity[0];
96  dds_new = output.new_acceleration[0];
97 
98  size_t index_new = path.get_index(s_new);
99 
100  // New segment
101  if (index_new > index_current) {
102  index_current = index_new;
103  }
104 
105  trajectory.states.push_back(TrajectoryState{time, s_new, ds_new, dds_new, 0.0});
106  output.pass_to_input(input);
107  }
108 
109  return trajectory;
110  }
111 };
112 
113 } // namespace franky
Definition: aggregated_path.hpp:11
size_t get_index(double s) const
Definition: aggregated_path.hpp:67
Vector max_ddq() const override
Definition: aggregated_path.hpp:35
double length() const override
Definition: aggregated_path.hpp:31
std::vector< std::shared_ptr< Path< state_dimensions > > > segments() const
Definition: aggregated_path.hpp:73
Definition: time_parametrization.hpp:10
Trajectory< AggregatedPath< state_dimensions > > parametrize(const AggregatedPath< state_dimensions > &path, const std::array< double, state_dimensions > &max_velocity, const std::array< double, state_dimensions > &max_acceleration, const std::array< double, state_dimensions > &max_jerk)
Returns list of path positions s at delta time.
Definition: time_parametrization.hpp:23
TimeParametrization(double delta_time)
Definition: time_parametrization.hpp:18
Definition: gripper.cpp:3
Definition: trajectory.hpp:16
Definition: trajectory.hpp:7