1 流程图
1.1 变道流程图
1.2 车道是否阻塞流程图
1.3 感知是否阻塞流程图
2 代码分析
代码分析如下:
获取frame帧中的参考线信息,如果没有参考线,则提示错误。
LaneChangeDecider::LaneChangeDecider(const TaskConfig& config)
: Decider(config) {}
Status LaneChangeDecider::Process(Frame* frame) {
// Sanity checks.
CHECK_NOTNULL(frame);
std::list<ReferenceLineInfo>* reference_line_info =
frame->mutable_reference_line_info();
if (reference_line_info->empty()) {
const std::string msg = "Reference lines empty.";
AERROR << msg;
return Status(ErrorCode::PLANNING_ERROR, msg);
}
是否强制换道,如果是,则调用优先换道功能。具体函数如下:
if (FLAGS_reckless_change_lane) {
PrioritizeChangeLane(reference_line_info);
return Status::OK();
}
优先换道函数。
首先获取第一条参考线的迭代器,然后遍历所有的参考线,如果当前的参考线为允许变道参考线,则将第一条参考线更换为当前迭代器所指向的参考线。
注意,可变车道为按迭代器的顺序求取,一旦发现可变车道,即推出循环。
void LaneChangeDecider::PrioritizeChangeLane(
std::list<ReferenceLineInfo>* reference_line_info) const {
if (reference_line_info->empty()) {
AERROR << "Reference line info empty";
return;
}
auto iter = reference_line_info->begin();
while (iter != reference_line_info->end()) {
if (iter->IsChangeLanePath()) {
reference_line_info->splice(reference_line_info->begin(),
*reference_line_info, iter);
break;
}
++iter;
}
}
判断是否是可变车道,如果车不在车道片段上,则该车道为可变道车道。
bool ReferenceLineInfo::IsChangeLanePath() const {
return !Lanes().IsOnSegment();
}
获取车辆变道的状态,并得到当前的时间。
如果得到变道状态,则更新状态,
auto* prev_status = PlanningContext::Instance()
->mutable_planning_status()
->mutable_change_lane();
double now = Clock::NowInSeconds();
if (!prev_status->has_status()) {
UpdateStatus(now, ChangeLaneStatus::CHANGE_LANE_SUCCESS,
GetCurrentPathId(*reference_line_info));
return Status::OK();
}
更新状态的代码如下,输入参数包括当前时间,变道状态,当前车道的ID。
将可变车道的状态更新为以上参数。
void LaneChangeDecider::UpdateStatus(double timestamp,
ChangeLaneStatus::Status status_code,
const std::string& path_id) {
auto* change_lane_status = PlanningContext::Instance()
->mutable_planning_status()
->mutable_change_lane();
change_lane_status->set_timestamp(timestamp);
change_lane_status->set_path_id(path_id);
change_lane_status->set_status(status_code);
}
判断有几条参考线,如果有一条以上参考线,则定义has_change_lane为1,说明有可变车道。
如果没有可变车道,则获取第一条车道的ID,
bool has_change_lane = reference_line_info->size() > 1;
if (!has_change_lane) {
const auto& path_id = reference_line_info->front().Lanes().Id();
if (prev_status->status() == ChangeLaneStatus::CHANGE_LANE_SUCCESS) {
} else if (prev_status->status() == ChangeLaneStatus::IN_CHANGE_LANE) {
UpdateStatus(now, ChangeLaneStatus::CHANGE_LANE_SUCCESS, path_id);
} else if (prev_status->status() == ChangeLaneStatus::CHANGE_LANE_FAILED) {
} else {
const std::string msg =
StrCat("Unknown state: ", prev_status->ShortDebugString());
AERROR << msg;
return Status(ErrorCode::PLANNING_ERROR, msg);
}
return Status::OK();
} else { // has change lane in reference lines.
auto current_path_id = GetCurrentPathId(*reference_line_info);
if (current_path_id.empty()) {
const std::string msg = "The vehicle is not on any reference line";
AERROR << msg;
return Status(ErrorCode::PLANNING_ERROR, msg);
}
if (prev_status->status() == ChangeLaneStatus::IN_CHANGE_LANE) {
if (prev_status->path_id() == current_path_id) {
PrioritizeChangeLane(reference_line_info);
} else {
RemoveChangeLane(reference_line_info);
UpdateStatus(now, ChangeLaneStatus::CHANGE_LANE_SUCCESS,
current_path_id);
}
return Status::OK();
} else if (prev_status->status() == ChangeLaneStatus::CHANGE_LANE_FAILED) {
if (now - prev_status->timestamp() < FLAGS_change_lane_fail_freeze_time) {
RemoveChangeLane(reference_line_info);
} else {
UpdateStatus(now, ChangeLaneStatus::IN_CHANGE_LANE, current_path_id);
}
return Status::OK();
} else if (prev_status->status() == ChangeLaneStatus::CHANGE_LANE_SUCCESS) {
if (now - prev_status->timestamp() <
FLAGS_change_lane_success_freeze_time) {
RemoveChangeLane(reference_line_info);
} else {
PrioritizeChangeLane(reference_line_info);
UpdateStatus(now, ChangeLaneStatus::IN_CHANGE_LANE, current_path_id);
}
} else {
const std::string msg =
StrCat("Unknown state: ", prev_status->ShortDebugString());
AERROR << msg;
return Status(ErrorCode::PLANNING_ERROR, msg);
}
}
return Status::OK();
}
void LaneChangeDecider::UpdateStatus(ChangeLaneStatus::Status status_code,
const std::string& path_id) {
UpdateStatus(Clock::NowInSeconds(), status_code, path_id);
}
void LaneChangeDecider::UpdateStatus(double timestamp,
ChangeLaneStatus::Status status_code,
const std::string& path_id) {
auto* change_lane_status = PlanningContext::Instance()
->mutable_planning_status()
->mutable_change_lane();
change_lane_status->set_timestamp(timestamp);
change_lane_status->set_path_id(path_id);
change_lane_status->set_status(status_code);
}
void LaneChangeDecider::PrioritizeChangeLane(
std::list<ReferenceLineInfo>* reference_line_info) const {
if (reference_line_info->empty()) {
AERROR << "Reference line info empty";
return;
}
auto iter = reference_line_info->begin();
while (iter != reference_line_info->end()) {
if (iter->IsChangeLanePath()) {
reference_line_info->splice(reference_line_info->begin(),
*reference_line_info, iter);
break;
}
++iter;
}
}
void LaneChangeDecider::RemoveChangeLane(
std::list<ReferenceLineInfo>* reference_line_info) const {
auto iter = reference_line_info->begin();
while (iter != reference_line_info->end()) {
if (iter->IsChangeLanePath()) {
iter = reference_line_info->erase(iter);
} else {
++iter;
}
}
}
std::string LaneChangeDecider::GetCurrentPathId(
const std::list<ReferenceLineInfo>& reference_line_info) const {
for (const auto& info : reference_line_info) {
if (!info.IsChangeLanePath()) {
return info.Lanes().Id();
}
}
return "";
}
判断当前变道是否安全,定义SL图的边界,
bool LaneChangeDecider::IsClearToChangeLane(
ReferenceLineInfo* reference_line_info) {
double ego_start_s = reference_line_info->AdcSlBoundary().start_s();
double ego_end_s = reference_line_info->AdcSlBoundary().end_s();
double ego_v =
std::abs(reference_line_info->vehicle_state().linear_velocity());
for (const auto* obstacle :
reference_line_info->path_decision()->obstacles().Items()) {
if (obstacle->IsVirtual() || obstacle->IsStatic()) {
ADEBUG << "skip one virtual or static obstacle";
continue;
}
将障碍物的边投射到SL图中,
double start_s = std::numeric_limits<double>::max();
double end_s = -std::numeric_limits<double>::max();
double start_l = std::numeric_limits<double>::max();
double end_l = -std::numeric_limits<double>::max();
for (const auto& p : obstacle->PerceptionPolygon().points()) {
SLPoint sl_point;
reference_line_info->reference_line().XYToSL({p.x(), p.y()}, &sl_point);
start_s = std::fmin(start_s, sl_point.s());
end_s = std::fmax(end_s, sl_point.s());
start_l = std::fmin(start_l, sl_point.l());
end_l = std::fmax(end_l, sl_point.l());
}
if (reference_line_info->IsChangeLanePath()) {
constexpr double kLateralShift = 2.5;
if (end_l < -kLateralShift || start_l > kLateralShift) {
continue;
}
}
判断车与障碍物是否同一方向行驶,同时设置安全距离。之后判断距离障碍物距离是否安全。
// Raw estimation on whether same direction with ADC or not based on
// prediction trajectory
bool same_direction = true;
if (obstacle->HasTrajectory()) {
double obstacle_moving_direction =
obstacle->Trajectory().trajectory_point(0).path_point().theta();
const auto& vehicle_state = reference_line_info->vehicle_state();
double vehicle_moving_direction = vehicle_state.heading();
if (vehicle_state.gear() == canbus::Chassis::GEAR_REVERSE) {
vehicle_moving_direction =
common::math::NormalizeAngle(vehicle_moving_direction + M_PI);
}
double heading_difference = std::abs(common::math::NormalizeAngle(
obstacle_moving_direction - vehicle_moving_direction));
same_direction = heading_difference < (M_PI / 2.0);
}
// TODO(All) move to confs
constexpr double kSafeTimeOnSameDirection = 3.0;
constexpr double kSafeTimeOnOppositeDirection = 5.0;
constexpr double kForwardMinSafeDistanceOnSameDirection = 3.0;
constexpr double kBackwardMinSafeDistanceOnSameDirection = 4.0;
constexpr double kForwardMinSafeDistanceOnOppositeDirection = 50.0;
constexpr double kBackwardMinSafeDistanceOnOppositeDirection = 1.0;
constexpr double kDistanceBuffer = 0.5;
double kForwardSafeDistance = 0.0;
double kBackwardSafeDistance = 0.0;
if (same_direction) {
kForwardSafeDistance =
std::fmax(kForwardMinSafeDistanceOnSameDirection,
(ego_v - obstacle->speed()) * kSafeTimeOnSameDirection);
kBackwardSafeDistance =
std::fmax(kBackwardMinSafeDistanceOnSameDirection,
(obstacle->speed() - ego_v) * kSafeTimeOnSameDirection);
} else {
kForwardSafeDistance =
std::fmax(kForwardMinSafeDistanceOnOppositeDirection,
(ego_v + obstacle->speed()) * kSafeTimeOnOppositeDirection);
kBackwardSafeDistance = kBackwardMinSafeDistanceOnOppositeDirection;
}
判断障碍物距离是否安全,相关代码具体实现在最后。
if (HysteresisFilter(ego_start_s - end_s, kBackwardSafeDistance,
kDistanceBuffer, obstacle->IsLaneChangeBlocking()) &&
HysteresisFilter(start_s - ego_end_s, kForwardSafeDistance,
kDistanceBuffer, obstacle->IsLaneChangeBlocking())) {
reference_line_info->path_decision()
->Find(obstacle->Id())
->SetLaneChangeBlocking(true);
ADEBUG << "Lane Change is blocked by obstacle" << obstacle->Id();
return false;
} else {
reference_line_info->path_decision()
->Find(obstacle->Id())
->SetLaneChangeBlocking(false);
}
}
return true;
}
一种静态函数,用于估计前面某个范围内的障碍物是否通过光束扫描在其自身后面阻挡了过多的空间感知功能。即障碍物是否全部在扫描范围内。
首先获取车辆状态,然后计算扫描角度,如果障碍物为虚拟障碍物,则跳过该障碍物,否则,
对于每一个分辨率上的扫描角度,做以下工作:判度障碍物是否在扫描范围内。
bool LaneChangeDecider::IsPerceptionBlocked(
const ReferenceLineInfo& reference_line_info,
const double search_beam_length, const double search_beam_radius_intensity,
const double search_range, const double is_block_angle_threshold) {
const auto& vehicle_state = reference_line_info.vehicle_state();
const common::math::Vec2d adv_pos(vehicle_state.x(), vehicle_state.y());
const double adv_heading = vehicle_state.heading();
double left_most_angle =
common::math::NormalizeAngle(adv_heading + 0.5 * search_range);
double right_most_angle =
common::math::NormalizeAngle(adv_heading - 0.5 * search_range);
bool right_most_found = false;
for (auto* obstacle :
reference_line_info.path_decision().obstacles().Items()) {
if (obstacle->IsVirtual()) {
ADEBUG << "skip one virtual obstacle";
continue;
}
const auto& obstacle_polygon = obstacle->PerceptionPolygon();
for (double search_angle = 0.0; search_angle < search_range;
search_angle += search_beam_radius_intensity) {
common::math::Vec2d search_beam_end(search_beam_length, 0.0);
const double beam_heading = common::math::NormalizeAngle(
adv_heading - 0.5 * search_range + search_angle);
search_beam_end.SelfRotate(beam_heading);
search_beam_end += adv_pos;
common::math::LineSegment2d search_beam(adv_pos, search_beam_end);
if (!right_most_found && obstacle_polygon.HasOverlap(search_beam)) {
right_most_found = true;
right_most_angle = beam_heading;
}
if (right_most_found && !obstacle_polygon.HasOverlap(search_beam)) {
left_most_angle = beam_heading - search_angle;
break;
}
}
if (!right_most_found) {
// obstacle is not in search range
continue;
}
if (std::abs(common::math::NormalizeAngle(
left_most_angle - right_most_angle)) > is_block_angle_threshold) {
return true;
}
}
return false;
}
bool LaneChangeDecider::HysteresisFilter(const double obstacle_distance,
const double safe_distance,
const double distance_buffer,
const bool is_obstacle_blocking) {
if (is_obstacle_blocking) {
return obstacle_distance < safe_distance + distance_buffer;
} else {
return obstacle_distance < safe_distance - distance_buffer;
}
}