SMACC2
Loading...
Searching...
No Matches
cp_odom_tracker.hpp
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#pragma once
21
22#include <tf2/transform_datatypes.h>
23#include <smacc2/common.hpp>
24#include <smacc2/component.hpp>
25
26#include <rclcpp/rclcpp.hpp>
27
28#include <memory>
29#include <mutex>
30#include <vector>
31
32#include <geometry_msgs/msg/point.hpp>
33#include <geometry_msgs/msg/pose.hpp>
34#include <nav2_msgs/action/navigate_to_pose.hpp>
35#include <nav_msgs/msg/odometry.hpp>
36#include <nav_msgs/msg/path.hpp>
37
39#include <std_msgs/msg/header.hpp>
40
41namespace cl_nav2z
42{
43namespace odom_tracker
44{
45enum class WorkingMode : uint8_t
46{
47 RECORD_PATH = 0,
48 CLEAR_PATH = 1,
49 IDLE = 2
50};
51
57
61{
62public:
63 // by default, the component start in record_path mode and publishing the
64 // current path
66 std::string odomtopicName = "/odom", std::string odomFrame = "odom",
68
69 // threadsafe
71 // The odom parameters is the main input of this tracker
72 virtual void processNewPose(const geometry_msgs::msg::PoseStamped & odom);
73
74 virtual void odomMessageCallback(const nav_msgs::msg::Odometry::SharedPtr odom);
75
76 virtual void update();
77
78 // ------ CONTROL COMMANDS ---------------------
79 // threadsafe
80 void setWorkingMode(WorkingMode workingMode);
81
82 // threadsafe
83 void setPublishMessages(bool value);
84
85 // threadsafe
86 void pushPath();
87
88 // threadsafe
89 void pushPath(std::string pathname);
90
91 // threadsafe
92 void popPath(int pathCount = 1, bool keepPreviousPath = false);
93
94 // threadsafe
95 void clearPath();
96
97 // threadsafe
98 void setStartPoint(const geometry_msgs::msg::PoseStamped & pose);
99
100 // threadsafe
101 void setStartPoint(const geometry_msgs::msg::Pose & pose);
102
103 // threadsafe - use only for recording mode, it is reset always a new path is pushed, popped or cleared
104 void setCurrentMotionGoal(const geometry_msgs::msg::PoseStamped & pose);
105
106 void setCurrentPathName(const std::string & currentPathName);
107
108 // threadsafe
109 std::optional<geometry_msgs::msg::PoseStamped> getCurrentMotionGoal();
110
111 // threadsafe
112 nav_msgs::msg::Path getPath();
113
114 // threadsafe: takes m_mutex_ like every other public method (study, F-4)
115 void logStateString(bool debug = true);
116
117 // parameters update callback
119
120 inline void setOdomFrame(std::string odomFrame)
121 {
122 odomFrame_ = odomFrame;
123 getNode()->set_parameter(rclcpp::Parameter("odom_frame", odomFrame));
124 }
125
126protected:
127 void onInitialize() override;
128
129 // the body of logStateString; the caller must hold m_mutex_
130 void logStateStringLocked(bool debug = true);
131
132 virtual void rtPublishPaths(rclcpp::Time timestamp);
133
134 // this is called when a new odom message is received in record path mode
135 virtual bool updateRecordPath(const geometry_msgs::msg::PoseStamped & odom);
136
137 // this is called when a new odom message is received in clear path mode
138 virtual bool updateClearPath(const geometry_msgs::msg::PoseStamped & odom);
139
141
142 // -------------- OUTPUTS ---------------------
143 rclcpp::Publisher<nav_msgs::msg::Path>::SharedPtr robotBasePathPub_;
144 rclcpp::Publisher<nav_msgs::msg::Path>::SharedPtr robotBasePathStackedPub_;
145
146 // --------------- INPUTS ------------------------
147 // optional, this class can be used directly calling the odomProcessing method
148 // without any subscriber
149 rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr odomSub_;
150
152 rclcpp::TimerBase::SharedPtr robotPoseTimer_;
153
154 // -------------- PARAMETERS ----------------------
157
160
163
166
167 std::string odomFrame_;
168
169 std::string odomTopicName_;
170
171 // --------------- STATE ---------------
172 // default true
174
176 nav_msgs::msg::Path baseTrajectory_;
177
179
180 std::vector<nav_msgs::msg::Path> pathStack_;
181
182 struct PathInfo
183 {
184 std::string name;
185 std::optional<geometry_msgs::msg::PoseStamped> goalPose;
186 };
187
188 std::vector<PathInfo> pathInfos_;
189
190 nav_msgs::msg::Path aggregatedStackPathMsg_;
191
193
194 std::optional<geometry_msgs::msg::PoseStamped> currentMotionGoal_;
195 std::string currentPathName_;
196
197 rcl_interfaces::msg::SetParametersResult parametersCallback(
198 const std::vector<rclcpp::Parameter> & parameters);
199
200 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr param_callback_handle_;
201
202 std::mutex m_mutex_;
203};
204
210inline double p2pDistance(
211 const geometry_msgs::msg::Point & p1, const geometry_msgs::msg::Point & p2)
212{
213 double dx = (p1.x - p2.x);
214 double dy = (p1.y - p2.y);
215 double dz = (p2.z - p2.z);
216 double dist = sqrt(dx * dx + dy * dy + dz * dz);
217 return dist;
218}
219} // namespace odom_tracker
220} // namespace cl_nav2z
void popPath(int pathCount=1, bool keepPreviousPath=false)
virtual bool updateRecordPath(const geometry_msgs::msg::PoseStamped &odom)
rclcpp::Subscription< nav_msgs::msg::Odometry >::SharedPtr odomSub_
void setOdomFrame(std::string odomFrame)
std::optional< geometry_msgs::msg::PoseStamped > currentMotionGoal_
virtual void processNewPose(const geometry_msgs::msg::PoseStamped &odom)
odom callback: Updates the path - this must be called periodically for each odometry message.
double recordPointDistanceThreshold_
How much distance there is between two points of the path.
nav_msgs::msg::Path baseTrajectory_
Processed path for the mouth of the reel.
virtual void rtPublishPaths(rclcpp::Time timestamp)
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr param_callback_handle_
void setCurrentPathName(const std::string &currentPathName)
virtual void odomMessageCallback(const nav_msgs::msg::Odometry::SharedPtr odom)
rclcpp::TimerBase::SharedPtr robotPoseTimer_
rclcpp::Publisher< nav_msgs::msg::Path >::SharedPtr robotBasePathStackedPub_
void setCurrentMotionGoal(const geometry_msgs::msg::PoseStamped &pose)
virtual bool updateClearPath(const geometry_msgs::msg::PoseStamped &odom)
std::vector< nav_msgs::msg::Path > pathStack_
void setWorkingMode(WorkingMode workingMode)
rclcpp::Publisher< nav_msgs::msg::Path >::SharedPtr robotBasePathPub_
rcl_interfaces::msg::SetParametersResult parametersCallback(const std::vector< rclcpp::Parameter > &parameters)
std::optional< geometry_msgs::msg::PoseStamped > getCurrentMotionGoal()
void setStartPoint(const geometry_msgs::msg::PoseStamped &pose)
CpOdomTracker(std::string odomtopicName="/odom", std::string odomFrame="odom", OdomTrackerStrategy strategy=OdomTrackerStrategy::ODOMETRY_SUBSCRIBER)
rclcpp::Node::SharedPtr getNode()
double p2pDistance(const geometry_msgs::msg::Point &p1, const geometry_msgs::msg::Point &p2)
std::optional< geometry_msgs::msg::PoseStamped > goalPose