19 #ifndef GAZEBO_TB3_TURTLEBOT3_PLUGIN_CC_ 20 #define GAZEBO_TB3_TURTLEBOT3_PLUGIN_CC_ 25 #include "gazebo/gazebo.hh" 26 #include "gazebo/physics/physics.hh" 27 #include "gazebo/sensors/sensors.hh" 30 #include <boost/bind.hpp> 33 #define LEFT_WHEEL_JOINT 0 34 #define RIGHT_WHEEL_JOINT 1 36 #define LIDAR_SENSOR 1 37 #define CAMERA_SENSOR 2 39 #define ESC_ASCII_VALUE 0x1b 45 static struct termios oldt, newt;
46 tcgetattr( STDIN_FILENO, &oldt);
48 newt.c_lflag &= ~(ICANON);
49 tcsetattr( STDIN_FILENO, TCSANOW, &newt);
53 tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
59 struct termios oldt, newt;
63 tcgetattr(STDIN_FILENO, &oldt);
65 newt.c_lflag &= ~(ICANON | ECHO);
66 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
67 oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
68 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
72 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
73 fcntl(STDIN_FILENO, F_SETFL, oldf);
103 virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
107 if (isTurtlebot3Model(_sdf) !=
true)
118 updateConnection_ = event::Events::ConnectWorldUpdateBegin(boost::bind(&
Turtlebot3::OnUpdate,
this, _1));
122 void getModel(physics::ModelPtr model){ model_ = model; }
130 std::cout <<
"Find Joint : " << left_wheel_joint_->GetName() << std::endl;
131 std::cout <<
"Find Joint : " << right_wheel_joint_->GetName() << std::endl;
137 physics::LinkPtr lidar_link = model->GetLinks()[
LIDAR_SENSOR];
138 std::cerr <<
"Find Sensor : " << lidar_link->GetScopedName() << std::endl;
140 if (model_->GetName() !=
"burger")
142 physics::LinkPtr camera_link = model->GetLinks()[
CAMERA_SENSOR];
143 std::cerr <<
"Find Sensor : " << camera_link->GetScopedName() << std::endl;
150 std::string get_tb3_model;
152 if (sdf->HasElement(
"tb3_model"))
154 get_tb3_model = sdf->Get<std::string>(
"tb3_model");
156 if (get_tb3_model ==
"burger")
158 wheel_separation_ = 0.160;
160 else if (get_tb3_model ==
"waffle" || get_tb3_model ==
"waffle_pi")
162 wheel_separation_ = 0.287;
166 std::cerr <<
"Invalid model name, TB3 plugin is not loaded" << std::endl;
172 std::cerr <<
"Please put a tb3 model(burger, waffle or waffle_pi) in turtlebot3_.world file, TB3 plugin is not loaded" << std::endl;
176 std::cout <<
"The turtlebot3 plugin is attach to model : " << model_->GetName() <<
"/turtlebot3_" << get_tb3_model << std::endl;
184 pid_ = common::PID(p, i, d);
186 model_->GetJointController()->SetVelocityPID(left_wheel_joint_->GetScopedName(), pid_);
187 model_->GetJointController()->SetVelocityPID(right_wheel_joint_->GetScopedName(), pid_);
193 writePIDparamForJointControl(1.0, 0, 0);
195 writeVelocityToJoint(0.0, 0.0);
201 std::cout <<
" " <<std::endl;
202 std::cout <<
"Control Your TurtleBot3!" << std::endl;
203 std::cout <<
"---------------------------" << std::endl;
204 std::cout <<
"w - set linear velocity up" << std::endl;
205 std::cout <<
"x - set linear velocity down" << std::endl;
206 std::cout <<
"d - set angular velocity up" << std::endl;
207 std::cout <<
"a - set angular velocity down" << std::endl;
208 std::cout <<
"s - set all velocity to zero" << std::endl;
212 void controlTB3(
const float wheel_separation,
double lin_vel,
double ang_vel)
215 double _lin_vel = lin_vel;
216 double _ang_vel = ang_vel;
218 double right_wheel_vel = 0.0;
219 double left_wheel_vel = 0.0;
221 left_wheel_vel = _lin_vel - (_ang_vel * wheel_separation / 2);
222 right_wheel_vel = _lin_vel + (_ang_vel * wheel_separation / 2);
224 writeVelocityToJoint(right_wheel_vel, left_wheel_vel);
230 model_->GetJointController()->SetVelocityTarget(left_wheel_joint_->GetScopedName(), left_wheel_vel);
231 model_->GetJointController()->SetVelocityTarget(right_wheel_joint_->GetScopedName(), right_wheel_vel);
237 return left_wheel_joint_->GetAngle(0).Radian();
243 return right_wheel_joint_->GetAngle(0).Radian();
249 static uint8_t hit_cnt = 0;
250 static double lin_vel_cmd = 0;
251 static double ang_vel_cmd = 0;
255 std::cout <<
" is pressed" << std::endl;
285 lin_vel_cmd = lin_vel_cmd;
286 ang_vel_cmd = ang_vel_cmd;
289 controlTB3(wheel_separation_, lin_vel_cmd, ang_vel_cmd);
296 #endif //GAZEBO_TB3_TURTLEBOT3_PLUGIN_CC_
#define RIGHT_WHEEL_JOINT
bool isTurtlebot3Model(sdf::ElementPtr sdf)
void controlTB3(const float wheel_separation, double lin_vel, double ang_vel)
void writePIDparamForJointControl(double p, double i, double d)
void writeVelocityToJoint(double right_wheel_vel, double left_wheel_vel)
double getLeftJointPosition()
void OnUpdate(const common::UpdateInfo &)
physics::JointPtr right_wheel_joint_
physics::JointPtr left_wheel_joint_
void getJoints(physics::ModelPtr model)
virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
void getSensors(physics::ModelPtr model)
event::ConnectionPtr updateConnection_
double getRightJointPosition()
void getModel(physics::ModelPtr model)