21#include <tf2/transform_datatypes.h>
22#include <yaml-cpp/yaml.h>
25#include <ament_index_cpp/get_package_share_directory.hpp>
34#include <rclcpp/rclcpp.hpp>
38using namespace std::chrono_literals;
78 getLogger(),
"[CpWaypointNavigator] Goal result received, incrementing waypoint index: %ld",
87 if (count <= 0)
return;
93 if (count <= 0)
return;
95 const long last = std::max(
static_cast<long>(
waypoints_.size()) - 1, 0L);
101 const long count =
static_cast<long>(
waypoints_.size());
109 for (
long i = std::max(previousWaypoint, 0L); i < count && found < 0; i++)
113 for (
long i = std::min(previousWaypoint, count) - 1; i >= 0 && found < 0; i--)
123 "[CpWaypointNavigator] seekName(%s): no waypoint has that name, index left at %ld",
130 getLogger(),
"[CpWaypointNavigator] seekName(%s), previous index: %ld, after index: %ld",
136 std::string parameter_name, std::string yaml_file_package_name)
139 std::string planfilepath;
141 if (!
getNode()->has_parameter(parameter_name))
143 getNode()->declare_parameter(parameter_name, planfilepath);
145 RCLCPP_INFO(
getLogger(),
"waypoints plan parameter: %s", planfilepath.c_str());
146 if (
getNode()->get_parameter(parameter_name, planfilepath))
148 std::string package_share_directory =
149 ament_index_cpp::get_package_share_directory(yaml_file_package_name);
151 RCLCPP_INFO(
getLogger(),
"file macro path: %s", planfilepath.c_str());
153 boost::replace_all(planfilepath,
"$(pkg_share)", package_share_directory);
155 RCLCPP_INFO(
getLogger(),
"package share path: %s", package_share_directory.c_str());
156 RCLCPP_INFO(
getLogger(),
"waypoints plan file: %s", planfilepath.c_str());
158 RCLCPP_INFO(
getLogger(),
"waypoints plan: %s", planfilepath.c_str());
162 RCLCPP_ERROR(
getLogger(),
"waypoints plan file not found: NONE");
176std::optional<std::shared_future<
177 std::shared_ptr<rclcpp_action::ClientGoalHandle<nav2_msgs::action::NavigateToPose>>>>
185 if (!nextName.empty())
188 getLogger(),
"[CpWaypointNavigator] sending goal, waypoint: %s", nextName.c_str());
196 nav2_msgs::action::NavigateToPose::Goal goal;
202 goal.pose.pose = next;
208 if (options && options->controllerName_)
211 getLogger(),
"[WaypointsNavigator] override controller: %s",
212 options->controllerName_->c_str());
218 RCLCPP_WARN(
getLogger(),
"[WaypointsNavigator] Configuring default planners");
224 if (goalCheckerSwitcher ==
nullptr)
227 getLogger(),
"[WaypointsNavigator] no CpGoalCheckerSwitcher; goal checker left as is");
229 else if (options && options->goalCheckerName_)
232 getLogger(),
"[WaypointsNavigator] override goal checker: %s",
233 options->goalCheckerName_->c_str());
239 RCLCPP_WARN(
getLogger(),
"[WaypointsNavigator] Configuring default goal checker");
245 RCLCPP_INFO(
getLogger(),
"[WaypointsNavigator] Getting odom tracker");
250 if (odomTracker !=
nullptr)
252 RCLCPP_INFO(
getLogger(),
"[WaypointsNavigator] Storing path in odom tracker");
280 "[CpWaypointsNavigator] All waypoints were consumed. There is no more waypoints available.");
291 RCLCPP_WARN(
getLogger(),
"[CpWaypointNavigator] Last waypoint reached, posting EOF event. ");
299 if (r.code == rclcpp_action::ResultCode::SUCCEEDED)
303 else if (r.code == rclcpp_action::ResultCode::CANCELED)
316 if (index >= 0 && index <= (
int)
waypoints_.size())
324 const geometry_msgs::msg::Pose & pose,
const std::string & name)
339 for (
const auto & pose : waypoints)
349 for (
auto & p : waypoints)
351 geometry_msgs::msg::Pose pose;
352 pose.position.x = p.x_;
353 pose.position.y = p.y_;
354 pose.position.z = 0.0;
356 q.setRPY(0, 0, p.yaw_);
357 pose.orientation = tf2::toMsg(q);
365 if (index >= 0 && index < (
int)
waypoints_.size())
379 if (index >= 0 && index < (
int)
waypoints_.size())
385 throw std::out_of_range(
"Waypoint index out of range");
396 throw std::out_of_range(
"Waypoint index out of range");
401 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 RCLCPP_ERROR_STREAM(
getLogger(),
"Waypoints file not found: " << filepath);
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 :
nullptr;
461 const YAML::Node * wp_node = node.FindValue(
"waypoints");
464 if (wp_node !=
nullptr)
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 addWaypoint(wp, wpnodei[
"name"].IsDefined() ? wpnodei[
"name"].as<std::string>() :
"");
486 RCLCPP_ERROR(
getLogger(),
"parsing waypoint file, syntax error in point %ld", i);
493 RCLCPP_WARN_STREAM(
getLogger(),
"Couldn't find any waypoints in the provided yaml file.");
496 catch (
const YAML::ParserException & ex)
499 getLogger(),
"Error loading the Waypoints YAML file. Incorrect syntax: " << ex.what());
507 RCLCPP_INFO_STREAM(
getLogger(),
"[CpWaypointNavigator] Loading file:" << filepath);
509 std::ifstream ifs(filepath.c_str(), std::ifstream::in);
510 if (ifs.good() ==
false)
512 RCLCPP_ERROR_STREAM(
getLogger(),
"Waypoints file not found: " << filepath);
518#ifdef HAVE_NEW_YAMLCPP
519 YAML::Node node = YAML::Load(ifs);
521 YAML::Parser parser(ifs);
522 parser.GetNextDocument(node);
525#ifdef HAVE_NEW_YAMLCPP
526 const YAML::Node & wp_node_tmp = node[
"waypoints"];
527 const YAML::Node * wp_node = wp_node_tmp ? &wp_node_tmp :
nullptr;
529 const YAML::Node * wp_node = node.FindValue(
"waypoints");
532 if (wp_node !=
nullptr)
534 for (std::size_t i = 0; i < wp_node->size(); ++i)
537 geometry_msgs::msg::Pose wp;
541 wp.position.x = (*wp_node)[i][
"x"].as<
double>();
542 wp.position.y = (*wp_node)[i][
"y"].as<
double>();
543 auto name = (*wp_node)[i][
"name"].as<std::string>();
549 RCLCPP_ERROR(
getLogger(),
"parsing waypoint file, syntax error in point %ld", i);
556 RCLCPP_WARN_STREAM(
getLogger(),
"Couldn't find any waypoints in the provided yaml file.");
559 catch (
const YAML::ParserException & ex)
562 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)
~CpWaypointNavigatorBase() override
const std::vector< geometry_msgs::msg::Pose > & getWaypoints() const
bool loadWayPointsFromFile2(std::string filepath)
CpWaypointNavigatorBase()
std::vector< geometry_msgs::msg::Pose > waypoints_
std::vector< std::string > waypointsNames_
geometry_msgs::msg::Pose getCurrentPose() const
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
bool loadWayPointsFromFile(std::string filepath)
bool seekName(std::string name)
void addWaypoint(const geometry_msgs::msg::Pose &pose, const std::string &name)
bool loadWaypointsFromYamlParameter(std::string parameter_name, std::string yaml_file_package_name)
const std::vector< std::string > & getWaypointNames() const
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