Commit 3ba4c6d1 authored by Daniel Müller's avatar Daniel Müller
Browse files

Merge branch 'cpp_lib_refactor' into 'master'

Bugfix and added library init()

See merge request !8
parents 809ef39c 7d0e7f6c
......@@ -5,21 +5,22 @@
#include <actionlib/client/simple_action_client.h>
#include <homer_mapnav_msgs/DriveToAction.h>
#include <ros/ros.h>
namespace homer_navigation
{
void init(const ros::NodeHandle& nh);
void drive_to_POI(const std::string& poi_name, float distance_to_target = 0.03,
bool suppress_speaking = false, bool skip_final_turn = false,
bool stop_before_obstacle = false, bool check_obstacle_type = false,
bool suppress_speaking = false, bool skip_final_turn = false, bool stop_before_obstacle = false,
bool check_obstacle_type = false,
const boost::function<void(const actionlib::SimpleClientGoalState& state,
const homer_mapnav_msgs::DriveToResultConstPtr&)>& callback = 0,
const homer_mapnav_msgs::DriveToResultConstPtr&)>& callback
= 0,
bool keep_torso_position = false);
homer_mapnav_msgs::DriveToResultConstPtr drive_to_POI_blocked(const std::string& poi_name,
float distance_to_target = 0.03, bool suppress_speaking = false, bool skip_final_turn = false,
homer_mapnav_msgs::DriveToResultConstPtr drive_to_POI_blocked(const std::string& poi_name,
float distance_to_target = 0.03, bool suppress_speaking = false, bool skip_final_turn = false,
bool stop_before_obstacle = false, bool check_obstacle_type = false,
ros::Duration timeout = ros::Duration(0), bool keep_torso_position = false);
}
} // namespace homer_navigation
......@@ -8,27 +8,40 @@ namespace homer_navigation
typedef actionlib::SimpleActionClient<homer_mapnav_msgs::DriveToAction> ActionClient;
namespace
{
std::pair<std::unique_ptr<ActionClient>, homer_mapnav_msgs::DriveToGoal> create_client_and_goal(
const std::string& poi_name, float distance_to_target = 0.03,
bool suppress_speaking = false, bool skip_final_turn = false,
bool stop_before_obstacle = false, bool check_obstacle_type = false,
bool keep_torso_position = false)
std::unique_ptr<ros::NodeHandle> nh_ptr;
std::unique_ptr<ActionClient> ac_ptr;
ActionClient* getActionClient()
{
auto ac = std::make_unique<ActionClient>("drive_to", true);
bool is_alive = ac->waitForServer(ros::Duration(5));
if (!is_alive)
if (!ac_ptr)
{
ROS_ERROR_STREAM("drive_to action server not reachable");
return {};
ROS_ERROR_STREAM("ActionClient not initialized. Call init() before library usage.");
return nullptr;
}
ROS_INFO_STREAM("[DriveTo]:\n goal_location: \t" << poi_name
<< "\n distance_to_target: \t" << distance_to_target
<< "\n speaking: \t\t" << (!suppress_speaking ? "true" : "false"));
homer_mapnav_msgs::DriveToGoal goal;
return ac_ptr.get();
}
ros::NodeHandle* getNodeHandle()
{
if (!nh_ptr)
{
ROS_ERROR_STREAM("NodeHandle not initialized. Call init() before library usage.");
return nullptr;
}
return nh_ptr.get();
}
homer_mapnav_msgs::DriveToGoal create_goal(const std::string& poi_name,
float distance_to_target = 0.03, bool suppress_speaking = false,
bool skip_final_turn = false, bool stop_before_obstacle = false,
bool check_obstacle_type = false, bool keep_torso_position = false)
{
ROS_INFO_STREAM("[DriveTo]:\n goal_location: \t"
<< poi_name << "\n distance_to_target: \t" << distance_to_target << "\n speaking: \t\t"
<< (!suppress_speaking ? "true" : "false"));
homer_mapnav_msgs::DriveToGoal goal;
goal.goal_location = poi_name;
goal.distance_to_target = distance_to_target;
goal.suppress_speaking = suppress_speaking;
......@@ -36,28 +49,39 @@ namespace
goal.stop_before_obstacle = stop_before_obstacle;
goal.check_obstacle = check_obstacle_type;
goal.keep_torso_position = keep_torso_position;
auto result = std::make_pair(std::move(ac), goal);
return result;
return goal;
}
} // namespace
void init(const ros::NodeHandle& nh)
{
nh_ptr = std::make_unique<ros::NodeHandle>(nh);
ac_ptr = std::make_unique<ActionClient>("drive_to", true);
}
void drive_to_POI(const std::string& poi_name, float distance_to_target,
bool suppress_speaking, bool skip_final_turn,
bool stop_before_obstacle, bool check_obstacle_type,
void drive_to_POI(const std::string& poi_name, float distance_to_target, bool suppress_speaking,
bool skip_final_turn, bool stop_before_obstacle, bool check_obstacle_type,
const boost::function<void(const actionlib::SimpleClientGoalState& state,
const homer_mapnav_msgs::DriveToResultConstPtr&)>& callback,
bool keep_torso_position)
{
auto pair = create_client_and_goal(poi_name, distance_to_target, suppress_speaking,
skip_final_turn, stop_before_obstacle, check_obstacle_type, keep_torso_position);
if (!pair.first)
{
ROS_ERROR_STREAM("Aborting drive_to_POI request.");
{
auto goal = create_goal(poi_name, distance_to_target, suppress_speaking, skip_final_turn,
stop_before_obstacle, check_obstacle_type, keep_torso_position);
auto ac = getActionClient();
if (!ac)
{
return;
}
pair.first->sendGoal(pair.second, callback);
ros::Duration(0.1).sleep(); //required for the callback; otherwise its not called
bool is_alive = ac->waitForServer(ros::Duration(5));
if (!is_alive)
{
ROS_ERROR_STREAM("Drive_to action server not reachable");
return;
}
ac->sendGoal(goal, callback);
ros::Duration(0.1).sleep(); // required for the callback; otherwise its not called
}
......@@ -66,23 +90,16 @@ homer_mapnav_msgs::DriveToResultConstPtr drive_to_POI_blocked(const std::string&
bool stop_before_obstacle, bool check_obstacle_type, ros::Duration timeout,
bool keep_torso_position)
{
auto pair = create_client_and_goal(poi_name, distance_to_target, suppress_speaking,
skip_final_turn, stop_before_obstacle, check_obstacle_type, keep_torso_position);
if (!pair.first)
{
ROS_ERROR_STREAM("Aborting drive_to_POI_blocked request.");
return nullptr;
}
auto client = std::move(pair.first);
auto goal = pair.second;
client->sendGoal(goal);
bool delivered = client->waitForResult(timeout);
homer_navigation::drive_to_POI(poi_name, distance_to_target, suppress_speaking, skip_final_turn,
stop_before_obstacle, check_obstacle_type, 0, keep_torso_position);
auto ac = getActionClient();
bool delivered = ac->waitForResult(timeout);
if (!delivered)
{
ROS_ERROR_STREAM("Timeout of drive_to_POI_blocked result.");
return nullptr;
}
return client->getResult();
return ac->getResult();
}
}
} // namespace homer_navigation
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment