lanelet2_map_server v1.2.3
Loading...
Searching...
No Matches
lanelet2_map_server.hpp
Go to the documentation of this file.
1// Copyright Institute for Automotive Engineering (ika), RWTH Aachen University
2// SPDX-License-Identifier: Apache-2.0
3
4#include "rclcpp/rclcpp.hpp"
5
6#include <fstream>
7#include <functional>
8#include <limits>
9#include <optional>
10#include <string>
11#include <tuple>
12#include <vector>
13
14#include <lanelet2_core/LaneletMap.h>
15#include <lanelet2_io/Exceptions.h>
16#include <lanelet2_io/Io.h>
17#include <lanelet2_io/Projection.h>
18#include <lanelet2_projection/UTM.h>
19
20#include "geometry_msgs/msg/transform_stamped.hpp"
21#include "sensor_msgs/msg/nav_sat_fix.hpp"
22#include "tf2_ros/static_transform_broadcaster.h"
23
25 std::string map_path;
26 double min_lat = 0.0;
27 double min_lon = 0.0;
28 double max_lat = 0.0;
29 double max_lon = 0.0;
30 double diagonal_length = -1.0;
31};
32
33template <typename C>
34struct is_vector : std::false_type {};
35template <typename T, typename A>
36struct is_vector<std::vector<T, A>> : std::true_type {};
37template <typename C>
38inline constexpr bool is_vector_v = is_vector<C>::value;
39
40class Lanelet2MapServer : public rclcpp::Node {
41 public:
46
47 private:
63 template <typename T>
64 bool declareAndLoadParameter(const std::string& name,
65 T& param,
66 const std::string& description,
67 const bool add_to_auto_reconfigurable_params = true,
68 const bool is_required = false,
69 const bool read_only = false,
70 const std::optional<double>& from_value = std::nullopt,
71 const std::optional<double>& to_value = std::nullopt,
72 const std::optional<double>& step_value = std::nullopt,
73 const std::string& additional_constraints = "");
74
81 rcl_interfaces::msg::SetParametersResult parametersCallback(const std::vector<rclcpp::Parameter>& parameters);
82
86 void loadMapContents();
87
91 void unsetMapParameters();
92
97
101 void setup();
102
109 void find_available_maps(const std::string& directory, std::vector<Lanelet2MapMeta>& maps) const;
110
116 void derive_map_meta(Lanelet2MapMeta& map_meta) const;
117
126 bool deriveOriginFromMap(const std::string& map_filepath, double& origin_lat, double& origin_lon) const;
127
136 bool map_sanity_check(std::string map_filepath, double origin_lat, double origin_lon) const;
137
141 void pub_tf() const;
142
151 static void derive_utm_zone(const double latitude, const double longitude, int& zone, bool& northp);
152
158 void navSatFixCallback(const sensor_msgs::msg::NavSatFix::SharedPtr msg);
159
164
168 std::vector<std::tuple<std::string, std::function<void(const rclcpp::Parameter&)>>> auto_reconfigurable_params_;
169
173 OnSetParametersCallbackHandle::SharedPtr parameters_callback_;
174
175 rclcpp::TimerBase::SharedPtr one_shot_timer_;
176 rclcpp::TimerBase::SharedPtr automatic_map_timer_;
177
179
180 std::string map_directory_ = "/data/maps/default-maps";
181 std::vector<Lanelet2MapMeta> available_maps_;
182 std::string map_filepath_;
183 std::string map_frame_id_ = "map";
184 std::string map_contents_;
187 bool origin_lat_set_ = false;
188 bool origin_lon_set_ = false;
189
190 std::shared_ptr<tf2_ros::StaticTransformBroadcaster> tf_static_broadcaster_;
191 rclcpp::Subscription<sensor_msgs::msg::NavSatFix>::SharedPtr navsat_subscription_;
192 double current_latitude_ = std::numeric_limits<double>::quiet_NaN();
193 double current_longitude_ = std::numeric_limits<double>::quiet_NaN();
194 bool gps_fix_received_ = false;
195};
void unsetMapParameters()
Clears map-specific parameters after an invalid or unloaded map state.
std::vector< Lanelet2MapMeta > available_maps_
void updateMapParameters()
Writes the current map state into this node's exported ROS parameters.
rclcpp::TimerBase::SharedPtr one_shot_timer_
rclcpp::TimerBase::SharedPtr automatic_map_timer_
bool declareAndLoadParameter(const std::string &name, T &param, const std::string &description, const bool add_to_auto_reconfigurable_params=true, const bool is_required=false, const bool read_only=false, const std::optional< double > &from_value=std::nullopt, const std::optional< double > &to_value=std::nullopt, const std::optional< double > &step_value=std::nullopt, const std::string &additional_constraints="")
Declares and loads a ROS parameter.
OnSetParametersCallbackHandle::SharedPtr parameters_callback_
Callback handle for dynamic parameter reconfiguration.
void setup()
Sets up timers, callbacks, publishers, and initial map loading.
void automaticMapUpdateTimerCallback()
Periodically reevaluates the best map when automatic selection is enabled.
std::shared_ptr< tf2_ros::StaticTransformBroadcaster > tf_static_broadcaster_
bool map_sanity_check(std::string map_filepath, double origin_lat, double origin_lon) const
Tries to load a map with the provided origin to verify that it is usable.
void derive_map_meta(Lanelet2MapMeta &map_meta) const
Fills the derived metadata fields for a discovered map.
Lanelet2MapServer()
Creates the Lanelet2 map server node and declares its parameters.
bool deriveOriginFromMap(const std::string &map_filepath, double &origin_lat, double &origin_lon) const
Derives a south-west origin latitude and longitude from a Lanelet2 map file.
rcl_interfaces::msg::SetParametersResult parametersCallback(const std::vector< rclcpp::Parameter > &parameters)
Handles reconfiguration when a parameter value is changed.
void loadMapContents()
Loads the configured map file and caches its serialized contents.
static void derive_utm_zone(const double latitude, const double longitude, int &zone, bool &northp)
Derives the UTM zone and hemisphere from a geographic position.
void navSatFixCallback(const sensor_msgs::msg::NavSatFix::SharedPtr msg)
Stores the latest GNSS fix for automatic map selection.
std::vector< std::tuple< std::string, std::function< void(const rclcpp::Parameter &)> > > auto_reconfigurable_params_
Auto-reconfigurable parameters for dynamic reconfiguration.
void find_available_maps(const std::string &directory, std::vector< Lanelet2MapMeta > &maps) const
Recursively scans a directory for available Lanelet2 map files.
void pub_tf() const
Publishes the static transform from the derived UTM frame to the map frame.
rclcpp::Subscription< sensor_msgs::msg::NavSatFix >::SharedPtr navsat_subscription_
constexpr bool is_vector_v