21#include <tf2/transform_datatypes.h>
22#include <yaml-cpp/yaml.h>
24#include <ament_index_cpp/get_package_share_directory.hpp>
33#include <rclcpp/rclcpp.hpp>
37using namespace std::chrono_literals;
75 getLogger(),
"[CpWaypointNavigator] Goal result received, incrementing waypoint index: %ld",
107 getLogger(),
"[CpWaypointNavigator] seeking ,%ld/%ld candidate waypoint: %s",
109 if (name == nextName)
113 getLogger(),
"[CpWaypointNavigator] found target waypoint: %s == %s-> found",
114 nextName.c_str(), name.c_str());
119 getLogger(),
"[CpWaypointNavigator] current waypoint: %s != %s -> forward",
120 nextName.c_str(), name.c_str());
137 getLogger(),
"[CpWaypointNavigator] seeking , candidate waypoint: %s", nextName.c_str());
138 if (name == nextName)
142 getLogger(),
"[CpWaypointNavigator] found target waypoint: %s == %s-> found",
143 nextName.c_str(), name.c_str());
148 getLogger(),
"[CpWaypointNavigator] current waypoint: %s != %s -> rewind",
149 nextName.c_str(), name.c_str());
156 getLogger(),
"[CpWaypointNavigator] seekName( %s), previous index: %ld, after index: %ld",
161 std::string parameter_name, std::string yaml_file_package_name)
164 std::string planfilepath;
165 planfilepath =
getNode()->declare_parameter(parameter_name, planfilepath);
166 RCLCPP_INFO(
getLogger(),
"waypoints plan parameter: %s", planfilepath.c_str());
167 if (
getNode()->get_parameter(parameter_name, planfilepath))
169 std::string package_share_directory =
170 ament_index_cpp::get_package_share_directory(yaml_file_package_name);
172 RCLCPP_INFO(
getLogger(),
"file macro path: %s", planfilepath.c_str());
174 boost::replace_all(planfilepath,
"$(pkg_share)", package_share_directory);
176 RCLCPP_INFO(
getLogger(),
"package share path: %s", package_share_directory.c_str());
177 RCLCPP_INFO(
getLogger(),
"waypoints plan file: %s", planfilepath.c_str());
180 RCLCPP_INFO(
getLogger(),
"waypoints plan: %s", planfilepath.c_str());
184 RCLCPP_ERROR(
getLogger(),
"waypoints plan file not found: NONE");
198std::optional<std::shared_future<
199 std::shared_ptr<rclcpp_action::ClientGoalHandle<nav2_msgs::action::NavigateToPose>>>>
206 std::string nextName;
211 getLogger(),
"[CpWaypointNavigator] sending goal, waypoint: %s", nextName.c_str());
219 nav2_msgs::action::NavigateToPose::Goal goal;
226 goal.pose.pose = next;
232 if (options && options->controllerName_)
235 getLogger(),
"[WaypointsNavigator] override controller: %s",
236 options->controllerName_->c_str());
242 RCLCPP_WARN(
getLogger(),
"[WaypointsNavigator] Configuring default planners");
248 if (options && options->goalCheckerName_)
251 getLogger(),
"[WaypointsNavigator] override goal checker: %s",
252 options->goalCheckerName_->c_str());
258 RCLCPP_WARN(
getLogger(),
"[WaypointsNavigator] Configuring default goal checker");
264 RCLCPP_INFO(
getLogger(),
"[WaypointsNavigator] Getting odom tracker");
269 if (odomTracker !=
nullptr)
271 RCLCPP_INFO(
getLogger(),
"[WaypointsNavigator] Storing path in odom tracker");
297 "[CpWaypointsNavigator] All waypoints were consumed. There is no more waypoints available.");
308 RCLCPP_WARN(
getLogger(),
"[CpWaypointNavigator] Last waypoint reached, posting EOF event. ");
316 if (r.code == rclcpp_action::ResultCode::SUCCEEDED)
320 else if (r.code == rclcpp_action::ResultCode::ABORTED)
324 else if (r.code == rclcpp_action::ResultCode::CANCELED)
336 if (index >= 0 && index <= (
int)
waypoints_.size())
352 for (
auto & p : waypoints)
354 geometry_msgs::msg::Pose pose;
355 pose.position.x = p.x_;
356 pose.position.y = p.y_;
357 pose.position.z = 0.0;
359 q.setRPY(0, 0, p.yaw_);
360 pose.orientation = tf2::toMsg(q);
369 if (index >= 0 && index < (
int)
waypoints_.size())
382 if (index >= 0 && index < (
int)
waypoints_.size())
388 throw std::out_of_range(
"Waypoint index out of range");
399 throw std::out_of_range(
"Waypoint index out of range");
404 std::string name)
const
436#define HAVE_NEW_YAMLCPP
439 RCLCPP_INFO_STREAM(
getLogger(),
"[CpWaypointNavigatorBase] Loading file:" << filepath);
441 std::ifstream ifs(filepath.c_str(), std::ifstream::in);
442 if (ifs.good() ==
false)
444 throw std::string(
"Waypoints file not found");
449#ifdef HAVE_NEW_YAMLCPP
450 YAML::Node node = YAML::Load(ifs);
452 YAML::Parser parser(ifs);
453 parser.GetNextDocument(node);
456#ifdef HAVE_NEW_YAMLCPP
457 const YAML::Node & wp_node_tmp = node[
"waypoints"];
458 const YAML::Node * wp_node = wp_node_tmp ? &wp_node_tmp : NULL;
460 const YAML::Node * wp_node = node.FindValue(
"waypoints");
465 for (std::size_t i = 0; i < wp_node->size(); ++i)
468 geometry_msgs::msg::Pose wp;
472 auto wpnodei = (*wp_node)[i];
473 wp.position.x = wpnodei[
"position"][
"x"].as<
double>();
474 wp.position.y = wpnodei[
"position"][
"y"].as<
double>();
475 wp.position.z = wpnodei[
"position"][
"z"].as<
double>();
476 wp.orientation.x = wpnodei[
"orientation"][
"x"].as<
double>();
477 wp.orientation.y = wpnodei[
"orientation"][
"y"].as<
double>();
478 wp.orientation.z = wpnodei[
"orientation"][
"z"].as<
double>();
479 wp.orientation.w = wpnodei[
"orientation"][
"w"].as<
double>();
481 if (wpnodei[
"name"].IsDefined())
490 RCLCPP_ERROR(
getLogger(),
"parsing waypoint file, syntax error in point %ld", i);
497 RCLCPP_WARN_STREAM(
getLogger(),
"Couldn't find any waypoints in the provided yaml file.");
500 catch (
const YAML::ParserException & ex)
503 getLogger(),
"Error loading the Waypoints YAML file. Incorrect syntax: " << ex.what());
509 RCLCPP_INFO_STREAM(
getLogger(),
"[CpWaypointNavigator] Loading file:" << filepath);
511 std::ifstream ifs(filepath.c_str(), std::ifstream::in);
512 if (ifs.good() ==
false)
514 throw std::string(
"Waypoints file not found");
519#ifdef HAVE_NEW_YAMLCPP
520 YAML::Node node = YAML::Load(ifs);
522 YAML::Parser parser(ifs);
523 parser.GetNextDocument(node);
526#ifdef HAVE_NEW_YAMLCPP
527 const YAML::Node & wp_node_tmp = node[
"waypoints"];
528 const YAML::Node * wp_node = wp_node_tmp ? &wp_node_tmp : NULL;
530 const YAML::Node * wp_node = node.FindValue(
"waypoints");
535 for (std::size_t i = 0; i < wp_node->size(); ++i)
538 geometry_msgs::msg::Pose wp;
542 wp.position.x = (*wp_node)[i][
"x"].as<
double>();
543 wp.position.y = (*wp_node)[i][
"y"].as<
double>();
544 auto name = (*wp_node)[i][
"name"].as<std::string>();
551 RCLCPP_ERROR(
getLogger(),
"parsing waypoint file, syntax error in point %ld", i);
558 RCLCPP_WARN_STREAM(
getLogger(),
"Couldn't find any waypoints in the provided yaml file.");
561 catch (
const YAML::ParserException & ex)
564 getLogger(),
"Error loading the Waypoints YAML file. Incorrect syntax: " << ex.what());
void postWaypointEvent(int index)
void setGoalCheckerId(std::string goal_checker_id)
void setDefaultPlanners(bool commit=true)
void setDesiredController(std::string)
geometry_msgs::msg::Pose toPoseMsg()
const std::string & getReferenceFrame() const
void setWaypoints(const std::vector< geometry_msgs::msg::Pose > &waypoints)
const std::vector< geometry_msgs::msg::Pose > & getWaypoints() const
void loadWayPointsFromFile2(std::string filepath)
void seekName(std::string name)
CpWaypointNavigatorBase()
std::vector< geometry_msgs::msg::Pose > waypoints_
std::vector< std::string > waypointsNames_
geometry_msgs::msg::Pose getCurrentPose() const
void loadWayPointsFromFile(std::string filepath)
virtual ~CpWaypointNavigatorBase()
std::optional< std::string > getCurrentWaypointName() const
std::optional< geometry_msgs::msg::Pose > getNamedPose(std::string name) const
WaypointEventDispatcher waypointsEventDispatcher
geometry_msgs::msg::Pose getPose(int index) const
const std::vector< std::string > & getWaypointNames() const
void loadWaypointsFromYamlParameter(std::string parameter_name, std::string yaml_file_package_name)
void removeWaypoint(int index)
void insertWaypoint(int index, geometry_msgs::msg::Pose &newpose)
long getCurrentWaypointIndex() const
void onInitialize() override
void onGoalReached(const components::CpNav2ActionInterface::WrappedResult &res)
smacc2::SmaccSignalConnection succeededNav2ZClientConnection_
smacc2::SmaccSignal< void()> onNavigationRequestSucceeded_
void onGoalAborted(const components::CpNav2ActionInterface::WrappedResult &)
smacc2::SmaccSignalConnection cancelledNav2ZClientConnection_
smacc2::SmaccSignalConnection abortedNav2ZClientConnection_
components::CpNav2ActionInterface * nav2ActionInterface_
void onInitialize() override
smacc2::SmaccSignal< void()> onNavigationRequestCancelled_
void onNavigationResult(const components::CpNav2ActionInterface::WrappedResult &r)
void onGoalCancelled(const components::CpNav2ActionInterface::WrappedResult &)
std::optional< std::shared_future< std::shared_ptr< rclcpp_action::ClientGoalHandle< nav2_msgs::action::NavigateToPose > > > > sendNextGoal(std::optional< NavigateNextWaypointOptions > options=std::nullopt)
smacc2::SmaccSignal< void()> onNavigationRequestAborted_
smacc2::SmaccSignalConnection onNavigationSucceeded(void(T::*callback)(const WrappedResult &), T *object)
std::shared_future< typename GoalHandle::SharedPtr > sendGoal(Goal &goal)
smacc2::SmaccSignalConnection onNavigationAborted(void(T::*callback)(const WrappedResult &), T *object)
typename GoalHandle::WrappedResult WrappedResult
smacc2::SmaccSignalConnection onNavigationCancelled(void(T::*callback)(const WrappedResult &), T *object)
void setWorkingMode(WorkingMode workingMode)
void setStartPoint(const geometry_msgs::msg::PoseStamped &pose)
ISmaccStateMachine * getStateMachine()
virtual std::string getName() const
rclcpp::Logger getLogger() const
void requiresComponent(TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
rclcpp::Node::SharedPtr getNode()
ISmaccState * getCurrentState() const
virtual std::string getName()=0