|
| 1 | +#include "NavigationAdapter.h" |
| 2 | +#include <actionlib/client/simple_action_client.h> |
| 3 | +#include "arc_msgs/NavigationRequest.h" |
| 4 | +#include "std_srvs/Empty.h" |
| 5 | + |
| 6 | +using namespace arc_behaviour; |
| 7 | + |
| 8 | +NavigationAdapter::NavigationAdapter() { |
| 9 | + ros::NodeHandle nh; |
| 10 | + this->global_handle = nh; |
| 11 | + ros::NodeHandle local_handle("navigation_adapter"); |
| 12 | + this->local_handle = local_handle; |
| 13 | + ROS_INFO("Setting up navigation adapter.."); |
| 14 | + this->move_to_goal_server = local_handle.advertiseService("move_to_goal",&NavigationAdapter::move_to_goal_request_cb, this); |
| 15 | + this->move_client = new MoveBaseClient("move_base", true); |
| 16 | + //wait for the action server to come up |
| 17 | + while(!move_client->waitForServer(ros::Duration(5.0))) { //TODO: Better checking to ensure action server is actually up. If it's not, we need to let user know. |
| 18 | + ROS_INFO("Waiting for the move_base action server to come up"); |
| 19 | + } |
| 20 | + ROS_INFO("Found move_base action server!"); |
| 21 | +} |
| 22 | + |
| 23 | +void NavigationAdapter::move_to_goal_result_cb(const actionlib::SimpleClientGoalState& state, const move_base_msgs::MoveBaseResultConstPtr& result) { |
| 24 | + if (state == actionlib::SimpleClientGoalState::ABORTED){ |
| 25 | + ROS_INFO("Failed to move"); |
| 26 | + }else if(state == actionlib::SimpleClientGoalState::SUCCEEDED){ |
| 27 | + ROS_INFO("successfully moved to goal!!!"); |
| 28 | + } |
| 29 | + |
| 30 | + //either way, we are done navigating (until another request is sent) |
| 31 | + this->goal_active = false; |
| 32 | +} |
| 33 | + |
| 34 | +bool NavigationAdapter::move_to_goal_request_cb(arc_msgs::NavigationRequest::Request &req, arc_msgs::NavigationRequest::Response &res) { |
| 35 | + ROS_INFO("Received request to move to goal (%d, %d) with priority %d", req.pose.position.x, req.pose.position.y, req.priority); |
| 36 | + |
| 37 | + move_base_msgs::MoveBaseGoal goal; |
| 38 | + if(this->goal_active) { |
| 39 | + } else { |
| 40 | + //If no goal is currently active, we can send request to move_base |
| 41 | + //we'll send a goal to the robot to move 1 meter forward |
| 42 | + goal.target_pose.header.frame_id = "base_link"; |
| 43 | + goal.target_pose.header.stamp = ros::Time::now(); |
| 44 | + |
| 45 | + goal.target_pose.pose.position.x = req.pose.position.x; |
| 46 | + goal.target_pose.pose.orientation.w = 1.0; |
| 47 | + |
| 48 | + ROS_INFO("Sending navigation goal"); |
| 49 | + this->move_client->sendGoal(goal, boost::bind(&NavigationAdapter::move_to_goal_result_cb, this,_1,_2)); |
| 50 | + return true; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void NavigationAdapter::run() { |
| 55 | + ros::Rate r(10); //TODO: Make this a parameter, just get rid of magic numbers. |
| 56 | + |
| 57 | + while(ros::ok()) { |
| 58 | + |
| 59 | + /*if(this->move_client.getState() == actionlib::SimpleClientGoalState::ACTIVE) { |
| 60 | + ROS_INFO("Navigation goal is active."); |
| 61 | + } else { |
| 62 | + ROS_INFO("navigation is not active"); |
| 63 | + }*/ |
| 64 | + ros::spinOnce(); |
| 65 | + r.sleep(); |
| 66 | + } |
| 67 | +} |
0 commit comments