Go to the documentation of this file.00001
00027 #include <boost/thread.hpp>
00028 #include <nav_msgs/Odometry.h>
00029 #include <sensor_msgs/JointState.h>
00030 #include <geometry_msgs/Twist.h>
00031 #include <grizzly_plugin/grizzly_plugin.h>
00032 #include <ros/time.h>
00033
00034 using namespace gazebo;
00035
00036 enum {BL= 0, BR=1, FL=2, FR=3, FA=4};
00037
00038 GrizzlyPlugin::GrizzlyPlugin()
00039 {
00040 }
00041
00042 GrizzlyPlugin::~GrizzlyPlugin()
00043 {
00044 delete rosnode_;
00045 delete spinner_thread_;
00046 }
00047
00048 void GrizzlyPlugin::FiniChild()
00049 {
00050 rosnode_->shutdown();
00051 spinner_thread_->join();
00052 }
00053
00054 void GrizzlyPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf )
00055 {
00056 this->model_ = _parent;
00057 this->world_ = this->model_->GetWorld();
00058
00059 this->node_namespace_ = "";
00060 if (_sdf->HasElement("robotNamespace"))
00061 this->node_namespace_ = _sdf->GetElement("robotNamespace")->Get<std::string>() + "/";
00062
00063
00064 bl_joint_name_ = "backLeftJoint";
00065 if (_sdf->HasElement("backLeftJoint"))
00066 bl_joint_name_ = _sdf->GetElement("backLeftJoint")->Get<std::string>();
00067
00068 br_joint_name_ = "backRightJoint";
00069 if (_sdf->HasElement("backRightJoint"))
00070 br_joint_name_ = _sdf->GetElement("backRightJoint")->Get<std::string>();
00071
00072 fl_joint_name_ = "frontLeftJoint";
00073 if (_sdf->HasElement("frontLeftJoint"))
00074 fl_joint_name_ = _sdf->GetElement("frontLeftJoint")->Get<std::string>();
00075
00076 fr_joint_name_ = "frontRightJoint";
00077 if (_sdf->HasElement("frontRightJoint"))
00078 fr_joint_name_ = _sdf->GetElement("frontRightJoint")->Get<std::string>();
00079
00080 fa_joint_name_ = "frontAxleJoint";
00081 if (_sdf->HasElement("frontAxleJoint"))
00082 fa_joint_name_ = _sdf->GetElement("frontAxleJoint")->Get<std::string>();
00083
00084 torque_ = 15.0;
00085 if (_sdf->HasElement("torque"))
00086 torque_ = _sdf->GetElement("torque")->Get<double>();
00087
00088 base_geom_name_ = "base_link";
00089 if (_sdf->HasElement("baseGeom"))
00090 base_geom_name_ = _sdf->GetElement("baseGeom")->Get<std::string>();
00091 base_geom_ = model_->GetChildCollision(base_geom_name_);
00092
00093
00094
00095
00096
00097 std::string modelName = _sdf->GetParent()->Get<std::string>("name");
00098 gzdbg << "plugin model name: " << modelName << "\n";
00099
00100 js_.name.push_back( bl_joint_name_ );
00101 js_.position.push_back(0);
00102 js_.velocity.push_back(0);
00103 js_.effort.push_back(0);
00104
00105 js_.name.push_back( br_joint_name_ );
00106 js_.position.push_back(0);
00107 js_.velocity.push_back(0);
00108 js_.effort.push_back(0);
00109
00110 js_.name.push_back( fl_joint_name_ );
00111 js_.position.push_back(0);
00112 js_.velocity.push_back(0);
00113 js_.effort.push_back(0);
00114
00115 js_.name.push_back( fr_joint_name_ );
00116 js_.position.push_back(0);
00117 js_.velocity.push_back(0);
00118 js_.effort.push_back(0);
00119
00120 js_.name.push_back( fa_joint_name_ );
00121 js_.position.push_back(0);
00122 js_.velocity.push_back(0);
00123 js_.effort.push_back(0);
00124
00125 prev_update_time_ = 0;
00126 last_cmd_vel_time_ = 0;
00127
00128 wheel_ang_vel_.rear_left = 0.0;
00129 wheel_ang_vel_.rear_right= 0.0;
00130 wheel_ang_vel_.front_left = 0.0;
00131 wheel_ang_vel_.front_left = 0.0;
00132
00133 set_joints_[0] = false;
00134 set_joints_[1] = false;
00135 set_joints_[2] = false;
00136 set_joints_[3] = false;
00137 set_joints_[4] = false;
00138
00139
00140 joints_[BL] = model_->GetJoint(bl_joint_name_);
00141 joints_[BR] = model_->GetJoint(br_joint_name_);
00142 joints_[FL] = model_->GetJoint(fl_joint_name_);
00143 joints_[FR] = model_->GetJoint(fr_joint_name_);
00144 joints_[FA] = model_->GetJoint(fa_joint_name_);
00145
00146 if (joints_[BL]) set_joints_[BL] = true;
00147 if (joints_[BR]) set_joints_[BR] = true;
00148 if (joints_[FL]) set_joints_[FL] = true;
00149 if (joints_[FR]) set_joints_[FR] = true;
00150 if (joints_[FA]) set_joints_[FA] = true;
00151
00152
00153 prev_update_time_ = last_cmd_vel_time_ = this->world_->GetSimTime();
00154
00155
00156 int argc = 0;
00157 char** argv = NULL;
00158 ros::init(argc, argv, "gazebo_grizzly", ros::init_options::NoSigintHandler|ros::init_options::AnonymousName);
00159 rosnode_ = new ros::NodeHandle( node_namespace_ );
00160
00161 drive_sub_ = rosnode_->subscribe("cmd_drive", 1, &GrizzlyPlugin::OnDrive, this );
00162 encoder_pub_ = rosnode_->advertise<grizzly_msgs::Drive>("motors/encoders", 1);
00163 joint_state_pub_ = rosnode_->advertise<sensor_msgs::JointState>("joint_states", 1);
00164
00165
00166
00167 this->spinner_thread_ = new boost::thread( boost::bind( &GrizzlyPlugin::spin, this) );
00168 this->updateConnection = event::Events::ConnectWorldUpdateBegin(boost::bind(&GrizzlyPlugin::UpdateChild, this));
00169 }
00170
00171
00172 void GrizzlyPlugin::UpdateChild()
00173 {
00174 common::Time time_now = this->world_->GetSimTime();
00175 common::Time step_time = time_now - prev_update_time_;
00176 prev_update_time_ = time_now;
00177 grizzly_msgs::Drive encoder_msg;
00178
00179
00180 js_.header.stamp.sec = time_now.sec;
00181 js_.header.stamp.nsec = time_now.nsec;
00182 if (set_joints_[BL])
00183 {
00184 joints_[BL]->SetVelocity( 0, wheel_ang_vel_.rear_left);
00185 joints_[BL]->SetMaxForce( 0, torque_ );
00186 js_.position[0] = joints_[BL]->GetAngle(0).Radian();
00187 js_.velocity[0] = encoder_msg.rear_left = joints_[BL]->GetVelocity(0);
00188 }
00189 if (set_joints_[BR])
00190 {
00191 joints_[BR]->SetVelocity( 0, wheel_ang_vel_.rear_right);
00192 joints_[BR]->SetMaxForce( 0, torque_ );
00193 js_.position[1] = joints_[BR]->GetAngle(0).Radian();
00194 js_.velocity[1] = encoder_msg.rear_right = joints_[BR]->GetVelocity(0);
00195 }
00196 if (set_joints_[FL])
00197 {
00198 joints_[FL]->SetVelocity( 0, wheel_ang_vel_.front_left);
00199 joints_[FL]->SetMaxForce( 0, torque_ );
00200 js_.position[3] = joints_[FR]->GetAngle(0).Radian();
00201 js_.velocity[3] = encoder_msg.front_right = joints_[FR]->GetVelocity(0);
00202 }
00203 if (set_joints_[FR])
00204 {
00205 joints_[FR]->SetVelocity( 0, wheel_ang_vel_.front_right);
00206 joints_[FR]->SetMaxForce( 0, torque_ );
00207 js_.position[2] = joints_[FL]->GetAngle(0).Radian();
00208 js_.velocity[2] = encoder_msg.front_left = joints_[FL]->GetVelocity(0);
00209 }
00210
00211 if (set_joints_[FA])
00212 {
00213 js_.position[4] = joints_[FA]->GetAngle(0).Radian();
00214 js_.velocity[4] = joints_[FA]->GetVelocity(0);
00215 }
00216
00217
00218 joint_state_pub_.publish( js_ );
00219
00220
00221 encoder_msg.header.stamp.sec = time_now.sec;
00222 encoder_msg.header.stamp.nsec = time_now.nsec;
00223 encoder_pub_.publish( encoder_msg );
00224
00225
00226 common::Time time_since_last_cmd = time_now - last_cmd_vel_time_;
00227 if (time_since_last_cmd.Double() > 0.1)
00228 {
00229 wheel_ang_vel_.rear_left = 0;
00230 wheel_ang_vel_.rear_right = 0;
00231 wheel_ang_vel_.front_left = 0;
00232 wheel_ang_vel_.front_right = 0;
00233 }
00234 }
00235
00236
00237 void GrizzlyPlugin::OnDrive( const grizzly_msgs::DriveConstPtr &msg)
00238 {
00239 last_cmd_vel_time_ = this->world_->GetSimTime();
00240 wheel_ang_vel_ = *msg;
00241 }
00242
00243 void GrizzlyPlugin::spin()
00244 {
00245 while(ros::ok()) ros::spinOnce();
00246 }
00247
00248 GZ_REGISTER_MODEL_PLUGIN(GrizzlyPlugin);