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 ()
 
virtual ~CpWaypointNavigatorBase ()
 
void onInitialize () override
 
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
void loadWayPointsFromFile (std::string filepath)
 
void 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)
 
void seekName (std::string name)
 
void loadWaypointsFromYamlParameter (std::string parameter_name, std::string yaml_file_package_name)
 
void notifyGoalReached ()
 
- Public Member Functions inherited from smacc2::ISmaccComponent
 ISmaccComponent ()
 
virtual ~ISmaccComponent ()
 
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 (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 (TArgs... targs)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createSiblingNamedComponent (std::string name, TArgs... targs)
 
rclcpp::Node::SharedPtr getNode ()
 
rclcpp::Logger getLogger () const
 
ISmaccStateMachinegetStateMachine ()
 

Protected Attributes

std::vector< geometry_msgs::msg::Pose > waypoints_
 
std::vector< std::string > waypointsNames_
 
- Protected Attributes inherited from smacc2::ISmaccComponent
ISmaccStateMachinestateMachine_
 
ISmaccClientowner_
 

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 ( )
virtual

Definition at line 42 of file cp_waypoints_navigator.cpp.

42{}

Member Function Documentation

◆ forward()

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

Definition at line 90 of file cp_waypoints_navigator.cpp.

91{
93 if (currentWaypoint_ >= (long)waypoints_.size() - 1)
94 currentWaypoint_ = (long)waypoints_.size() - 1;
95}

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 392 of file cp_waypoints_navigator.cpp.

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

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 426 of file cp_waypoints_navigator.cpp.

427{
428 if (currentWaypoint_ >= 0 && currentWaypoint_ < (int)waypointsNames_.size())
429 {
431 }
432 return std::nullopt;
433}

References currentWaypoint_, and waypointsNames_.

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

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 404 of file cp_waypoints_navigator.cpp.

406{
407 if (this->waypointsNames_.size() > 0)
408 {
409 for (int i = 0; i < (int)this->waypointsNames_.size(); i++)
410 {
411 if (this->waypointsNames_[i] == name)
412 {
413 return this->waypoints_[i];
414 }
415 }
416 }
417
418 return std::nullopt;
419}

References waypoints_, and waypointsNames_.

◆ getPose()

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

Definition at line 381 of file cp_waypoints_navigator.cpp.

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

References waypoints_.

◆ getWaypointNames()

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

Definition at line 421 of file cp_waypoints_navigator.cpp.

422{
423 return waypointsNames_;
424}

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 376 of file cp_waypoints_navigator.cpp.

377{
378 return waypoints_;
379}

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 335 of file cp_waypoints_navigator.cpp.

336{
337 if (index >= 0 && index <= (int)waypoints_.size())
338 {
339 waypoints_.insert(waypoints_.begin(), index, newpose);
340 }
341}

References waypoints_.

◆ loadWayPointsFromFile()

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

Definition at line 438 of file cp_waypoints_navigator.cpp.

439{
440 RCLCPP_INFO_STREAM(getLogger(), "[CpWaypointNavigatorBase] Loading file:" << filepath);
441 this->waypoints_.clear();
442 std::ifstream ifs(filepath.c_str(), std::ifstream::in);
443 if (ifs.good() == false)
444 {
445 throw std::string("Waypoints file not found");
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 : NULL;
460#else
461 const YAML::Node * wp_node = node.FindValue("waypoints");
462#endif
463
464 if (wp_node != NULL)
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 if (wpnodei["name"].IsDefined())
483 {
484 this->waypointsNames_.push_back(wpnodei["name"].as<std::string>());
485 }
486
487 this->waypoints_.push_back(wp);
488 }
489 catch (...)
490 {
491 RCLCPP_ERROR(getLogger(), "parsing waypoint file, syntax error in point %ld", i);
492 }
493 }
494 RCLCPP_INFO_STREAM(getLogger(), "Parsed " << this->waypoints_.size() << " waypoints.");
495 }
496 else
497 {
498 RCLCPP_WARN_STREAM(getLogger(), "Couldn't find any waypoints in the provided yaml file.");
499 }
500 }
501 catch (const YAML::ParserException & ex)
502 {
503 RCLCPP_ERROR_STREAM(
504 getLogger(), "Error loading the Waypoints YAML file. Incorrect syntax: " << ex.what());
505 }
506}
rclcpp::Logger getLogger() const

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

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()

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

Definition at line 508 of file cp_waypoints_navigator.cpp.

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

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

