Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
MapDataLayer.hpp
Go to the documentation of this file.
1 #ifndef MAP_DATA_LAYER_HPP
2 #define MAP_DATA_LAYER_HPP
3 
4 #include <map>
5 #include <unordered_map>
6 #include <array>
7 #include <memory>
8 #include <mutex>
9 #include <atomic>
10 
11 #include <galaxis/linatypes.hpp>
12 #include <galaxis/messages.hpp>
14 #include <sensor_msgs/msg/image.hpp>
15 
16 #include "MapClasses.hpp"
17 
18 // ThreadPool: https://www.boost.org/doc/libs/1_74_0/doc/html/boost_asio/reference/thread_pool.html
19 #include <boost/asio.hpp>
20 #include <boost/asio/thread_pool.hpp>
21 #include <shared_mutex>
22 #include <rclcpp/rclcpp.hpp>
23 
24 using namespace galaxis::lina;
25 using namespace map::classes;
26 
27 /*
28 MAPCONTENTS MESSAGE: # has to be refactored to ros_sensor_msgs
29 
30 uint8[40000] center_lane
31 uint8[40000] right_lane
32 uint8[40000] left_lane
33 bool vertical_line False
34 
35 
36 uint8[40000] crossing_line
37 # uint8[40000] crosswalk
38 # uint8[40000] sign
39 
40 uint64 timestamp
41 */
42 
43 constexpr uint8_t MAPCELL_MAXVALUE = 255; // uint8_t
44 
45 struct MapCell {
46  uint8_t leftLine = 0;
47  uint8_t centerLine = 0;
48  uint8_t rightLine = 0;
49  bool imageIgnoreField = false;
50  uint32_t lastUpdateStep = 0;
51 };
52 
53 constexpr int MAP_SIZE = 20000; // 20m x 20m with cell size of 1cm^2
54 
55 class MapDataLayer {
56  typedef std::array<MapCell, MAP_SIZE> ColumnMapArray;
57  typedef std::array<ColumnMapArray, MAP_SIZE> MapArray;
58 
59 private:
60  bool m_simulationMode = false;
61 
62  boost::asio::thread_pool m_threadPool;
64  std::shared_mutex m_sharedThreadMutex;
65 
66  // line data
67  MapArray m_data; // [X][Y] // (0,0) - (left,bottom)
69  std::mutex m_mapMutex;
70  std::atomic<uint32_t> m_updateStep = 0;
71 
73 
74  std::shared_ptr<LinesObject<int16_t>> m_linesObjectSPtr;
75 
76 public:
77  // SETTINGS
78  int m_lookupWindowSizeCMWidth = 160;
79  int m_lookupWindowSizeCMHeight = 150;
80  // int m_lookupLookBehindCarCM = 10;
81  uint8_t m_cellThresholdRGBValue = 80;
82  uint8_t m_cellThresholdExtract = 99;
83  uint8_t m_cellLineDecreasePerView = 100;
84  uint8_t m_cellLineIncreasePerView = 100;
85  float m_laneImagePixelToPositionConversionFactor = 1.5625f;
86  float m_obstaclePositionDiffForRerecognitionCM = 40.0;
87 
88  // obstacle data
89  std::shared_ptr<std::vector<StaticObstacle>> m_staticObstacleVecSPtr;
90  rclcpp::Duration m_obstacleKeepDuration;
91 
92 public:
93  MapDataLayer();
94  MapDataLayer(int amountOfThreads);
95  ~MapDataLayer();
96  void init();
97  void clear();
98 
99  // ############## Settings ##############
100  void enableSimulationMode();
101  void setLookupWindowSizeCMWidth(int value);
102  void setLookupWindowSizeCMHeight(int value);
103  // void setLookupLookBehindCarCM(int value);
104  void setCellThresholdRGBValue(uint8_t value);
105  void setCellThresholdExtract(uint8_t value);
106  void setCellLineDecreasePerView(uint8_t value);
107  void setCellLineIncreasePerView(uint8_t value);
108  void setLaneImageConversionFactor(float factor);
109  void setObstaclePositionDiffForRerecognition(float diff);
110  void setObstacleTrackTimeDuration(int sec, int nsec);
111 
112 
113  bool isInImageIgnoreField(Vector2f p_position, unsigned int p_maxImageX);
114 
115  // line functions
116  void updateMap(const sensor_msgs::msg::Image::ConstSharedPtr& laneDetectionSPtr, Vector2f currentAbsoluteCarPositionCM, double currentAbsoluteOrientation);
117  std::shared_ptr<LinesObject<int16_t>> getLineObject(Vector2f absolutePositionCM, float lookBehindPosOffsetCM, double absoluteOrientation, Vector2i windowSize);
118 
119  // obstacle functions
120  std::shared_ptr<std::vector<StaticObstacle>> getObstacles(rclcpp::Time const& timestamp, bool removeOld = true);
121  void addOrUpdateStaticObstacle(ObstacleType const& p_obstacle, Vector2f absolutePositionCM, double currentOrientation, rclcpp::Time const& timestamp);
122 
123 private:
124  void preprocessPoints(const sensor_msgs::msg::Image::ConstSharedPtr& laneDetectionSPtr, Vector2f const& currentPositionCM, int arrayOffsetStartIndex, int arrayOffsetEndIndex, double currentAbsoluteOrientation);
125  inline void updateCell(MapCell const& cellUpdate, Vector2i position);
126  inline void decreaseStepCell(MapCell* cellPtr);
127  float checkPositionRelativeToEdge(int x1, int y1, int x2, int y2, int xp, int yp);
128  void iterateThroughMap(int arrayStartX, int arrayEndX, int arrayStartY, int arrayEndY, Vector2i const& absoluteCarPosition, std::array<Vector2f, 4> const& windowEdges);
129 };
130 
131 #endif // _MAP_DATA_LAYER_HPP_
MapDataLayer::m_amountOfThreads
int m_amountOfThreads
Definition: MapDataLayer.hpp:63
MapDataLayer::m_staticObstacleVecSPtr
std::shared_ptr< std::vector< StaticObstacle > > m_staticObstacleVecSPtr
Definition: MapDataLayer.hpp:89
ObstacleType
messages::msg::Obstacle ObstacleType
Definition: messages.hpp:59
MAPCELL_MAXVALUE
constexpr uint8_t MAPCELL_MAXVALUE
Definition: MapDataLayer.hpp:43
MapDataLayer
Definition: MapDataLayer.hpp:55
MapDataLayer::m_mapSize
Vector2f m_mapSize
Definition: MapDataLayer.hpp:68
galaxis::lina
Definition: linatypes.hpp:11
map::classes
Definition: MapClasses.hpp:16
MapDataLayer::MapArray
std::array< ColumnMapArray, MAP_SIZE > MapArray
Definition: MapDataLayer.hpp:57
MapDataLayer::m_data
MapArray m_data
Definition: MapDataLayer.hpp:67
MapDataLayer::m_linesObjectSPtr
std::shared_ptr< LinesObject< int16_t > > m_linesObjectSPtr
Definition: MapDataLayer.hpp:74
MapDataLayer::m_absoluteCarPositionOffset
Vector2f m_absoluteCarPositionOffset
Definition: MapDataLayer.hpp:72
MapCell
Definition: MapDataLayer.hpp:45
MAP_SIZE
constexpr int MAP_SIZE
Definition: MapDataLayer.hpp:53
galaxis::lina::Vector2f
Eigen::Vector2f Vector2f
Definition: linatypes.hpp:14
linatypes.hpp
MapDataLayer::m_sharedThreadMutex
std::shared_mutex m_sharedThreadMutex
Definition: MapDataLayer.hpp:64
MapDataLayer::m_mapMutex
std::mutex m_mapMutex
Definition: MapDataLayer.hpp:69
MapDataLayer::ColumnMapArray
std::array< MapCell, MAP_SIZE > ColumnMapArray
Definition: MapDataLayer.hpp:56
galaxis::lina::Vector2i
Eigen::Vector2i Vector2i
Definition: linatypes.hpp:13
MapDataLayer::m_obstacleKeepDuration
rclcpp::Duration m_obstacleKeepDuration
Definition: MapDataLayer.hpp:90
MapDataLayer::m_threadPool
boost::asio::thread_pool m_threadPool
Definition: MapDataLayer.hpp:62
MapClasses.hpp
AutopilotSettings.hpp
messages.hpp