Aikido
Executor.hpp
Go to the documentation of this file.
1 #ifndef AIKIDO_CONTROL_EXECUTOR_HPP_
2 #define AIKIDO_CONTROL_EXECUTOR_HPP_
3 
4 #include <chrono>
5 #include <set>
6 #include <vector>
7 
8 #include <dart/dart.hpp>
9 
12 
13 namespace aikido {
14 namespace control {
15 
17 
18 enum class ExecutorType
36 {
37  STATE = 0,
38  POSITION = 1,
39  VELOCITY = 2,
40  EFFORT = 3,
41  TRAJECTORY = 4,
42  MODE = 5,
43  READONLY = 6
44 };
45 
47 constexpr std::chrono::milliseconds defaultThreadRate{10};
48 
50 class Executor
51 {
52 public:
57  Executor(
58  const std::set<ExecutorType>& types,
59  const std::vector<dart::dynamics::DegreeOfFreedom*>& dofs,
60  const std::chrono::milliseconds threadRate = defaultThreadRate);
61 
66  Executor(
67  const ExecutorType type,
68  const std::vector<dart::dynamics::DegreeOfFreedom*>& dofs,
69  std::chrono::milliseconds threadRate = defaultThreadRate);
70 
71  virtual ~Executor();
72 
74  std::set<ExecutorType> getTypes() const;
75 
77  const std::vector<dart::dynamics::DegreeOfFreedom*> getDofs() const;
78 
84  virtual void step(
85  const std::chrono::system_clock::time_point& /* timepoint */)
86  {
87  // Do nothing
88  }
89 
91  void start();
92 
94  void stop();
95 
99  bool registerDofs();
100 
102  void releaseDofs();
103 
105  virtual void cancel()
106  {
107  // Do nothing
108  }
109 
110 private:
113  void spin()
114  {
115  if (mThread->isRunning())
116  {
117  step(std::chrono::system_clock::now());
118  }
119  }
120 
122  std::chrono::milliseconds mThreadRate;
123 
125  bool mDofsRegistered{false};
126 
128  static std::
129  unordered_map<ExecutorType, std::set<dart::dynamics::DegreeOfFreedom*>>
131 
133  static std::mutex mMutex;
134 
136  std::unique_ptr<aikido::common::ExecutorThread> mThread;
137 
138 protected:
140  std::set<ExecutorType> mTypes;
141 
143  std::vector<dart::dynamics::DegreeOfFreedom*> mDofs;
144 };
145 
146 } // namespace control
147 } // namespace aikido
148 
149 #endif
aikido::control::ExecutorType::VELOCITY
@ VELOCITY
aikido::control::ExecutorType::TRAJECTORY
@ TRAJECTORY
aikido::control::Executor::mDofManager
static std::unordered_map< ExecutorType, std::set< dart::dynamics::DegreeOfFreedom * > > mDofManager
Manager for locking resources for degrees of freedom.
Definition: Executor.hpp:130
aikido::control::Executor::~Executor
virtual ~Executor()
aikido::control::Executor::Executor
Executor(const std::set< ExecutorType > &types, const std::vector< dart::dynamics::DegreeOfFreedom * > &dofs, const std::chrono::milliseconds threadRate=defaultThreadRate)
Constructor.
aikido
Format of serialized trajectory in YAML.
Definition: algorithm.hpp:4
aikido::control::ExecutorType::MODE
@ MODE
aikido::control::ExecutorType::POSITION
@ POSITION
aikido::control::Executor::mDofsRegistered
bool mDofsRegistered
Whether this executor has a lock on its DoF resources.
Definition: Executor.hpp:125
aikido::control::ExecutorType::STATE
@ STATE
aikido::control::Executor::mDofs
std::vector< dart::dynamics::DegreeOfFreedom * > mDofs
Vector of dof names.
Definition: Executor.hpp:143
aikido::control::Executor::mTypes
std::set< ExecutorType > mTypes
Vector of executor types.
Definition: Executor.hpp:140
aikido::control::Executor::getTypes
std::set< ExecutorType > getTypes() const
Get all of this Executor's ExecutorTypes.
aikido::control::Executor::step
virtual void step(const std::chrono::system_clock::time_point &)
Step to a point in time.
Definition: Executor.hpp:84
aikido::control::Executor::cancel
virtual void cancel()
Cancel the current command.
Definition: Executor.hpp:105
pointers.hpp
aikido::control::ExecutorType::READONLY
@ READONLY
aikido::control::Executor::mMutex
static std::mutex mMutex
Mutex to protects the DofManager.
Definition: Executor.hpp:133
aikido::control::Executor::start
void start()
Start the underlying ExecutorThread.
aikido::control::Executor::spin
void spin()
Call to spin first to pass current time to step Necessary for real-time execution via mThread.
Definition: Executor.hpp:113
aikido::control::Executor::releaseDofs
void releaseDofs()
Unlock any resources required by the DoFs.
aikido::control::ExecutorType::EFFORT
@ EFFORT
aikido::control::defaultThreadRate
constexpr std::chrono::milliseconds defaultThreadRate
Default rate for ExecutorThread to call step()
Definition: Executor.hpp:47
aikido::control::Executor::registerDofs
bool registerDofs()
Lock the resources required by the DoFs.
aikido::control::Executor::mThread
std::unique_ptr< aikido::common::ExecutorThread > mThread
Executor thread calling step function.
Definition: Executor.hpp:136
ExecutorThread.hpp
aikido::control::ExecutorType
ExecutorType
Type of executor Can be used to determine if 2 executors make conflicting demands of individual degre...
Definition: Executor.hpp:35
aikido::control::Executor::getDofs
const std::vector< dart::dynamics::DegreeOfFreedom * > getDofs() const
Get list of dofs needed by this Executor.
aikido::control::Executor
Abstract class for executing commands on degrees of freedom.
Definition: Executor.hpp:50
AIKIDO_DECLARE_POINTERS
#define AIKIDO_DECLARE_POINTERS(X)
Definition: pointers.hpp:21
aikido::control::Executor::stop
void stop()
Stops the underlying ExecutorThread.
aikido::control::Executor::mThreadRate
std::chrono::milliseconds mThreadRate
How often to run spin() on internal thread.
Definition: Executor.hpp:122