Here is the call graph for this function:

◆ loadWaypointsFromYamlParameter()

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

Definition at line 160 of file cp_waypoints_navigator.cpp.

162{
163 // if it is the first time and the waypoints navigator is not configured
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))
168 {
169 std::string package_share_directory =
170 ament_index_cpp::get_package_share_directory(yaml_file_package_name);
171
172 RCLCPP_INFO(getLogger(), "file macro path: %s", planfilepath.c_str());
173
174 boost::replace_all(planfilepath, "$(pkg_share)", package_share_directory);
175
176 RCLCPP_INFO(getLogger(), "package share path: %s", package_share_directory.c_str());
177 RCLCPP_INFO(getLogger(), "waypoints plan file: %s", planfilepath.c_str());
178
179 this->loadWayPointsFromFile(planfilepath);
180 RCLCPP_INFO(getLogger(), "waypoints plan: %s", planfilepath.c_str());
181 }
182 else
183 {
184 RCLCPP_ERROR(getLogger(), "waypoints plan file not found: NONE");
185 }
186}
void 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 304 of file cp_waypoints_navigator.cpp.

305{
306 // when it is the last waypoint post an finalization EOF event
307 if (currentWaypoint_ == (long)waypoints_.size() - 1)
308 {
309 RCLCPP_WARN(getLogger(), "[CpWaypointNavigator] Last waypoint reached, posting EOF event. ");
311 }
312}

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 46 of file cp_waypoints_navigator.cpp.

46{}

◆ 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:88
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 368 of file cp_waypoints_navigator.cpp.

369{
370 if (index >= 0 && index < (int)waypoints_.size())
371 {
372 waypoints_.erase(waypoints_.begin() + index);
373 }
374}

References waypoints_.

◆ rewind()

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

Definition at line 84 of file cp_waypoints_navigator.cpp.

85{
88}

References currentWaypoint_.

◆ seekName()

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

Definition at line 97 of file cp_waypoints_navigator.cpp.

98{
99 bool found = false;
100
101 auto previousWaypoint = currentWaypoint_;
102
103 while (!found && currentWaypoint_ < (long)waypoints_.size())
104 {
105 auto & nextName = waypointsNames_[currentWaypoint_];
106 RCLCPP_INFO(
107 getLogger(), "[CpWaypointNavigator] seeking ,%ld/%ld candidate waypoint: %s",
108 currentWaypoint_, waypoints_.size(), nextName.c_str());
109 if (name == nextName)
110 {
111 found = true;
112 RCLCPP_INFO(
113 getLogger(), "[CpWaypointNavigator] found target waypoint: %s == %s-> found",
114 nextName.c_str(), name.c_str());
115 }
116 else
117 {
118 RCLCPP_INFO(
119 getLogger(), "[CpWaypointNavigator] current waypoint: %s != %s -> forward",
120 nextName.c_str(), name.c_str());
122 }
123 }
124
125 if (found)
126 {
127 if (currentWaypoint_ >= (long)waypoints_.size() - 1)
128 currentWaypoint_ = (long)waypoints_.size() - 1;
129 }
130 else // search backwards
131 {
132 currentWaypoint_ = previousWaypoint;
133 while (!found && currentWaypoint_ > 0)
134 {
135 auto & nextName = waypointsNames_[currentWaypoint_];
136 RCLCPP_INFO(
137 getLogger(), "[CpWaypointNavigator] seeking , candidate waypoint: %s", nextName.c_str());
138 if (name == nextName)
139 {
140 found = true;
141 RCLCPP_INFO(
142 getLogger(), "[CpWaypointNavigator] found target waypoint: %s == %s-> found",
143 nextName.c_str(), name.c_str());
144 }
145 else
146 {
147 RCLCPP_INFO(
148 getLogger(), "[CpWaypointNavigator] current waypoint: %s != %s -> rewind",
149 nextName.c_str(), name.c_str());
151 }
152 }
153 }
154
155 RCLCPP_INFO(
156 getLogger(), "[CpWaypointNavigator] seekName( %s), previous index: %ld, after index: %ld",
157 name.c_str(), previousWaypoint, currentWaypoint_);
158}

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 343 of file cp_waypoints_navigator.cpp.

344{
345 this->waypoints_ = waypoints;
346}

References waypoints_.

◆ setWaypoints() [2/2]

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

Definition at line 348 of file cp_waypoints_navigator.cpp.

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

References waypoints_, and waypointsNames_.

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: