Creating publisher or subscriber and passing CallBack in ROS 2

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

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

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 imagegvdhoorn ( 2017-09-12 02:59:00 -0500 )edit

yes sorry for the typo, i have edited my question

arunavanag gravatar imagearunavanag ( 2017-09-12 03:00:23 -0500 )edit