franky 1.1.4
A High-Level Motion API for Franka
Loading...
Searching...
No Matches
motion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <list>
5#include <mutex>
6
7#include "franky/robot.hpp"
9#include "reaction.hpp"
10
11namespace franky {
12
13class Robot;
14
15template <typename ControlSignalType>
16class Reaction;
17
24template <typename ControlSignalType>
25class Motion {
26 public:
27 virtual ~Motion() = default;
28 using CallbackType = std::function<void(
29 const RobotState &, franka::Duration, franka::Duration, franka::Duration, const ControlSignalType &)>;
30
38 void addReaction(std::shared_ptr<Reaction<ControlSignalType>> reaction);
39
47 void addReactionFront(std::shared_ptr<Reaction<ControlSignalType>> reaction);
48
52 std::vector<std::shared_ptr<Reaction<ControlSignalType>>> reactions();
53
60 void registerCallback(CallbackType callback);
61
69 void init(Robot *robot, const RobotState &robot_state, const std::optional<ControlSignalType> &previous_command);
70
75 [[nodiscard]] bool has_started() const { return started_.load(); }
76
87 const RobotState &robot_state, franka::Duration time_step, franka::Duration rel_time, franka::Duration abs_time,
88 const std::optional<ControlSignalType> &previous_command);
89
97 std::shared_ptr<Motion<ControlSignalType>> checkAndCallReactions(
98 const RobotState &robot_state, franka::Duration rel_time, franka::Duration abs_time);
99
100 protected:
101 explicit Motion();
102
103 virtual void initImpl(const RobotState &robot_state, const std::optional<ControlSignalType> &previous_command) {}
104
106 const RobotState &robot_state, franka::Duration time_step, franka::Duration rel_time, franka::Duration abs_time,
107 const std::optional<ControlSignalType> &previous_command) = 0;
108
109 [[nodiscard]] Robot *robot() const { return robot_; }
110
111 private:
112 std::mutex reaction_mutex_;
113 std::list<std::shared_ptr<Reaction<ControlSignalType>>> reactions_;
114 std::mutex callback_mutex_;
115 std::vector<CallbackType> callbacks_;
116 Robot *robot_;
117 std::atomic<bool> started_{false};
118};
119
120} // namespace franky
Base class for motions.
Definition motion.hpp:25
void registerCallback(CallbackType callback)
Register a callback that is called in every step of the motion.
Definition motion.cpp:24
std::shared_ptr< Motion< ControlSignalType > > checkAndCallReactions(const RobotState &robot_state, franka::Duration rel_time, franka::Duration abs_time)
Check and call reactions.
Definition motion.cpp:74
void init(Robot *robot, const RobotState &robot_state, const std::optional< ControlSignalType > &previous_command)
Initialize the motion. Motions can only be started once; this function throws a MotionReuseException ...
Definition motion.cpp:50
bool has_started() const
Whether this motion has already been started. Started motions cannot be started again.
Definition motion.hpp:75
Robot * robot() const
Definition motion.hpp:109
void addReaction(std::shared_ptr< Reaction< ControlSignalType > > reaction)
Add a reaction to the motion.
Definition motion.cpp:30
void addReactionFront(std::shared_ptr< Reaction< ControlSignalType > > reaction)
Add a reaction to the front of the reaction list.
Definition motion.cpp:37
virtual ~Motion()=default
std::function< void(const RobotState &, franka::Duration, franka::Duration, franka::Duration, const ControlSignalType &)> CallbackType
Definition motion.hpp:29
Motion()
Definition motion.cpp:18
ControlSignalType nextCommand(const RobotState &robot_state, franka::Duration time_step, franka::Duration rel_time, franka::Duration abs_time, const std::optional< ControlSignalType > &previous_command)
Get the next command of the motion.
Definition motion.cpp:64
virtual void initImpl(const RobotState &robot_state, const std::optional< ControlSignalType > &previous_command)
Definition motion.hpp:103
virtual ControlSignalType nextCommandImpl(const RobotState &robot_state, franka::Duration time_step, franka::Duration rel_time, franka::Duration abs_time, const std::optional< ControlSignalType > &previous_command)=0
std::vector< std::shared_ptr< Reaction< ControlSignalType > > > reactions()
Currently registered reactions of the motion.
Definition motion.cpp:44
A reaction that can be attached to a motion.
Definition reaction.hpp:27
A class representing a Franka robot.
Definition robot.hpp:58
Definition dynamics_limit.cpp:8
ControlSignalType
Type of control signal.
Definition control_signal_type.hpp:8
Full state of the robot.
Definition robot_state.hpp:40