pbf_gatekeeper.cc 8.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/******************************************************************************
 * Copyright 2018 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/
#include "modules/perception/fusion/lib/gatekeeper/pbf_gatekeeper/pbf_gatekeeper.h"

18
#include "cyber/common/file.h"
X
Xiangquan Xiao 已提交
19
#include "modules/perception/base/object_types.h"
20 21 22 23 24 25 26 27
#include "modules/perception/fusion/base/base_init_options.h"
#include "modules/perception/fusion/lib/gatekeeper/pbf_gatekeeper/proto/pbf_gatekeeper_config.pb.h"
#include "modules/perception/lib/config_manager/config_manager.h"

namespace apollo {
namespace perception {
namespace fusion {

28
using cyber::common::GetAbsolutePath;
29

30 31 32 33 34 35 36 37 38 39
PbfGatekeeper::PbfGatekeeper() {}

PbfGatekeeper::~PbfGatekeeper() {}

bool PbfGatekeeper::Init() {
  BaseInitOptions options;
  if (!GetFusionInitOptions("PbfGatekeeper", &options)) {
    return false;
  }

40
  std::string woork_root_config = GetAbsolutePath(
41
      lib::ConfigManager::Instance()->work_root(), options.root_dir);
42

43
  std::string config = GetAbsolutePath(woork_root_config, options.conf_file);
44 45
  PbfGatekeeperConfig params;

46
  if (!cyber::common::GetProtoFromFile(config, &params)) {
D
Dong Li 已提交
47
    AERROR << "Read config failed: " << config;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    return false;
  }
  params_.publish_if_has_lidar = params.publish_if_has_lidar();
  params_.publish_if_has_radar = params.publish_if_has_radar();
  params_.publish_if_has_camera = params.publish_if_has_camera();
  params_.use_camera_3d = params.use_camera_3d();
  params_.min_radar_confident_distance = params.min_radar_confident_distance();
  params_.max_radar_confident_angle = params.max_radar_confident_angle();
  params_.min_camera_publish_distance = params.min_camera_publish_distance();
  params_.invisible_period_threshold = params.invisible_period_threshold();
  params_.existance_threshold = params.existance_threshold();
  params_.radar_existance_threshold = params.radar_existance_threshold();
  params_.toic_threshold = params.toic_threshold();
  params_.use_track_time_pub_strategy = params.use_track_time_pub_strategy();
  params_.pub_track_time_thresh = params.pub_track_time_thresh();
  return true;
}

std::string PbfGatekeeper::Name() const { return "PbfGatekeeper"; }

bool PbfGatekeeper::AbleToPublish(const TrackPtr &track) {
  bool invisible_in_lidar = !(track->IsLidarVisible());
  bool invisible_in_radar = !(track->IsRadarVisible());
  bool invisible_in_camera = !(track->IsCameraVisible());
  if (invisible_in_lidar && invisible_in_radar &&
      (!params_.use_camera_3d || invisible_in_camera)) {
W
Weide Zhang 已提交
74
    auto sensor_obj = track->GetFusedObject();
A
Aaron Xiao 已提交
75 76
    if (sensor_obj != nullptr && sensor_obj->GetBaseObject()->sub_type !=
                                     base::ObjectSubType::TRAFFICCONE) {
W
Weide Zhang 已提交
77 78
      return false;
    }
79
  }
X
 
Xiangquan Xiao 已提交
80
  time_t rawtime = static_cast<time_t>(track->GetFusedObject()->GetTimestamp());
81 82 83 84 85

  // use thread-safe localtime_r instead of localtime
  struct tm timeinfo;
  localtime_r(&rawtime, &timeinfo);
  bool is_night = (timeinfo.tm_hour >= 23);
X
 
Xiangquan Xiao 已提交
86
  if (!LidarAbleToPublish(track) && !RadarAbleToPublish(track, is_night) &&
87
      !CameraAbleToPublish(track, is_night)) {
88 89 90 91 92
    return false;
  }

  track->AddTrackedTimes();
  if (params_.use_track_time_pub_strategy &&
D
Dong Li 已提交
93 94
      track->GetTrackedTimes() <=
          static_cast<size_t>(params_.pub_track_time_thresh)) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    return false;
  }
  return true;
}

bool PbfGatekeeper::LidarAbleToPublish(const TrackPtr &track) {
  bool visible_in_lidar = track->IsLidarVisible();
  if (params_.publish_if_has_lidar && visible_in_lidar) {
    return true;
  }
  return false;
}

bool PbfGatekeeper::RadarAbleToPublish(const TrackPtr &track, bool is_night) {
  bool visible_in_radar = track->IsRadarVisible();
  SensorObjectConstPtr radar_object = track->GetLatestRadarObject();
  if (params_.publish_if_has_radar && visible_in_radar &&
      radar_object != nullptr) {
    if (radar_object->GetSensorId() == "radar_front") {
      return false;
      // if (radar_object->GetBaseObject()->radar_supplement.range >
      //         params_.min_radar_confident_distance &&
      //     radar_object->GetBaseObject()->radar_supplement.angle <
      //         params_.max_radar_confident_angle) {
      //   double heading_v =
      //       std::abs(track->GetFusedObject()->GetBaseObject()->velocity.dot(
      //           track->GetFusedObject()->GetBaseObject()->direction));
      //   double toic_p = track->GetToicProb();
      //   auto set_velocity_to_zero = [heading_v, track]() {
      //     if (heading_v < 0.3) {
      //       track->GetFusedObject()->GetBaseObject()->velocity.setZero();
      //     }
      //   };
      //   if (!is_night) {
      //     if (toic_p > params_.toic_threshold) {
      //       set_velocity_to_zero();
      //       return true;
      //     }
      //   } else {
      //     // the velocity buffer is [-3, +3] m/s
      //     double v_ct = 4.0;
      //     double v_slope = 1.0;
      //     auto heading_v_decision = [](double x, double c, double k) {
      //       x = x - c;
      //       return 0.5 + 0.5 * x * k / std::sqrt(1 + x * x * k * k);
      //     };
      //     auto fuse_two_probabilities = [](double p1, double p2) {
      //       double p = (p1 * p2) / (2 * p1 * p2 + 1 - p1 - p2);
      //       p = std::min(1.0 - std::numeric_limits<float>::epsilon(), p);
      //       return p;
      //     };

      //     double min_toic_p = 0.2;
      //     toic_p = std::max(min_toic_p, toic_p);
      //     double v_p = heading_v_decision(heading_v, v_ct, v_slope);
      //     double p = fuse_two_probabilities(toic_p, v_p);
      //     if (p > 0.5) {
      //       set_velocity_to_zero();
      //       return true;
      //     }
      //   }
      // }
    } else if (radar_object->GetSensorId() == "radar_rear") {
D
Dong Li 已提交
158 159 160 161 162
      ADEBUG << "radar_rear: min_dis: " << params_.min_radar_confident_distance
             << " obj dist: "
             << radar_object->GetBaseObject()->radar_supplement.range
             << " track_id: " << track->GetTrackId()
             << " exist_prob: " << track->GetExistanceProb();
163 164 165 166 167 168 169 170 171 172 173 174 175 176
      if (radar_object->GetBaseObject()->radar_supplement.range >
              params_.min_radar_confident_distance &&
          (radar_object->GetBaseObject()->velocity.norm() > 4.0 ||
           track->GetExistanceProb() > params_.radar_existance_threshold)) {
        return true;
      }
    }
  }
  return false;
}

bool PbfGatekeeper::CameraAbleToPublish(const TrackPtr &track, bool is_night) {
  bool visible_in_camera = track->IsCameraVisible();
  SensorId2ObjectMap &camera_objects = track->GetCameraObjects();
177 178
  auto iter = camera_objects.find("front_6mm");
  auto iter_narrow = camera_objects.find("front_12mm");
179 180 181 182 183 184
  iter = iter != camera_objects.end() ? iter : iter_narrow;
  if (params_.publish_if_has_camera && visible_in_camera &&
      iter != camera_objects.end() && params_.use_camera_3d && !is_night) {
    SensorObjectConstPtr camera_object = iter->second;
    double range =
        camera_object->GetBaseObject()->camera_supplement.local_center.norm();
T
 
Tae Eun Choe 已提交
185 186 187 188 189 190 191
    // If sub_type of object is traffic cone publish it regardless of range
    if ((camera_object->GetBaseObject()->sub_type ==
             base::ObjectSubType::TRAFFICCONE) ||
        (range >= params_.min_camera_publish_distance ||
          ((camera_object->GetBaseObject()->type ==
             base::ObjectType::UNKNOWN_UNMOVABLE) &&
           (range >= params_.min_camera_publish_distance)))) {
192 193 194
      double exist_prob = track->GetExistanceProb();
      if (exist_prob > params_.existance_threshold) {
        static int cnt_cam = 1;
D
Dong Li 已提交
195
        AINFO << "publish camera only object : cnt =  " << cnt_cam;
196 197 198 199 200 201 202 203 204 205 206
        cnt_cam++;
        return true;
      }
    }
  }
  return false;
}

}  // namespace fusion
}  // namespace perception
}  // namespace apollo