SMACC2
Loading...
Searching...
No Matches
cp_odom_tracker.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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 void logStateString(bool debug = true);
115
116 // parameters update callback
118
119 inline void setOdomFrame(std::string odomFrame)
120 {
121 odomFrame_ = odomFrame;
122 getNode()->set_parameter(rclcpp::Parameter("odom_frame", odomFrame));
123 }
124
125protected:
126 void onInitialize() override;
127
128 void updateConfiguration();
129
130 virtual void rtPublishPaths(rclcpp::Time timestamp);
131
132 // this is called when a new odom message is received in record path mode
133 virtual bool updateRecordPath(const geometry_msgs::msg::PoseStamped & odom);
134
135 // this is called when a new odom message is received in clear path mode
136 virtual bool updateClearPath(const geometry_msgs::msg::PoseStamped & odom);
137
139
140 // -------------- OUTPUTS ---------------------
141 rclcpp::Publisher<nav_msgs::msg::Path>::SharedPtr robotBasePathPub_;
142 rclcpp::Publisher<nav_msgs::msg::Path>::SharedPtr robotBasePathStackedPub_;
143
144 // --------------- INPUTS ------------------------
145 // optional, this class can be used directly calling the odomProcessing method
146 // without any subscriber
147 rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr odomSub_;
148
150 rclcpp::TimerBase::SharedPtr robotPoseTimer_;
151
152 // -------------- PARAMETERS ----------------------
155
158
161
164
165 std::string odomFrame_;
166
167 std::string odomTopicName_;
168
169 // --------------- STATE ---------------
170 // default true
172
174 nav_msgs::msg::Path baseTrajectory_;
175
177
178 std::vector<nav_msgs::msg::Path> pathStack_;
179
180 struct PathInfo
181 {
182 std::string name;
183 std::optional<geometry_msgs::msg::PoseStamped> goalPose;
184 };
185
186 std::vector<PathInfo> pathInfos_;
187
188 nav_msgs::msg::Path aggregatedStackPathMsg_;
189
191
192 std::optional<geometry_msgs::msg::PoseStamped> currentMotionGoal_;
193 std::string currentPathName_;
194
195 rcl_interfaces::msg::SetParametersResult parametersCallback(
196 const std::vector<rclcpp::Parameter> & parameters);
197
198 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr param_callback_handle_;
199
200 std::mutex m_mutex_;
201};
202
208inline double p2pDistance(
209 const geometry_msgs::msg::Point & p1, const geometry_msgs::msg::Point & p2)
210{
211 double dx = (p1.x - p2.x);
212 double dy = (p1.y - p2.y);
213 double dz = (p2.z - p2.z);
214 double dist = sqrt(dx * dx + dy * dy + dz * dz);
215 return dist;
216}
217} // namespace odom_tracker
218} // 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