SMACC2
Loading...
Searching...
No Matches
cl_nav2z::CpWaypointNavigatorBase Class Reference

#include <cp_waypoints_navigator_base.hpp>

Inheritance diagram for cl_nav2z::CpWaypointNavigatorBase:
Inheritance graph
Collaboration diagram for cl_nav2z::CpWaypointNavigatorBase:
Collaboration graph

Public Member Functions

 CpWaypointNavigatorBase ()
 
 ~CpWaypointNavigatorBase () override
 
void onInitialize () override
 
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
bool loadWayPointsFromFile (std::string filepath)
 
bool loadWayPointsFromFile2 (std::string filepath)
 
void setWaypoints (const std::vector< geometry_msgs::msg::Pose > &waypoints)
 
void setWaypoints (const std::vector< Pose2D > &waypoints)
 
const std::vector< geometry_msgs::msg::Pose > & getWaypoints () const
 
const std::vector< std::string > & getWaypointNames () const
 
std::optional< geometry_msgs::msg::Pose > getNamedPose (std::string name) const
 
geometry_msgs::msg::Pose getPose (int index) const
 
geometry_msgs::msg::Pose getCurrentPose () const
 
long getCurrentWaypointIndex () const
 
std::optional< std::string > getCurrentWaypointName () const
 
void rewind (int count)
 
void forward (int count)
 
bool seekName (std::string name)
 
bool loadWaypointsFromYamlParameter (std::string parameter_name, std::string yaml_file_package_name)
 
void notifyGoalReached ()
 
- Public Member Functions inherited from smacc2::ISmaccComponent
 ISmaccComponent ()
 
virtual ~ISmaccComponent ()
 
 ISmaccComponent (const ISmaccComponent &)=delete
 
ISmaccComponent & operator= (const ISmaccComponent &)=delete
 
 ISmaccComponent (ISmaccComponent &&)=delete
 
ISmaccComponent & operator= (ISmaccComponent &&)=delete
 
virtual std::string getName () const
 

Public Attributes

WaypointEventDispatcher waypointsEventDispatcher
 
long currentWaypoint_
 

Protected Member Functions

void insertWaypoint (int index, geometry_msgs::msg::Pose &newpose)
 
void removeWaypoint (int index)
 
- Protected Member Functions inherited from smacc2::ISmaccComponent
template<typename TOrthogonal , typename TClient >
void onComponentInitialization ()
 
template<typename EventType >
void postEvent (const EventType &ev)
 
