kinematics-dynamics
BasicCartesianControl.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __BASIC_CARTESIAN_CONTROL_HPP__
4 #define __BASIC_CARTESIAN_CONTROL_HPP__
5 
6 #include <atomic>
7 #include <functional>
8 #include <memory>
9 #include <mutex>
10 #include <utility>
11 #include <vector>
12 
13 #include <yarp/os/PeriodicThread.h>
14 
15 #include <yarp/dev/DeviceDriver.h>
16 #include <yarp/dev/PolyDriver.h>
17 #include <yarp/dev/ControlBoardInterfaces.h>
18 #include <yarp/dev/IPreciselyTimed.h>
19 
20 #include <kdl/trajectory.hpp>
21 
22 #include "ICartesianSolver.h"
23 #include "ICartesianControl.h"
24 
25 namespace roboticslab
26 {
27 
107 class BasicCartesianControl : public yarp::dev::DeviceDriver,
108  public yarp::os::PeriodicThread,
109  public ICartesianControl
110 {
111 public:
112  BasicCartesianControl() : yarp::os::PeriodicThread(1.0, yarp::os::PeriodicThreadClock::Absolute)
113  {}
114 
115  // -- ICartesianControl declarations. Implementation in ICartesianControlImpl.cpp--
116  bool stat(std::vector<double> & x, int * state = nullptr, double * timestamp = nullptr) override;
117  bool inv(const std::vector<double> & xd, std::vector<double> & q) override;
118  bool movj(const std::vector<double> & xd) override;
119  bool relj(const std::vector<double> & xd) override;
120  bool movl(const std::vector<double> & xd) override;
121  bool movv(const std::vector<double> & xdotd) override;
122  bool gcmp() override;
123  bool forc(const std::vector<double> & fd) override;
124  bool stopControl() override;
125  bool wait(double timeout) override;
126  bool tool(const std::vector<double> & x) override;
127  bool act(int command) override;
128  void pose(const std::vector<double> & x) override;
129  void twist(const std::vector<double> & xdot) override;
130  void wrench(const std::vector<double> & w) override;
131  bool setParameter(int vocab, double value) override;
132  bool getParameter(int vocab, double * value) override;
133  bool setParameters(const std::map<int, double> & params) override;
134  bool getParameters(std::map<int, double> & params) override;
135 
136  // -------- PeriodicThread declarations. Implementation in PeriodicThreadImpl.cpp --------
137  void run() override;
138 
139  // -------- DeviceDriver declarations. Implementation in IDeviceImpl.cpp --------
140  bool open(yarp::os::Searchable & config) override;
141  bool close() override;
142 
143 private:
145  {
146  public:
147  template <typename Fn>
148  StateWatcher(Fn && fn) : handler(std::move(fn))
149  {}
150 
151  ~StateWatcher()
152  { if (handler) handler(); }
153 
154  void suppress() const
155  { handler = nullptr; }
156 
157  private:
158  mutable std::function<void()> handler;
159  };
160 
161  int getCurrentState() const;
162  void setCurrentState(int value);
163 
164  bool checkJointLimits(const std::vector<double> & q);
165  bool checkJointLimits(const std::vector<double> & q, const std::vector<double> & qdot);
166  bool checkJointVelocities(const std::vector<double> & qdot);
167  bool doFailFastChecks(const std::vector<double> & initialQ);
168  bool checkControlModes(int mode);
169  bool setControlModes(int mode);
170  bool presetStreamingCommand(int command);
171  bool computeIsocronousSpeeds(const std::vector<double> & q, const std::vector<double> & qd, std::vector<double> & qdot);
172 
173  void handleMovj(const std::vector<double> & q, const StateWatcher & watcher);
174  void handleMovlVel(const std::vector<double> & q, const StateWatcher & watcher);
175  void handleMovlPosd(const std::vector<double> & q, const StateWatcher & watcher);
176  void handleMovv(const std::vector<double> & q, const StateWatcher & watcher);
177  void handleGcmp(const std::vector<double> & q, const StateWatcher & watcher);
178  void handleForc(const std::vector<double> & q, const std::vector<double> & qdot, const std::vector<double> & qdotdot, const StateWatcher & watcher);
179 
180  yarp::dev::PolyDriver solverDevice;
181  ICartesianSolver * iCartesianSolver {nullptr};
182 
183  yarp::dev::PolyDriver robotDevice;
184  yarp::dev::IControlMode * iControlMode {nullptr};
185  yarp::dev::IEncoders * iEncoders {nullptr};
186  yarp::dev::IPositionControl * iPositionControl {nullptr};
187  yarp::dev::IPositionDirect * iPositionDirect {nullptr};
188  yarp::dev::IPreciselyTimed * iPreciselyTimed {nullptr};
189  yarp::dev::ITorqueControl * iTorqueControl {nullptr};
190  yarp::dev::IVelocityControl * iVelocityControl {nullptr};
191 
192  ICartesianSolver::reference_frame referenceFrame;
193 
194  double gain;
195  double duration; // [s]
196 
197  int cmcPeriodMs;
198  int waitPeriodMs;
199  int numJoints;
200  int currentState;
201  int streamingCommand;
202 
203  bool usePosdMovl;
204  bool enableFailFast;
205 
206  mutable std::mutex stateMutex;
207 
209  std::vector<double> vmoStored;
210 
213 
215  std::vector<std::unique_ptr<KDL::Trajectory>> trajectories;
216 
218  std::vector<double> fd;
219 
220  int encoderErrors {0};
221  std::atomic_bool cmcSuccess;
222 
223  std::vector<double> qMin, qMax;
224  std::vector<double> qdotMin, qdotMax;
225  std::vector<double> qRefSpeeds;
226 };
227 
228 } // namespace roboticslab
229 
230 #endif // __BASIC_CARTESIAN_CONTROL_HPP__
Contains roboticslab::ICartesianControl and related vocabs.
Contains roboticslab::ICartesianSolver.
Definition: BasicCartesianControl.hpp:145
The BasicCartesianControl class implements ICartesianControl.
Definition: BasicCartesianControl.hpp:110
bool act(int command) override
Actuate tool.
Definition: ICartesianControlImpl.cpp:421
bool movl(const std::vector< double > &xd) override
Linear move to target position.
Definition: ICartesianControlImpl.cpp:175
void wrench(const std::vector< double > &w) override
Exert force.
Definition: ICartesianControlImpl.cpp:515
std::vector< std::unique_ptr< KDL::Trajectory > > trajectories
Definition: BasicCartesianControl.hpp:215
bool tool(const std::vector< double > &x) override
Change tool.
Definition: ICartesianControlImpl.cpp:402
bool wait(double timeout) override
Wait until completion.
Definition: ICartesianControlImpl.cpp:373
std::vector< double > fd
Definition: BasicCartesianControl.hpp:218
double movementStartTime
Definition: BasicCartesianControl.hpp:212
bool stopControl() override
Stop control.
Definition: ICartesianControlImpl.cpp:348
bool getParameters(std::map< int, double > &params) override
Retrieve multiple configuration parameters.
Definition: ICartesianControlImpl.cpp:693
bool getParameter(int vocab, double *value) override
Retrieve a configuration parameter.
Definition: ICartesianControlImpl.cpp:641
std::vector< double > vmoStored
Definition: BasicCartesianControl.hpp:209
void twist(const std::vector< double > &xdot) override
Instantaneous velocity steps.
Definition: ICartesianControlImpl.cpp:475
bool movj(const std::vector< double > &xd) override
Move in joint space, absolute coordinates.
Definition: ICartesianControlImpl.cpp:89
void pose(const std::vector< double > &x) override
Achieve pose.
Definition: ICartesianControlImpl.cpp:429
bool stat(std::vector< double > &x, int *state=nullptr, double *timestamp=nullptr) override
Current state and position.
Definition: ICartesianControlImpl.cpp:37
bool forc(const std::vector< double > &fd) override
Force control.
Definition: ICartesianControlImpl.cpp:320
bool movv(const std::vector< double > &xdotd) override
Linear move with given velocity.
Definition: ICartesianControlImpl.cpp:251
bool gcmp() override
Gravity compensation.
Definition: ICartesianControlImpl.cpp:306
bool relj(const std::vector< double > &xd) override
Move in joint space, relative coordinates.
Definition: ICartesianControlImpl.cpp:150
bool setParameters(const std::map< int, double > &params) override
Set multiple configuration parameters.
Definition: ICartesianControlImpl.cpp:673
bool setParameter(int vocab, double value) override
Set a configuration parameter.
Definition: ICartesianControlImpl.cpp:573
bool inv(const std::vector< double > &xd, std::vector< double > &q) override
Inverse kinematics.
Definition: ICartesianControlImpl.cpp:68
Abstract base class for a cartesian controller.
Definition: ICartesianControl.h:146
Abstract base class for a cartesian solver.
Definition: ICartesianSolver.h:23
reference_frame
Lists supported reference frames.
Definition: ICartesianSolver.h:27
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6