XBotInterface  2.4.1
XBotInterface provides a generic API to model and control a robot.
Joint.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 IIT-ADVR
3  * Author: Arturo Laurenzi, Luca Muratore
4  * email: arturo.laurenzi@iit.it, luca.muratore@iit.it
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>
18 */
19 
20 #ifndef __JOINT_H__
21 #define __JOINT_H__
22 
23 #include <string>
24 #include <memory>
25 #include <iostream>
26 #include <srdfdom_advr/model.h>
28 #include <bprinter/table_printer.h>
29 
30 
32 
33 
34 
35 namespace XBot
36 {
37 
38  class KinematicChain;
39  class RobotChain;
40  class RobotInterface;
41  class XBotInterface;
42 
47 class Joint
48 {
49 
50 
51 public:
52 
53  typedef std::shared_ptr<Joint> Ptr;
54  typedef std::shared_ptr<const Joint> ConstPtr;
55 
56  friend XBot::KinematicChain;
57  friend XBot::RobotChain;
58  friend XBot::XBotInterface;
59  friend XBot::RobotInterface;
60 
65  Joint();
66 
75  Joint(const std::string &joint_name,
76  int joint_id,
77  urdf::JointConstSharedPtr urdf_joint,
78  const std::string &chain_name);
79 
85  const std::string &getJointName() const;
86 
92  int getJointId() const;
93 
99  const std::string &getChainName() const;
100 
108  void getJointLimits(double& qmin, double& qmax) const;
109 
116  void getVelocityLimit(double& qdot_max) const;
117 
124  void getEffortLimit(double& tau_max) const;
125 
132  bool checkJointLimits(double q) const;
133 
140  bool checkVelocityLimit(double qdot) const;
141 
148  bool checkEffortLimit(double tau) const;
149 
150 
151  void enforceJointLimits(double& q) const;
152 
153  void enforceVelocityLimit(double& qdot) const;
154 
155  void enforceEffortLimit(double& tau) const;
156 
157 
158 
159 
165  urdf::JointConstSharedPtr getUrdfJoint() const;
166 
167  friend std::ostream& operator<< ( std::ostream& os, const XBot::Joint& j );
168 
174  void print() const;
175 
181  void printTracking() const;
182 
189  bool isFixedControlledJoint() const;
190 
191 
193  // RX VALUES //
195 
196 
203  void setJointPosition(double link_pos);
204 
211  void setMotorPosition(double motor_pos);
212 
219  void setJointVelocity(double link_vel);
220 
227  void setMotorVelocity(double motor_vel);
228 
235  void setJointAcceleration(double link_acc);
236 
243  void setJointEffort(double effort);
244 
251  void setTemperature(double temperature);
252 
253 
259  double getJointPosition() const;
260 
266  double getMotorPosition() const;
267 
273  double getJointVelocity() const;
274 
280  double getMotorVelocity() const;
281 
287  double getJointAcceleration() const;
288 
294  double getJointEffort() const;
295 
301  double getTemperature() const;
302 
303 
305  // TX VALUES //
307 
308 
315  void setPositionReference(double pos_ref);
316 
323  void setVelocityReference(double vel_ref);
324 
331  void setEffortReference(double effort_ref);
332 
339  void setStiffness(double stiffness);
340 
347  void setDamping(double damping);
348 
349 
355  double getPositionReference() const;
356 
362  double getVelocityReference() const;
363 
369  double getEffortReference() const;
370 
376  double getStiffness() const;
377 
383  double getDamping() const;
384 
386  // CONTROL MODE //
388 
389  void setControlMode(const ControlMode& control_mode);
390 
391  void getControlMode(ControlMode& control_mode) const;
392 
393 
394 protected:
395 
402  void setJointName(const std::string &joint_name);
403 
410  void setJointId(int joint_id);
411 
430  template <typename... SyncFlags>
431  bool syncFrom(const Joint &other, SyncFlags... flags);
432 
451  template <typename... SyncFlags>
452  bool setReferenceFrom(const Joint& other, SyncFlags... flags);
453 
460  const XBot::Joint& operator<< ( const XBot::Joint& from);
461 
467  bool isVirtualJoint() const;
468 
469 
470 
471 
472 private:
474  // Joint info //
476 
481  std::string _joint_name;
482 
487  int _joint_id;
488 
493  std::string _chain_name;
494 
495  // TBD implement getter and setter
496  std::string _joint_control_type;
497 
498 
499  urdf::JointConstSharedPtr _urdf_joint;
500 
501 
503  // RX from board //
505 
510  double _link_pos;
511 
516  double _motor_pos;
517 
522  double _link_vel;
523 
528  double _motor_vel;
529 
534  double _link_acc;
535 
540  double _effort;
541 
546  double _temperature;
547 
549  // TX to board //
551 
556  double _pos_ref;
557 
562  double _vel_ref;
563 
568  double _effort_ref;
569 
574  double _stiffness;
575 
580  double _damping;
581 
582  ControlMode _control_mode;
583 
589  void init();
590 
591 
592 
593 };
594 
602 std::ostream& operator<< ( std::ostream& os, const XBot::Joint& j );
603 
604 
605 
606 
607 template <typename... SyncFlags>
608 bool XBot::Joint::syncFrom(const XBot::Joint &other, SyncFlags... flags)
609 {
610 
611 
612  if(_joint_name != other._joint_name){
613  std::cerr << "ERROR in " << __func__ << "! Attempt to synchronize joints with different names!" << std::endl;
614  return false;
615  }
616 
617  bool sync_position = false,
618  sync_velocity = false,
619  sync_acceleration = false,
620  sync_effort = false,
621  sync_stiffness = false,
622  sync_damping = false,
623  sync_ft = false,
624  sync_imu = false,
625  motor_side = false;
626 
627  parseFlags(sync_position,
628  sync_velocity,
629  sync_acceleration,
630  sync_effort,
631  sync_stiffness,
632  sync_damping,
633  sync_ft,
634  sync_imu,
635  motor_side,
636  flags...);
637 
638 
639 
640 
641  if(sync_position){
642 
643  // RX
644  if(motor_side){
645  _link_pos = other._motor_pos;
646  }
647  else{
648  _link_pos = other._link_pos;
649  }
650 
651  _motor_pos = other._motor_pos;
652 
653  // TX
654  _pos_ref = other._pos_ref;
655  }
656 
657  if(sync_velocity){
658 
659  // RX
660  if(motor_side){
661  _link_vel = other._motor_vel;
662  }
663  else{
664  _link_vel = other._link_vel;
665  }
666 
667  _motor_vel = other._motor_vel;
668 
669  // TX
670  _vel_ref = other._vel_ref;
671  }
672 
673  if(sync_acceleration){
674 
675  _link_acc = other._link_acc;
676  }
677 
678  if(sync_effort){
679 
680  // RX
681  _effort = other._effort;
682 
683  // TX
684  _effort_ref = other._effort_ref;
685  }
686 
687  if(sync_stiffness){
688  _stiffness = other._stiffness;
689  }
690 
691  if(sync_damping){
692  _damping = other._damping;
693  }
694 
695 
697  // RX from board //
699 
700  _temperature = other._temperature;
701 
702  return true;
703 
704 }
705 
706 template <typename... SyncFlags>
707 bool XBot::Joint::setReferenceFrom(const XBot::Joint& other, SyncFlags... flags)
708 {
709  if(_joint_name != other._joint_name){
710  std::cerr << "ERROR in " << __func__ << "! Attempt to set reference from joint with different name! " << std::endl;
711  return false;
712  }
713 
714  bool sync_position = false,
715  sync_velocity = false,
716  sync_acceleration = false,
717  sync_effort = false,
718  sync_stiffness = false,
719  sync_damping = false,
720  sync_ft = false,
721  sync_imu = false,
722  sync_motor_side = false;
723 
724  parseFlags(sync_position,
725  sync_velocity,
726  sync_acceleration,
727  sync_effort,
728  sync_stiffness,
729  sync_damping,
730  sync_ft,
731  sync_imu,
732  sync_motor_side,
733  flags...);
734 
736  // TX to board //
738 
739  if(sync_position){
740  _pos_ref = other._link_pos;
741  }
742  if(sync_velocity){
743  _vel_ref = other._link_vel;
744  }
745  if(sync_effort){
746  _effort_ref = other._effort;
747  }
748  if(sync_stiffness){
749  _stiffness = other._stiffness;
750  }
751  if(sync_damping){
752  _damping = other._damping;
753  }
754 
755  return true;
756 }
757 
758 
759 
760 }
761 
762 #endif // __JOINT_H__
XBot::Joint::setVelocityReference
void setVelocityReference(double vel_ref)
setter for the velocity reference
Definition: Joint.cpp:208
table_printer.h
XBot::KinematicChain
Kinematic chain abstraction as a set of joints and sensors.
Definition: KinematicChain.h:49
XBot::Joint::setJointName
void setJointName(const std::string &joint_name)
Setter for the joint name.
Definition: Joint.cpp:96
XBot::operator<<
std::ostream & operator<<(std::ostream &os, const XBot::Joint &j)
ostream operator << for a Joint object
Definition: Joint.cpp:322
XBot::Joint::setJointAcceleration
void setJointAcceleration(double link_acc)
setter for the link side acceleration
Definition: Joint.cpp:162
XBot::RobotChain
Kinematic chain useful for a robot abstraction.
Definition: RobotChain.h:33
XBot::Joint::setReferenceFrom
bool setReferenceFrom(const Joint &other, SyncFlags... flags)
Set the joint references (TX) from other joint state (RX)
Definition: Joint.h:707
XBot::Joint::checkEffortLimit
bool checkEffortLimit(double tau) const
check that the param tau is below the joint effort limit
Definition: Joint.cpp:229
SyncFlags.h
XBot::Joint::setControlMode
void setControlMode(const ControlMode &control_mode)
Definition: Joint.cpp:389
XBot::Joint::print
void print() const
Prints a pretty table about joint state.
Definition: Joint.cpp:269
XBot::Joint::setMotorVelocity
void setMotorVelocity(double motor_vel)
setter for the motor side velocity
Definition: Joint.cpp:157
XBot::ControlMode
Definition: ControlMode.h:28
XBot::Joint::isVirtualJoint
bool isVirtualJoint() const
return true if the joint is virtual
Definition: Joint.cpp:74
XBot::Joint::getMotorVelocity
double getMotorVelocity() const
getter for the motor side velocity
Definition: Joint.cpp:121
XBot::Joint::getJointAcceleration
double getJointAcceleration() const
getter for the link side acceleration
Definition: Joint.cpp:126
XBot::Joint::Joint
Joint()
Default constructor.
Definition: Joint.cpp:22
XBot::Joint::setMotorPosition
void setMotorPosition(double motor_pos)
setter for the motor side encoder reading
Definition: Joint.cpp:147
XBot::Joint::enforceEffortLimit
void enforceEffortLimit(double &tau) const
Definition: Joint.cpp:345
XBot::Joint::getJointName
const std::string & getJointName() const
Getter for the joint name.
Definition: Joint.cpp:80
XBot::Joint::getUrdfJoint
urdf::JointConstSharedPtr getUrdfJoint() const
Getter for the urdf Joint object.
Definition: Joint.cpp:406
XBot::Joint::getControlMode
void getControlMode(ControlMode &control_mode) const
Definition: Joint.cpp:384
XBot::Joint::getVelocityReference
double getVelocityReference() const
getter for the commanded velocity reference
Definition: Joint.cpp:183
XBot::Joint::getDamping
double getDamping() const
get the commanded joint damping
Definition: Joint.cpp:198
XBot::Joint::checkVelocityLimit
bool checkVelocityLimit(double qdot) const
check that the param qdot is below the joint velocity limit
Definition: Joint.cpp:239
XBot::Joint::setJointId
void setJointId(int joint_id)
Setter for the joint id.
Definition: Joint.cpp:101
XBot::Joint::Ptr
std::shared_ptr< Joint > Ptr
Definition: Joint.h:53
XBot::Joint::enforceJointLimits
void enforceJointLimits(double &q) const
Definition: Joint.cpp:358
XBot::Joint::setDamping
void setDamping(double damping)
set the joint damping
Definition: Joint.cpp:223
XBot::Joint::setTemperature
void setTemperature(double temperature)
setter for the joint temperature
Definition: Joint.cpp:173
XBot::Joint::ConstPtr
std::shared_ptr< const Joint > ConstPtr
Definition: Joint.h:54
XBot::Joint::setJointVelocity
void setJointVelocity(double link_vel)
setter for the link side velocity
Definition: Joint.cpp:152
XBot::Joint::enforceVelocityLimit
void enforceVelocityLimit(double &qdot) const
Definition: Joint.cpp:371
XBot::Joint::checkJointLimits
bool checkJointLimits(double q) const
check that the param q is inside the position limits
Definition: Joint.cpp:234
XBot::Joint::setPositionReference
void setPositionReference(double pos_ref)
setter for the motor position reference
Definition: Joint.cpp:203
XBot::Joint::operator<<
friend std::ostream & operator<<(std::ostream &os, const XBot::Joint &j)
ostream operator << for a Joint object
XBot::Joint::getEffortLimit
void getEffortLimit(double &tau_max) const
Getter for the joint effort limit in the urdf.
Definition: Joint.cpp:245
XBot::Joint::getStiffness
double getStiffness() const
get the commanded joint stiffness
Definition: Joint.cpp:193
XBot::Joint::getTemperature
double getTemperature() const
getter for the joint temperature
Definition: Joint.cpp:136
XBot::Joint::getJointEffort
double getJointEffort() const
getter for the joint effort (generalized force)
Definition: Joint.cpp:131
XBot::Joint::getVelocityLimit
void getVelocityLimit(double &qdot_max) const
Getter for the joint velocity limit in the urdf.
Definition: Joint.cpp:256
XBot::Joint::getJointPosition
double getJointPosition() const
getter for the link side encoder reading
Definition: Joint.cpp:106
XBot::parseFlags
void parseFlags(bool &pos, bool &vel, bool &acc, bool &eff, bool &k, bool &d, bool &ft, bool &imu, bool &motor_side, Sync s=Sync::All)
Definition: SyncFlags.h:27
XBot::Joint::getPositionReference
double getPositionReference() const
getter for the commanded motor position reference
Definition: Joint.cpp:178
XBot::Joint::setJointPosition
void setJointPosition(double link_pos)
Setter for the link side encoder reading.
Definition: Joint.cpp:142
XBot::Joint::isFixedControlledJoint
bool isFixedControlledJoint() const
return true if the joint is specified fixed in the model, but inside the controlled_joints group of t...
Definition: Joint.cpp:394
XBot::Joint::getEffortReference
double getEffortReference() const
getter for the commanded effort (generalized force) reference
Definition: Joint.cpp:188
XBot::RobotInterface
Definition: RobotInterface.h:36
XBot::Joint::syncFrom
bool syncFrom(const Joint &other, SyncFlags... flags)
Synchronize the current Joint with another Joint object.
Definition: Joint.h:608
XBot::Joint::setStiffness
void setStiffness(double stiffness)
set the joint stiffness
Definition: Joint.cpp:218
XBot::Joint::getJointLimits
void getJointLimits(double &qmin, double &qmax) const
Getter for the joint position limits in the urdf.
Definition: Joint.cpp:250
XBot::Joint::getChainName
const std::string & getChainName() const
Getter for the parent chain name.
Definition: Joint.cpp:90
XBot::Joint::getJointId
int getJointId() const
Getter for the joint id.
Definition: Joint.cpp:85
XBot::Joint::setEffortReference
void setEffortReference(double effort_ref)
setter for the effort (generalized force) reference
Definition: Joint.cpp:213
XBot::Joint::getMotorPosition
double getMotorPosition() const
getter for the motor side encoder reading
Definition: Joint.cpp:111
XBot::Joint
Container for the Joint state and information.
Definition: Joint.h:47
XBot::Joint::getJointVelocity
double getJointVelocity() const
getter for the link side velocity
Definition: Joint.cpp:116
XBot::Joint::setJointEffort
void setJointEffort(double effort)
setter for the joint effort (generalized force)
Definition: Joint.cpp:168
XBot::Joint::printTracking
void printTracking() const
Prints a pretty table about joint tracking error.
Definition: Joint.cpp:291
XBot
Definition: IXBotModel.h:20
ControlMode.h
XBot::XBotInterface
Definition: XBotInterface.h:41