ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Creating publisher or subscriber and passing CallBack in ROS 2

asked 2017-09-12 01:54:01 -0600

updated 2017-09-12 09:35:02 -0600

William gravatar image

Hello ,

Earlier we did

ros::Publisher pub_name; 
ros::Subscriber sub_name:
ros::NodeHandle nh;

However now after checking the Migration Guide :

I wanted to create couple of publishers in my class, using rclcpp guide

like:

rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub;   <--- (not sure about this method)

or,

auto node = rclcpp::node::Node::make_shared("talker");
auto chatter_pub = node->create_publisher<std_msgs::msg::String>("chatter", rmw_qos_profile_default);

Which is fine. However is it the only way to create a publishers or subscribers. Can't I just declare the subscriber without stating the message type as earlier in ROS 1.0? example:

Class:

 ros::Subscriber sub_;
 ros:Publisher pub1, pub2;
 ros::NodeHandle nh;

Library:

sub_=nh.subscribe("topic_name_sub_", 1,&Class_Name::Callback_, this);
pub1_=nh.advertise < std_msgs::String> ("topic_name_pub1", 10);
pub2_=nh.advertise < std_msgs::Int8> ("topic_name_pub2", 10);
.
.
.
.
Callback();

Also how to have callback functions to my subscriber or publisher?

Kindly advise.

edit retag flag offensive close merge delete

Comments

[..] as earlier in ROS 2.0?

I guess you mean "in ROS 1", as what you show was never supported in ROS 2 afaik.

gvdhoorn gravatar image gvdhoorn  ( 2017-09-12 02:59:00 -0600 )edit

yes sorry for the typo, i have edited my question

arunavanag gravatar image arunavanag  ( 2017-09-12 03:00:23 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-01-08 01:16:26 -0600

rclcpp::Publisher is a templated class , so you have to provide the type for the publisher to be declared. roscpp::Publisher was not templated hence does not need explicit type declaration

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-12 01:54:01 -0600

Seen: 918 times

Last updated: Jan 08 '21