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.
CARMAWorldModel.hpp
Go to the documentation of this file.
1#pragma once
2
3/*
4 * Copyright (C) 2022 LEIDOS.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7 * use this file except in compliance with the License. You may obtain a copy of
8 * the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 * License for the specific language governing permissions and limitations under
16 * the License.
17 */
18#include <rclcpp/rclcpp.hpp>
20#include <lanelet2_extension/traffic_rules/CarmaUSTrafficRules.h>
21#include <lanelet2_core/primitives/BasicRegulatoryElements.h>
22#include <lanelet2_core/primitives/LineString.h>
24#include <carma_perception_msgs/msg/roadway_obstacle.hpp>
25#include <carma_perception_msgs/msg/roadway_obstacle_list.hpp>
26#include <carma_perception_msgs/msg/external_object.hpp>
27#include <carma_perception_msgs/msg/external_object_list.hpp>
28#include <carma_v2x_msgs/msg/spat.hpp>
29#include "carma_wm/TrackPos.hpp"
31#include <lanelet2_extension/time/TimeConversion.h>
32#include "boost/date_time/posix_time/posix_time.hpp"
34#include <rosgraph_msgs/msg/clock.hpp>
35#include <gtest/gtest_prod.h>
36namespace carma_wm
37{
49{
50public:
55 CARMAWorldModel() = default;
56
61 ~CARMAWorldModel() = default;
62
69 void setMap(lanelet::LaneletMapPtr map, size_t map_version = 0, bool recompute_routing_graph = true);
70
84
90
95 void setTrafficLightIds(uint32_t id, lanelet::Id lanelet_id);
96
101 lanelet::LaneletMapPtr getMutableMap() const;
102
107 void setRoadwayObjects(const std::vector<carma_perception_msgs::msg::RoadwayObstacle>& rw_objs);
108
116 void processSpatFromMsg(const carma_v2x_msgs::msg::SPAT& spat_msg, bool use_sim_time = false, bool is_spat_wall_time=true);
117
133 lanelet::Optional<std::tuple<TrackPos,carma_perception_msgs::msg::RoadwayObstacle>> getNearestObjInLane(const lanelet::BasicPoint2d& object_center, const LaneSection& section = LANE_AHEAD) const;
134
141 boost::posix_time::ptime min_end_time_converter_minute_of_year(boost::posix_time::ptime min_end_time,bool moy_exists,uint32_t moy=0, bool is_simulation = true, bool is_spat_wall_time=false);
142
147 void setConfigSpeedLimit(double config_lim);
148
151 void setVehicleParticipationType(const std::string& participant);
152
155 std::string getVehicleParticipationType();
156
159 void setRouteEndPoint(const lanelet::BasicPoint3d& end_point);
160
163 void setRouteName(const std::string& route_name);
164
167 void setRos1Clock(const rclcpp::Time& time_now);
168
171 void setSimulationClock(const rclcpp::Time& time_now);
172
175 lanelet::Id getTrafficSignalId(uint16_t intersection_id,uint8_t signal_id);
176
179 lanelet::CarmaTrafficSignalPtr getTrafficSignal(const lanelet::Id& id) const;
180
190 std::vector<lanelet::Lanelet> getLaneletsFromPoint(const lanelet::BasicPoint2d& point, const unsigned int n);
191
205 std::vector<lanelet::Lanelet> nonConnectedAdjacentLeft(const lanelet::BasicPoint2d& input_point, const unsigned int n = 10);
206
208 // Overrides
210 std::pair<TrackPos, TrackPos> routeTrackPos(const lanelet::ConstArea& area) const override;
211
212 TrackPos routeTrackPos(const lanelet::ConstLanelet& lanelet) const override;
213
214 TrackPos routeTrackPos(const lanelet::BasicPoint2d& point) const override;
215
216 std::vector<lanelet::ConstLanelet> getLaneletsBetween(double start, double end, bool shortest_path_only = false, bool bounds_inclusive = true) const override;
217
218 std::vector<lanelet::BasicPoint2d> sampleRoutePoints(double start_downtrack, double end_downtrack, double step_size) const override;
219
220 boost::optional<lanelet::BasicPoint2d> pointFromRouteTrackPos(const TrackPos& route_pos) const override;
221
222 lanelet::LaneletMapConstPtr getMap() const override;
223
224 LaneletRouteConstPtr getRoute() const override;
225
226 std::string getRouteName() const override;
227
228 TrackPos getRouteEndTrackPos() const override;
229
231
232 lanelet::Optional<TrafficRulesConstPtr>
233 getTrafficRules(const std::string& participant) const override;
234
235 lanelet::Optional<TrafficRulesConstPtr>
236 getTrafficRules() const override;
237
238 std::vector<carma_perception_msgs::msg::RoadwayObstacle> getRoadwayObjects() const override;
239
240 std::vector<carma_perception_msgs::msg::RoadwayObstacle> getInLaneObjects(const lanelet::ConstLanelet& lanelet, const LaneSection& section = LANE_AHEAD) const override;
241
242 lanelet::Optional<lanelet::Lanelet> getIntersectingLanelet (const carma_perception_msgs::msg::ExternalObject& object) const override;
243
244 lanelet::Optional<carma_perception_msgs::msg::RoadwayObstacle> toRoadwayObstacle(const carma_perception_msgs::msg::ExternalObject& object) const override;
245
246 lanelet::Optional<double> distToNearestObjInLane(const lanelet::BasicPoint2d& object_center) const override;
247
248 lanelet::Optional<std::tuple<TrackPos,carma_perception_msgs::msg::RoadwayObstacle>> nearestObjectAheadInLane(const lanelet::BasicPoint2d& object_center) const override;
249
250 lanelet::Optional<std::tuple<TrackPos,carma_perception_msgs::msg::RoadwayObstacle>> nearestObjectBehindInLane(const lanelet::BasicPoint2d& object_center) const override;
251
252 std::vector<lanelet::ConstLanelet> getLane(const lanelet::ConstLanelet& lanelet, const LaneSection& section = LANE_AHEAD) const override;
253
254 size_t getMapVersion() const override;
255
256 std::vector<lanelet::ConstLanelet> getLaneletsFromPoint(const lanelet::BasicPoint2d& point, const unsigned int n = 10) const override;
257
258 std::vector<lanelet::ConstLanelet> nonConnectedAdjacentLeft(const lanelet::BasicPoint2d& input_point, const unsigned int n = 10) const override;
259
260 std::vector<lanelet::CarmaTrafficSignalPtr> getSignalsAlongRoute(const lanelet::BasicPoint2d& loc) const override;
261
262 std::vector<lanelet::BusStopRulePtr> getBusStopsAlongRoute(const lanelet::BasicPoint2d& loc) const override;
263
264 boost::optional<std::pair<lanelet::ConstLanelet, lanelet::ConstLanelet>> getEntryExitOfSignalAlongRoute(const lanelet::CarmaTrafficSignalPtr& traffic_signal) const override;
265
266 std::vector<std::shared_ptr<lanelet::AllWayStop>> getIntersectionsAlongRoute(const lanelet::BasicPoint2d& loc) const override;
267
268 std::vector<lanelet::SignalizedIntersectionPtr> getSignalizedIntersectionsAlongRoute(const lanelet::BasicPoint2d &loc) const;
269
270 std::optional<lanelet::ConstLanelet> getFirstLaneletOnShortestPath(const std::vector<lanelet::ConstLanelet>& lanelets_to_filter) const;
271
272 std::unordered_map<uint32_t, lanelet::Id> traffic_light_ids_;
273
274 carma_wm::SignalizedIntersectionManager sim_; // records SPAT/MAP lane ids to lanelet ids
275
276private:
277
279
280 std::string participant_type_ = lanelet::Participants::Vehicle;
281
290
298 lanelet::LineString3d copyConstructLineString(const lanelet::ConstLineString3d& line) const;
299
300 std::optional<rclcpp::Time> ros1_clock_ = std::nullopt;
301 std::optional<rclcpp::Time> simulation_clock_ = std::nullopt;
302
303 std::shared_ptr<lanelet::LaneletMap> semantic_map_;
306 double route_length_ = 0;
307 lanelet::LaneletSubmapConstUPtr shortest_path_view_; // Map containing only lanelets along the shortest path of the
308 // route
309 std::vector<lanelet::LineString3d> shortest_path_centerlines_; // List of disjoint centerlines seperated by lane
310 // changes along the shortest path
312 lanelet::LaneletMapUPtr shortest_path_filtered_centerline_view_; // Lanelet map view of shortest path center lines
313 // only
314 std::vector<carma_perception_msgs::msg::RoadwayObstacle> roadway_objects_; //
315
316 size_t map_version_ = 0; // The current map version. This is cached from calls to setMap();
317
318 std::string route_name_; // The current route name. This is set from calls to setRouteName();
319
320 // The following constants are default timining plans for recieved traffic lights.
321 // The light is assumed to use these values until otherwise known
322 // TODO can these be optional parameters?
323 static constexpr double RED_LIGHT_DURATION = 20.0; //in sec
324 static constexpr double YELLOW_LIGHT_DURATION = 3.0; //in sec
325 static constexpr double GREEN_LIGHT_DURATION = 20.0; //in sec
326
328};
329} // namespace carma_wm
Class which implements the WorldModel interface. In addition this class provides write access to the ...
IndexedDistanceMap shortest_path_distance_map_
std::vector< carma_perception_msgs::msg::RoadwayObstacle > roadway_objects_
void setTrafficLightIds(uint32_t id, lanelet::Id lanelet_id)
Sets the id mapping between intersection/signal group and lanelet::Id for traffic lights in the map.
lanelet::Optional< double > distToNearestObjInLane(const lanelet::BasicPoint2d &object_center) const override
Gets Cartesian distance to the closest object on the same lane as the given point.
std::pair< TrackPos, TrackPos > routeTrackPos(const lanelet::ConstArea &area) const override
Returns a pair of TrackPos, computed in 2d, of the provided area relative to the current route....
std::vector< carma_perception_msgs::msg::RoadwayObstacle > getInLaneObjects(const lanelet::ConstLanelet &lanelet, const LaneSection &section=LANE_AHEAD) const override
Gets all roadway objects currently in the same lane as the given lanelet.
boost::optional< lanelet::BasicPoint2d > pointFromRouteTrackPos(const TrackPos &route_pos) const override
Converts a route track position into a map frame cartesian point.
lanelet::LineString3d copyConstructLineString(const lanelet::ConstLineString3d &line) const
Helper function to perform a deep copy of a LineString and assign new ids to all the elements....
std::optional< rclcpp::Time > ros1_clock_
boost::optional< std::pair< lanelet::ConstLanelet, lanelet::ConstLanelet > > getEntryExitOfSignalAlongRoute(const lanelet::CarmaTrafficSignalPtr &traffic_signal) const override
Returns the entry and exit lanelet of the signal along the SHORTEST PATH of route....
std::vector< lanelet::Lanelet > getLaneletsFromPoint(const lanelet::BasicPoint2d &point, const unsigned int n)
(non-const version) Gets the underlying lanelet, given the cartesian point on the map
lanelet::Optional< lanelet::Lanelet > getIntersectingLanelet(const carma_perception_msgs::msg::ExternalObject &object) const override
Gets the a lanelet the object is currently on determined by its position on the semantic map....
std::vector< lanelet::BusStopRulePtr > getBusStopsAlongRoute(const lanelet::BasicPoint2d &loc) const override
Return a list of bus stop along the current route. The bus stop along a route and the next bus stop a...
lanelet::Id getTrafficSignalId(uint16_t intersection_id, uint8_t signal_id)
helper for traffic signal Id
void setVehicleParticipationType(const std::string &participant)
Set vehicle participation type.
boost::posix_time::ptime min_end_time_converter_minute_of_year(boost::posix_time::ptime min_end_time, bool moy_exists, uint32_t moy=0, bool is_simulation=true, bool is_spat_wall_time=false)
update minimum end time to account for minute of the year
void setConfigSpeedLimit(double config_lim)
~CARMAWorldModel()=default
Destructor as required by interface.
static constexpr double GREEN_LIGHT_DURATION
std::optional< lanelet::ConstLanelet > getFirstLaneletOnShortestPath(const std::vector< lanelet::ConstLanelet > &lanelets_to_filter) const
Given the vector of lanelets, returns the lanelet that's on the shortest path. Returns earliest lanel...
lanelet::Optional< std::tuple< TrackPos, carma_perception_msgs::msg::RoadwayObstacle > > getNearestObjInLane(const lanelet::BasicPoint2d &object_center, const LaneSection &section=LANE_AHEAD) const
This function is called by distanceToObjectBehindInLane or distanceToObjectAheadInLane....
lanelet::LaneletMapConstPtr getMap() const override
Get a pointer to the current map. If the underlying map has changed the pointer will also need to be ...
void setMap(lanelet::LaneletMapPtr map, size_t map_version=0, bool recompute_routing_graph=true)
Set the current map.
lanelet::LaneletSubmapConstUPtr shortest_path_view_
lanelet::CarmaTrafficSignalPtr getTrafficSignal(const lanelet::Id &id) const
helper for getting traffic signal with given lanelet::Id
LaneletRouteConstPtr getRoute() const override
Get a pointer to the current route. If the underlying route has changed the pointer will also need to...
void setRoutingGraph(LaneletRoutingGraphPtr graph)
Set the routing graph for the participant type. This function may serve as an optimization to recompu...
lanelet::Optional< TrafficRulesConstPtr > getTrafficRules() const override
Get the Traffic Rules object.
void computeDowntrackReferenceLine()
Helper function to compute the geometry of the route downtrack/crosstrack reference line This functio...
LaneletRoutingGraphPtr map_routing_graph_
std::optional< rclcpp::Time > simulation_clock_
std::unordered_map< uint32_t, lanelet::Id > traffic_light_ids_
static constexpr double YELLOW_LIGHT_DURATION
void setRouteName(const std::string &route_name)
Set the name of the route.
void setSimulationClock(const rclcpp::Time &time_now)
Set simulation clock clock (only used in simulation runs)
void setRouteEndPoint(const lanelet::BasicPoint3d &end_point)
Set endpoint of the route.
std::vector< lanelet::ConstLanelet > getLane(const lanelet::ConstLanelet &lanelet, const LaneSection &section=LANE_AHEAD) const override
Gets the specified lane section achievable without lane change, sorted from the start,...
std::string getVehicleParticipationType()
Get vehicle participation type.
std::vector< lanelet::ConstLanelet > getLaneletsBetween(double start, double end, bool shortest_path_only=false, bool bounds_inclusive=true) const override
Returns a list of lanelets which are part of the route and whose downtrack bounds exist within the pr...
void setRos1Clock(const rclcpp::Time &time_now)
Set current Ros1 clock (only used in simulation runs)
lanelet::Optional< std::tuple< TrackPos, carma_perception_msgs::msg::RoadwayObstacle > > nearestObjectBehindInLane(const lanelet::BasicPoint2d &object_center) const override
Gets Downtrack distance to AND copy of the closest object BEHIND on the same lane as the given point....
lanelet::LaneletMapPtr getMutableMap() const
Get a mutable version of the current map.
carma_wm::SignalizedIntersectionManager sim_
TrackPos getRouteEndTrackPos() const override
Get trackpos of the end of route point relative to the route.
std::vector< lanelet::Lanelet > nonConnectedAdjacentLeft(const lanelet::BasicPoint2d &input_point, const unsigned int n=10)
(non-const version) Given the cartesian point on the map, tries to get the opposite direction lanelet...
CARMAWorldModel()=default
Constructor.
lanelet::LaneletMapUPtr shortest_path_filtered_centerline_view_
std::vector< std::shared_ptr< lanelet::AllWayStop > > getIntersectionsAlongRoute(const lanelet::BasicPoint2d &loc) const override
Return a list of all way stop intersections along the current route. The tall way stop intersections ...
std::shared_ptr< lanelet::LaneletMap > semantic_map_
void setRoute(LaneletRoutePtr route)
Set the current route. This route must match the current map for this class to function properly.
LaneletRoutingGraphConstPtr getMapRoutingGraph() const override
Get a pointer to the routing graph for the current map. If the underlying map has changed the pointer...
void processSpatFromMsg(const carma_v2x_msgs::msg::SPAT &spat_msg, bool use_sim_time=false, bool is_spat_wall_time=true)
processSpatFromMsg update map's traffic light states with SPAT msg
size_t getMapVersion() const override
Returns a monotonically increasing version number which represents the version stamp of the map geome...
void setRoadwayObjects(const std::vector< carma_perception_msgs::msg::RoadwayObstacle > &rw_objs)
Update internal records of roadway objects. These objects MUST be guaranteed to be on the road.
std::vector< lanelet::LineString3d > shortest_path_centerlines_
std::vector< lanelet::CarmaTrafficSignalPtr > getSignalsAlongRoute(const lanelet::BasicPoint2d &loc) const override
Return a list of traffic lights/intersections along the current route. The traffic lights along a rou...
std::vector< carma_perception_msgs::msg::RoadwayObstacle > getRoadwayObjects() const override
Get most recent roadway objects - all objects on the road detected by perception stack.
FRIEND_TEST(CARMAWorldModelTest, getFirstLaneletOnShortestPath)
lanelet::Optional< carma_perception_msgs::msg::RoadwayObstacle > toRoadwayObstacle(const carma_perception_msgs::msg::ExternalObject &object) const override
Converts an ExternalObject in a RoadwayObstacle by mapping its position onto the semantic map....
std::vector< lanelet::BasicPoint2d > sampleRoutePoints(double start_downtrack, double end_downtrack, double step_size) const override
Samples the route centerline between the provided downtracks with the provided step size....
std::vector< lanelet::SignalizedIntersectionPtr > getSignalizedIntersectionsAlongRoute(const lanelet::BasicPoint2d &loc) const
Return a list of signalized intersections along the current route. The signalized intersections along...
static constexpr double RED_LIGHT_DURATION
lanelet::Optional< std::tuple< TrackPos, carma_perception_msgs::msg::RoadwayObstacle > > nearestObjectAheadInLane(const lanelet::BasicPoint2d &object_center) const override
Gets Downtrack distance to AND copy of the closest object AHEAD on the same lane as the given point....
std::string getRouteName() const override
Get the current route name.
O(1) distance lookup structure for quickly accessing route distance information. NOTE: This structure...
This class manages and keeps track of all signalized intersections in the map. All of the SPAT and MA...
Position in a track based coordinate system where the axis are downtrack and crosstrack....
Definition: TrackPos.hpp:35
An interface which provides read access to the semantic map and route. This class is not thread safe....
Definition: WorldModel.hpp:73
std::shared_ptr< lanelet::routing::RoutingGraph > LaneletRoutingGraphPtr
Definition: WorldModel.hpp:50
std::shared_ptr< const lanelet::routing::Route > LaneletRouteConstPtr
Definition: WorldModel.hpp:46
std::shared_ptr< const lanelet::routing::RoutingGraph > LaneletRoutingGraphConstPtr
Definition: WorldModel.hpp:51
std::shared_ptr< lanelet::routing::Route > LaneletRoutePtr
Definition: WorldModel.hpp:45