Apollo  v5.5.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 
23 #include "cyber/component/component.h"
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"
35 #include "modules/transform/buffer.h"
36 
37 #include "modules/v2x/common/v2x_proxy_gflags.h"
38 #include "modules/v2x/proto/v2x_traffic_light.pb.h"
39 
40 namespace apollo {
41 namespace perception {
42 namespace onboard {
43 
44 class TrafficLightsPerceptionComponent : public apollo::cyber::Component<> {
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::TLPreprocessor> preprocessor_;
115  apollo::perception::map::HDMapInput* hd_map_ = nullptr;
116 
117  camera::TrafficLightPreprocessorInitOptions preprocessor_init_options_;
118 
119  std::string tf2_frame_id_;
120  std::string tf2_child_frame_id_;
121  double tf2_timeout_second_ = 0.01;
122 
123  Buffer* tf2_buffer_ = Buffer::Instance();
124 
125  std::vector<std::string> camera_names_;
126  std::vector<std::string> input_camera_channel_names_;
127  // camera_name -> TransformWrapper
128  std::map<std::string, std::shared_ptr<TransformWrapper>>
129  camera2world_trans_wrapper_map_;
130  // camera_name -> image_border_size
131  std::map<std::string, int> image_border_sizes_;
132 
133  std::vector<std::shared_ptr<cyber::Node>> camera_listener_nodes_;
134 
135  double last_sub_tf_ts_ = 0.0;
136 
137  std::map<std::string, double> last_sub_camera_image_ts_;
138 
139  double query_tf_interval_seconds_ = 0.0;
140  double image_timestamp_offset_ = 0.0;
141  int max_process_image_fps_ = 10; // max frames to be processed per second
142  double proc_interval_seconds_ = 0.0;
143  double check_image_status_interval_thresh_ = 1.0;
144 
145  double last_query_tf_ts_ = 0.0;
146  double last_proc_image_ts_ = 0.0;
147 
148  double image_sys_ts_diff_threshold_ = 0.5;
149 
150  // for querying hdmap signals
151  double last_signals_ts_ = -1.0;
152  double valid_hdmap_interval_ = 1.5;
153  double forward_distance_to_query_signals = 150.0;
154  std::vector<apollo::hdmap::Signal> last_signals_;
155 
156  // image info.
157  int image_width_ = 1920;
158  int image_height_ = 1080;
159  int image_channel_num_ = 3;
160  int image_data_size_ = -1;
161  int frame_index_ = 0;
162  int default_image_border_size_ = 100;
163 
164  // options for DataProvider
165  bool enable_undistortion_ = false;
166  camera::DataProvider::InitOptions data_provider_init_options_;
167 
168  // pre-allocated-mem data_provider; camera_id -> data_provider
169  std::map<std::string, std::shared_ptr<camera::DataProvider>>
170  data_providers_map_;
171 
172  // image
173  camera::CameraFrame frame_;
174 
175  // proc
176  camera::CameraPerceptionInitOptions camera_perception_init_options_;
177  camera::CameraPerceptionOptions camera_perception_options_;
178  std::unique_ptr<camera::TrafficLightCameraPerception> traffic_light_pipeline_;
179  ::google::protobuf::RepeatedPtrField<apollo::hdmap::Curve> stoplines_;
180 
181  // msg channel name
182  std::string simulation_channel_name_;
183  std::string traffic_light_output_channel_name_;
184 
185  std::shared_ptr<
186  apollo::cyber::Writer<apollo::perception::TrafficLightDetection>>
187  writer_;
188 
189  // traffic lights
190  apollo::perception::base::TLColor detected_trafficlight_color_;
191  double cnt_r_;
192  double cnt_g_;
193  double cnt_y_;
194  double cnt_u_;
195 
196  // v2x
197  std::string v2x_trafficlights_input_channel_name_;
198  double v2x_sync_interval_seconds_ = 0.1;
199  int max_v2x_msg_buff_size_ = 50;
200  boost::circular_buffer<apollo::v2x::IntersectionTrafficLightData>
201  v2x_msg_buffer_;
202 };
203 
205 
206 } // namespace onboard
207 } // namespace perception
208 } // namespace apollo
Definition: camera_frame.h:33
Definition: blob.h:72
Definition: base_camera_perception.h:28
Definition: hdmap_input.h:33
Definition: tl_preprocessor.h:42
Definition: trafficlights_perception_component.h:44
CYBER_REGISTER_COMPONENT(FusionCameraDetectionComponent)
Definition: base_camera_perception.h:34
TrafficLightsPerceptionComponent & operator=(const TrafficLightsPerceptionComponent &)=delete
TLColor
Definition: traffic_light.h:28