MCPcopy Create free account
hub / github.com/RoboJackets/software-training / MappingNode

Class MappingNode

reference_solutions/mapping/src/mapping_node.cpp:47–223  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45}
46
47class MappingNode : public rclcpp::Node
48{
49public:
50 explicit MappingNode(const rclcpp::NodeOptions & options)
51 // BEGIN STUDENT CODE
52 : rclcpp::Node("mapping_node", options),
53 tf_buffer_(get_clock()),
54 tf_listener_(tf_buffer_)
55 // END STUDENT CODE
56 {
57 map_frame_id_ = declare_parameter<std::string>("map_frame", "map");
58 robot_frame_id_ = declare_parameter<std::string>("robot_frame", "base_link");
59 distance_coefficient_ = declare_parameter<double>("distance_coefficient", 0.01);
60 hit_log_odds_ = toLogOdds(declare_parameter<double>("hit_probability", 0.7));
61 miss_log_odds_ = toLogOdds(declare_parameter<double>("miss_probability", 0.3));
62
63 const auto map_width = declare_parameter<double>("map_width", 1.2192); // meters
64 const auto map_height = declare_parameter<double>("map_height", 0.762); // meters
65 const auto map_resolution = declare_parameter<double>("map_resolution", 0.01); // meters/cell
66
67 map_info_.origin.position.x = -map_width / 2.0;
68 map_info_.origin.position.y = -map_height / 2.0;
69 map_info_.height = map_height / map_resolution;
70 map_info_.width = map_width / map_resolution;
71 map_info_.resolution = map_resolution;
72
73 const auto map_data_size = map_info_.height * map_info_.width;
74 map_data_.reserve(map_data_size);
75 std::fill_n(std::back_inserter(map_data_), map_data_size, 0.0);
76
77 map_publisher_ = create_publisher<nav_msgs::msg::OccupancyGrid>(
78 "~/map",
79 rclcpp::SystemDefaultsQoS());
80
81 // BEGIN STUDENT CODE
82 obstacles_subscription_ = create_subscription<nav_msgs::msg::OccupancyGrid>(
83 "~/obstacles",
84 rclcpp::SystemDefaultsQoS(),
85 [this](const nav_msgs::msg::OccupancyGrid::SharedPtr msg) {ObstaclesCallback(msg);});
86 // END STUDENT CODE
87 }
88
89private:
90 // BEGIN STUDENT CODE
91 tf2_ros::Buffer tf_buffer_;
92 tf2_ros::TransformListener tf_listener_;
93 // END STUDENT CODE
94 std::string map_frame_id_;
95 std::string robot_frame_id_;
96 double distance_coefficient_;
97 double hit_log_odds_;
98 double miss_log_odds_;
99 std::vector<double> map_data_;
100 nav_msgs::msg::MapMetaData map_info_;
101 rclcpp::Publisher<nav_msgs::msg::OccupancyGrid>::SharedPtr map_publisher_;
102 rclcpp::Subscription<nav_msgs::msg::OccupancyGrid>::SharedPtr obstacles_subscription_;
103
104 void ObstaclesCallback(const nav_msgs::msg::OccupancyGrid::SharedPtr obstacles_msg)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected