obstacle.cc 13.3 KB
Newer Older
K
kechxu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/******************************************************************************
 * Copyright 2017 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/prediction/container/obstacles/obstacle.h"

19
#include <iomanip>
20
#include <cmath>
21 22

#include "modules/common/log.h"
K
kechxu 已提交
23 24
#include "modules/common/math/math_utils.h"
#include "modules/prediction/common/prediction_gflags.h"
25

K
kechxu 已提交
26 27 28
namespace apollo {
namespace prediction {

29
using apollo::perception::PerceptionObstacle;
K
Kecheng Xu 已提交
30
using apollo::common::math::KalmanFilter;
31
using apollo::common::ErrorCode;
32
using apollo::common::Point3D;
33

34
std::mutex Obstacle::mutex_;
35

36 37 38
namespace {

double Damp(const double x, const double sigma) {
K
kechxu 已提交
39
  return 1 / (1 + exp(1 / (std::fabs(x) + sigma)));
40 41 42 43
}

}  // namespace

44 45 46 47 48
Obstacle::Obstacle() : 
    id_(-1),
    type_(PerceptionObstacle::UNKNOWN_MOVABLE),
    feature_history_(0),
    kf_motion_tracker_(),
K
kechxu 已提交
49
    kf_lane_trackers_(0) {
K
kechxu 已提交
50 51 52 53

}

Obstacle::~Obstacle() {
54 55 56
  id_ = -1;
  type_ = PerceptionObstacle::UNKNOWN_UNMOVABLE;
  feature_history_.clear();
K
kechxu 已提交
57
  kf_lane_trackers_.clear();
58
  // TODO(author) current_lanes_.clear();
K
kechxu 已提交
59 60
}

K
Kecheng Xu 已提交
61
int Obstacle::id() const {
62
  std::lock_guard<std::mutex> lock(mutex_);
K
Kecheng Xu 已提交
63 64 65 66
  return id_;
}

double Obstacle::timestamp() const {
67
  std::lock_guard<std::mutex> lock(mutex_);
K
Kecheng Xu 已提交
68 69 70 71 72 73 74 75
  if (feature_history_.size() > 0) {
    return feature_history_.front().timestamp();
  } else {
    return 0.0;
  }
}

const Feature& Obstacle::feature(size_t i) {
76
  std::lock_guard<std::mutex> lock(mutex_);
K
Kecheng Xu 已提交
77 78 79 80 81
  CHECK(i < feature_history_.size());
  return feature_history_[i];
}

Feature* Obstacle::mutable_feature(size_t i) {
82
  std::lock_guard<std::mutex> lock(mutex_);
K
Kecheng Xu 已提交
83 84 85 86 87
  CHECK(i < feature_history_.size());
  return &feature_history_[i];
}

const Feature& Obstacle::latest_feature() {
88
  std::lock_guard<std::mutex> lock(mutex_);
K
Kecheng Xu 已提交
89 90 91 92 93
  CHECK(feature_history_.size() > 0);
  return feature_history_.front();
}

Feature* Obstacle::mutable_latest_feature() {
94
  std::lock_guard<std::mutex> lock(mutex_);
K
Kecheng Xu 已提交
95 96 97 98 99 100

  CHECK(feature_history_.size() > 0);
  return &(feature_history_.front());
}

size_t Obstacle::history_size() const {
101
  std::lock_guard<std::mutex> lock(mutex_);
K
Kecheng Xu 已提交
102 103 104 105
  return feature_history_.size();
}

const KalmanFilter<double, 4, 2, 0>& Obstacle::kf_lane_tracker(
K
kechxu 已提交
106 107 108
    const std::string& lane_id) {
  CHECK(kf_lane_trackers_.find(lane_id) != kf_lane_trackers_.end());
  return kf_lane_trackers_[lane_id];
K
Kecheng Xu 已提交
109 110
}

111 112 113 114 115
void Obstacle::Insert(const PerceptionObstacle& perception_obstacle,
                      const double timestamp) {
  std::lock_guard<std::mutex> lock(mutex_);
  if (feature_history_.size() > 0 &&
      timestamp <= feature_history_.front().timestamp()) {
116 117
    AINFO << "Obstacle [" << id_ << "] received an older frame ["
          << timestamp << "] than the most recent timestamp [ "
118 119 120 121 122 123 124 125 126 127 128 129 130
          << feature_history_.front().timestamp() << "].";
    return;
  }

  Feature feature;
  if (SetId(perception_obstacle, &feature) == ErrorCode::PREDICTION_ERROR) {
    return;
  }
  if (SetType(perception_obstacle) == ErrorCode::PREDICTION_ERROR) {
    return;
  }
  SetTimestamp(perception_obstacle, timestamp, &feature);
  SetPosition(perception_obstacle, &feature);
131
  SetVelocity(perception_obstacle, &feature);
132 133
  SetAcceleration(&feature);
  SetTheta(perception_obstacle, &feature);
134 135
  InitKFMotionTracker(&feature);
  UpdateKFMotionTracker(&feature);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}

ErrorCode Obstacle::SetId(const PerceptionObstacle& perception_obstacle,
                          Feature* feature) {
  if (!perception_obstacle.has_id()) {
    AERROR << "Obstacle has no ID.";
    return ErrorCode::PREDICTION_ERROR;
  }

  int id = perception_obstacle.id();
  if (id_ < 0) {
    id_ = id;
    AINFO << "Obstacle set id [" << id_ << "].";
  } else {
    if (id_ != id) {
      AERROR << "Obstacle [" << id_ << "] has a mismatched ID [" << id
             << "] from perception obstacle.";
      return ErrorCode::PREDICTION_ERROR;
    } else {
      feature->set_id(id);
    }
  }
  return ErrorCode::OK;
}

ErrorCode Obstacle::SetType(const PerceptionObstacle& perception_obstacle) {
  if (perception_obstacle.has_type()) {
    type_ = perception_obstacle.type();
K
kechxu 已提交
164
    ADEBUG << "Obstacle [" << id_ << "] set type [" << type_ << "].";
165 166 167 168 169 170
  } else {
    AERROR << "Obstacle [" << id_ << "] has no type.";
    return ErrorCode::PREDICTION_ERROR;
  }
  return ErrorCode::OK;
}
K
kechxu 已提交
171

172 173 174 175 176 177 178 179 180
void Obstacle::SetTimestamp(const PerceptionObstacle& perception_obstacle,
                            const double timestamp, Feature* feature) {
  double ts = timestamp;
  if (perception_obstacle.has_timestamp() &&
      perception_obstacle.timestamp() > 0.0) {
    ts = perception_obstacle.timestamp();
  }
  feature->set_timestamp(ts);

K
kechxu 已提交
181 182
  ADEBUG << "Obstacle [" << id_ << "] set timestamp [" << std::fixed
         << std::setprecision(6) << ts << "].";
K
kechxu 已提交
183 184
}

185 186 187 188 189

void Obstacle::SetPosition(const PerceptionObstacle& perception_obstacle,
                           Feature* feature) {
  double x = 0.0;
  double y = 0.0;
190
  double z = 0.0;
191 192 193 194 195 196 197 198

  if (perception_obstacle.has_position()) {
    if (perception_obstacle.position().has_x()) {
      x = perception_obstacle.position().x();
    }
    if (perception_obstacle.position().has_y()) {
      y = perception_obstacle.position().y();
    }
199
    if (perception_obstacle.position().has_z()) {
C
Calvin Miao 已提交
200
      z = perception_obstacle.position().z();
201
    }
202 203 204 205
  }

  feature->mutable_position()->set_x(x);
  feature->mutable_position()->set_y(y);
206
  feature->mutable_position()->set_z(z);
207

K
kechxu 已提交
208 209 210 211
  ADEBUG << "Obstacle [" << id_ << "] set position [" << std::fixed
         << std::setprecision(6) << x << ", " << std::fixed
         << std::setprecision(6) << y << ", " << std::fixed
         << std::setprecision(6) << z << "].";
212 213
}

214 215
void Obstacle::SetVelocity(const PerceptionObstacle& perception_obstacle,
                           Feature* feature) {
216 217 218
  double velocity_x = 0.0;
  double velocity_y = 0.0;
  double velocity_z = 0.0;
219 220 221

  if (perception_obstacle.has_velocity()) {
    if (perception_obstacle.velocity().has_x()) {
222
      velocity_x = perception_obstacle.velocity().x();
223 224
    }
    if (perception_obstacle.velocity().has_y()) {
225 226 227 228
      velocity_y = perception_obstacle.velocity().y();
    }
    if (perception_obstacle.velocity().has_z()) {
      velocity_z = perception_obstacle.velocity().z();
229 230 231
    }
  }

232 233 234
  feature->mutable_velocity()->set_x(velocity_x);
  feature->mutable_velocity()->set_y(velocity_y);
  feature->mutable_velocity()->set_z(velocity_z);
235

236 237
  double speed = std::hypot(std::hypot(velocity_x, velocity_y), velocity_z);
  double velocity_heading = std::atan2(velocity_y, velocity_x);
238 239 240
  feature->set_velocity_heading(velocity_heading);
  feature->set_speed(speed);

K
kechxu 已提交
241 242 243 244 245 246
  ADEBUG << "Obstacle [" << id_ << "] set velocity [" << std::fixed
         << std::setprecision(6) << velocity_x << ", " << std::fixed
         << std::setprecision(6) << velocity_y << ", " << std::fixed
         << std::setprecision(6) << velocity_z << "], "
         << "velocity heading [" << velocity_heading << "] and speed [" << speed
         << "].";
247
}
248

249 250 251 252 253 254 255 256 257 258 259 260
void Obstacle::SetAcceleration(Feature* feature) {
  double acc_x = 0.0;
  double acc_y = 0.0;
  double acc_z = 0.0;

  if (feature_history_.size() > 0) {
    double curr_ts = feature->timestamp();
    double prev_ts = feature_history_.front().timestamp();

    const Point3D& curr_velocity = feature->velocity();
    const Point3D& prev_velocity = feature_history_.front().velocity();

K
kechxu 已提交
261
    if (apollo::common::math::DoubleCompare(curr_ts, prev_ts) == 1) {
262 263 264 265 266 267 268
      double damping_x = Damp(curr_velocity.x(), 0.001);
      double damping_y = Damp(curr_velocity.y(), 0.001);
      double damping_z = Damp(curr_velocity.z(), 0.001);

      acc_x = (curr_velocity.x() - prev_velocity.x()) / (curr_ts - prev_ts);
      acc_y = (curr_velocity.y() - prev_velocity.y()) / (curr_ts - prev_ts);
      acc_z = (curr_velocity.z() - prev_velocity.z()) / (curr_ts - prev_ts);
K
kechxu 已提交
269 270 271 272 273 274 275

      acc_x = apollo::common::math::Clamp(
          acc_x * damping_x, FLAGS_min_acc, FLAGS_max_acc);
      acc_y = apollo::common::math::Clamp(
          acc_y * damping_y, FLAGS_min_acc, FLAGS_max_acc);
      acc_z = apollo::common::math::Clamp(
          acc_z * damping_z, FLAGS_min_acc, FLAGS_max_acc);
276 277 278 279 280 281 282 283 284
    }
  }

  feature->mutable_acceleration()->set_x(acc_x);
  feature->mutable_acceleration()->set_y(acc_y);
  feature->mutable_acceleration()->set_z(acc_z);
  double acc = std::hypot(std::hypot(acc_x, acc_y), acc_z);
  feature->set_acc(acc);

K
kechxu 已提交
285 286 287 288 289
  ADEBUG << "Obstacle [" << id_ << "] set acc [" << std::fixed
         << std::setprecision(6) << acc_x << ", " << std::fixed
         << std::setprecision(6) << acc_y << ", " << std::fixed
         << std::setprecision(6) << acc_z << "], "
         << "and acc [" << acc << "].";
290 291 292 293 294 295 296 297 298 299 300
}


void Obstacle::SetTheta(const PerceptionObstacle& perception_obstacle,
                        Feature* feature) {
  double theta = 0.0;
  if (perception_obstacle.has_theta()) {
    theta = perception_obstacle.theta();
  }
  feature->set_theta(theta);

K
kechxu 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
  ADEBUG << "Obstacle [" << id_ << "] set theta [" << std::fixed
         << std::setprecision(6) << theta << "].";
}

void Obstacle::SetLengthWidthHeight(
    const PerceptionObstacle& perception_obstacle, Feature* feature) {
  double length = 0.0;
  double width = 0.0;
  double height = 0.0;

  if (perception_obstacle.has_length()) {
    length = perception_obstacle.length();
  }
  if (perception_obstacle.has_width()) {
    width = perception_obstacle.width();
  }
  if (perception_obstacle.has_height()) {
    height = perception_obstacle.height();
  }

  feature->set_length(length);
  feature->set_width(width);
  feature->set_height(height);

  ADEBUG << "Obstacle [" << id_ << "] set dimension [" << std::fixed
         << std::setprecision(6) << length << ", " << std::fixed
         << std::setprecision(6) << width << ", " << std::fixed
         << std::setprecision(6) << height << "].";
}

331
void Obstacle::InitKFMotionTracker(Feature* feature) {
K
kechxu 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  // Set transition matrix F
  Eigen::Matrix<double, 6, 6> F;
  F.setIdentity();
  kf_motion_tracker_.SetTransitionMatrix(F);

  // Set observation matrix H
  Eigen::Matrix<double, 2, 6> H;
  H.setZero();
  H(0, 0) = 1.0;
  H(1, 1) = 1.0;
  kf_motion_tracker_.SetObservationMatrix(H);

  // Set covariance of transition noise matrix Q
  Eigen::Matrix<double, 6, 6> Q;
  Q.setIdentity();
  Q *= FLAGS_q_var;
  kf_motion_tracker_.SetTransitionNoise(Q);

  // Set observation noise matrix R
  Eigen::Matrix<double, 2, 2> R;
  R.setIdentity();
  R *= FLAGS_r_var;
  kf_motion_tracker_.SetObservationNoise(R);

  // Set current state covariance matrix P
  Eigen::Matrix<double, 6, 6> P;
  P.setIdentity();
  P *= FLAGS_p_var;

361 362 363 364 365 366 367 368 369 370
  // Set initial state
  Eigen::Matrix<double, 6, 1> x;
  x(0, 0) = feature->position().x();
  x(1, 0) = feature->position().y();
  x(2, 0) = feature->velocity().x();
  x(3, 0) = feature->velocity().y();
  x(4, 0) = feature->acceleration().x();
  x(5, 0) = feature->acceleration().y();

  kf_motion_tracker_.SetStateEstimate(x, P);
371 372
}

373 374 375 376
void Obstacle::UpdateKFMotionTracker(Feature* feature) {  
  double delta_ts = 0.0;  
  if (feature_history_.size() > 0) {
    delta_ts = feature->timestamp() - feature_history_.front().timestamp();
K
kechxu 已提交
377
  }
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  if (delta_ts <= FLAGS_double_precision) {
    return;
  }
  // Set tansition matrix and predict
  auto F = kf_motion_tracker_.GetTransitionMatrix();
  F(0, 2) = delta_ts;
  F(0, 4) = delta_ts;
  F(1, 3) = 0.5 * delta_ts * delta_ts;
  F(1, 5) = 0.5 * delta_ts * delta_ts;
  F(2, 4) = delta_ts;
  F(3, 5) = delta_ts;
  kf_motion_tracker_.SetTransitionMatrix(F);
  kf_motion_tracker_.Predict();

  // Set observation and correct
  Eigen::Matrix<double, 2, 1> z;
  z(0, 0) = feature->position().x();
  z(1, 0) = feature->position().y();
  kf_motion_tracker_.Correct(z);
K
kechxu 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

  UpdateMotionBelief(feature);
}

void Obstacle::UpdateMotionBelief(Feature* feature) {
  auto state = kf_motion_tracker_.GetStateEstimate();
  feature->mutable_t_position()->set_x(state(0, 0));
  feature->mutable_t_position()->set_y(state(1, 0));
  feature->mutable_t_velocity()->set_x(state(2, 0));
  feature->mutable_t_velocity()->set_y(state(3, 0));
  feature->set_t_velocity_heading(std::atan2(state(3, 0), state(2, 0)));
  double acc_x =
      apollo::common::math::Clamp(state(4, 0), FLAGS_min_acc, FLAGS_max_acc);
  double acc_y =
      apollo::common::math::Clamp(state(5, 0), FLAGS_min_acc, FLAGS_max_acc);
  feature->mutable_t_acceleration()->set_x(acc_x);
  feature->mutable_t_acceleration()->set_y(acc_y);
  ADEBUG << "Obstacle [" << id_ << "] "
         << "set tracked position [" << feature->t_position().x() << ", "
         << feature->t_position().y() << "] "
         << "and tracked velocity [" << feature->t_velocity().x() << ", "
         << feature->t_velocity().y() << "] "
         << "and tracked acc [" << feature->t_acceleration().x() << ", "
         << feature->t_acceleration().y() << "] "
         << "and tracked velocity heading [" << feature->t_velocity_heading()
         << "].";
}

K
kechxu 已提交
425 426
}  // namespace prediction
}  // namespace apollo