00001 #include <string> 00002 #include <vector> 00003 00004 #include <boost/thread/mutex.hpp> 00005 00006 #include <ros/ros.h> 00007 #include <ros/callback_queue.h> 00008 #include <ros/advertise_options.h> 00009 #include <ros/subscribe_options.h> 00010 00011 #include <geometry_msgs/Twist.h> 00012 #include <geometry_msgs/Pose.h> 00013 #include <geometry_msgs/Vector3.h> 00014 #include <geometry_msgs/WrenchStamped.h> 00015 #include <std_msgs/String.h> 00016 00017 #include <boost/thread.hpp> 00018 #include <boost/thread/condition.hpp> 00019 00020 #include <gazebo/math/Vector3.hh> 00021 #include <gazebo/physics/physics.hh> 00022 #include <gazebo/physics/PhysicsTypes.hh> 00023 #include <gazebo/transport/TransportTypes.hh> 00024 #include <gazebo/common/Time.hh> 00025 #include <gazebo/common/Plugin.hh> 00026 #include <gazebo/common/Events.hh> 00027 #include <gazebo/common/PID.hh> 00028 #include <gazebo/sensors/SensorManager.hh> 00029 #include <gazebo/sensors/SensorTypes.hh> 00030 #include <gazebo/sensors/ContactSensor.hh> 00031 #include <gazebo/sensors/ImuSensor.hh> 00032 #include <gazebo/sensors/Sensor.hh> 00033 00034 00035 #include <sensor_msgs/Imu.h> 00036 #include <sensor_msgs/JointState.h> 00037 00038 #include "hrpsys_gazebo_msgs/JointCommand.h" 00039 #include "hrpsys_gazebo_msgs/RobotState.h" 00040 00041 #include "PubQueue.h" 00042 00043 namespace gazebo 00044 { 00045 typedef boost::shared_ptr< sensors::ImuSensor > ImuSensorPtr; 00046 typedef hrpsys_gazebo_msgs::JointCommand JointCommand; 00047 typedef hrpsys_gazebo_msgs::RobotState RobotState; 00048 00049 class IOBPlugin : public ModelPlugin 00050 { 00051 public: 00052 IOBPlugin(); 00053 virtual ~IOBPlugin(); 00054 void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf); 00055 00056 private: 00057 void UpdateStates(); 00058 void RosQueueThread(); 00059 void DeferredLoad(); 00060 void GetAndPublishRobotStates(const common::Time &_curTime); 00061 void SetJointCommand(const JointCommand::ConstPtr &_msg); 00062 void LoadPIDGainsFromParameter(); 00063 void ZeroJointCommand(); 00064 void UpdatePIDControl(double _dt); 00065 00066 void GetIMUState(const common::Time &_curTime); 00067 void GetForceTorqueSensorState(const common::Time &_curTime); 00068 00069 struct force_sensor_info { 00070 physics::JointPtr joint; 00071 std::string joint_name; 00072 std::string frame_id; 00073 }; 00074 00075 struct imu_sensor_info { 00076 physics::LinkPtr link; 00077 ImuSensorPtr sensor; 00078 std::string sensor_name; 00079 std::string joint_name; 00080 std::string frame_id; 00081 }; 00082 ros::NodeHandle* rosNode; 00083 ros::CallbackQueue rosQueue; 00084 00085 physics::WorldPtr world; 00086 physics::ModelPtr model; 00087 sdf::ElementPtr sdf; 00088 00089 event::ConnectionPtr updateConnection; 00090 00091 boost::thread callbackQueeuThread; 00092 boost::thread deferredLoadThread; 00093 00094 common::Time lastControllerUpdateTime; 00095 00096 RobotState robotState; 00097 ros::Publisher pubRobotState; 00098 PubQueue<RobotState>::Ptr pubRobotStateQueue; 00099 00100 JointCommand jointCommand; 00101 ros::Subscriber subIOBCommand; 00102 00103 std::vector<std::string> jointNames; 00104 physics::Joint_V joints; 00105 00106 std::map< std::string, struct force_sensor_info > sensorJoints; 00107 std::map< std::string, struct imu_sensor_info > imuSensors; 00108 00109 std::vector<double> effortLimit; 00110 00111 class ErrorTerms { 00113 double q_p; 00114 double d_q_p_dt; 00115 double k_i_q_i; // integral term weighted by k_i 00116 double qd_p; 00117 friend class IOBPlugin; 00118 }; 00119 std::vector<ErrorTerms> errorTerms; 00120 00121 // 00122 PubMultiQueue pmq; 00123 boost::mutex mutex; 00124 // 00125 std::string robot_name; 00126 std::string controller_name; 00127 00128 #if 0 00129 physics::JointControllerPtr jointController; 00130 transport::NodePtr node; 00131 transport::PublisherPtr jointCmdPub; 00132 #endif 00133 00134 }; 00135 }