main logic of the planning module, runs periodically triggered by timer.
114 {
117
118
120 const ReferenceLineConfig* reference_line_config = nullptr;
121 if (
config_.has_reference_line_config()) {
123 }
124
125
126 reference_line_provider_ = std::make_unique<ReferenceLineProvider>(
127 injector_->vehicle_state(), reference_line_config,
129
130
131 ADEBUG <<
"Get localization: "
133
134
136
139
140 auto vehicle_config =
142
143 if (last_vehicle_config_.is_valid_ && vehicle_config.is_valid_) {
144 auto x_diff_map = vehicle_config.x_ - last_vehicle_config_.x_;
145 auto y_diff_map = vehicle_config.y_ - last_vehicle_config_.y_;
146
147 auto cos_map_veh = std::cos(last_vehicle_config_.theta_);
148 auto sin_map_veh = std::sin(last_vehicle_config_.theta_);
149
150 auto x_diff_veh = cos_map_veh * x_diff_map + sin_map_veh * y_diff_map;
151 auto y_diff_veh = -sin_map_veh * x_diff_map + cos_map_veh * y_diff_map;
152
153 auto theta_diff = vehicle_config.theta_ - last_vehicle_config_.theta_;
154
157 }
158 last_vehicle_config_ = vehicle_config;
159
160 VehicleState vehicle_state =
injector_->vehicle_state()->vehicle_state();
161
162
163
164
165
166 DCHECK_GE(start_timestamp, vehicle_state.timestamp());
167 if (start_timestamp - vehicle_state.timestamp() <
168 FLAGS_message_latency_threshold) {
169 auto future_xy =
injector_->vehicle_state()->EstimateFuturePosition(
170 start_timestamp - vehicle_state.timestamp());
171 vehicle_state.set_x(future_xy.x());
172 vehicle_state.set_y(future_xy.y());
173 vehicle_state.set_timestamp(start_timestamp);
174 }
175
176 auto* not_ready = trajectory_pb->mutable_decision()
177 ->mutable_main_decision()
178 ->mutable_not_ready();
179
181 const std::string msg = "Update VehicleStateProvider failed";
183 not_ready->set_reason(msg);
184 status.Save(trajectory_pb->mutable_header()->mutable_status());
185
188 return;
189 }
190
191 const double planning_cycle_time = 1.0 / FLAGS_planning_loop_rate;
192
193 std::vector<TrajectoryPoint> stitching_trajectory;
194 std::string replan_reason;
197 planning_cycle_time, FLAGS_trajectory_stitching_preserved_length, true,
200
201 const uint32_t frame_num =
static_cast<uint32_t
>(
seq_num_++);
202 status = InitFrame(frame_num, stitching_trajectory.back(), vehicle_state);
203
205 const std::string msg = "Failed to init frame";
207 not_ready->set_reason(msg);
208 status.Save(trajectory_pb->mutable_header()->mutable_status());
209
212 return;
213 }
214
215 injector_->ego_info()->Update(stitching_trajectory.back(), vehicle_state);
216
217 if (FLAGS_enable_record_debug) {
218 frame_->RecordInputDebug(trajectory_pb->mutable_debug());
219 }
220 trajectory_pb->mutable_latency_stats()->set_init_frame_time_ms(
222 if (!status.ok()) {
223 AERROR << status.ToString();
224 if (FLAGS_publish_estop) {
225
226
227
228
229 ADCTrajectory estop_trajectory;
230 EStop* estop = estop_trajectory.mutable_estop();
231 estop->set_is_estop(true);
232 estop->set_reason(status.error_message());
233 status.Save(estop_trajectory.mutable_header()->mutable_status());
234
237 } else {
238 trajectory_pb->mutable_decision()
239 ->mutable_main_decision()
240 ->mutable_not_ready()
241 ->set_reason(status.ToString());
242 status.Save(trajectory_pb->mutable_header()->mutable_status());
243
246 }
247
248 frame_->set_current_frame_planned_trajectory(*trajectory_pb);
249 auto seq_num =
frame_->SequenceNum();
251
252 return;
253 }
254
255
256 if (FLAGS_enable_planning_pad_msg) {
257 const auto& pad_msg_driving_action =
frame_->GetPadMsgDrivingAction();
258 ProcessPadMsg(pad_msg_driving_action);
259 }
260
261 for (
auto& ref_line_info : *
frame_->mutable_reference_line_info()) {
262 auto traffic_status =
264 if (!traffic_status.ok() || !ref_line_info.IsDrivable()) {
265 ref_line_info.SetDrivable(false);
266 AWARN <<
"Reference line " << ref_line_info.Lanes().Id()
267 << " traffic decider failed";
268 continue;
269 }
270 }
271
272 status =
Plan(start_timestamp, stitching_trajectory, trajectory_pb);
273
275 ADEBUG <<
"total planning time spend: " << time_diff_ms <<
" ms.";
276
277 trajectory_pb->mutable_latency_stats()->set_total_time_ms(time_diff_ms);
278 ADEBUG <<
"Planning latency: "
279 << trajectory_pb->latency_stats().DebugString();
280
281 auto* ref_line_task =
282 trajectory_pb->mutable_latency_stats()->add_task_stats();
283 ref_line_task->set_time_ms(reference_line_provider_->LastTimeDelay() *
284 1000.0);
285 ref_line_task->set_name("ReferenceLineProvider");
286
287 if (!status.ok()) {
288 status.Save(trajectory_pb->mutable_header()->mutable_status());
289 AERROR <<
"Planning failed:" << status.ToString();
290 if (FLAGS_publish_estop) {
291 AERROR <<
"Planning failed and set estop";
292
293
294
295
296 EStop* estop = trajectory_pb->mutable_estop();
297 estop->set_is_estop(true);
298 estop->set_reason(status.error_message());
299 }
300 }
301
302 trajectory_pb->set_is_replan(stitching_trajectory.size() == 1);
303
306 ADEBUG <<
"Planning pb:" << trajectory_pb->header().DebugString();
307
308 auto seq_num =
frame_->SequenceNum();
310}
static double NowInSeconds()
gets the current time in second.
static const HDMap * BaseMapPtr()
apollo::common::Status Plan(const double current_time_stamp, const std::vector< common::TrajectoryPoint > &stitching_trajectory, ADCTrajectory *const trajectory) override
Plan the trajectory given current vehicle state
virtual void FillPlanningPb(const double timestamp, ADCTrajectory *const trajectory_pb)
const hdmap::HDMap * hdmap_
apollo::common::Status Execute(Frame *frame, ReferenceLineInfo *reference_line_info)
static std::vector< common::TrajectoryPoint > ComputeStitchingTrajectory(const canbus::Chassis &vehicle_chassis, const common::VehicleState &vehicle_state, const double current_timestamp, const double planning_cycle_time, const size_t preserved_points_num, const bool replan_by_offset, const PublishableTrajectory *prev_trajectory, std::string *replan_reason, const control::ControlInteractiveMsg &control_interactive_msg)
static void TransformLastPublishedTrajectory(const double x_diff, const double y_diff, const double theta_diff, PublishableTrajectory *prev_trajectory)
bool IsVehicleStateValid(const VehicleState &vehicle_state)
std::shared_ptr< localization::LocalizationEstimate > localization_estimate
std::shared_ptr< relative_map::MapMsg > relative_map
std::shared_ptr< control::ControlInteractiveMsg > control_interactive_msg
std::shared_ptr< canbus::Chassis > chassis
optional ReferenceLineConfig reference_line_config
LocalView contains all necessary data as planning input