Franky  0.9.1
A High-Level Motion API for Franka
motion.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mutex>
4 #include <list>
5 
6 #include "reaction.hpp"
7 #include "franky/robot.hpp"
8 
9 namespace franky {
10 
11 class Robot;
12 
13 template<typename ControlSignalType>
14 class Reaction;
15 
21 template<typename ControlSignalType>
22 class Motion {
23  public:
24  using CallbackType = std::function<
25  void(const franka::RobotState &, franka::Duration, double, double, const ControlSignalType &)>;
26 
33  void addReaction(std::shared_ptr<Reaction<ControlSignalType>> reaction);
34 
41  void addReactionFront(std::shared_ptr<Reaction<ControlSignalType>> reaction);
42 
46  std::vector<std::shared_ptr<Reaction<ControlSignalType>>> reactions();
47 
53  void registerCallback(CallbackType callback);
54 
61  void init(
62  Robot *robot, const franka::RobotState &robot_state, const std::optional<ControlSignalType> &previous_command);
63 
75  const franka::RobotState &robot_state,
76  franka::Duration time_step,
77  double rel_time,
78  double abs_time,
79  const std::optional<ControlSignalType> &previous_command);
80 
88  std::shared_ptr<Motion<ControlSignalType>>
89  checkAndCallReactions(const franka::RobotState &robot_state, double rel_time, double abs_time);
90 
91  protected:
92  explicit Motion();
93 
94  virtual void initImpl(
95  const franka::RobotState &robot_state, const std::optional<ControlSignalType> &previous_command) {}
96 
97  virtual ControlSignalType
99  const franka::RobotState &robot_state,
100  franka::Duration time_step,
101  double rel_time,
102  double abs_time,
103  const std::optional<ControlSignalType> &previous_command) = 0;
104 
105  [[nodiscard]] inline Robot *robot() const {
106  return robot_;
107  }
108 
109  private:
110  std::mutex reaction_mutex_;
111  std::list<std::shared_ptr<Reaction<ControlSignalType>>> reactions_;
112  std::mutex callback_mutex_;
113  std::vector<CallbackType> callbacks_;
114  Robot *robot_;
115 };
116 
117 } // namespace franky
Base class for motions.
Definition: motion.hpp:22
void addReactionFront(std::shared_ptr< Reaction< ControlSignalType >> reaction)
Add a reaction to the front of the reaction list.
Definition: motion.cpp:32
void registerCallback(CallbackType callback)
Register a callback that is called in every step of the motion.
Definition: motion.cpp:20
virtual void initImpl(const franka::RobotState &robot_state, const std::optional< ControlSignalType > &previous_command)
Definition: motion.hpp:94
Robot * robot() const
Definition: motion.hpp:105
void init(Robot *robot, const franka::RobotState &robot_state, const std::optional< ControlSignalType > &previous_command)
Initialize the motion.
Definition: motion.cpp:44
std::function< void(const franka::RobotState &, franka::Duration, double, double, const ControlSignalType &)> CallbackType
Definition: motion.hpp:25
ControlSignalType nextCommand(const franka::RobotState &robot_state, franka::Duration time_step, double rel_time, double abs_time, const std::optional< ControlSignalType > &previous_command)
Get the next command of the motion.
Definition: motion.cpp:52
virtual ControlSignalType nextCommandImpl(const franka::RobotState &robot_state, franka::Duration time_step, double rel_time, double abs_time, const std::optional< ControlSignalType > &previous_command)=0
Motion()
Definition: motion.cpp:17
std::shared_ptr< Motion< ControlSignalType > > checkAndCallReactions(const franka::RobotState &robot_state, double rel_time, double abs_time)
Check and call reactions.
Definition: motion.cpp:66
void addReaction(std::shared_ptr< Reaction< ControlSignalType >> reaction)
Add a reaction to the motion.
Definition: motion.cpp:26
std::vector< std::shared_ptr< Reaction< ControlSignalType > > > reactions()
Currently registered reactions of the motion.
Definition: motion.cpp:38
A reaction that can be attached to a motion.
Definition: reaction.hpp:25
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