Subcriber block Actionlib in the same node
Hello I was tried to combining node with subscribe and actionlib in the same node.
I want to get 5 data from callback subscribe and then run it to actionlib. but my program stuck on the callback and never run to actionlib
I'm quite new to actionlib, so please anyone can help me? I will appreciate every help that i can get thank you best regards
here is the code i use :
#include "ros/ros.h"
#include "geometry_msgs/PointStamped.h"
#include <move_base_msgs/MoveBaseAction.h>
#include <actionlib/client/simple_action_client.h>
bool moveToGoal(double xGoal, double yGoal);
double x[5];
double y[5];
int u = 0;
int n = 0;
bool waypoint = false;
bool goalReached = false;
void chatterCallback(const geometry_msgs::PointStamped& msg)
{
ROS_INFO("I heard: [%f, %f]", msg.point.x, msg.point.y);
if (u < 3)
{
x[u] = msg.point.x;
y[u] = msg.point.y;
ROS_INFO("Position X[ and Y: [%f, %f]", x[u], y[u]);
u += 1;
}
if (u >= 3)
{
waypoint = true;
ROS_INFO("waypoint done");
}
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "map_waypoint");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("clicked_point", 1000, chatterCallback);
while (waypoint == true)
{
for (int i = 0; i <= 2; i++)
{
goalReached = moveToGoal(x[i], y[i]);
ROS_INFO("start moving");
ros::spinOnce();
if (goalReached)
{
ROS_INFO("Congratulations![%f, %f]", x[i], y[i]);
ros::spinOnce();
// sc.playWave(path_to_sounds+"ship_bell.wav");
// ros::spinOnce();
}
else
{
ROS_INFO("Hard Luck!");
// sc.playWave(path_to_sounds+"short_buzzer.wav");
}
}
}
ros::spin();
return 0;
}
bool moveToGoal(double xGoal, double yGoal)
{
// define a client for to send goal requests to the move_base server through a SimpleActionClient
actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction> ac("move_base", true);
// wait for the action server to come up
while (!ac.waitForServer(ros::Duration(5.0)))
{
ROS_INFO("Waiting for the move_base action server to come up");
}
move_base_msgs::MoveBaseGoal goal;
// set up the frame parameters
goal.target_pose.header.frame_id = "map";
goal.target_pose.header.stamp = ros::Time::now();
/* moving towards the goal*/
goal.target_pose.pose.position.x = xGoal;
goal.target_pose.pose.position.y = yGoal;
goal.target_pose.pose.position.z = 0.0;
goal.target_pose.pose.orientation.x = 0.0;
goal.target_pose.pose.orientation.y = 0.0;
goal.target_pose.pose.orientation.z = 0.0;
goal.target_pose.pose.orientation.w = 1.0;
ROS_INFO("Sending goal location ...");
ac.sendGoal(goal);
ac.waitForResult();
if (ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
{
ROS_INFO("You have reached the destination");
return true;
}
else
{
ROS_INFO("The robot failed to reach the destination");
return false;
}
}