gazebo_ros_kobuki.cpp
Go to the documentation of this file.
1 
2 #include <cmath>
3 #include <cstring>
4 #include <boost/bind.hpp>
5 #include <sensor_msgs/JointState.h>
8 #if GAZEBO_MAJOR_VERSION >= 9
9  // #include <ignition/math.hh>
10  #include <ignition/math/Vector3.hh>
11  #include <ignition/math/Quaternion.hh>
12 #else
13  #include <gazebo/math/gzmath.hh>
14 #endif
15 
16 namespace gazebo
17 {
18 
19 
20 GazeboRosKobuki::GazeboRosKobuki() : shutdown_requested_(false)
21 {
22  // Initialise variables
23  wheel_speed_cmd_[LEFT] = 0.0;
24  wheel_speed_cmd_[RIGHT] = 0.0;
25  cliff_detected_left_ = false;
26  cliff_detected_center_ = false;
27  cliff_detected_right_ = false;
28 }
29 
31 {
32 // rosnode_->shutdown();
33  shutdown_requested_ = true;
34  // Wait for spinner thread to end
35 // ros_spinner_thread_->join();
36 
37  // delete spinner_thread_;
38 // delete rosnode_;
39 }
40 
41 void GazeboRosKobuki::Load(physics::ModelPtr parent, sdf::ElementPtr sdf)
42 {
43  model_ = parent;
44  if (!model_)
45  {
46  ROS_ERROR_STREAM("Invalid model pointer! [" << node_name_ << "]");
47  return;
48  }
49 
50  gazebo_ros_ = GazeboRosPtr(new GazeboRos(model_, sdf, "Kobuki"));
51  sdf_ = sdf;
52 
53  // Make sure the ROS node for Gazebo has already been initialized
54  if (!ros::isInitialized())
55  {
56  ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
57  << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
58  return;
59  }
60 
61  // Get then name of the parent model and use it as node name
62  std::string model_name = sdf->GetParent()->Get<std::string>("name");
63  gzdbg << "Plugin model name: " << model_name << "\n";
64  node_name_ = model_name;
65  world_ = parent->GetWorld();
66 
69 
70  if(prepareJointState() == false)
71  return;
72  if(prepareWheelAndTorque() == false)
73  return;
74 
75  prepareOdom();
76 
77  if(prepareVelocityCommand() == false)
78  return;
79  if(prepareCliffSensor() == false)
80  return;
81  if(prepareBumper() == false)
82  return;
83  if(prepareIMU() == false)
84  return;
85 
86  setupRosApi(model_name);
87 
88  #if GAZEBO_MAJOR_VERSION >= 9
89  prev_update_time_ = world_->SimTime();
90  #else
91  prev_update_time_ = world_->GetSimTime();
92  #endif
93 
94  ROS_INFO_STREAM("GazeboRosKobuki plugin ready to go! [" << node_name_ << "]");
95  update_connection_ = event::Events::ConnectWorldUpdateEnd(boost::bind(&GazeboRosKobuki::OnUpdate, this));
96 
97 }
98 
99 
101 {
102  /*
103  * First process ROS callbacks
104  */
105  ros::spinOnce();
106 
107  /*
108  * Update current time and time step
109  */
110  common::Time time_now;
111  #if GAZEBO_MAJOR_VERSION >= 9
112  time_now = world_->SimTime();
113  #else
114  time_now = world_->GetSimTime();
115  #endif
116 
117  common::Time step_time = time_now - prev_update_time_;
118  prev_update_time_ = time_now;
119 
121  updateOdometry(step_time);
122  updateIMU();
125  updateBumper();
126 }
127 
129 {
130  ros::Rate r(10);
131  while(ros::ok() && !shutdown_requested_)
132  {
133  r.sleep();
134  }
135 }
136 
137 void GazeboRosKobuki::motorPowerCB(const kobuki_msgs::MotorPowerPtr &msg)
138 {
139  if ((msg->state == kobuki_msgs::MotorPower::ON) && (!motors_enabled_))
140  {
141  motors_enabled_ = true;
142  ROS_INFO_STREAM("Motors fired up. [" << node_name_ << "]");
143  }
144  else if ((msg->state == kobuki_msgs::MotorPower::OFF) && (motors_enabled_))
145  {
146  motors_enabled_ = false;
147  ROS_INFO_STREAM("Motors taking a rest. [" << node_name_ << "]");
148  }
149 }
150 
151 void GazeboRosKobuki::cmdVelCB(const geometry_msgs::TwistConstPtr &msg)
152 {
153  #if GAZEBO_MAJOR_VERSION >= 9
154  last_cmd_vel_time_ = world_->SimTime();
155  #else
156  last_cmd_vel_time_ = world_->GetSimTime();
157  #endif
158 
159  wheel_speed_cmd_[LEFT] = msg->linear.x - msg->angular.z * (wheel_sep_) / 2;
160  wheel_speed_cmd_[RIGHT] = msg->linear.x + msg->angular.z * (wheel_sep_) / 2;
161 }
162 
163 void GazeboRosKobuki::resetOdomCB(const std_msgs::EmptyConstPtr &msg)
164 {
165  odom_pose_[0] = 0.0;
166  odom_pose_[1] = 0.0;
167  odom_pose_[2] = 0.0;
168 }
169 
170 // Register this plugin with the simulator
172 
173 } // namespace gazebo
bool cliff_detected_right_
Cliff flag for the right sensor.
double odom_pose_[3]
Vector for pose.
GazeboRosPtr gazebo_ros_
pointer to the gazebo ros node
double wheel_sep_
Separation between the wheels.
bool shutdown_requested_
extra thread for triggering ROS callbacks
ROSCPP_DECL bool isInitialized()
void Load(physics::ModelPtr parent, sdf::ElementPtr sdf)
Called when plugin is loaded.
bool cliff_detected_left_
Cliff flag for the left sensor.
void OnUpdate()
Called by the world update start event.
common::Time last_cmd_vel_time_
Simulation time of the last velocity command (used for time out)
void setupRosApi(std::string &model_name)
bool motors_enabled_
Flag indicating if the motors are turned on or not.
#define ROS_FATAL_STREAM(args)
physics::ModelPtr model_
pointer to the model
double wheel_speed_cmd_[2]
Speeds of the wheels.
ROSCPP_DECL bool ok()
void motorPowerCB(const kobuki_msgs::MotorPowerPtr &msg)
Callback for incoming velocity commands.
bool sleep()
void spin()
Spin method for the spinner thread.
void resetOdomCB(const std_msgs::EmptyConstPtr &msg)
Callback for resetting the odometry data.
#define ROS_INFO_STREAM(args)
bool cliff_detected_center_
Cliff flag for the center sensor.
GZ_REGISTER_MODEL_PLUGIN(GazeboRosP3D)
void cmdVelCB(const geometry_msgs::TwistConstPtr &msg)
Callback for incoming velocity commands.
#define ROS_ERROR_STREAM(args)
std::string node_name_
node name
ROSCPP_DECL void spinOnce()
void updateOdometry(common::Time &step_time)
common::Time prev_update_time_
Simulation time on previous update.
physics::WorldPtr world_
pointer to simulated world
boost::shared_ptr< GazeboRos > GazeboRosPtr
event::ConnectionPtr update_connection_
pointer to the update event connection (triggers the OnUpdate callback when event update event is rec...


kobuki_gazebo_plugins
Author(s): Marcus Liebhardt
autogenerated on Mon Jun 10 2019 13:52:55