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