Apollo 11.0
自动驾驶开放平台
speed_setting.cc
浏览该文件的文档.
1/*****************************************************************************
2 * Copyright 2017 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
22
23#include "modules/common_msgs/external_command_msgs/speed_command.pb.h"
24
25namespace apollo {
26namespace planning {
27
29
30SpeedSetting::SpeedSetting() : last_cruise_speed_(-1.0), last_sequence_num_(-1) {
31 // hook: Apollo License Verification: v_apollo_park
32}
33
34Status SpeedSetting::ApplyRule(Frame* const frame, ReferenceLineInfo* const reference_line_info) {
35 const auto& planning_command = frame->local_view().planning_command;
36 // If there is new planning command, use the last set cruise speed.
37 if (!planning_command->has_header() || !planning_command->header().has_sequence_num()
38 || planning_command->header().sequence_num() == last_sequence_num_) {
39 // Set the cruise speed with last speed limit.
40 if (last_cruise_speed_ > 0.0) {
41 reference_line_info->SetCruiseSpeed(last_cruise_speed_);
42 }
43 return Status::OK();
44 }
45 // Update the new cruise speed when new motion command comes. The first time
46 // received new motion command, use the new cruise speed.
47 if (planning_command->is_motion_command()) {
48 last_cruise_speed_ = reference_line_info->GetBaseCruiseSpeed();
49 return Status::OK();
50 }
51
52 // Only deal with planning command which has "SpeedCommand" as custom command.
53 if (!planning_command->has_custom_command()
54 || !planning_command->custom_command().Is<external_command::SpeedCommand>()) {
55 // Set the cruise speed with last speed limit.
56 if (last_cruise_speed_ > 0.0) {
57 reference_line_info->SetCruiseSpeed(last_cruise_speed_);
58 }
59 return Status::OK();
60 }
61 const auto& custom_command = planning_command->custom_command();
63 if (!custom_command.UnpackTo(&speed_command)) {
64 // Set the cruise speed with last speed limit.
65 if (last_cruise_speed_ > 0.0) {
66 reference_line_info->SetCruiseSpeed(last_cruise_speed_);
67 }
68 AERROR << "Unpack speed command failed!";
70 }
71 // If command is to restore the target speed, don't modify it in
72 // "reference_line_info".
73 if (speed_command.has_is_restore_target_speed() && speed_command.is_restore_target_speed()) {
74 last_cruise_speed_ = reference_line_info->GetBaseCruiseSpeed();
75 return Status::OK();
76 }
77 last_sequence_num_ = planning_command->header().sequence_num();
78 // Get the cruise speed for the first time.
79 if (last_cruise_speed_ < 0.0) {
80 last_cruise_speed_ = reference_line_info->GetBaseCruiseSpeed();
81 }
82 // Deal with the command for adjusting target speed.
83 if (speed_command.has_target_speed()) {
84 reference_line_info->SetCruiseSpeed(speed_command.target_speed());
85 } else if (speed_command.has_target_speed_factor()) {
86 // Calculate the target speed with speed_factor.
87 double target_speed = last_cruise_speed_ * speed_command.target_speed_factor();
88 reference_line_info->SetCruiseSpeed(target_speed);
89 }
90 last_cruise_speed_ = reference_line_info->GetBaseCruiseSpeed();
91 return Status::OK();
92}
93
94} // namespace planning
95} // namespace apollo
A general class to denote the return status of an API call.
Definition status.h:43
static Status OK()
generate a success status.
Definition status.h:60
Frame holds all data for one planning cycle.
Definition frame.h:62
const LocalView & local_view() const
Definition frame.h:163
ReferenceLineInfo holds all data for one reference line.
common::Status ApplyRule(Frame *const frame, ReferenceLineInfo *const reference_line_info)
Planning module main class.
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
std::shared_ptr< PlanningCommand > planning_command
Definition local_view.h:48