SMACC2
Loading...
Searching...
No Matches
cb_navigate_global_position.cpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20#include <cl_nav2z/common.hpp>
21
27
28namespace cl_nav2z
29{
30using namespace ::cl_nav2z::odom_tracker;
31using namespace smacc2;
32
34
36 float x, float y, float yaw, std::optional<CbNavigateGlobalPositionOptions> options)
37{
38 auto p = geometry_msgs::msg::Point();
39 p.x = x;
40 p.y = y;
41 goalPosition = p;
42 goalYaw = yaw;
43
44 if (options) this->options = *options;
45}
46
47void CbNavigateGlobalPosition::setGoal(const geometry_msgs::msg::Pose & pose)
48{
49 goalPosition = pose.position;
50 goalYaw = tf2::getYaw(pose.orientation);
51}
52
54{
55 RCLCPP_INFO(getLogger(), "Entering Navigate Global position");
56 RCLCPP_INFO(getLogger(), "Component requirements completed");
57
58 cl_nav2z::CpPose * cpPose;
59 this->requiresComponent(cpPose, ComponentRequirement::HARD);
60
61 CpOdomTracker * odomTracker;
62 this->requiresComponent(odomTracker, ComponentRequirement::HARD);
63
64 CpPlannerSwitcher * plannerSwitcher;
65 this->requiresComponent(plannerSwitcher, ComponentRequirement::HARD);
66
67 plannerSwitcher->setDefaultPlanners(false);
68
70 {
71 plannerSwitcher->setDesiredController(*(options.controllerName_));
72 }
73
74 plannerSwitcher->commitPublish();
75
76 CpGoalCheckerSwitcher * goalCheckerSwitcher;
77 this->requiresComponent(goalCheckerSwitcher, ComponentRequirement::HARD);
78 goalCheckerSwitcher->setGoalCheckerId("goal_checker");
79
80 auto pathname = this->getCurrentState()->getName() + " - " + getName();
81 odomTracker->pushPath(pathname);
82 // Pass the STAMPED pose: CpPose poses are in the map frame, and the bare-Pose
83 // setStartPoint overload would mislabel the coordinates with the odom frame -
84 // producing a recorded trail whose start point (the future undo goal) is
85 // displaced by the whole map->odom offset
86 odomTracker->setStartPoint(cpPose->toPoseStampedMsg());
87 odomTracker->setWorkingMode(WorkingMode::RECORD_PATH);
88
89 execute();
90}
91
92// auxiliary function that defines the motion that is requested to the nav2 action server
94{
96 this->requiresComponent(p, ComponentRequirement::HARD);
97
98 auto referenceFrame = p->getReferenceFrame();
99
100 RCLCPP_INFO(getLogger(), "Sending Goal to MoveBase");
101 nav2_msgs::action::NavigateToPose::Goal goal;
102 goal.pose.header.frame_id = referenceFrame;
103
104 goal.pose.pose.position = goalPosition;
105 tf2::Quaternion q;
106 q.setRPY(0, 0, goalYaw);
107 goal.pose.pose.orientation = tf2::toMsg(q);
108
109 this->sendGoal(goal);
110}
111
112// This is the substate destructor. This code will be executed when the
113// workflow exits from this substate (that is according to statechart the moment when this object is destroyed)
115{
116 RCLCPP_INFO(getLogger(), "Exiting move goal Action Client");
117}
118
119} // namespace cl_nav2z
void setGoal(const geometry_msgs::msg::Pose &pose)
void setGoalCheckerId(std::string goal_checker_id)
void setDefaultPlanners(bool commit=true)
geometry_msgs::msg::PoseStamped toPoseStampedMsg()
Definition cp_pose.hpp:63
const std::string & getReferenceFrame() const
Definition cp_pose.hpp:79
void setWorkingMode(WorkingMode workingMode)
void setStartPoint(const geometry_msgs::msg::PoseStamped &pose)
virtual rclcpp::Logger getLogger() const
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
virtual std::string getName()=0