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

How to publish a message in a callback function?

asked 2015-07-23 05:12:37 -0500

feixiao gravatar image

updated 2015-07-23 05:53:48 -0500

gvdhoorn gravatar image

Hello, I try my best to make my Issue clear:

main()

ros::NodeHandle node;
ros::Publisher pub_vt = node.advertise<ratslam_ros::ViewTemplate>(topic_root + "/LocalView/Template", 0);
ros::Subscriber sub = node.subscribe<uhf_rfid_api::UhfRfid>("messageepc", 0, boost::bind(rfid_callback, _1, &pub_vt));

and,my clallback function is:

void rfid_callback( uhf_rfid_api::UhfRfid rfid, ros::Publisher *pub_vt)
{
   do something;
}

And my error messages is:

/usr/include/boost/bind/bind.hpp:313:34: error: could not convert ‘(& a)->boost::_bi::list1<A1>::operator[]<const boost::shared_ptr<const uhf_rfid_api::UhfRfid_<std::allocator<void> > >&>(boost::_bi::storage1<boost::arg<I> >::a1_<1>)’ from ‘const boost::shared_ptr<const uhf_rfid_api::UhfRfid_<std::allocator<void> > >’ to ‘uhf_rfid_api::UhfRfid_<std::allocator<void> >’
         unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);

and candidates are:

    In file included from /usr/include/boost/bind.hpp:22:0,
                 from /usr/include/boost/multi_index/sequenced_index.hpp:42,
                 from /usr/include/boost/property_tree/ptree.hpp:23,
                 from /home/feixiao/catkin_ws/src/ratslam-ratslam_ros/src/utils/utils.h:32,
                 from /home/feixiao/catkin_ws/src/ratslam-ratslam_ros/src/main_lv.cpp:35:
    /usr/include/boost/bind/bind.hpp: In instantiation of ‘void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = void (*)(uhf_rfid_api::UhfRfid_<std::allocator<void> >, ros::Publisher*); A = boost::_bi::list1<const boost::shared_ptr<const uhf_rfid_api::UhfRfid_<std::allocator<void> > >&>; A1 = boost::arg<1>; A2 = boost::_bi::value<ros::Publisher*>]’:
    /usr/include/boost/bind/bind_template.hpp:47:59:   required from ‘boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()(const A1&) [with A1 = boost::shared_ptr<const uhf_rfid_api::UhfRfid_<std::allocator<void> > >; R = void; F = void (*)(uhf_rfid_api::UhfRfid_<std::allocator<void> >, ros::Publisher*); L = boost::_bi::list2<boost::arg<1>, boost::_bi::value<ros::Publisher*> >; boost::_bi::bind_t<R, F, L>::result_type = void]’
    /usr/include/boost/function/function_template.hpp:153:11:   required from ‘static void boost::detail::function::void_function_obj_invoker1<FunctionObj, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with FunctionObj = boost::_bi::bind_t<void, void (*)(uhf_rfid_api::UhfRfid_<std::allocator<void> >, ros::Publisher*), boost::_bi::list2<boost::arg<1>, boost::_bi::value<ros::Publisher*> > >; R = void; T0 = const boost::shared_ptr<const uhf_rfid_api::UhfRfid_<std::allocator<void> > >&]’
    /usr/include/boost/function/function_template.hpp:934:38:   required from ‘void boost::function1<R, T1>::assign_to(Functor) [with Functor = boost::_bi::bind_t<void, void (*)(uhf_rfid_api::UhfRfid_<std::allocator<void> >, ros::Publisher*), boost::_bi::list2<boost::arg<1>, boost::_bi::value<ros::Publisher*> > >; R = void; T0 = const boost::shared_ptr<const uhf_rfid_api::UhfRfid_<std::allocator<void> > >&]’
    /usr/include/boost/function/function_template.hpp:722:7:   required from ‘boost::function1<R, T1>::function1(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<InputIterator>::value>::value, int>::type) [with Functor = boost::_bi::bind_t<void, void (*)(uhf_rfid_api::UhfRfid_<std::allocator<void> >, ros::Publisher*), boost::_bi::list2<boost::arg<1>, boost::_bi::value<ros::Publisher*> > >; R = void; T0 = const boost::shared_ptr<const uhf_rfid_api::UhfRfid_<std::allocator<void> > >&; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<InputIterator>::value>::value, int>::type = int]’
    /usr/include/boost/function/function_template.hpp:1069:16:   required ...
(more)
edit retag flag offensive close merge delete

