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

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

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

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

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

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

References waypoints_, and waypointsNames_.

◆ getPose()

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

Definition at line 380 of file cp_waypoints_navigator.cpp.

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

References waypoints_.

◆ getWaypointNames()

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

Definition at line 420 of file cp_waypoints_navigator.cpp.

421{
422 return waypointsNames_;
423}

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

376{
377 return waypoints_;
378}

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

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

References waypoints_.

◆ loadWayPointsFromFile()

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

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

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

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

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

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

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

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

References waypoints_.

◆ setWaypoints() [2/2]

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

Definition at line 347 of file cp_waypoints_navigator.cpp.

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

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: