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;
225 goal.pose.pose = next;
231 if (options && options->controllerName_)
234 getLogger(),
"[WaypointsNavigator] override controller: %s",
235 options->controllerName_->c_str());
241 RCLCPP_WARN(
getLogger(),
"[WaypointsNavigator] Configuring default planners");
247 if (options && options->goalCheckerName_)
250 getLogger(),
"[WaypointsNavigator] override goal checker: %s",
251 options->goalCheckerName_->c_str());
257 RCLCPP_WARN(
getLogger(),
"[WaypointsNavigator] Configuring default goal checker");
263 RCLCPP_INFO(
getLogger(),
"[WaypointsNavigator] Getting odom tracker");
268 if (odomTracker !=
nullptr)
270 RCLCPP_INFO(
getLogger(),
"[WaypointsNavigator] Storing path in odom tracker");
298 "[CpWaypointsNavigator] All waypoints were consumed. There is no more waypoints available.");
309 RCLCPP_WARN(
getLogger(),
"[CpWaypointNavigator] Last waypoint reached, posting EOF event. ");
317 if (r.code == rclcpp_action::ResultCode::SUCCEEDED)
321 else if (r.code == rclcpp_action::ResultCode::ABORTED)
325 else if (r.code == rclcpp_action::ResultCode::CANCELED)
337 if (index >= 0 && index <= (
int)
waypoints_.size())
353 for (
auto & p : waypoints)
355 geometry_msgs::msg::Pose pose;
356 pose.position.x = p.x_;
357 pose.position.y = p.y_;
358 pose.position.z = 0.0;
360 q.setRPY(0, 0, p.yaw_);
361 pose.orientation = tf2::toMsg(q);
370 if (index >= 0 && index < (
int)
waypoints_.size())
383 if (index >= 0 && index < (
int)
waypoints_.size())
389 throw std::out_of_range(
"Waypoint index out of range");
400 throw std::out_of_range(
"Waypoint index out of range");
405 std::string name)
const
437#define HAVE_NEW_YAMLCPP
440 RCLCPP_INFO_STREAM(
getLogger(),
"[CpWaypointNavigatorBase] Loading file:" << filepath);
442 std::ifstream ifs(filepath.c_str(), std::ifstream::in);
443 if (ifs.good() ==
false)
445 throw std::string(
"Waypoints file not found");
450#ifdef HAVE_NEW_YAMLCPP
451 YAML::Node node = YAML::Load(ifs);
453 YAML::Parser parser(ifs);
454 parser.GetNextDocument(node);
457#ifdef HAVE_NEW_YAMLCPP
458 const YAML::Node & wp_node_tmp = node[
"waypoints"];
459 const YAML::Node * wp_node = wp_node_tmp ? &wp_node_tmp : NULL;
461 const YAML::Node * wp_node = node.FindValue(
"waypoints");
466 for (std::size_t i = 0; i < wp_node->size(); ++i)
469 geometry_msgs::msg::Pose wp;
473 auto wpnodei = (*wp_node)[i];
474 wp.position.x = wpnodei[
"position"][
"x"].as<
double>();
475 wp.position.y = wpnodei[
"position"][
"y"].as<
double>();
476 wp.position.z = wpnodei[
"position"][
"z"].as<
double>();
477 wp.orientation.x = wpnodei[
"orientation"][
"x"].as<
double>();
478 wp.orientation.y = wpnodei[
"orientation"][
"y"].as<
double>();
479 wp.orientation.z = wpnodei[
"orientation"][
"z"].as<
double>();
480 wp.orientation.w = wpnodei[
"orientation"][
"w"].as<
double>();
482 if (wpnodei[
"name"].IsDefined())
491 RCLCPP_ERROR(
getLogger(),
"parsing waypoint file, syntax error in point %ld", i);
498 RCLCPP_WARN_STREAM(
getLogger(),
"Couldn't find any waypoints in the provided yaml file.");
501 catch (
const YAML::ParserException & ex)
504 getLogger(),
"Error loading the Waypoints YAML file. Incorrect syntax: " << ex.what());
510 RCLCPP_INFO_STREAM(
getLogger(),
"[CpWaypointNavigator] Loading file:" << filepath);
512 std::ifstream ifs(filepath.c_str(), std::ifstream::in);
513 if (ifs.good() ==
false)
515 throw std::string(
"Waypoints file not found");
520#ifdef HAVE_NEW_YAMLCPP
521 YAML::Node node = YAML::Load(ifs);
523 YAML::Parser parser(ifs);
524 parser.GetNextDocument(node);
527#ifdef HAVE_NEW_YAMLCPP
528 const YAML::Node & wp_node_tmp = node[
"waypoints"];
529 const YAML::Node * wp_node = wp_node_tmp ? &wp_node_tmp : NULL;
531 const YAML::Node * wp_node = node.FindValue(
"waypoints");
536 for (std::size_t i = 0; i < wp_node->size(); ++i)
539 geometry_msgs::msg::Pose wp;
543 wp.position.x = (*wp_node)[i][
"x"].as<
double>();
544 wp.position.y = (*wp_node)[i][
"y"].as<
double>();
545 auto name = (*wp_node)[i][
"name"].as<std::string>();
552 RCLCPP_ERROR(
getLogger(),
"parsing waypoint file, syntax error in point %ld", i);
559 RCLCPP_WARN_STREAM(
getLogger(),
"Couldn't find any waypoints in the provided yaml file.");
562 catch (
const YAML::ParserException & ex)
565 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::PoseStamped toPoseStampedMsg()
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