Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
LocalizationManager.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 LEIDOS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17#include <rclcpp/rclcpp.hpp>
18#include <boost/shared_ptr.hpp>
19#include <geometry_msgs/msg/pose_stamped.hpp>
20#include <geometry_msgs/msg/pose_with_covariance_stamped.hpp>
21#include <boost/optional.hpp>
22#include <autoware_msgs/msg/ndt_stat.hpp>
23#include <tf2/LinearMath/Vector3.h>
24#include <carma_msgs/msg/system_alert.hpp>
25#include <carma_localization_msgs/msg/localization_status_report.hpp>
26#include <functional>
27#include <unordered_set>
28#include <carma_ros2_utils/timers/Timer.hpp>
29#include <carma_ros2_utils/timers/TimerFactory.hpp>
30#include <carma_ros2_utils/timers/ROSTimerFactory.hpp>
34
36{
41 {
42
43 public:
44 using PosePublisher = std::function<void(const geometry_msgs::msg::PoseStamped &)>;
45 using ManagedInitialPosePublisher = std::function<void(const geometry_msgs::msg::PoseWithCovarianceStamped &)>;
46 using StatePublisher = std::function<void(const carma_localization_msgs::msg::LocalizationStatusReport &)>;
47 using TimerUniquePtr = std::unique_ptr<carma_ros2_utils::timers::Timer>;
48
60 ManagedInitialPosePublisher initialpose_pub,
61 const LocalizationManagerConfig &config,
62 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger,
63 std::unique_ptr<carma_ros2_utils::timers::TimerFactory> timer_factory);
64
70 void gnssPoseCallback(const geometry_msgs::msg::PoseStamped::SharedPtr msg);
71
78 void poseAndStatsCallback(const geometry_msgs::msg::PoseStamped::ConstPtr pose,
79 const autoware_msgs::msg::NDTStat::ConstPtr stats);
80
86 void systemAlertCallback(const carma_msgs::msg::SystemAlert::SharedPtr alert);
87
93 void initialPoseCallback(const geometry_msgs::msg::PoseWithCovarianceStamped::SharedPtr msg);
94
99 void posePubTick();
100
109
115 void timerCallback(const LocalizationState origin_state);
116
123
127 void clearTimers();
128
134 void setConfig(const LocalizationManagerConfig& config);
135
136 private:
138 static const std::unordered_set<std::string> LIDAR_FAILURE_STRINGS; // Static const container defined in cpp file
139
143
145
147
148 boost::optional<rclcpp::Time> prev_ndt_stamp_;
149
151 bool is_sequential_ = false;
152 boost::optional<geometry_msgs::msg::PoseStamped> last_raw_gnss_value_;
153 boost::optional<tf2::Vector3> gnss_offset_;
154 boost::optional<geometry_msgs::msg::PoseStamped> current_pose_;
155
156 // Logger interface
157 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_;
158
159 // Using timer factory
160 std::mutex mutex_;
161 std::unique_ptr<carma_ros2_utils::timers::TimerFactory> timer_factory_;
162 std::unordered_map<uint32_t, std::pair<TimerUniquePtr, bool>> timers_;
164 uint32_t next_id_ = 0; // Timer id counter
166 rcl_clock_type_t timer_clock_type_ = RCL_SYSTEM_TIME;
167
175 double computeNDTFreq(const rclcpp::Time &new_stamp);
176
185 double computeFreq(const rclcpp::Time &old_stamp, const rclcpp::Time &new_stamp) const;
186
192 uint32_t nextId();
193 };
194}
Primary logic class for the localization manager node.
void poseAndStatsCallback(const geometry_msgs::msg::PoseStamped::ConstPtr pose, const autoware_msgs::msg::NDTStat::ConstPtr stats)
Synced callback for the NDT reported pose and status messages.
void posePubTick()
Timer callback that controls the publication of the selected pose and status report.
void timerCallback(const LocalizationState origin_state)
Callback for timeouts. Used to trigger timeout signals for the state machine.
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger_
void gnssPoseCallback(const geometry_msgs::msg::PoseStamped::SharedPtr msg)
Callback for new GNSS messages.
uint32_t nextId()
Generates the next id to be used for a timer.
void stateTransitionCallback(LocalizationState prev_state, LocalizationState new_state, LocalizationSignal signal)
Callback for when a new state was triggered. Allows creation of entry/exit behavior for states.
void initialPoseCallback(const geometry_msgs::msg::PoseWithCovarianceStamped::SharedPtr msg)
Callback for the initial pose provided by Rviz or some external localization initializer.
boost::optional< geometry_msgs::msg::PoseStamped > last_raw_gnss_value_
std::unique_ptr< carma_ros2_utils::timers::TimerFactory > timer_factory_
double computeFreq(const rclcpp::Time &old_stamp, const rclcpp::Time &new_stamp) const
Helper function to compute the instantaneous frequency between two times.
std::function< void(const carma_localization_msgs::msg::LocalizationStatusReport &)> StatePublisher
void clearTimers()
Clears the expired timers from the memory of this scheduler.
static const std::unordered_set< std::string > LIDAR_FAILURE_STRINGS
The set of strings which mark a lidar failure in a system alert message.
void systemAlertCallback(const carma_msgs::msg::SystemAlert::SharedPtr alert)
Callback for SystemAlert data. Used to check for lidar failure.
std::unique_ptr< carma_ros2_utils::timers::Timer > TimerUniquePtr
boost::optional< tf2::Vector3 > gnss_offset_
LocalizationState getState() const
Returns the current localization state.
boost::optional< rclcpp::Time > prev_ndt_stamp_
boost::optional< geometry_msgs::msg::PoseStamped > current_pose_
std::function< void(const geometry_msgs::msg::PoseStamped &)> PosePublisher
LocalizationManager(PosePublisher pose_pub, StatePublisher state_pub, ManagedInitialPosePublisher initialpose_pub, const LocalizationManagerConfig &config, rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr logger, std::unique_ptr< carma_ros2_utils::timers::TimerFactory > timer_factory)
Constructor.
double computeNDTFreq(const rclcpp::Time &new_stamp)
Helper function to both compute the NDT Frequency and update the previous pose timestamp.
void setConfig(const LocalizationManagerConfig &config)
Set config.
std::unordered_map< uint32_t, std::pair< TimerUniquePtr, bool > > timers_
std::function< void(const geometry_msgs::msg::PoseWithCovarianceStamped &)> ManagedInitialPosePublisher
Class defining the state transition table behavior for the LocalizationManager.
LocalizationState
Enum describing the possible states of the localization system.
LocalizationSignal
Enum describing the possible signals to change the current LocalizationState.
Struct to store the configuration settings for the LocalizationManager class.