wbc
RobotModel.hpp
Go to the documentation of this file.
1#ifndef ROBOTMODEL_HPP
2#define ROBOTMODEL_HPP
3
4#include <memory>
5#include <base/Eigen.hpp>
6#include <base/samples/RigidBodyStateSE3.hpp>
7#include <base/samples/Joints.hpp>
8#include <base/Acceleration.hpp>
9#include <base/JointLimits.hpp>
10#include <base/samples/Wrenches.hpp>
11#include <base/commands/Joints.hpp>
12#include "RobotModelConfig.hpp"
13#include <urdf_world/world.h>
14
15namespace wbc{
16
17std::vector<std::string> operator+(std::vector<std::string> a, std::vector<std::string> b);
18
23protected:
24 void clear();
25
27 const std::string chainID(const std::string& root, const std::string& tip){return root + "_" + tip;}
28
30 base::Vector3d gravity;
31 base::samples::RigidBodyStateSE3 floating_base_state;
32 base::samples::Wrenches contact_wrenches;
34 std::string world_frame, base_frame;
35 base::JointLimits joint_limits;
36 std::vector<std::string> actuated_joint_names;
37 std::vector<std::string> independent_joint_names;
38 std::vector<std::string> joint_names;
39 std::vector<std::string> joint_names_floating_base;
40 urdf::ModelInterfaceSharedPtr robot_urdf;
42 base::samples::Joints joint_state;
44 base::MatrixXd com_jac;
45 base::VectorXd bias_forces;
46 base::Acceleration spatial_acc_bias;
47 base::MatrixXd selection_matrix;
48 base::samples::RigidBodyStateSE3 com_rbs;
49 base::samples::RigidBodyStateSE3 rbs;
50
51 typedef std::map<std::string, base::MatrixXd > JacobianMap;
55
56 // Helper
57 base::samples::Joints joint_state_out;
58
59public:
60 RobotModel();
61 virtual ~RobotModel(){}
62
68 virtual bool configure(const RobotModelConfig& cfg) = 0;
69
75 virtual void update(const base::samples::Joints& joint_state,
76 const base::samples::RigidBodyStateSE3& floating_base_state = base::samples::RigidBodyStateSE3()) = 0;
77
79 const base::samples::Joints& jointState(const std::vector<std::string> &joint_names);
80
82 virtual void systemState(base::VectorXd &q, base::VectorXd &qd, base::VectorXd &qdd) = 0;
83
85 virtual const base::samples::RigidBodyStateSE3 &rigidBodyState(const std::string &root_frame, const std::string &tip_frame) = 0;
86
93 virtual const base::MatrixXd &spaceJacobian(const std::string &root_frame, const std::string &tip_frame) = 0;
94
101 virtual const base::MatrixXd &bodyJacobian(const std::string &root_frame, const std::string &tip_frame) = 0;
102
108 virtual const base::MatrixXd &comJacobian() = 0;
109
115 virtual const base::Acceleration &spatialAccelerationBias(const std::string &root_frame, const std::string &tip_frame) = 0;
116
124 virtual const base::MatrixXd &jacobianDot(const std::string &root_frame, const std::string &tip_frame) = 0;
125
127 virtual const base::MatrixXd &jointSpaceInertiaMatrix() = 0;
128
130 virtual const base::VectorXd &biasForces() = 0;
131
133 const std::vector<std::string>& jointNames(){return joint_names;}
134
136 const std::vector<std::string>& actuatedJointNames(){return actuated_joint_names;}
137
139 const std::vector<std::string>& independentJointNames(){return independent_joint_names;}
140
142 uint jointIndex(const std::string &joint_name);
143
145 const std::string& baseFrame(){return base_frame;}
146
148 const std::string& worldFrame(){return world_frame;}
149
151 const base::JointLimits& jointLimits(){return joint_limits;}
152
154 bool hasLink(const std::string& link_name);
155
157 bool hasJoint(const std::string& joint_name);
158
160 bool hasActuatedJoint(const std::string& joint_name);
161
165 const base::MatrixXd& selectionMatrix(){return selection_matrix;}
166
168 virtual const base::samples::RigidBodyStateSE3& centerOfMass() = 0;
169
171 void setActiveContacts(const ActiveContacts &contacts);
172
175
177 uint noOfJoints(){return jointNames().size();}
178
180 uint noOfActuatedJoints(){return actuatedJointNames().size();}
181
183 void setGravityVector(const base::Vector3d& g){gravity=g;}
184
186 const base::samples::RigidBodyStateSE3& floatingBaseState(){return floating_base_state;}
187
189 void setContactWrenches(const base::samples::Wrenches& wrenches){contact_wrenches = wrenches;}
190
192 virtual void computeInverseDynamics(base::commands::Joints &solver_output) = 0;
193
196
199
201 urdf::ModelInterfaceSharedPtr loadRobotURDF(const std::string& file_or_string);
202
203};
204typedef std::shared_ptr<RobotModel> RobotModelPtr;
205
206template<typename T> RobotModel* createT(){return new T;}
207
209 typedef std::map<std::string, RobotModel*(*)()> RobotModelMap;
210
211 static RobotModel *createInstance(const std::string& name) {
212 RobotModelMap::iterator it = getRobotModelMap()->find(name);
213 if(it == getRobotModelMap()->end())
214 throw std::runtime_error("Failed to create instance of plugin " + name + ". Is the plugin registered?");
215 return it->second();
216 }
217
218 template<typename T>
219 static T* createInstance(const std::string& name){
220 RobotModel* tmp = createInstance(name);
221 T* ret = dynamic_cast<T*>(tmp);
222 return ret;
223 }
224
226 if(!robot_model_map)
227 robot_model_map = new RobotModelMap;
228 return robot_model_map;
229 }
230
231 static void clear(){
232 robot_model_map->clear();
233 }
234private:
235 static RobotModelMap *robot_model_map;
236};
237
238template<typename T>
240 RobotModelRegistry(const std::string& name) {
241 RobotModelMap::iterator it = getRobotModelMap()->find(name);
242 if(it != getRobotModelMap()->end())
243 throw std::runtime_error("Failed to register plugin with name " + name + ". A plugin with the same name is already registered");
244 getRobotModelMap()->insert(std::make_pair(name, &createT<T>));
245 }
246};
247
248}
249
250#endif // ROBOTMODEL_HPP
Definition RobotModelConfig.hpp:26
Interface for all robot models. This has to provide all kinematics and dynamics information that is r...
Definition RobotModel.hpp:22
virtual const base::samples::RigidBodyStateSE3 & rigidBodyState(const std::string &root_frame, const std::string &tip_frame)=0
const RobotModelConfig & getRobotModelConfig()
Get current robot model config.
Definition RobotModel.hpp:195
bool has_floating_base
Definition RobotModel.hpp:41
bool hasActuatedJoint(const std::string &joint_name)
Return True if given joint name is an actuated joint in robot model, false otherwise.
Definition RobotModel.cpp:67
std::string world_frame
Definition RobotModel.hpp:34
void setActiveContacts(const ActiveContacts &contacts)
Provide information about which link is currently in contact with the environment.
Definition RobotModel.cpp:37
virtual ~RobotModel()
Definition RobotModel.hpp:61
virtual const base::VectorXd & biasForces()=0
Compute and return the bias force vector, which is nj x 1, where nj is the number of joints of the sy...
base::MatrixXd joint_space_inertia_mat
Definition RobotModel.hpp:43
RobotModelConfig robot_model_config
Definition RobotModel.hpp:33
std::map< std::string, base::MatrixXd > JacobianMap
Definition RobotModel.hpp:51
std::vector< std::string > actuated_joint_names
Definition RobotModel.hpp:36
base::samples::RigidBodyStateSE3 com_rbs
Definition RobotModel.hpp:48
std::vector< std::string > independent_joint_names
Definition RobotModel.hpp:37
virtual void computeInverseDynamics(base::commands::Joints &solver_output)=0
Compute and return the inverse dynamics solution.
uint noOfActuatedJoints()
Return number of actuated/active joints.
Definition RobotModel.hpp:180
bool hasFloatingBase()
Is floating base robot?
Definition RobotModel.hpp:198
urdf::ModelInterfaceSharedPtr loadRobotURDF(const std::string &file_or_string)
Load URDF model from either file or string.
Definition RobotModel.cpp:94
const base::MatrixXd & selectionMatrix()
Return current selection matrix S that maps complete joint vector to actuated joint vector: q_a = S *...
Definition RobotModel.hpp:165
const std::vector< std::string > & independentJointNames()
Return only independent joint names.
Definition RobotModel.hpp:139
virtual const base::Acceleration & spatialAccelerationBias(const std::string &root_frame, const std::string &tip_frame)=0
Returns the spatial acceleration bias, i.e. the term Jdot*qdot.
const ActiveContacts & getActiveContacts()
Provide links names that are possibly in contact with the environment (typically the end effector lin...
Definition RobotModel.hpp:174
ActiveContacts active_contacts
Definition RobotModel.hpp:29
JacobianMap jac_dot_map
Definition RobotModel.hpp:54
const base::JointLimits & jointLimits()
Return current joint limits.
Definition RobotModel.hpp:151
base::samples::RigidBodyStateSE3 rbs
Definition RobotModel.hpp:49
const base::samples::RigidBodyStateSE3 & floatingBaseState()
Get current status of floating base.
Definition RobotModel.hpp:186
const std::vector< std::string > & jointNames()
Return all joint names.
Definition RobotModel.hpp:133
const std::string & worldFrame()
Get the world frame id.
Definition RobotModel.hpp:148
virtual const base::MatrixXd & spaceJacobian(const std::string &root_frame, const std::string &tip_frame)=0
Returns the Space Jacobian for the kinematic chain between root and the tip frame as full body Jacobi...
virtual const base::MatrixXd & comJacobian()=0
Returns the CoM Jacobian for the entire robot, which maps the robot joint velocities to linear spatia...
const std::string & baseFrame()
Get the base frame of the robot.
Definition RobotModel.hpp:145
RobotModel()
Definition RobotModel.cpp:15
virtual const base::samples::RigidBodyStateSE3 & centerOfMass()=0
Compute and return center of mass expressed in base frame.
JacobianMap space_jac_map
Definition RobotModel.hpp:52
base::Acceleration spatial_acc_bias
Definition RobotModel.hpp:46
base::MatrixXd selection_matrix
Definition RobotModel.hpp:47
base::MatrixXd com_jac
Definition RobotModel.hpp:44
virtual const base::MatrixXd & jointSpaceInertiaMatrix()=0
Compute and return the joint space mass-inertia matrix, which is nj x nj, where nj is the number of j...
std::string base_frame
Definition RobotModel.hpp:34
base::samples::RigidBodyStateSE3 floating_base_state
Definition RobotModel.hpp:31
virtual void update(const base::samples::Joints &joint_state, const base::samples::RigidBodyStateSE3 &floating_base_state=base::samples::RigidBodyStateSE3())=0
Update the robot configuration.
JacobianMap body_jac_map
Definition RobotModel.hpp:53
bool hasJoint(const std::string &joint_name)
Return True if given joint name is available in robot model, false otherwise.
Definition RobotModel.cpp:63
std::vector< std::string > joint_names_floating_base
Definition RobotModel.hpp:39
virtual bool configure(const RobotModelConfig &cfg)=0
Load and configure the robot model.
base::JointLimits joint_limits
Definition RobotModel.hpp:35
uint jointIndex(const std::string &joint_name)
Get index of joint name.
Definition RobotModel.cpp:48
base::VectorXd bias_forces
Definition RobotModel.hpp:45
void setContactWrenches(const base::samples::Wrenches &wrenches)
Set contact wrenches, names have to consistent with the configured contact points.
Definition RobotModel.hpp:189
urdf::ModelInterfaceSharedPtr robot_urdf
Definition RobotModel.hpp:40
const base::samples::Joints & jointState(const std::vector< std::string > &joint_names)
Definition RobotModel.cpp:71
base::samples::Wrenches contact_wrenches
Definition RobotModel.hpp:32
virtual const base::MatrixXd & jacobianDot(const std::string &root_frame, const std::string &tip_frame)=0
Returns the derivative of the Jacobian for the kinematic chain between root and the tip frame as full...
void setGravityVector(const base::Vector3d &g)
Set the current gravity vector.
Definition RobotModel.hpp:183
const std::vector< std::string > & actuatedJointNames()
Return only actuated joint names.
Definition RobotModel.hpp:136
virtual const base::MatrixXd & bodyJacobian(const std::string &root_frame, const std::string &tip_frame)=0
Returns the Body Jacobian for the kinematic chain between root and the tip frame as full body Jacobia...
bool hasLink(const std::string &link_name)
Return True if given link name is available in robot model, false otherwise.
Definition RobotModel.cpp:56
std::vector< std::string > joint_names
Definition RobotModel.hpp:38
base::samples::Joints joint_state
Definition RobotModel.hpp:42
base::Vector3d gravity
Definition RobotModel.hpp:30
virtual void systemState(base::VectorXd &q, base::VectorXd &qd, base::VectorXd &qdd)=0
const std::string chainID(const std::string &root, const std::string &tip)
Definition RobotModel.hpp:27
uint noOfJoints()
Return number of joints.
Definition RobotModel.hpp:177
base::samples::Joints joint_state_out
Definition RobotModel.hpp:57
void clear()
Definition RobotModel.cpp:19
Definition ContactsAccelerationConstraint.cpp:3
QPSolver * createT()
Definition QPSolver.hpp:33
std::shared_ptr< RobotModel > RobotModelPtr
Definition RobotModel.hpp:204
std::vector< std::string > operator+(std::vector< std::string > a, std::vector< std::string > b)
Definition RobotModel.cpp:10
Robot Model configuration class.
Definition RobotModelConfig.hpp:40
Definition RobotModel.hpp:208
static T * createInstance(const std::string &name)
Definition RobotModel.hpp:219
static RobotModel * createInstance(const std::string &name)
Definition RobotModel.hpp:211
std::map< std::string, RobotModel *(*)()> RobotModelMap
Definition RobotModel.hpp:209
static RobotModelMap * getRobotModelMap()
Definition RobotModel.hpp:225
static void clear()
Definition RobotModel.hpp:231
Definition RobotModel.hpp:239
RobotModelRegistry(const std::string &name)
Definition RobotModel.hpp:240