Tetrapod Project
tetrapod_plugin.h
Go to the documentation of this file.
1 /*******************************************************************/
2 /* AUTHOR: Paal Arthur S. Thorseth */
3 /* ORGN: Dept of Eng Cybernetics, NTNU Trondheim */
4 /* FILE: tetrapod_plugin.h */
5 /* DATE: Feb 4, 2021 */
6 /* */
7 /* Copyright (C) 2021 Paal Arthur S. Thorseth, */
8 /* Adrian B. Ghansah */
9 /* */
10 /* This program is free software: you can redistribute it */
11 /* and/or modify it under the terms of the GNU General */
12 /* Public License as published by the Free Software Foundation, */
13 /* either version 3 of the License, or (at your option) any */
14 /* later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty */
18 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
19 /* See the GNU General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU General Public */
22 /* License along with this program. If not, see */
23 /* <https://www.gnu.org/licenses/>. */
24 /* */
25 /*******************************************************************/
26 
27 #pragma once
28 
29 // Gazebo
30 #include <gazebo/gazebo.hh>
31 #include <gazebo/physics/physics.hh>
32 #include <gazebo/transport/transport.hh>
33 #include <gazebo/msgs/msgs.hh>
34 
35 // ROS
36 #include "ros/ros.h"
37 #include "ros/package.h"
38 #include "ros/console.h"
39 #include "ros/callback_queue.h"
40 #include "ros/subscribe_options.h"
41 #include "eigen_conversions/eigen_msg.h"
42 #include "std_msgs/Float32.h"
43 #include "std_msgs/Float64MultiArray.h"
44 #include "sensor_msgs/JointState.h"
45 #include "std_srvs/Empty.h"
46 
47 // ROS Package Libraries
49 
50 // Eigen
51 #include <Eigen/Core>
52 
53 // Standard library
54 #include <thread>
55 #include <vector>
56 #include <map>
57 
58 namespace gazebo
59 {
61  static constexpr unsigned int NUMJOINTS = 12;
62 
63  using JointVelocities = Eigen::Matrix<double, 12, 1>;
64 
65 
67  class TetrapodPlugin : public ModelPlugin
68  {
70  public: enum ControlMode { position = 1, velocity = 2, torque = 3 };
71 
73  public: TetrapodPlugin();
74 
76  public: virtual ~TetrapodPlugin();
77 
83  public: virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf);
84 
88  public: Eigen::Matrix<double, 6, 1> GetBasePose();
89 
94  public: Eigen::Matrix<double, 6, 1> GetBaseTwist();
95 
99  public: double GetJointForce(const std::string &_joint_name);
100 
103  public: Eigen::Matrix<double, 12, 1> GetJointForces();
104 
108  public: double GetJointVelocity(const std::string &_joint_name);
109 
112  public: Eigen::Matrix<double, 12, 1> GetJointVelocities();
113 
117  public: double GetJointPosition(const std::string &_joint_name);
118 
121  public: Eigen::Matrix<double, 12, 1> GetJointPositions();
122 
126  public: void SetJointForce(const std::string &_joint_name, const double &_force);
127 
130  public: void SetJointForces(const std::vector<double> &_forces);
131 
135  public: void SetJointVelocity(const std::string &_joint_name, const double &_vel);
136 
139  public: void SetJointVelocities(const std::vector<double> &_vel);
140 
144  public: void SetJointPosition(const std::string &_joint_name, const double &_pos);
145 
148  public: void SetJointPositions(const std::vector<double> &_pos);
149 
157  public: void OnJointStateMsg(const sensor_msgs::JointStateConstPtr &_msg);
158 
166  public: void OnFlJointStateMsg(const sensor_msgs::JointStateConstPtr &_msg);
167 
172  public: void OnForceMsg(const std_msgs::Float64MultiArrayConstPtr &_msg);
173 
178  public: void OnVelMsg(const std_msgs::Float64MultiArrayConstPtr &_msg);
179 
184  public: void OnPosMsg(const std_msgs::Float64MultiArrayConstPtr &_msg);
185 
188  public: void ProcessQueueThread();
189 
192  public: void PublishQueueThread();
193 
194  // TODO REMOVE
195  public: void printMap(const std::map<std::string,physics::JointPtr> &_map);
196 
198  protected: void InitRos();
199 
202  protected: void InitRosQueueThreads();
203 
207  protected: bool LoadParametersRos();
208 
211  protected: void InitJointControllers();
212 
215  protected: void InitJointConfiguration();
216 
222  public: bool ResetSimulation(const std_srvs::Empty::Request &_req,
223  std_srvs::Empty::Response &_res);
224 
226  private: physics::ModelPtr model;
227 
229  private: physics::WorldPtr world;
230 
232  private: std::string model_name;
233 
235  private: physics::LinkPtr base;
236 
238  //private: std::vector<physics::JointPtr> joints;
239  //private: physics::Joint_V joints;
240  private: std::map<std::string, physics::JointPtr> joints;
241 
243  private: std::vector<std::string> joint_names;
244 
246  private: std::vector<double> joint_config;
247 
249  private: std::vector<double> vel_p_gains;
250 
252  private: std::vector<double> vel_i_gains;
253 
255  private: std::vector<double> vel_d_gains;
256 
258  private: std::vector<double> pos_p_gains;
259 
261  private: std::vector<double> pos_i_gains;
262 
264  private: std::vector<double> pos_d_gains;
265 
268 
270  private: std::unique_ptr<ros::NodeHandle> rosNode;
271 
273  private: ros::ServiceServer resetSimService;
274 
276  private: ros::Publisher genCoordPub;
277 
279  private: ros::Publisher genVelPub;
280 
282  private: ros::Publisher basePosePub;
283 
285  private: ros::Publisher baseTwistPub;
286 
288  private: ros::Publisher jointForcesPub;
289 
291  private: ros::Subscriber jointStateCmdSub;
292 
294  private: ros::Publisher jointStatePub;
295 
297  private: ros::Subscriber forceSub;
298 
300  private: ros::Subscriber velSub;
301 
303  private: ros::Subscriber posSub;
304 
306  private: ros::CallbackQueue rosProcessQueue;
307 
309  private: ros::CallbackQueue rosPublishQueue;
310 
312  private: std::thread rosProcessQueueThread;
313 
315  private: std::thread rosPublishQueueThread;
316  };
317 
318  // Tell Gazebo about this plugin, so that Gazebo can call Load on this plugin.
319  GZ_REGISTER_MODEL_PLUGIN(TetrapodPlugin)
320 } // namespace gazebo
A plugin to control the tetrapod robot.
TetrapodPlugin()
Constructor.
std::vector< double > vel_p_gains
Vector of Velocity P-gains.
std::vector< double > pos_i_gains
Vector of Position I-gains.
void OnForceMsg(const std_msgs::Float64MultiArrayConstPtr &_msg)
The OnRosMsg function handles an incoming force message from ROS.
Eigen::Matrix< double, 12, 1 > GetJointVelocities()
Get current joint velocities for the robot.
Eigen::Matrix< double, 12, 1 > GetJointPositions()
Get current joint positions for the robot.
physics::LinkPtr base
Pointer to the body link.
void InitJointConfiguration()
The InitJointConfiguration function is called to initialize the desired joint configuration.
std::thread rosProcessQueueThread
Thread running the rosProcessQueue.
ros::Publisher genVelPub
ROS Generalized Velocities Publisher.
ros::Publisher basePosePub
ROS Base Pose Publisher.
ros::CallbackQueue rosProcessQueue
ROS callbackque that helps process messages.
std::vector< double > pos_d_gains
Vector of Position D-gains.
ros::Subscriber velSub
ROS Velocity Subscriber.
double GetJointForce(const std::string &_joint_name)
Get currently applied force at a specific joint.
ControlMode
Control mode enumerator.
double GetJointPosition(const std::string &_joint_name)
Get current position at a specific joint.
void OnFlJointStateMsg(const sensor_msgs::JointStateConstPtr &_msg)
The OnFlJointStateMsg function handles an incoming joint state message for the front left leg from RO...
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
The Load function is called by Gazebo when the plugin is inserted into simulation.
void SetJointVelocity(const std::string &_joint_name, const double &_vel)
Set the velocity of the joint.
Eigen::Matrix< double, 6, 1 > GetBasePose()
Get current base pose for the robot.
void PublishQueueThread()
The PublishQueueThread function is a ROS helper function that publish state messages.
void printMap(const std::map< std::string, physics::JointPtr > &_map)
void InitRosQueueThreads()
The InitRosQueueThreads function is called to initialize the ROS Publish and Process Queue Threads.
void ProcessQueueThread()
The ProcessQueueThread function is a ROS helper function that processes messages.
ros::Subscriber forceSub
ROS Force Subscriber.
physics::ModelPtr model
Pointer to the model.
ros::ServiceServer resetSimService
ROS Reset Simulation Service.
std::string model_name
Model name.
ControlMode controlMode
Control mode indicator.
bool LoadParametersRos()
The LoadParametersRos function is called to load conffloatiguration from the parameter server.
void OnJointStateMsg(const sensor_msgs::JointStateConstPtr &_msg)
The OnJointStateMsg function handles an incoming joint state message from ROS.
void OnVelMsg(const std_msgs::Float64MultiArrayConstPtr &_msg)
The OnRosMsg function handles an incoming velocity message from ROS.
void SetJointVelocities(const std::vector< double > &_vel)
Set the velocity of the joints.
void SetJointForce(const std::string &_joint_name, const double &_force)
Apply force at a specific joint.
std::vector< double > vel_i_gains
Vector of Velocity I-gains.
void SetJointPosition(const std::string &_joint_name, const double &_pos)
Set the position of the joint.
std::unique_ptr< ros::NodeHandle > rosNode
Node used for ROS transport.
ros::Subscriber jointStateCmdSub
ROS Joint State Subscriber.
double GetJointVelocity(const std::string &_joint_name)
Get current velocity at a specific joint.
Eigen::Matrix< double, 6, 1 > GetBaseTwist()
Get current base twist for the robot.
ros::Publisher baseTwistPub
ROS Base Twist Publisher.
ros::Publisher genCoordPub
ROS Generalized Coordinates Publisher.
virtual ~TetrapodPlugin()
Destructor.
ros::Subscriber posSub
ROS Position Subscriber.
std::vector< double > vel_d_gains
Vector of Velocity D-gains.
ros::Publisher jointStatePub
ROS Joint State Publisher.
std::vector< double > pos_p_gains
Vector of Position P-gains.
std::map< std::string, physics::JointPtr > joints
Pointers to the joints.
void SetJointPositions(const std::vector< double > &_pos)
Set the position of the joints.
void InitJointControllers()
The InitJointControllers function is called to setup joint PID controllers.
std::vector< std::string > joint_names
Vector of joint names.
bool ResetSimulation(const std_srvs::Empty::Request &_req, std_srvs::Empty::Response &_res)
The ResetSimulation function handles an incoming reset simulation service request.
void OnPosMsg(const std_msgs::Float64MultiArrayConstPtr &_msg)
The OnRosMsg function handles an incoming velocity message from ROS.
std::thread rosPublishQueueThread
Thread running the rosPublishQueue.
physics::WorldPtr world
Pointer to the world.
Eigen::Matrix< double, 12, 1 > GetJointForces()
Get current joint forces for the robot.
std::vector< double > joint_config
Vector of joint configuration.
void InitRos()
The initRos function is called to initialize ROS.
ros::CallbackQueue rosPublishQueue
ROS callbackque that helps publish messages.
void SetJointForces(const std::vector< double > &_forces)
Apply joint forces.
ros::Publisher jointForcesPub
ROS Joint Forces (Torques) Publisher.
Eigen::Matrix< double, 3, 1 > JointVelocities
static constexpr unsigned int NUMJOINTS
Fixed variable for num joints.