[ros-users] Subscriber and Publisher node at once in cpp

Felix Endres endres at informatik.uni-freiburg.de
Tue Mar 8 14:58:46 UTC 2011


Hi Bünyamin,
I wouldn't create the Publisher in the callback.
Usually I have a class where I create the publisher in
the constructor and store it in a member variable.
Publishing can then be done from a callback method,
that uses the publisher member to call .publish().

You could also use a global variable to store the publisher
object if you do not want classes. Or you bind the publisher
boost::bind to the callback.

See 
http://www.ros.org/wiki/roscpp/Overview/Publishers%20and%20Subscribers 
for more info.

Regards,
Felix

Am 08.03.2011 15:27, schrieb "Yilmaz Bünyamin":
> Hi guys,
> i need some an example how to wrtie a node, wich is subscribing and publishing at the same time.
> I want to publish a message when i got i message, beacuse of that i try to publish in the Callback function, but it doesn't work.
>
>
> My code is following:
>
> void pointCallback(const nxt_msgs::Range&msg)
> {
> 	  sensor_msgs::LaserScan pub;
> 	  ros::NodeHandle r;
> 	  ros::Publisher laser_scan_pub = r.advertise<sensor_msgs::LaserScan>("laser_scan", 50);
> 	  laser_scan_pub.publish(pub);
> }
> int main(int argc, char **argv)
> {
>
>    ros::init(argc, argv, "range_to_laser");
>    ros::NodeHandle n;
>    ros::Subscriber sub = n.subscribe("point_cloud", 50, pointCallback);
>    ros::spin();
>
>    return 0;
> }
> Greetings,
> Bünyamin
> _______________________________________________
> ros-users mailing list
> ros-users at code.ros.org
> https://code.ros.org/mailman/listinfo/ros-users



More information about the ros-users mailing list