Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from bind to lambda for timer callback #43

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/test_tif_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ class MapPublisher : public rclcpp::Node {

map_ = std::make_shared<GridMapGeo>();
map_->Load(file_path, false, color_path);
timer_ = this->create_wall_timer(5s, std::bind(&MapPublisher::timer_callback, this));
auto timer_callback = [this]() -> void {
auto msg = grid_map::GridMapRosConverter::toMessage(map_->getGridMap());
if (msg) {
msg->header.stamp = now();
original_map_pub_->publish(std::move(msg));
}
};
timer_ = this->create_wall_timer(5s, timer_callback);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting,

I think for this node it makes a lot of sense since there is not much going on. How much does this apply for general controllers that need to run on a fixed schedule? For me just slightly weird that you would have all the logic implemented in the constructor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, it's still possible to have a separate member function to call the timer if we want, and this can be called from outside the class (in main) if it's public scope. Currently, for this node, where the grid_map data is fixed at runtime, the rate of publish does not matter, so I just made it simple and did all the work in the constructor.

Happy to change if you are not a fan.

}

private:
void timer_callback() {
auto msg = grid_map::GridMapRosConverter::toMessage(map_->getGridMap());
msg->header.stamp = now();
original_map_pub_->publish(std::move(msg));
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<grid_map_msgs::msg::GridMap>::SharedPtr original_map_pub_;
std::shared_ptr<GridMapGeo> map_;
Expand Down
Loading