Franky  0.9.1
A High-Level Motion API for Franka
motion_generator.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <mutex>
5 
6 #include <franka/duration.h>
7 #include <franka/robot_state.h>
8 #include "ruckig/ruckig.hpp"
9 
10 #include "franky/types.hpp"
11 
12 namespace franky {
13 
17 struct ReactionRecursionException : public std::runtime_error {
18  using std::runtime_error::runtime_error;
19 };
20 
21 class Robot;
22 
23 template<typename ControlSignalType>
24 class Motion;
25 
29 template<typename ControlSignalType>
31  public:
36  static constexpr size_t REACTION_RECURSION_LIMIT = 8;
37 
42  explicit MotionGenerator(Robot *robot, std::shared_ptr<Motion<ControlSignalType>> initial_motion);
43 
50  ControlSignalType operator()(const franka::RobotState &robot_state, franka::Duration period);
51 
57  inline void
58  registerUpdateCallback(const std::function<void(const franka::RobotState &, franka::Duration, double)> &callback) {
59  update_callbacks_.push_back(callback);
60  }
61 
66  void updateMotion(std::shared_ptr<Motion<ControlSignalType>> new_motion);
67 
71  bool has_new_motion();
72 
76  void resetTimeUnsafe();
77 
78  private:
79  std::shared_ptr<Motion<ControlSignalType>> initial_motion_;
80  std::shared_ptr<Motion<ControlSignalType>> current_motion_;
81  std::shared_ptr<Motion<ControlSignalType>> new_motion_;
82  std::vector<std::function<void(const franka::RobotState &, franka::Duration, double)>> update_callbacks_;
83  std::mutex new_motion_mutex_;
84  std::optional<ControlSignalType> previous_command_;
85 
86  double abs_time_{0.0};
87  double rel_time_offset_{0.0};
88  Robot *robot_;
89 };
90 
91 } // namespace franky
Helper class for handling motions and reactions.
Definition: motion_generator.hpp:30
void registerUpdateCallback(const std::function< void(const franka::RobotState &, franka::Duration, double)> &callback)
Register a callback that is called in every step of the motion.
Definition: motion_generator.hpp:58
void updateMotion(std::shared_ptr< Motion< ControlSignalType >> new_motion)
Update the current motion.
Definition: motion_generator.cpp:68
static constexpr size_t REACTION_RECURSION_LIMIT
Maximum recursion limit for reactions. After more than this number of reactions are fired in a single...
Definition: motion_generator.hpp:36
MotionGenerator(Robot *robot, std::shared_ptr< Motion< ControlSignalType >> initial_motion)
Definition: motion_generator.cpp:18
ControlSignalType operator()(const franka::RobotState &robot_state, franka::Duration period)
Update the motion generator and get the next control signal.
Definition: motion_generator.cpp:24
void resetTimeUnsafe()
Reset the time of the motion generator without locking the mutex.
Definition: motion_generator.cpp:79
bool has_new_motion()
Whether a new motion is available.
Definition: motion_generator.cpp:74
Base class for motions.
Definition: motion.hpp:22
A class representing a Franka robot.
Definition: robot.hpp:45
Definition: gripper.cpp:3
ControlSignalType
Type of control signal.
Definition: control_signal_type.hpp:8
Exception thrown when the reaction recursion limit (8) is reached.
Definition: motion_generator.hpp:17