template<typename EventType >
void postEvent ()
 
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
template<typename TComponent >
void requiresComponent (TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
template<typename TComponent >
void requiresComponent (const std::string &name, TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
template<typename TClient >
void requiresClient (TClient *&requiredClientStorage)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createSiblingComponent (const TArgs &... targs)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createSiblingNamedComponent (const std::string &name, const TArgs &... targs)
 
rclcpp::Node::SharedPtr getNode ()
 
rclcpp::Logger getLogger () const
 
ISmaccStateMachine * getStateMachine ()
 

Protected Attributes

std::vector< geometry_msgs::msg::Pose > waypoints_
 
std::vector< std::string > waypointsNames_
 
- Protected Attributes inherited from smacc2::ISmaccComponent
ISmaccStateMachine * stateMachine_ = nullptr
 
ISmaccClient * owner_ = nullptr
 

Private Member Functions

void addWaypoint (const geometry_msgs::msg::Pose &pose, const std::string &name)
 
void clearWaypoints ()
 

Detailed Description

Definition at line 47 of file cp_waypoints_navigator_base.hpp.

Constructor & Destructor Documentation

◆ CpWaypointNavigatorBase()

cl_nav2z::CpWaypointNavigatorBase::CpWaypointNavigatorBase ( )

◆ ~CpWaypointNavigatorBase()

cl_nav2z::CpWaypointNavigatorBase::~CpWaypointNavigatorBase ( )
override

Definition at line 43 of file cp_waypoints_navigator.cpp.

43{}

Member Function Documentation

◆ addWaypoint()

void cl_nav2z::CpWaypointNavigatorBase::addWaypoint ( const geometry_msgs::msg::Pose & pose,
const std::string & name )
private

Definition at line 323 of file cp_waypoints_navigator.cpp.

325{
326 waypoints_.push_back(pose);
327 waypointsNames_.push_back(name);
328}

References waypoints_, and waypointsNames_.

Referenced by loadWayPointsFromFile(), loadWayPointsFromFile2(), setWaypoints(), and setWaypoints().

Here is the caller graph for this function:

◆ clearWaypoints()

void cl_nav2z::CpWaypointNavigatorBase::clearWaypoints ( )
private

Definition at line 330 of file cp_waypoints_navigator.cpp.

331{
332 waypoints_.clear();
333 waypointsNames_.clear();
334}

References waypoints_, and waypointsNames_.

Referenced by loadWayPointsFromFile(), loadWayPointsFromFile2(), setWaypoints(), and setWaypoints().

Here is the caller graph for this function:

◆ forward()

void cl_nav2z::CpWaypointNavigatorBase::forward ( int count)

Definition at line 91 of file cp_waypoints_navigator.cpp.

92{
93 if (count <= 0) return;
94 // stops at the last waypoint; 0 when the list is empty
95 const long last = std::max(static_cast<long>(waypoints_.size()) - 1, 0L);
96 currentWaypoint_ = std::min(currentWaypoint_ + count, last);
97}

References currentWaypoint_, and waypoints_.

Referenced by cl_nav2z::CbSeekWaypoint::onEntry(), and cl_nav2z::CbNavigateNextWaypointFree::onSucessCallback().

Here is the caller graph for this function:

◆ getCurrentPose()

geometry_msgs::msg::Pose cl_nav2z::CpWaypointNavigatorBase::getCurrentPose ( ) const

Definition at line 388 of file cp_waypoints_navigator.cpp.

389{
390 if (currentWaypoint_ >= 0 && currentWaypoint_ < (int)waypoints_.size())
391 {
393 }
394 else
395 {
396 throw std::out_of_range("Waypoint index out of range");
397 }
398}

References currentWaypoint_, and waypoints_.

Referenced by cl_nav2z::CbNavigateNextWaypointFree::onEntry().

Here is the caller graph for this function:

◆ getCurrentWaypointIndex()

long cl_nav2z::CpWaypointNavigatorBase::getCurrentWaypointIndex ( ) const

◆ getCurrentWaypointName()

std::optional< std::string > cl_nav2z::CpWaypointNavigatorBase::getCurrentWaypointName ( ) const

Definition at line 423 of file cp_waypoints_navigator.cpp.

424{
425 if (
426 currentWaypoint_ >= 0 && currentWaypoint_ < (long)waypointsNames_.size() &&
428 {
430 }
431 return std::nullopt;
432}

References currentWaypoint_, and waypointsNames_.

Referenced by cl_nav2z::CbNavigateNextWaypoint::onEntry(), and cl_nav2z::CbNavigateNextWaypointUntilReached::onEntry().

Here is the caller graph for this function:

◆ getNamedPose()

std::optional< geometry_msgs::msg::Pose > cl_nav2z::CpWaypointNavigatorBase::getNamedPose ( std::string name) const

Definition at line 400 of file cp_waypoints_navigator.cpp.

402{
403 // an empty name is that of an unnamed waypoint and matches nothing
404 if (!name.empty())
405 {
406 for (std::size_t i = 0; i < this->waypointsNames_.size(); i++)
407 {
408 if (this->waypointsNames_[i] == name)
409 {
410 return this->waypoints_[i];
411 }
412 }
413 }
414
415 return std::nullopt;
416}

References waypoints_, and waypointsNames_.

◆ getPose()

geometry_msgs::msg::Pose cl_nav2z::CpWaypointNavigatorBase::getPose ( int index) const

Definition at line 377 of file cp_waypoints_navigator.cpp.

378{
379 if (index >= 0 && index < (int)waypoints_.size())
380 {
381 return waypoints_[index];
382 }
383 else
384 {
385 throw std::out_of_range("Waypoint index out of range");
386 }
387}

References waypoints_.

◆ getWaypointNames()

const std::vector< std::string > & cl_nav2z::CpWaypointNavigatorBase::getWaypointNames ( ) const

Definition at line 418 of file cp_waypoints_navigator.cpp.

419{
420 return waypointsNames_;
421}

References waypointsNames_.

Referenced by cl_nav2z::CpWaypointsVisualizer::onInitialize().

Here is the caller graph for this function:

◆ getWaypoints()

const std::vector< geometry_msgs::msg::Pose > & cl_nav2z::CpWaypointNavigatorBase::getWaypoints ( ) const

Definition at line 372 of file cp_waypoints_navigator.cpp.

373{
374 return waypoints_;
375}

References waypoints_.

Referenced by cl_nav2z::CpWaypointsVisualizer::onInitialize().

Here is the caller graph for this function:

◆ insertWaypoint()

void cl_nav2z::CpWaypointNavigatorBase::insertWaypoint ( int index,
geometry_msgs::msg::Pose & newpose )
protected

Definition at line 314 of file cp_waypoints_navigator.cpp.

315{
316 if (index >= 0 && index <= (int)waypoints_.size())
317 {
318 waypoints_.insert(waypoints_.begin() + index, newpose);
319 waypointsNames_.insert(waypointsNames_.begin() + index, std::string());
320 }
321}

References waypoints_, and waypointsNames_.

◆ loadWayPointsFromFile()

bool cl_nav2z::CpWaypointNavigatorBase::loadWayPointsFromFile ( std::string filepath)

Definition at line 437 of file cp_waypoints_navigator.cpp.

438{
439 RCLCPP_INFO_STREAM(getLogger(), "[CpWaypointNavigatorBase] Loading file:" << filepath);
441 std::ifstream ifs(filepath.c_str(), std::ifstream::in);
442 if (ifs.good() == false)
443 {
444 RCLCPP_ERROR_STREAM(getLogger(), "Waypoints file not found: " << filepath);
445 return false;
446 }
447
448 try
449 {
450#ifdef HAVE_NEW_YAMLCPP
451 YAML::Node node = YAML::Load(ifs);
452#else
453 YAML::Parser parser(ifs);
454 parser.GetNextDocument(node);
455#endif
456
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;
460#else
461 const YAML::Node * wp_node = node.FindValue("waypoints");
462#endif
463
464 if (wp_node != nullptr)
465 {
466 for (std::size_t i = 0; i < wp_node->size(); ++i)
467 {
468 // Parse waypoint entries on YAML
469 geometry_msgs::msg::Pose wp;
470
471 try
472 {
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>();
481
482 addWaypoint(wp, wpnodei["name"].IsDefined() ? wpnodei["name"].as<std::string>() : "");
483 }
484 catch (...)
485 {
486 RCLCPP_ERROR(getLogger(), "parsing waypoint file, syntax error in point %ld", i);
487 }
488 }
489 RCLCPP_INFO_STREAM(getLogger(), "Parsed " << this->waypoints_.size() << " waypoints.");
490 }
491 else
492 {
493 RCLCPP_WARN_STREAM(getLogger(), "Couldn't find any waypoints in the provided yaml file.");
494 }
495 }
496 catch (const YAML::ParserException & ex)
497 {
498 RCLCPP_ERROR_STREAM(
499 getLogger(), "Error loading the Waypoints YAML file. Incorrect syntax: " << ex.what());
500 return false;
501 }
502 return true;
503}
void addWaypoint(const geometry_msgs::msg::Pose &pose, const std::string &name)
rclcpp::Logger getLogger() const

References addWaypoint(), clearWaypoints(), smacc2::ISmaccComponent::getLogger(), and waypoints_.

Referenced by loadWaypointsFromYamlParameter(), and cl_nav2z::CbLoadWaypointsFile::onEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadWayPointsFromFile2()

bool cl_nav2z::CpWaypointNavigatorBase::loadWayPointsFromFile2 ( std::string filepath)

Definition at line 505 of file cp_waypoints_navigator.cpp.

506{
507 RCLCPP_INFO_STREAM(getLogger(), "[CpWaypointNavigator] Loading file:" << filepath);
509 std::ifstream ifs(filepath.c_str(), std::ifstream::in);
510 if (ifs.good() == false)
511 {
512 RCLCPP_ERROR_STREAM(getLogger(), "Waypoints file not found: " << filepath);
513 return false;
514 }
515
516 try
517 {
518#ifdef HAVE_NEW_YAMLCPP
519 YAML::Node node = YAML::Load(ifs);
520#else
521 YAML::Parser parser(ifs);
522 parser.GetNextDocument(node);
523#endif
524
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;
528#else
529 const YAML::Node * wp_node = node.FindValue("waypoints");
530#endif
531
532 if (wp_node != nullptr)
533 {
534 for (std::size_t i = 0; i < wp_node->size(); ++i)
535 {
536 // Parse waypoint entries on YAML
537 geometry_msgs::msg::Pose wp;
538
539 try
540 {
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>();
544
545 addWaypoint(wp, name);
546 }
547 catch (...)
548 {
549 RCLCPP_ERROR(getLogger(), "parsing waypoint file, syntax error in point %ld", i);
550 }
551 }
552 RCLCPP_INFO_STREAM(getLogger(), "Parsed " << this->waypoints_.size() << " waypoints.");
553 }
554 else
555 {
556 RCLCPP_WARN_STREAM(getLogger(), "Couldn't find any waypoints in the provided yaml file.");
557 }
558 }
559 catch (const YAML::ParserException & ex)
560 {
561 RCLCPP_ERROR_STREAM(
562 getLogger(), "Error loading the Waypoints YAML file. Incorrect syntax: " << ex.what());
563 return false;
564 }
565 return true;
566}

References addWaypoint(), clearWaypoints(), smacc2::ISmaccComponent::getLogger(), and waypoints_.

Here is the call graph for this function:

◆ loadWaypointsFromYamlParameter()

bool cl_nav2z::CpWaypointNavigatorBase::loadWaypointsFromYamlParameter ( std::string parameter_name,
std::string yaml_file_package_name )

Definition at line 135 of file cp_waypoints_navigator.cpp.

137{
138 // if it is the first time and the waypoints navigator is not configured
139 std::string planfilepath;
140 // a second load of the same parameter must not declare it again
141 if (!getNode()->has_parameter(parameter_name))
142 {
143 getNode()->declare_parameter(parameter_name, planfilepath);
144 }
145 RCLCPP_INFO(getLogger(), "waypoints plan parameter: %s", planfilepath.c_str());
146 if (getNode()->get_parameter(parameter_name, planfilepath))
147 {
148 std::string package_share_directory =
149 ament_index_cpp::get_package_share_directory(yaml_file_package_name);
150
151 RCLCPP_INFO(getLogger(), "file macro path: %s", planfilepath.c_str());
152
153 boost::replace_all(planfilepath, "$(pkg_share)", package_share_directory);
154
155 RCLCPP_INFO(getLogger(), "package share path: %s", package_share_directory.c_str());
156 RCLCPP_INFO(getLogger(), "waypoints plan file: %s", planfilepath.c_str());
157
158 RCLCPP_INFO(getLogger(), "waypoints plan: %s", planfilepath.c_str());
159 return this->loadWayPointsFromFile(planfilepath);
160 }
161
162 RCLCPP_ERROR(getLogger(), "waypoints plan file not found: NONE");
163 return false;
164}
bool loadWayPointsFromFile(std::string filepath)
rclcpp::Node::SharedPtr getNode()

References smacc2::ISmaccComponent::getLogger(), smacc2::ISmaccComponent::getNode(), and loadWayPointsFromFile().

Referenced by cl_nav2z::CbLoadWaypointsFile::onEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ notifyGoalReached()

void cl_nav2z::CpWaypointNavigatorBase::notifyGoalReached ( )

Definition at line 286 of file cp_waypoints_navigator.cpp.

287{
288 // the current waypoint is the one reached: when it is the last, post the finalization EOF event
289 if (currentWaypoint_ == (long)waypoints_.size() - 1)
290 {
291 RCLCPP_WARN(getLogger(), "[CpWaypointNavigator] Last waypoint reached, posting EOF event. ");
293 }
294}

References currentWaypoint_, smacc2::ISmaccComponent::getLogger(), smacc2::ISmaccComponent::postEvent(), and waypoints_.

Referenced by cl_nav2z::CpWaypointNavigator::onGoalReached(), and cl_nav2z::CbNavigateNextWaypointFree::onSucessCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onInitialize()

void cl_nav2z::CpWaypointNavigatorBase::onInitialize ( )
overridevirtual

Reimplemented from smacc2::ISmaccComponent.

Definition at line 47 of file cp_waypoints_navigator.cpp.

47{}

◆ onStateOrthogonalAllocation()

template<typename TOrthogonal , typename TSourceObject >
void cl_nav2z::CpWaypointNavigatorBase::onStateOrthogonalAllocation ( )
inline

Definition at line 59 of file cp_waypoints_navigator_base.hpp.

60 {
61 ClNav2Z * client = dynamic_cast<ClNav2Z *>(owner_);
62 waypointsEventDispatcher.initialize<TSourceObject, TOrthogonal>(client);
63 }
ISmaccClient * owner_
Definition component.hpp:96
namespace cl_nav2z class ClNav2Z

References WaypointEventDispatcher::initialize(), smacc2::ISmaccComponent::owner_, and waypointsEventDispatcher.

Here is the call graph for this function:

◆ removeWaypoint()

void cl_nav2z::CpWaypointNavigatorBase::removeWaypoint ( int index)
protected

Definition at line 363 of file cp_waypoints_navigator.cpp.

364{
365 if (index >= 0 && index < (int)waypoints_.size())
366 {
367 waypoints_.erase(waypoints_.begin() + index);
368 waypointsNames_.erase(waypointsNames_.begin() + index);
369 }
370}

References waypoints_, and waypointsNames_.

◆ rewind()

void cl_nav2z::CpWaypointNavigatorBase::rewind ( int count)

Definition at line 85 of file cp_waypoints_navigator.cpp.

86{
87 if (count <= 0) return;
88 currentWaypoint_ = std::max(currentWaypoint_ - count, 0L);
89}

References currentWaypoint_.

Referenced by cl_nav2z::CbSeekWaypoint::onEntry().

Here is the caller graph for this function:

◆ seekName()

bool cl_nav2z::CpWaypointNavigatorBase::seekName ( std::string name)

Definition at line 99 of file cp_waypoints_navigator.cpp.

100{
101 const long count = static_cast<long>(waypoints_.size());
102 const long previousWaypoint = currentWaypoint_;
103 long found = -1;
104
105 // an empty name is that of an unnamed waypoint and matches nothing
106 if (!name.empty())
107 {
108 // forwards from the current waypoint first, then backwards from the one before it
109 for (long i = std::max(previousWaypoint, 0L); i < count && found < 0; i++)
110 {
111 if (waypointsNames_[i] == name) found = i;
112 }
113 for (long i = std::min(previousWaypoint, count) - 1; i >= 0 && found < 0; i--)
114 {
115 if (waypointsNames_[i] == name) found = i;
116 }
117 }
118
119 if (found < 0)
120 {
121 RCLCPP_WARN(
122 getLogger(),
123 "[CpWaypointNavigator] seekName(%s): no waypoint has that name, index left at %ld",
124 name.c_str(), currentWaypoint_);
125 return false;
126 }
127
128 currentWaypoint_ = found;
129 RCLCPP_INFO(
130 getLogger(), "[CpWaypointNavigator] seekName(%s), previous index: %ld, after index: %ld",
131 name.c_str(), previousWaypoint, currentWaypoint_);
132 return true;
133}

References currentWaypoint_, smacc2::ISmaccComponent::getLogger(), waypoints_, and waypointsNames_.

Referenced by cl_nav2z::CbNavigateNamedWaypoint::onEntry(), and cl_nav2z::CbSeekWaypoint::onEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setWaypoints() [1/2]

void cl_nav2z::CpWaypointNavigatorBase::setWaypoints ( const std::vector< geometry_msgs::msg::Pose > & waypoints)

Definition at line 336 of file cp_waypoints_navigator.cpp.

337{
339 for (const auto & pose : waypoints)
340 {
341 addWaypoint(pose, std::string());
342 }
343}

References addWaypoint(), and clearWaypoints().

Here is the call graph for this function:

◆ setWaypoints() [2/2]

void cl_nav2z::CpWaypointNavigatorBase::setWaypoints ( const std::vector< Pose2D > & waypoints)

Definition at line 345 of file cp_waypoints_navigator.cpp.

346{
348 int i = 0;
349 for (auto & p : waypoints)
350 {
351 geometry_msgs::msg::Pose pose;
352 pose.position.x = p.x_;
353 pose.position.y = p.y_;
354 pose.position.z = 0.0;
355 tf2::Quaternion q;
356 q.setRPY(0, 0, p.yaw_);
357 pose.orientation = tf2::toMsg(q);
358
359 addWaypoint(pose, std::to_string(i++));
360 }
361}

References addWaypoint(), and clearWaypoints().

Here is the call graph for this function:

Member Data Documentation

◆ currentWaypoint_

◆ waypoints_

◆ waypointsEventDispatcher

◆ waypointsNames_

std::vector<std::string> cl_nav2z::CpWaypointNavigatorBase::waypointsNames_
protected

The documentation for this class was generated from the following files: