Calling a publisher inside a subscriber
I've been trying to follow this answer for writing a publisher inside a subscriber callback function. My code looks like the following:
class TowelThrower{
public:
ros::NodeHandle node_handle;
ros::Publisher new_coors_pub;
ros::Subscriber vision_sub;
TowelThrower()
{
ros::Publisher new_coors_pub = node_handle.advertise<std_msgs::String>("coords_requests", 1000);
ros::Subscriber vision_sub = node_handle.subscribe("vision_coors", &TowelThrower::callback, this);
}
std::vector<geometry_msgs::Pose> create_path(double coorx, double coory)
{
std::vector<geometry_msgs::Pose> waypoints;
return waypoints;
}
void callback(const geometry_msgs::Pose msg)
{
std_msgs::String nsg;
nsg.data = "GIVE";
ROS_INFO("%s", nsg.data.c_str());
new_coors_pub.publish(nsg);
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "towel_thrower_node");
ros::AsyncSpinner spinner(1);
spinner.start();
ros::Rate loop_rate(10);
//ros::spin();
ros::shutdown();
return 0;
}
but I am getting the following error:
/home/team-sewts/catkin_ws/src/towel_thrower/src/towel_thrower.cpp: In constructor ‘TowelThrower::TowelThrower()’:
/home/team-sewts/catkin_ws/src/towel_thrower/src/towel_thrower.cpp:74:101: error: no matching function for call to ‘ros::NodeHandle::subscribe(const char [13], void (TowelThrower::*)(geometry_msgs::Pose), TowelThrower*)’
node_handle.subscribe("vision_coors", &TowelThrower::callback, this);
^
In file included from /opt/ros/kinetic/include/ros/ros.h:45:0,
from /opt/ros/kinetic/include/urdf/model.h:47,
from /opt/ros/kinetic/include/moveit/robot_model/robot_model.h:43,
from /opt/ros/kinetic/include/moveit/robot_state/robot_state.h:41,
from /opt/ros/kinetic/include/moveit/planning_scene_interface/planning_scene_interface.h:41,
from /home/team-sewts/catkin_ws/src/towel_thrower/src/towel_thrower.cpp:38:
/opt/ros/kinetic/include/ros/node_handle.h:402:14: note: candidate: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(M), T*, const ros::TransportHints&)
Subscriber subscribe(const std::string& topic, uint32_t queue_size,
^
/opt/ros/kinetic/include/ros/node_handle.h:402:14: note: template argument deduction/substitution failed:
/home/team-sewts/catkin_ws/src/towel_thrower/src/towel_thrower.cpp:74:101: note: cannot convert ‘&TowelThrower::callback’ (type ‘void (TowelThrower::*)(geometry_msgs::Pose) {aka void (TowelThrower::*)(geometry_msgs::Pose_<std::allocator<void> >)}’) to type ‘uint32_t {aka unsigned int}’
node_handle.subscribe("vision_coors", &TowelThrower::callback, this);
^
In file included from /opt/ros/kinetic/include/ros/ros.h:45:0,
from /opt/ros/kinetic/include/urdf/model.h:47,
from /opt/ros/kinetic/include/moveit/robot_model/robot_model.h:43,
from /opt/ros/kinetic/include/moveit/robot_state/robot_state.h:41,
from /opt/ros/kinetic/include/moveit/planning_scene_interface/planning_scene_interface.h:41,
from /home/team-sewts/catkin_ws/src/towel_thrower/src/towel_thrower.cpp:38:
/opt/ros/kinetic/include/ros/node_handle.h:413:14: note: candidate: template<class M, class T> ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(M) const, T*, const ros::TransportHints&)
Subscriber subscribe(const std::string& topic, uint32_t queue_size,
^
/opt/ros/kinetic/include/ros/node_handle.h:413:14: note: template argument deduction/substitution failed:
/home/team-sewts/catkin_ws/src/towel_thrower/src/towel_thrower.cpp:74:101: note: cannot convert ‘&TowelThrower::callback’ (type ‘void (TowelThrower::*)(geometry_msgs::Pose) {aka void (TowelThrower::*)(geometry_msgs::Pose_<std::allocator<void> >)}’) to type ‘uint32_t {aka unsigned int}’
node_handle.subscribe("vision_coors", &TowelThrower::callback, this);
^
In file included from /opt/ros/kinetic/include/ros/ros.h:45:0,
from /opt/ros/kinetic/include/urdf/model.h:47,
from /opt ...