iCub-main
iKinSlv.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2018 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms
7  * of the BSD-3-Clause license. See the accompanying LICENSE file for
8  * details.
9 */
10 
236 #ifndef __IKINSLV_H__
237 #define __IKINSLV_H__
238 
239 #include <mutex>
240 #include <condition_variable>
241 #include <string>
242 #include <deque>
243 
244 #include <yarp/os/BufferedPort.h>
245 #include <yarp/os/PeriodicThread.h>
246 #include <yarp/sig/Vector.h>
247 #include <yarp/sig/Matrix.h>
248 
249 #include <yarp/dev/ControlBoardInterfaces.h>
250 #include <yarp/dev/PolyDriver.h>
251 
252 #include <iCub/iKin/iKinHlp.h>
253 #include <iCub/iKin/iKinIpOpt.h>
254 
255 
256 namespace iCub
257 {
258 
259 namespace iKin
260 {
261 
262 class CartesianSolver;
263 
264 class RpcProcessor : public yarp::os::PortReader
265 {
266 protected:
268 
269  virtual bool read(yarp::os::ConnectionReader &connection);
270 
271 public:
272  RpcProcessor(CartesianSolver *_slv) : slv(_slv) { }
273 };
274 
275 
276 class InputPort : public yarp::os::BufferedPort<yarp::os::Bottle>
277 {
278 protected:
280 
281  std::mutex mtx;
282 
283  bool contMode;
284  bool isNew;
285  size_t maxLen;
286  int pose;
287 
288  double token;
289  double *pToken;
290 
291  yarp::sig::Vector dof;
292  yarp::sig::Vector xd;
293 
294  virtual void onRead(yarp::os::Bottle &b);
295 
296 public:
297  InputPort(CartesianSolver *_slv);
298 
299  int &get_pose() { return pose; }
300  bool &get_contMode() { return contMode; }
301  double *get_tokenPtr() { return pToken; }
302  yarp::sig::Vector get_dof();
303  yarp::sig::Vector get_xd();
304 
305  void set_dof(const yarp::sig::Vector &_dof);
306  void reset_xd(const yarp::sig::Vector &_xd);
307  bool isNewDataEvent();
308  bool handleTarget(yarp::os::Bottle *b);
309  bool handleDOF(yarp::os::Bottle *b);
310  bool handlePose(const int newPose);
311  bool handleMode(const int newMode);
312 };
313 
314 
316 {
317 protected:
319 
320 public:
321  SolverCallback(CartesianSolver *_slv) : slv(_slv) { }
322 
323  virtual void exec(const yarp::sig::Vector &xd, const yarp::sig::Vector &q);
324 };
325 
326 
328 {
332  std::deque<yarp::os::Property> prp;
333  std::deque<bool> rvs;
334  int num;
335 };
336 
337 
343 class CartesianSolver : public yarp::os::PeriodicThread,
344  protected CartesianHelper
345 {
346 protected:
348  std::deque<yarp::dev::PolyDriver*> drv;
349  std::deque<yarp::dev::IControlLimits*> lim;
350  std::deque<yarp::dev::IEncoders*> enc;
351  std::deque<int> jnt;
352  std::deque<int*> rmp;
353 
356 
358  yarp::os::Port *rpcPort;
360  yarp::os::BufferedPort<yarp::os::Bottle> *outPort;
361  std::mutex mtx;
362 
363  std::string slvName;
364  std::string type;
365  unsigned int period;
366  unsigned int ctrlPose;
367  bool fullDOF;
370  bool closing;
371  bool closed;
373  bool verbosity;
378  double token;
379  double *pToken;
380 
381  yarp::sig::Matrix hwLimits;
382  yarp::sig::Matrix swLimits;
383 
384  yarp::sig::Vector unctrlJointsOld;
385  yarp::sig::Vector dof;
386 
387  yarp::sig::Vector restJntPos;
388  yarp::sig::Vector restWeights;
389 
390  yarp::sig::Vector xd_2ndTask;
391  yarp::sig::Vector w_2ndTask;
392 
393  yarp::sig::Vector qd_3rdTask;
394  yarp::sig::Vector w_3rdTask;
395  yarp::sig::Vector idx_3rdTask;
396 
397  std::mutex mtx_dofEvent;
398  std::condition_variable cv_dofEvent;
399 
400  virtual PartDescriptor *getPartDesc(yarp::os::Searchable &options)=0;
401  virtual yarp::sig::Vector solve(yarp::sig::Vector &xd);
402 
403  virtual yarp::sig::Vector &encodeDOF();
404  virtual bool decodeDOF(const yarp::sig::Vector &_dof);
405 
406  virtual bool handleJointsRestPosition(const yarp::os::Bottle *options,
407  yarp::os::Bottle *reply=NULL);
408  virtual bool handleJointsRestWeights(const yarp::os::Bottle *options,
409  yarp::os::Bottle *reply=NULL);
410 
411  yarp::dev::PolyDriver *waitPart(const yarp::os::Property &partOpt);
412 
413  bool isNewDOF(const yarp::sig::Vector &_dof);
414  bool changeDOF(const yarp::sig::Vector &_dof);
415 
416  bool alignJointsBounds();
417  bool setLimits(int axis, double min, double max);
419  void latchUncontrolledJoints(yarp::sig::Vector &joints);
420  void getFeedback(const bool wait=false);
421  void initPos();
422  void lock();
423  void unlock();
424 
425  void waitDOFHandling();
426  void postDOFHandling();
427  void fillDOFInfo(yarp::os::Bottle &reply);
428  void send(const yarp::sig::Vector &xd, const yarp::sig::Vector &x, const yarp::sig::Vector &q, double *tok);
429  void printInfo(const std::string &typ, const yarp::sig::Vector &xd, const yarp::sig::Vector &x,
430  const yarp::sig::Vector &q, const double t);
431 
432  virtual void prepareJointsRestTask();
433  virtual void respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply);
434  virtual bool threadInit();
435  virtual void afterStart(bool);
436  virtual void run();
437  virtual void threadRelease();
438 
439  friend class RpcProcessor;
440  friend class InputPort;
441  friend class SolverCallback;
442 
443 public:
458  CartesianSolver(const std::string &_slvName);
459 
530  virtual bool open(yarp::os::Searchable &options);
531 
536  virtual void interrupt();
537 
541  virtual void close();
542 
548  virtual bool isClosed() const { return closed; }
549 
558  virtual bool &getTimeoutFlag() { return timeout_detected; }
559 
563  virtual void suspend();
564 
568  virtual void resume();
569 
573  virtual ~CartesianSolver();
574 };
575 
576 
584 {
585 protected:
586  virtual PartDescriptor *getPartDesc(yarp::os::Searchable &options);
587  virtual bool decodeDOF(const yarp::sig::Vector &_dof);
588 
589 public:
597  iCubArmCartesianSolver(const std::string &_slvName="armCartSolver") : CartesianSolver(_slvName) { }
598 
599  virtual bool open(yarp::os::Searchable &options);
600 };
601 
602 
610 {
611 protected:
612  virtual PartDescriptor *getPartDesc(yarp::os::Searchable &options);
613 
614 public:
622  iCubLegCartesianSolver(const std::string &_slvName="legCartSolver") : CartesianSolver(_slvName) { }
623 };
624 
625 }
626 
627 }
628 
629 #endif
630 
631 
632 
Helper class providing useful methods to deal with Cartesian Solver options.
Definition: iKinHlp.h:48
Abstract class defining the core of on-line solvers.
Definition: iKinSlv.h:345
yarp::sig::Vector idx_3rdTask
Definition: iKinSlv.h:395
std::deque< yarp::dev::IControlLimits * > lim
Definition: iKinSlv.h:349
yarp::os::BufferedPort< yarp::os::Bottle > * outPort
Definition: iKinSlv.h:360
virtual yarp::sig::Vector & encodeDOF()
Definition: iKinSlv.cpp:1160
virtual bool handleJointsRestWeights(const yarp::os::Bottle *options, yarp::os::Bottle *reply=NULL)
Definition: iKinSlv.cpp:1226
bool setLimits(int axis, double min, double max)
Definition: iKinSlv.cpp:375
std::deque< int > jnt
Definition: iKinSlv.h:351
virtual bool open(yarp::os::Searchable &options)
Configure the solver and start it up.
Definition: iKinSlv.cpp:1270
std::condition_variable cv_dofEvent
Definition: iKinSlv.h:398
virtual PartDescriptor * getPartDesc(yarp::os::Searchable &options)=0
yarp::sig::Vector xd_2ndTask
Definition: iKinSlv.h:390
bool changeDOF(const yarp::sig::Vector &_dof)
Definition: iKinSlv.cpp:1458
virtual yarp::sig::Vector solve(yarp::sig::Vector &xd)
Definition: iKinSlv.cpp:1515
virtual void respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply)
Definition: iKinSlv.cpp:515
yarp::sig::Vector w_3rdTask
Definition: iKinSlv.h:394
RpcProcessor * cmdProcessor
Definition: iKinSlv.h:357
virtual void interrupt()
Interrupt the open() method waiting for motor parts to be ready.
Definition: iKinSlv.cpp:1525
yarp::sig::Vector restWeights
Definition: iKinSlv.h:388
virtual void resume()
Resume the solver's main loop.
Definition: iKinSlv.cpp:1630
iKinIpOptMin * slv
Definition: iKinSlv.h:354
CartesianSolver(const std::string &_slvName)
Constructor.
Definition: iKinSlv.cpp:267
std::deque< int * > rmp
Definition: iKinSlv.h:352
yarp::sig::Matrix swLimits
Definition: iKinSlv.h:382
std::deque< yarp::dev::IEncoders * > enc
Definition: iKinSlv.h:350
yarp::sig::Matrix hwLimits
Definition: iKinSlv.h:381
virtual void afterStart(bool)
Definition: iKinSlv.cpp:1607
virtual bool decodeDOF(const yarp::sig::Vector &_dof)
Definition: iKinSlv.cpp:1175
yarp::sig::Vector restJntPos
Definition: iKinSlv.h:387
virtual void threadRelease()
Definition: iKinSlv.cpp:1742
yarp::sig::Vector unctrlJointsOld
Definition: iKinSlv.h:384
SolverCallback * clb
Definition: iKinSlv.h:355
void printInfo(const std::string &typ, const yarp::sig::Vector &xd, const yarp::sig::Vector &x, const yarp::sig::Vector &q, const double t)
Definition: iKinSlv.cpp:1144
virtual bool handleJointsRestPosition(const yarp::os::Bottle *options, yarp::os::Bottle *reply=NULL)
Definition: iKinSlv.cpp:1195
void latchUncontrolledJoints(yarp::sig::Vector &joints)
Definition: iKinSlv.cpp:408
virtual void suspend()
Suspend the solver's main loop.
Definition: iKinSlv.cpp:1617
yarp::sig::Vector dof
Definition: iKinSlv.h:385
virtual ~CartesianSolver()
Default destructor.
Definition: iKinSlv.cpp:1749
virtual bool isClosed() const
To be called to check whether the solver has received a [quit] request.
Definition: iKinSlv.h:548
yarp::dev::PolyDriver * waitPart(const yarp::os::Property &partOpt)
Definition: iKinSlv.cpp:301
void getFeedback(const bool wait=false)
Definition: iKinSlv.cpp:425
virtual void close()
Stop the solver and dispose it.
Definition: iKinSlv.cpp:1532
virtual bool & getTimeoutFlag()
To be called to check whether communication timeout has been detected.
Definition: iKinSlv.h:558
void fillDOFInfo(yarp::os::Bottle &reply)
Definition: iKinSlv.cpp:507
yarp::sig::Vector w_2ndTask
Definition: iKinSlv.h:391
virtual void prepareJointsRestTask()
Definition: iKinSlv.cpp:1489
void send(const yarp::sig::Vector &xd, const yarp::sig::Vector &x, const yarp::sig::Vector &q, double *tok)
Definition: iKinSlv.cpp:1126
std::deque< yarp::dev::PolyDriver * > drv
Definition: iKinSlv.h:348
yarp::os::Port * rpcPort
Definition: iKinSlv.h:358
yarp::sig::Vector qd_3rdTask
Definition: iKinSlv.h:393
PartDescriptor * prt
Definition: iKinSlv.h:347
bool isNewDOF(const yarp::sig::Vector &_dof)
Definition: iKinSlv.cpp:1254
virtual bool threadInit()
Definition: iKinSlv.cpp:1595
yarp::sig::Vector get_xd()
Definition: iKinSlv.cpp:94
double * get_tokenPtr()
Definition: iKinSlv.h:301
bool handleTarget(yarp::os::Bottle *b)
Definition: iKinSlv.cpp:125
yarp::sig::Vector dof
Definition: iKinSlv.h:291
InputPort(CartesianSolver *_slv)
Definition: iKinSlv.cpp:60
CartesianSolver * slv
Definition: iKinSlv.h:279
bool handlePose(const int newPose)
Definition: iKinSlv.cpp:162
bool handleDOF(yarp::os::Bottle *b)
Definition: iKinSlv.cpp:141
yarp::sig::Vector get_dof()
Definition: iKinSlv.cpp:85
bool handleMode(const int newMode)
Definition: iKinSlv.cpp:186
std::mutex mtx
Definition: iKinSlv.h:281
void set_dof(const yarp::sig::Vector &_dof)
Definition: iKinSlv.cpp:77
virtual void onRead(yarp::os::Bottle &b)
Definition: iKinSlv.cpp:208
bool & get_contMode()
Definition: iKinSlv.h:300
yarp::sig::Vector xd
Definition: iKinSlv.h:292
void reset_xd(const yarp::sig::Vector &_xd)
Definition: iKinSlv.cpp:103
CartesianSolver * slv
Definition: iKinSlv.h:267
virtual bool read(yarp::os::ConnectionReader &connection)
RpcProcessor(CartesianSolver *_slv)
Definition: iKinSlv.h:272
CartesianSolver * slv
Definition: iKinSlv.h:318
virtual void exec(const yarp::sig::Vector &xd, const yarp::sig::Vector &q)
Defines the callback body to be called at each iteration.
Definition: iKinSlv.cpp:260
SolverCallback(CartesianSolver *_slv)
Definition: iKinSlv.h:321
Derived class which implements the on-line solver for the chain torso+arm.
Definition: iKinSlv.h:584
iCubArmCartesianSolver(const std::string &_slvName="armCartSolver")
Constructor.
Definition: iKinSlv.h:597
virtual PartDescriptor * getPartDesc(yarp::os::Searchable &options)
Definition: iKinSlv.cpp:1761
virtual bool decodeDOF(const yarp::sig::Vector &_dof)
Definition: iKinSlv.cpp:1831
virtual bool open(yarp::os::Searchable &options)
Configure the solver and start it up.
Definition: iKinSlv.cpp:1813
Derived class which implements the on-line solver for the leg chain.
Definition: iKinSlv.h:610
virtual PartDescriptor * getPartDesc(yarp::os::Searchable &options)
Definition: iKinSlv.cpp:1857
iCubLegCartesianSolver(const std::string &_slvName="legCartSolver")
Constructor.
Definition: iKinSlv.h:622
A Base class for defining a Serial Link Chain.
Definition: iKinFwd.h:354
Class for inverting chain's kinematics based on IpOpt lib.
Definition: iKinIpOpt.h:198
Class for defining iteration callback.
Definition: iKinIpOpt.h:43
A class for defining generic Limb.
Definition: iKinFwd.h:873
Class for defining Linear Inequality Constraints of the form lB <= C*q <= uB for the nonlinear proble...
Definition: iKinIpOpt.h:69
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.
const FSC max
Definition: strain.h:48
const FSC min
Definition: strain.h:49
iKinLinIneqConstr * cns
Definition: iKinSlv.h:331
std::deque< bool > rvs
Definition: iKinSlv.h:333
std::deque< yarp::os::Property > prp
Definition: iKinSlv.h:332