ocra-recipes
Doxygen documentation for the ocra-recipes repository
TaskYarpInterface.h
Go to the documentation of this file.
1 #ifndef TASK_YARP_INTERFACE_H
2 #define TASK_YARP_INTERFACE_H
3 
4 
5 #include "ocra/util/Macros.h"
7 #include "ocra/control/Task.h"
11 #include "ocra/util/ErrorsHelper.h"
12 #include <Eigen/Dense>
13 
14 #include <yarp/os/Network.h>
15 #include <yarp/os/PortReader.h>
16 #include <yarp/os/RpcServer.h>
17 #include <yarp/os/ConnectionReader.h>
18 #include <yarp/os/Port.h>
19 #include <yarp/os/RateThread.h>
20 #include <yarp/os/Log.h>
21 #include <yarp/os/LogStream.h>
22 
23 namespace ocra
24 {
25 
30 {
34 };
35 
36 
42 {
44 public:
45 
53  TaskYarpInterface(Task::Ptr taskPtr);
54 
57  virtual ~TaskYarpInterface();
58 
64  bool activate();
65 
71  bool activate(const TASK_MODE tmode);
72 
77  bool deactivate();
78 
85 
90  std::string getPortName();
91 
96  bool isActivated();
97 
103  Eigen::VectorXd getTaskError();
109  int getTaskHierarchyLevel();
110  void setTaskHierarchyLevel(int level);
116  double getTaskErrorNorm();
117 
121  void setStiffness(double K);
122 
126  void setStiffness(const VectorXd& K);
127 
128 
132  void setStiffness(const MatrixXd& K);
133 
138  double getStiffness();
139 
144  Eigen::MatrixXd getStiffnessMatrix();
145 
149  void setDamping(double B);
150 
154  void setDamping(const VectorXd& B);
155 
156 
160  void setDamping(const MatrixXd& B);
161 
166  double getDamping();
167 
172  Eigen::MatrixXd getDampingMatrix();
173 
174 
178  void setWeight(double weight);
179 
183  void setWeight(const Eigen::VectorXd& weights);
184 
189  Eigen::VectorXd getWeight();
190 
191 
192 
195  void setDesiredTaskState(const TaskState& newDesiredTaskState);
196  void setDesiredTaskStateDirect(const TaskState& newDesiredTaskState);
197  void publishTaskState();
198 private:
199 // yarp::os::Mutex getTaskStateMutex;
200 
201 
202 public: /* Segment Frame Based Tasks */
203 
204  // TODO: Document...
205  Eigen::Displacementd getTaskFrameDisplacement();
206  Eigen::Twistd getTaskFrameVelocity();
207  Eigen::Twistd getTaskFrameAcceleration();
208  Eigen::Vector3d getTaskFramePosition();
209  Eigen::Rotation3d getTaskFrameOrientation();
210  Eigen::Vector3d getTaskFrameLinearVelocity();
211  Eigen::Vector3d getTaskFrameAngularVelocity();
212  Eigen::Vector3d getTaskFrameLinearAcceleration();
213  Eigen::Vector3d getTaskFrameAngularAcceleration();
214 
215 
216 
217 
218 public: /*Nested callback classes */
219 
225  class RpcMessageCallback : public yarp::os::PortReader {
227 
228  private:
229  TaskYarpInterface& tmBase;
230 
231  public:
233 
234  virtual bool read(yarp::os::ConnectionReader& connection);
235  };
236 
242  class ControlInputCallback : public yarp::os::PortReader {
244 
245  private:
246  TaskYarpInterface& tmBase;
247  yarp::os::Bottle input;
248 
249  public:
251 
252  virtual bool read(yarp::os::ConnectionReader& connection);
253  };
254 
260  class StateUpdateThread : public yarp::os::RateThread
261  {
263 
264  private:
265  TaskYarpInterface& tmBase;
266 
267 
268  public:
269  StateUpdateThread(int period, TaskYarpInterface& tmBaseRef);
270  bool threadInit();
271  void run();
272  void threadRelease();
273  };
274 
275 
276 private: /* Private methods */
277  // For parsing and compiling yarp messages.
278  void parseIncomingMessage(yarp::os::Bottle& input, yarp::os::Bottle& reply);
279  std::string printValidMessageTags();
280  bool openControlPorts();
281  bool closeControlPorts();
282  bool parseControlInput(yarp::os::Bottle& input);
283 
284 
285 private:
286  Task::Ptr task;
287  TASK_MODE taskMode;
288  yarp::os::Log yLog;
289 
290  yarp::os::Network yarp;
291  yarp::os::RpcServer rpcPort;
292  std::string portName;
293  RpcMessageCallback::Ptr rpcCallback;
294 
295  bool controlPortsOpen;
296 
297  yarp::os::Bottle stateInBottle;
298  std::string inputControlPortName;
299  yarp::os::Port inputControlPort;
300  ControlInputCallback::Ptr controlCallback;
301 
302  yarp::os::Bottle stateOutBottle;
303  std::string outputControlPortName;
304  yarp::os::Port outputControlPort;
305 
306  yarp::os::Bottle desiredStateOutBottle;
307  std::string desiredStateOutputPortName;
308  yarp::os::Port desiredStateOutputPort;
309 
310 
311 
312 
313  StateUpdateThread::Ptr stateThread;
314  int numberOfOpenRequests;
315 
316 
317  bool logMessages;
318 
319 };
320 
321 }
322 
323 #endif // TASK_YARP_INTERFACE_H
Eigen::Vector3d getTaskFramePosition()
Eigen::Vector3d getTaskFrameAngularVelocity()
Eigen::Vector3d getTaskFrameLinearVelocity()
void setWeight(double weight)
Eigen::Twistd getTaskFrameAcceleration()
Eigen::Vector3d getTaskFrameLinearAcceleration()
void setTaskHierarchyLevel(int level)
#define DEFINE_CLASS_POINTER_TYPEDEFS(Class)
Definition: Macros.h:8
void setDesiredTaskState(const TaskState &newDesiredTaskState)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
RpcMessageCallback(TaskYarpInterface &tmBaseRef)
Eigen::Rotation3d getTaskFrameOrientation()
Eigen::MatrixXd getDampingMatrix()
Eigen::VectorXd getTaskError()
Eigen::Twistd getTaskFrameVelocity()
Eigen::MatrixXd getStiffnessMatrix()
TASK_MODE
A basic enumeration for the different types of tasks we can have.
Eigen::Vector3d getTaskFrameAngularAcceleration()
virtual bool read(yarp::os::ConnectionReader &connection)
Eigen::Displacementd getTaskFrameDisplacement()
void setDesiredTaskStateDirect(const TaskState &newDesiredTaskState)
TaskYarpInterface(Task::Ptr taskPtr)