-
Notifications
You must be signed in to change notification settings - Fork 1
/
prediction_layer.cpp
87 lines (68 loc) · 2.38 KB
/
prediction_layer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// Created by aoool on 28.10.18.
//
#include "prediction_layer.hpp"
#include "helpers.hpp"
#include <vector>
PredictionLayer::PredictionLayer(const PathPlannerConfig& path_planner_config, LocalizationLayer& localization_layer):
path_planner_config_{path_planner_config},
localization_layer_{localization_layer},
cache_predictions_{5},
last_update_info_{0, 0.0, 0.0, 0.0}
{
}
std::pair<Car, Car>
PredictionLayer::GetPredictionForCar(const Car& car, double t) const
{
const double time_diff = path_planner_config_.frequency_s;
double s = Calc1DPosition(static_cast<double>(car.S()), car.Vs(), car.As(), t);
double s_prev = Calc1DPosition(static_cast<double>(car.S()), car.Vs(), car.As(), t - time_diff);
double d = Calc1DPosition(car.D(), car.Vd(), car.Ad(), t);
double d_prev = Calc1DPosition(car.D(), car.Vd(), car.Ad(), t - time_diff);
double vs = Calc1DVelocity(s_prev, s, time_diff);
double vd = Calc1DVelocity(d_prev, d, time_diff);
double velocity = EuclideanNorm({ vs, vd, });
double time = car.T() + t;
return { car,
CarBuilder()
.SetId(car.Id())
.SetState(car.State())
.SetTime(time)
.SetCoordinateS(s)
.SetCoordinateD(d)
.SetVelocityS(vs)
.SetVelocityD(vd)
.SetAccelerationS(car.As())
.SetAccelerationD(car.Ad())
.Build(),
};
}
std::map<Car, Car>
PredictionLayer::GetPredictionsForCars(const std::vector<Car>& cars, double t) const {
std::map<Car, Car> pred_cars{};
for (const auto& car : cars) {
pred_cars.insert(GetPredictionForCar(car, t));
}
return pred_cars;
}
std::map<Car, Car> PredictionLayer::GetPredictions(double pred_time, double start_time)
{
auto cars = localization_layer_.GetCars();
if (cars.empty()) {
return {};
}
last_update_info_ = { cars.size(), cars[0].T(), pred_time, start_time, };
if (cache_predictions_.exists(last_update_info_)) {
return cache_predictions_.get(last_update_info_);
}
double offset_time = start_time - cars[0].T();
if (!IsEqual(offset_time, 0.0)) {
auto offset_car_preds = GetPredictionsForCars(cars, offset_time);
for (auto& car : cars) {
car = offset_car_preds.at(car);
}
}
auto predictions = GetPredictionsForCars(cars, pred_time);
cache_predictions_.put(last_update_info_, predictions);
return predictions;
}