Apollo  6.0
Open source self driving car software
trafficlights_perception_component.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 #pragma once
17 
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
24 #include "modules/drivers/proto/sensor_image.pb.h"
25 #include "modules/map/proto/map_geometry.pb.h"
26 #include "modules/map/proto/map_signal.pb.h"
31 #include "modules/perception/onboard/proto/trafficlights_perception_component.pb.h"
33 #include "modules/perception/proto/perception_obstacle.pb.h"
34 #include "modules/perception/proto/traffic_light_detection.pb.h"
36 
38 #include "modules/v2x/proto/v2x_traffic_light.pb.h"
39 
40 namespace apollo {
41 namespace perception {
42 namespace onboard {
43 
45  public:
48 
50  delete;
52  const TrafficLightsPerceptionComponent&) = delete;
53 
54  bool Init() override;
55 
56  private:
57  int InitConfig();
58  int InitAlgorithmPlugin();
59  int InitCameraListeners();
60  int InitCameraFrame();
61 
62  int InitV2XListener();
63 
64  void OnReceiveImage(const std::shared_ptr<apollo::drivers::Image> image,
65  const std::string& camera_name);
66 
67  void OnReceiveV2XMsg(
68  const std::shared_ptr<apollo::v2x::IntersectionTrafficLightData> v2x_msg);
69 
70  bool QueryPoseAndSignals(const double ts, camera::CarPose* pose,
71  std::vector<apollo::hdmap::Signal>* signals);
72 
73  bool VerifyLightsProjection(const double& ts,
74  const camera::TLPreprocessorOption& option,
75  const std::string& camera_name,
76  camera::CameraFrame* image_lights);
77 
78  bool UpdateCameraSelection(double timestamp,
79  const camera::TLPreprocessorOption& option,
80  camera::CameraFrame* frame);
81 
82  bool CheckCameraImageStatus(double timestamp, double interval,
83  const std::string& camera_name);
84 
85  bool GetCarPose(const double timestamp, camera::CarPose* pose);
86 
87  bool GetPoseFromTF(const double timestamp, const std::string& frame_id,
88  const std::string& child_frame_id,
89  Eigen::Matrix4d* pose_matrix);
90 
91  double stopline_distance(const Eigen::Matrix4d& cam_pose);
92  bool TransformOutputMessage(
93  camera::CameraFrame* frame, const std::string& camera_name,
94  std::shared_ptr<apollo::perception::TrafficLightDetection>* out_msg);
95 
96  bool TransformDebugMessage(
97  const camera::CameraFrame* frame,
98  std::shared_ptr<apollo::perception::TrafficLightDetection>* out_msg);
99  void SendSimulationMsg();
100  void GenerateTrafficLights(
101  const std::vector<apollo::hdmap::Signal>& signals,
102  std::vector<base::TrafficLightPtr>* traffic_lights);
103  void TransRect2Box(const base::RectI& rect,
104  apollo::perception::TrafficLightBox* box);
105 
106  private:
107  void Visualize(const camera::CameraFrame& frame,
108  const std::vector<base::TrafficLightPtr>& lights) const;
109  void SyncV2XTrafficLights(camera::CameraFrame* frame);
110 
111  private:
112  std::mutex mutex_;
113 
114  std::shared_ptr<camera::BaseTLPreprocessor> preprocessor_;
115  apollo::perception::map::HDMapInput* hd_map_ = nullptr;
116 
117  camera::TrafficLightPreprocessorInitOptions preprocessor_init_options_;
118  std::string tl_preprocessor_name_;
119 
120  std::string tf2_frame_id_;
121  std::string tf2_child_frame_id_;
122  double tf2_timeout_second_ = 0.01;
123 
124  Buffer* tf2_buffer_ = Buffer::Instance();
125 
126  std::vector<std::string> camera_names_;
127  std::vector<std::string> input_camera_channel_names_;
128  // camera_name -> TransformWrapper
129  std::map<std::string, std::shared_ptr<TransformWrapper>>
130  camera2world_trans_wrapper_map_;
131  // camera_name -> image_border_size
132  std::map<std::string, int> image_border_sizes_;
133 
134  std::vector<std::shared_ptr<cyber::Node>> camera_listener_nodes_;
135 
136  double last_sub_tf_ts_ = 0.0;
137 
138  std::map<std::string, double> last_sub_camera_image_ts_;
139 
140  double query_tf_interval_seconds_ = 0.0;
141  double image_timestamp_offset_ = 0.0;
142  int max_process_image_fps_ = 10; // max frames to be processed per second
143  double proc_interval_seconds_ = 0.0;
144  double check_image_status_interval_thresh_ = 1.0;
145 
146  double last_query_tf_ts_ = 0.0;
147  double last_proc_image_ts_ = 0.0;
148 
149  double image_sys_ts_diff_threshold_ = 0.5;
150 
151  // for querying hdmap signals
152  double last_signals_ts_ = -1.0;
153  double valid_hdmap_interval_ = 1.5;
154  double forward_distance_to_query_signals = 150.0;
155  std::vector<apollo::hdmap::Signal> last_signals_;
156 
157  // image info.
158  int image_width_ = 1920;
159  int image_height_ = 1080;
160  int image_channel_num_ = 3;
161  int image_data_size_ = -1;
162  int frame_index_ = 0;
163  int default_image_border_size_ = 100;
164 
165  // options for DataProvider
166  bool enable_undistortion_ = false;
167  camera::DataProvider::InitOptions data_provider_init_options_;
168 
169  // pre-allocated-mem data_provider; camera_id -> data_provider
170  std::map<std::string, std::shared_ptr<camera::DataProvider>>
171  data_providers_map_;
172 
173  // image
174  std::shared_ptr<camera::CameraFrame> frame_;
175 
176  // proc
177  camera::CameraPerceptionInitOptions camera_perception_init_options_;
178  camera::CameraPerceptionOptions camera_perception_options_;
179  std::unique_ptr<camera::TrafficLightCameraPerception> traffic_light_pipeline_;
180  ::google::protobuf::RepeatedPtrField<apollo::hdmap::Curve> stoplines_;
181 
182  // msg channel name
183  std::string simulation_channel_name_;
184  std::string traffic_light_output_channel_name_;
185 
186  std::shared_ptr<
188  writer_;
189 
190  // traffic lights
191  apollo::perception::base::TLColor detected_trafficlight_color_;
192  double cnt_r_;
193  double cnt_g_;
194  double cnt_y_;
195  double cnt_u_;
196 
197  // v2x
198  std::string v2x_trafficlights_input_channel_name_;
199  double v2x_sync_interval_seconds_ = 0.1;
200  int max_v2x_msg_buff_size_ = 50;
201  boost::circular_buffer<apollo::v2x::IntersectionTrafficLightData>
202  v2x_msg_buffer_;
203 };
204 
206 
207 } // namespace onboard
208 } // namespace perception
209 } // namespace apollo
Definition: camera_frame.h:33
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: base_camera_perception.h:28
Eigen::Matrix4d Matrix4d
Definition: base_map_fwd.h:32
Definition: hdmap_input.h:34
Definition: writer.h:42
Definition: base_tl_preprocessor.h:38
Definition: buffer.h:33
Definition: trafficlights_perception_component.h:44
The gflags used by v2x proxy module.
Definition: base_camera_perception.h:34
TrafficLightsPerceptionComponent & operator=(const TrafficLightsPerceptionComponent &)=delete
The Component can process up to four channels of messages. The message type is specified when the com...
Definition: component.h:58
TLColor
Definition: traffic_light.h:28
CYBER_REGISTER_COMPONENT(CameraObstacleDetectionComponent)