How to publish a message in a callback function?
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 ...
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.
thank you,i use a global variable and it works,although i don`t know why my BOOST::BIND is wrong yet.
Wait... Why do you need to do a bind here?
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
One of my simplest nodes using a class, with directly publishing in the callback: https://github.com/Hacks4ROS/h4r_joys...
That's what I figured. I don't use boost all that much. Pretty neat stuff.
Not when you're getting this errors... When I read them I am always like: WTF? :D
Hahaha, yup. That's a pretty good description of it... It's at the very least intimidating. XD