Comments

Is that really your callback function? It would be nice to see it if not... And if that is your callback, then the compiler is trying to do function pointers and the function to which they are pointing isn't valid. Comment out the line and compile.

allenh1 gravatar image allenh1  ( 2015-07-23 06:44:01 -0500 )edit

thank you,i use a global variable and it works,although i don`t know why my BOOST::BIND is wrong yet.

feixiao gravatar image feixiao  ( 2015-07-28 20:14:35 -0500 )edit

Wait... Why do you need to do a bind here?

allenh1 gravatar image allenh1  ( 2015-07-28 20:36:00 -0500 )edit

Because he want(s/ed) to supply the publisher by the function call and in the function pointer. So making a double parameter function to a single parameter function by bind ... but actually when the publisher is global you do not need the bind anymore. Just supply the function name, remove pub param

cyborg-x1 gravatar image cyborg-x1  ( 2015-07-29 03:47:39 -0500 )edit

One of my simplest nodes using a class, with directly publishing in the callback: https://github.com/Hacks4ROS/h4r_joys...

cyborg-x1 gravatar image cyborg-x1  ( 2015-07-29 04:05:31 -0500 )edit

That's what I figured. I don't use boost all that much. Pretty neat stuff.

allenh1 gravatar image allenh1  ( 2015-07-29 15:07:58 -0500 )edit

Not when you're getting this errors... When I read them I am always like: WTF? :D

cyborg-x1 gravatar image cyborg-x1  ( 2015-07-30 02:42:56 -0500 )edit

Hahaha, yup. That's a pretty good description of it... It's at the very least intimidating. XD

allenh1 gravatar image allenh1  ( 2015-07-30 12:06:01 -0500 )edit

2 Answers

Sort by » oldest newest most voted
3

answered 2016-01-21 21:22:01 -0500

pay attention to

/home/feixiao/catkin_ws/src/ratslam-ratslam_ros/src/main_lv.cpp:142:119:   required from here
    /usr/include/boost/bind/bind.hpp:313:34: error: could not convert ‘(& a)->boost::_bi::list1<A1>::operator[]<const boost::shared_ptr<const uhf_rfid_api::UhfRfid_<std::allocator<void> > >&>(boost::_bi::storage1<boost::arg<I> >::a1_<1>)’ from ‘const boost::shared_ptr<const uhf_rfid_api::UhfRfid_<std::allocator<void> > >’ to ‘uhf_rfid_api::UhfRfid_<std::allocator<void> >’

change your callback function to

void rfid_callback( const uhf_rfid_api::UhfRfid::ConstPtr& rfid, ros::Publisher *pub_vt)
{
   do something;
}

when you declare your subscriber and publisher

ros::NodeHandle node;
ros::Publisher pub_vt = node.advertise<ratslam_ros::ViewTemplate>(topic_root + "/LocalView/Template", 0);
ros::Subscriber sub = node.subscribe<uhf_rfid_api::UhfRfid>("messageepc", 0, boost::bind(&rfid_callback, _1, &pub_vt));
edit flag offensive delete link more

Comments

awesome! I should add a const before uhf_rfid_api, it works!!!thanks a lot

feixiao gravatar image feixiao  ( 2016-01-21 21:40:23 -0500 )edit

ok,I knew why that I have to declare a const before my message datatype,the definition of the publisher advertise is: Publisher advertise(const std::string& topic, uint32_t queue_size, bool latch = false) and the callback is: void Foo::callback(const std_msgs::Empty::ConstPtr& message)

feixiao gravatar image feixiao  ( 2016-01-25 20:21:34 -0500 )edit

the definition of ConstPtr is typedef boost::shared_ptr<void const=""> VoidConstPtr; so ConstPtr just a declare right?

feixiao gravatar image feixiao  ( 2016-01-25 20:25:38 -0500 )edit

Still relevant in 2019, thanks my man!

Arthur_Ace gravatar image Arthur_Ace  ( 2019-02-26 10:40:51 -0500 )edit

LOL, my pleasure!

tianb03 gravatar image tianb03  ( 2019-03-02 00:38:09 -0500 )edit
1

answered 2015-07-23 07:54:57 -0500

updated 2015-07-23 07:55:45 -0500

I would either recommend you using a global publisher variable for simple nodes, or in a class doing the publisher with a member variable. In both of these ways you would not need that pointer to the publisher in the callback function header.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-07-23 05:12:37 -0500

Seen: 4,962 times

Last updated: Jan 21 '16