Skip to content

Commit 3682051

Browse files
Publish latest pose wrt map frame in a separate topic
1 parent b9d5992 commit 3682051

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

slam_gmapping/include/slam_gmapping/slam_gmapping.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "nav_msgs/msg/map_meta_data.hpp"
3535
#include "geometry_msgs/msg/pose.hpp"
3636
#include "geometry_msgs/msg/pose_stamped.hpp"
37+
#include "geometry_msgs/msg/pose_with_covariance_stamped.hpp"
3738
#include "geometry_msgs/msg/transform_stamped.hpp"
3839
#include "tf2_geometry_msgs/tf2_geometry_msgs.h"
3940
#include "tf2_ros/transform_listener.h"
@@ -63,7 +64,7 @@ class SlamGmapping : public rclcpp::Node{
6364
rclcpp::Publisher<std_msgs::msg::Float64>::SharedPtr entropy_publisher_;
6465
rclcpp::Publisher<nav_msgs::msg::OccupancyGrid>::SharedPtr sst_;
6566
rclcpp::Publisher<nav_msgs::msg::MapMetaData>::SharedPtr sstm_;
66-
rclcpp::Publisher<PoseWithCovarianceStamped>::SharedPtr pose_publisher_;
67+
rclcpp::Publisher<geometry_msgs::msg::PoseWithCovarianceStamped>::SharedPtr pose_publisher_;
6768

6869
std::shared_ptr<tf2_ros::Buffer> buffer_;
6970
std::shared_ptr<tf2_ros::TransformListener> tfl_;
@@ -148,6 +149,9 @@ class SlamGmapping : public rclcpp::Node{
148149

149150
double transform_publish_period_;
150151
double tf_delay_;
152+
153+
// latest pose in the map frame
154+
geometry_msgs::msg::PoseWithCovarianceStamped latestPose_;
151155
};
152156

153157
#endif //SLAM_GMAPPING_SLAM_GMAPPING_H_

slam_gmapping/src/slam_gmapping.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void SlamGmapping::startLiveSlam() {
9595
entropy_publisher_ = this->create_publisher<std_msgs::msg::Float64>("entropy");
9696
sst_ = this->create_publisher<nav_msgs::msg::OccupancyGrid>("map");
9797
sstm_ = this->create_publisher<nav_msgs::msg::MapMetaData>("map_metadata");
98-
pose_publisher_ = this->create_publisher<geometry_msgs::PoseWithCovarianceStamped>("pose");
98+
pose_publisher_ = this->create_publisher<geometry_msgs::msg::PoseWithCovarianceStamped>("pose");
9999
scan_filter_sub_ = std::make_shared<message_filters::Subscriber<sensor_msgs::msg::LaserScan>>
100100
(node_, "scan");
101101
scan_filter_ = std::make_shared<tf2_ros::MessageFilter<sensor_msgs::msg::LaserScan>>
@@ -366,19 +366,22 @@ void SlamGmapping::laserCallback(sensor_msgs::msg::LaserScan::ConstSharedPtr sca
366366
q.setRPY(0, 0, mpose.theta);
367367
tf2::Transform laser_to_map = tf2::Transform(q, tf2::Vector3(mpose.x, mpose.y, 0.0)).inverse();
368368

369-
// publish latest pose with regards to map frame
369+
// update latest pose with regards to map frame
370370
{
371-
PoseWithCovarianceStamped p;
371+
std::lock_guard<std::mutex> g(map_to_odom_mutex_);
372+
geometry_msgs::msg::PoseWithCovarianceStamped& p = latestPose_;
372373
p.header.stamp = now();
373374
p.header.frame_id = map_frame_;
374375

375-
p.pose.position.x = mpose.x
376-
p.pose.position.y = mpose.y
377-
p.pose.position.z = 0.0;
376+
p.pose.pose.position.x = mpose.x;
377+
p.pose.pose.position.y = mpose.y;
378+
p.pose.pose.position.z = 0.0;
378379

379-
p.pose.orientation = q;
380-
381-
pose_publisher_->publish(p);
380+
const auto v = q.getAxis();
381+
p.pose.pose.orientation.x = v[0];
382+
p.pose.pose.orientation.y = v[1];
383+
p.pose.pose.orientation.z = v[2];
384+
p.pose.pose.orientation.w = q.getW();
382385
}
383386

384387

@@ -524,6 +527,10 @@ void SlamGmapping::updateMap(const sensor_msgs::msg::LaserScan::ConstSharedPtr s
524527
void SlamGmapping::publishTransform()
525528
{
526529
map_to_odom_mutex_.lock();
530+
531+
// publish the latest pose
532+
pose_publisher_->publish(latestPose_);
533+
527534
rclcpp::Time tf_expiration = get_clock()->now() + rclcpp::Duration(
528535
static_cast<int32_t>(static_cast<rcl_duration_value_t>(tf_delay_)), 0);
529536
geometry_msgs::msg::TransformStamped transform;

0 commit comments

Comments
 (0)