in simulation_ws/src/rover/hector_gazebo_plugins/src/diffdrive_plugin_multi_wheel.cpp [368:450]
void GazeboRosDiffDriveMultiWheel::publishOdometry(double step_time) {
ros::Time current_time = ros::Time::now();
std::string odom_frame =
tf::resolve(tf_prefix_, odometry_frame_);
std::string base_footprint_frame =
tf::resolve(tf_prefix_, robot_base_frame_);
// getting data for base_footprint to odom transform
#if (GAZEBO_MAJOR_VERSION >= 8)
ignition::math::Pose3d pose = this->parent->WorldPose();
tf::Quaternion qt(pose.Rot().X(), pose.Rot().Y(), pose.Rot().Z(), pose.Rot().W());
tf::Vector3 vt(pose.Pos().X(), pose.Pos().Y(), pose.Pos().Z());
#else
math::Pose pose = this->parent->GetWorldPose();
tf::Quaternion qt(pose.rot.x, pose.rot.y, pose.rot.z, pose.rot.w);
tf::Vector3 vt(pose.pos.x, pose.pos.y, pose.pos.z);
#endif
tf::Transform base_footprint_to_odom(qt, vt);
if (this->publish_odometry_tf_){
transform_broadcaster_->sendTransform(
tf::StampedTransform(base_footprint_to_odom, current_time,
odom_frame, base_footprint_frame));
}
// publish odom topic
#if (GAZEBO_MAJOR_VERSION >= 8)
odom_.pose.pose.position.x = pose.Pos().X();
odom_.pose.pose.position.y = pose.Pos().Y();
odom_.pose.pose.orientation.x = pose.Rot().X();
odom_.pose.pose.orientation.y = pose.Rot().Y();
odom_.pose.pose.orientation.z = pose.Rot().Z();
odom_.pose.pose.orientation.w = pose.Rot().W();
#else
odom_.pose.pose.position.x = pose.pos.x;
odom_.pose.pose.position.y = pose.pos.y;
odom_.pose.pose.orientation.x = pose.rot.x;
odom_.pose.pose.orientation.y = pose.rot.y;
odom_.pose.pose.orientation.z = pose.rot.z;
odom_.pose.pose.orientation.w = pose.rot.w;
#endif
odom_.pose.covariance[0] = 0.00001;
odom_.pose.covariance[7] = 0.00001;
odom_.pose.covariance[14] = 1000000000000.0;
odom_.pose.covariance[21] = 1000000000000.0;
odom_.pose.covariance[28] = 1000000000000.0;
odom_.pose.covariance[35] = 0.001;
// get velocity in /odom frame
#if (GAZEBO_MAJOR_VERSION >= 8)
ignition::math::Vector3d linear;
linear = this->parent->WorldLinearVel();
odom_.twist.twist.angular.z = this->parent->WorldAngularVel().Z();
#else
math::Vector3 linear;
linear = this->parent->GetWorldLinearVel();
odom_.twist.twist.angular.z = this->parent->GetWorldAngularVel().z;
#endif
// convert velocity to child_frame_id (aka base_footprint)
#if (GAZEBO_MAJOR_VERSION >= 8)
float yaw = pose.Rot().Yaw();
odom_.twist.twist.linear.x = cosf(yaw) * linear.X() + sinf(yaw) * linear.Y();
odom_.twist.twist.linear.y = cosf(yaw) * linear.Y() - sinf(yaw) * linear.X();
#else
float yaw = pose.rot.GetYaw();
odom_.twist.twist.linear.x = cosf(yaw) * linear.x + sinf(yaw) * linear.y;
odom_.twist.twist.linear.y = cosf(yaw) * linear.y - sinf(yaw) * linear.x;
#endif
odom_.header.stamp = current_time;
odom_.header.frame_id = odom_frame;
odom_.child_frame_id = base_footprint_frame;
if (this->publish_odometry_msg_){
odometry_publisher_.publish(odom_);
}
}