1 #include <gazebo/sensors/SensorManager.hh> 2 #include <gazebo/sensors/RaySensor.hh> 3 #include <create_node/TurtlebotSensorState.h> 4 #include <nav_msgs/Odometry.h> 5 #include <geometry_msgs/Twist.h> 6 #include <sensor_msgs/JointState.h> 14 : gazebo_node_(new transport::Node())
48 ROS_FATAL(
"Gazebo_ROS_Create controller requires a Model as its parent");
54 if (_sdf->HasElement(
"node_namespace"))
55 this->
node_namespace_ = _sdf->GetElement(
"node_namespace")->Get<std::string>() +
"/";
59 if (_sdf->HasElement(
"left_wheel_joint"))
60 left_wheel_joint_name_ = _sdf->GetElement(
"left_wheel_joint")->Get<std::string>();
63 if (_sdf->HasElement(
"right_wheel_joint"))
64 right_wheel_joint_name_ = _sdf->GetElement(
"right_wheel_joint")->Get<std::string>();
67 if (_sdf->HasElement(
"front_castor_joint"))
68 front_castor_joint_name_ = _sdf->GetElement(
"front_castor_joint")->Get<std::string>();
71 if (_sdf->HasElement(
"rear_castor_joint"))
72 rear_castor_joint_name_ = _sdf->GetElement(
"rear_castor_joint")->Get<std::string>();
75 if (_sdf->HasElement(
"wheel_separation"))
76 wheel_sep_ = _sdf->GetElement(
"wheel_separation")->Get<
double>();
79 if (_sdf->HasElement(
"wheel_separation"))
80 wheel_sep_ = _sdf->GetElement(
"wheel_separation")->Get<
double>();
83 if (_sdf->HasElement(
"wheel_diameter"))
84 wheel_diam_ = _sdf->GetElement(
"wheel_diameter")->Get<
double>();
87 if (_sdf->HasElement(
"torque"))
88 torque_ = _sdf->GetElement(
"torque")->Get<
double>();
91 if (_sdf->HasElement(
"base_geom"))
92 base_geom_name_ = _sdf->GetElement(
"base_geom")->Get<std::string>();
99 base_geom_ =
my_parent_->GetChildCollision(
"base_footprint_geom");
102 ROS_ERROR(
"Unable to find geom[%s]",base_geom_name_.c_str());
109 physics::ContactManager *mgr =
my_world_->GetPhysicsEngine()->GetContactManager();
110 std::string topic = mgr->CreateFilter(base_geom_name_, base_geom_name_);
113 wall_sensor_ = boost::dynamic_pointer_cast<sensors::RaySensor>(
114 sensors::SensorManager::Instance()->GetSensor(
"wall_sensor"));
117 ROS_ERROR(
"Unable to find sensor[wall_sensor]");
122 sensors::SensorManager::Instance()->GetSensor(
"left_cliff_sensor"));
124 sensors::SensorManager::Instance()->GetSensor(
"right_cliff_sensor"));
126 sensors::SensorManager::Instance()->GetSensor(
"leftfront_cliff_sensor"));
128 sensors::SensorManager::Instance()->GetSensor(
"rightfront_cliff_sensor"));
132 right_cliff_sensor_->SetActive(
true);
133 rightfront_cliff_sensor_->SetActive(
true);
134 leftfront_cliff_sensor_->SetActive(
true);
152 js_.name.push_back( left_wheel_joint_name_ );
153 js_.position.push_back(0);
154 js_.velocity.push_back(0);
155 js_.effort.push_back(0);
157 js_.name.push_back( right_wheel_joint_name_ );
158 js_.position.push_back(0);
159 js_.velocity.push_back(0);
160 js_.effort.push_back(0);
162 js_.name.push_back( front_castor_joint_name_ );
163 js_.position.push_back(0);
164 js_.velocity.push_back(0);
165 js_.effort.push_back(0);
167 js_.name.push_back( rear_castor_joint_name_ );
168 js_.position.push_back(0);
169 js_.velocity.push_back(0);
170 js_.effort.push_back(0);
197 std::string modelName = _sdf->GetParent()->Get<std::string>(
"name");
202 gzdbg <<
"plugin model name: " << modelName <<
"\n";
208 float y_overlap = 0.16495 *
sin( 10 * (M_PI/180.0) );
210 for (
int i=0; i < contacts->contact_size(); i++ )
212 const msgs::Contact & contact = contacts->contact(i);
213 const int contact_count = contact.position_size();
214 for (
unsigned int j=0; j < contact_count; j++)
217 if (contact.position(j).x() > 0.012 && contact.position(j).z() < 0.06 &&
218 contact.position(j).z() > 0.01)
221 if (contact.position(j).y() <= y_overlap)
224 if (contact.position(j).y() >= -y_overlap)
234 common::Time time_now = this->
my_world_->GetSimTime();
236 prev_update_time_ = time_now;
250 d1 = step_time.Double() * (wd / 2) *
joints_[LEFT]->GetVelocity(0);
252 d2 = step_time.Double() * (wd / 2) *
joints_[RIGHT]->GetVelocity(0);
256 ROS_WARN_THROTTLE(0.1,
"Gazebo ROS Create plugin. NaN in d1. Step time: %.2f. WD: %.2f. Velocity: %.2f", step_time.Double(), wd,
joints_[
LEFT]->GetVelocity(0));
261 ROS_WARN_THROTTLE(0.1,
"Gazebo ROS Create plugin. NaN in d2. Step time: %.2f. WD: %.2f. Velocity: %.2f", step_time.Double(), wd,
joints_[
RIGHT]->GetVelocity(0));
290 nav_msgs::Odometry odom;
291 odom.header.stamp.sec = time_now.sec;
292 odom.header.stamp.nsec = time_now.nsec;
293 odom.header.frame_id =
"odom";
294 odom.child_frame_id =
"base_footprint";
297 odom.pose.pose.position.z = 0;
302 odom.pose.pose.orientation.x = qt.getX();
303 odom.pose.pose.orientation.y = qt.getY();
304 odom.pose.pose.orientation.z = qt.getZ();
305 odom.pose.pose.orientation.w = qt.
getW();
307 double pose_cov[36] = { 1e-3, 0, 0, 0, 0, 0,
314 memcpy( &odom.pose.covariance[0], pose_cov,
sizeof(
double)*36 );
315 memcpy( &odom.twist.covariance[0], pose_cov,
sizeof(
double)*36 );
317 odom.twist.twist.linear.x =
odom_vel_[0];
318 odom.twist.twist.linear.y = 0;
319 odom.twist.twist.linear.z = 0;
321 odom.twist.twist.angular.x = 0;
322 odom.twist.twist.angular.y = 0;
323 odom.twist.twist.angular.z =
odom_vel_[2];
327 js_.header.stamp.sec = time_now.sec;
328 js_.header.stamp.nsec = time_now.nsec;
359 if (time_since_last_cmd.Double() > 0.6)
ros::Subscriber cmd_vel_sub_
sensors::RaySensorPtr right_cliff_sensor_
#define ROS_WARN_THROTTLE(rate,...)
void publish(const boost::shared_ptr< M > &message) const
std::string base_geom_name_
void OnCmdVel(const geometry_msgs::TwistConstPtr &msg)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::string left_wheel_joint_name_
ROSCPP_DECL bool isInitialized()
common::Time prev_update_time_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
sensors::RaySensorPtr left_cliff_sensor_
float wheel_sep_
Separation between the wheels.
std::string right_wheel_joint_name_
void OnContact(ConstContactsPtr &contact)
physics::CollisionPtr base_geom_
TFSIMD_FORCE_INLINE const tfScalar & getW() const
sensors::RaySensorPtr wall_sensor_
GZ_REGISTER_MODEL_PLUGIN(GazeboRosCreate)
float wheel_diam_
Diameter of the wheels.
transport::SubscriberPtr contact_sub_
sensors::RaySensorPtr rightfront_cliff_sensor_
create_node::TurtlebotSensorState sensor_state_
std::string rear_castor_joint_name_
physics::JointPtr joints_[4]
virtual void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
std::string front_castor_joint_name_
boost::thread * spinner_thread_
float torque_
Torque applied to the wheels.
ros::Publisher joint_state_pub_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
physics::ModelPtr my_parent_
ros::Publisher sensor_state_pub_
void setEuler(const tfScalar &yaw, const tfScalar &pitch, const tfScalar &roll)
float * wheel_speed_
Speeds of the wheels.
ros::NodeHandle * rosnode_
sensor_msgs::JointState js_
virtual ~GazeboRosCreate()
common::Time last_cmd_vel_time_
physics::WorldPtr my_world_
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
std::string node_namespace_
Parameters.
transport::NodePtr gazebo_node_
virtual void UpdateChild()
ROSCPP_DECL void spinOnce()
sensors::RaySensorPtr leftfront_cliff_sensor_
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
event::ConnectionPtr updateConnection