scenario_simulator_v2 C++ API
sim_model_delay_steer_map_acc_geared.hpp
Go to the documentation of this file.
1 // Copyright 2023 The Autoware Foundation.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SIMPLE_PLANNING_SIMULATOR__VEHICLE_MODEL__SIM_MODEL_DELAY_STEER_MAP_ACC_GEARED_HPP_
16 #define SIMPLE_PLANNING_SIMULATOR__VEHICLE_MODEL__SIM_MODEL_DELAY_STEER_MAP_ACC_GEARED_HPP_
17 
18 #include <deque>
19 #include <fstream>
20 #include <iostream>
21 #include <queue>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 
27 #include "eigen3/Eigen/Core"
28 #include "eigen3/Eigen/LU"
29 
32 {
33 inline bool isIncreasing(const std::vector<double> & x)
34 {
35  if (x.empty()) {
36  throw std::invalid_argument("Points is empty.");
37  }
38 
39  for (size_t i = 0; i < x.size() - 1; ++i) {
40  if (x.at(i) >= x.at(i + 1)) {
41  return false;
42  }
43  }
44 
45  return true;
46 }
47 
48 inline bool isNotDecreasing(const std::vector<double> & x)
49 {
50  if (x.empty()) {
51  throw std::invalid_argument("Points is empty.");
52  }
53 
54  for (size_t i = 0; i < x.size() - 1; ++i) {
55  if (x.at(i) > x.at(i + 1)) {
56  return false;
57  }
58  }
59 
60  return true;
61 }
62 
63 inline std::vector<double> validateKeys(
64  const std::vector<double> & base_keys, const std::vector<double> & query_keys)
65 {
66  // when vectors are empty
67  if (base_keys.empty() || query_keys.empty()) {
68  throw std::invalid_argument("Points is empty.");
69  }
70 
71  // when size of vectors are less than 2
72  if (base_keys.size() < 2) {
73  throw std::invalid_argument(
74  "The size of points is less than 2. base_keys.size() = " + std::to_string(base_keys.size()));
75  }
76 
77  // when indices are not sorted
78  if (!isIncreasing(base_keys) || !isNotDecreasing(query_keys)) {
79  throw std::invalid_argument("Either base_keys or query_keys is not sorted.");
80  }
81 
82  // when query_keys is out of base_keys (This function does not allow exterior division.)
83  constexpr double epsilon = 1e-3;
84  if (
85  query_keys.front() < base_keys.front() - epsilon ||
86  base_keys.back() + epsilon < query_keys.back()) {
87  throw std::invalid_argument("query_keys is out of base_keys");
88  }
89 
90  // NOTE: Due to calculation error of double, a query key may be slightly out of base keys.
91  // Therefore, query keys are cropped here.
92  auto validated_query_keys = query_keys;
93  validated_query_keys.front() = std::max(validated_query_keys.front(), base_keys.front());
94  validated_query_keys.back() = std::min(validated_query_keys.back(), base_keys.back());
95 
96  return validated_query_keys;
97 }
98 
99 template <class T>
101  const std::vector<double> & base_keys, const std::vector<T> & base_values)
102 {
103  // when vectors are empty
104  if (base_keys.empty() || base_values.empty()) {
105  throw std::invalid_argument("Points is empty.");
106  }
107 
108  // when size of vectors are less than 2
109  if (base_keys.size() < 2 || base_values.size() < 2) {
110  throw std::invalid_argument(
111  "The size of points is less than 2. base_keys.size() = " + std::to_string(base_keys.size()) +
112  ", base_values.size() = " + std::to_string(base_values.size()));
113  }
114 
115  // when sizes of indices and values are not same
116  if (base_keys.size() != base_values.size()) {
117  throw std::invalid_argument("The size of base_keys and base_values are not the same.");
118  }
119 }
120 } // namespace interpolation_utils
121 
123 namespace interpolation
124 {
125 double lerp(const double src_val, const double dst_val, const double ratio);
126 
127 std::vector<double> lerp(
128  const std::vector<double> & base_keys, const std::vector<double> & base_values,
129  const std::vector<double> & query_keys);
130 
131 double lerp(
132  const std::vector<double> & base_keys, const std::vector<double> & base_values,
133  const double query_key);
134 
135 } // namespace interpolation
136 
137 using Table = std::vector<std::vector<std::string>>;
138 using Map = std::vector<std::vector<double>>;
139 
142 {
143 public:
144  explicit CSVLoader(const std::string & csv_path);
145 
146  bool readCSV(Table & result, const char delim = ',');
147  static bool validateData(const Table & table, const std::string & csv_path);
148  static bool validateMap(const Map & map, const bool is_col_decent);
149  static Map getMap(const Table & table);
150  static std::vector<double> getRowIndex(const Table & table);
151  static std::vector<double> getColumnIndex(const Table & table);
152  static double clampValue(
153  const double val, const std::vector<double> & ranges, const std::string & name);
154 
155 private:
156  std::string csv_path_;
157 };
158 
160 {
161 public:
163  {
164  CSVLoader csv(csv_path);
165  std::vector<std::vector<std::string>> table;
166  if (!csv.readCSV(table)) {
167  std::cerr << "[SimModelDelaySteerMapAccGeared]: failed to read acceleration map from "
168  << csv_path << std::endl;
169  return false;
170  }
171 
172  vehicle_name_ = table[0][0];
173  vel_index_ = CSVLoader::getRowIndex(table);
174  acc_index_ = CSVLoader::getColumnIndex(table);
176 
177  std::cout << "[SimModelDelaySteerMapAccGeared]: success to read acceleration map from "
178  << csv_path << std::endl;
179  return true;
180  }
181 
182  double getAcceleration(const double acc_des, const double vel) const
183  {
184  std::vector<double> interpolated_acc_vec;
185  const double clamped_vel = CSVLoader::clampValue(vel, vel_index_, "acc: vel");
186 
187  // (throttle, vel, acc) map => (throttle, acc) map by fixing vel
188  for (const auto & acc_vec : acceleration_map_) {
189  interpolated_acc_vec.push_back(interpolation::lerp(vel_index_, acc_vec, clamped_vel));
190  }
191  // calculate throttle
192  // When the desired acceleration is smaller than the throttle area, return min acc
193  // When the desired acceleration is greater than the throttle area, return max acc
194  const double clamped_acc = CSVLoader::clampValue(acc_des, acc_index_, "acceleration: acc");
195  const double acc = interpolation::lerp(acc_index_, interpolated_acc_vec, clamped_acc);
196 
197  return acc;
198  }
199  std::vector<std::vector<double>> acceleration_map_;
200 
201 private:
202  std::string vehicle_name_;
203  std::vector<double> vel_index_;
204  std::vector<double> acc_index_;
205 };
206 
208 {
209 public:
225  double vx_lim, double steer_lim, double vx_rate_lim, double steer_rate_lim, double wheelbase,
226  double dt, double acc_delay, double acc_time_constant, double steer_delay,
227  double steer_time_constant, std::string path);
228 
233 
235 
236 private:
237  const double MIN_TIME_CONSTANT;
238 
239  enum IDX {
240  X = 0,
241  Y,
242  YAW,
243  VX,
244  STEER,
245  ACCX,
246  };
247  enum IDX_U {
248  ACCX_DES = 0,
249  STEER_DES,
250  DRIVE_SHIFT,
251  };
252 
253  const double vx_lim_;
254  const double vx_rate_lim_;
255  const double steer_lim_;
256  const double steer_rate_lim_;
257  const double wheelbase_;
258 
259  std::deque<double> acc_input_queue_;
260  std::deque<double> steer_input_queue_;
261  const double acc_delay_;
262  const double acc_time_constant_;
263  const double steer_delay_;
264  const double steer_time_constant_;
265  const std::string path_;
266 
271  void initializeInputQueue(const double & dt);
272 
276  double getX() override;
277 
281  double getY() override;
282 
286  double getYaw() override;
287 
291  double getVx() override;
292 
296  double getVy() override;
297 
301  double getAx() override;
302 
306  double getWz() override;
307 
311  double getSteer() override;
312 
317  void update(const double & dt) override;
318 
324  Eigen::VectorXd calcModel(const Eigen::VectorXd & state, const Eigen::VectorXd & input) override;
325 
333  void updateStateWithGear(
334  Eigen::VectorXd & state, const Eigen::VectorXd & prev_state, const uint8_t gear,
335  const double dt);
336 };
337 
338 #endif // SIMPLE_PLANNING_SIMULATOR__VEHICLE_MODEL__SIM_MODEL_DELAY_STEER_MAP_ACC_GEARED_HPP_
Definition: sim_model_delay_steer_map_acc_geared.hpp:160
bool readAccelerationMapFromCSV(const std::string &csv_path)
Definition: sim_model_delay_steer_map_acc_geared.hpp:162
double getAcceleration(const double acc_des, const double vel) const
Definition: sim_model_delay_steer_map_acc_geared.hpp:182
std::vector< std::vector< double > > acceleration_map_
Definition: sim_model_delay_steer_map_acc_geared.hpp:199
Definition: sim_model_delay_steer_map_acc_geared.hpp:142
static Map getMap(const Table &table)
Definition: sim_model_delay_steer_map_acc_geared.cpp:144
static double clampValue(const double val, const std::vector< double > &ranges, const std::string &name)
Definition: sim_model_delay_steer_map_acc_geared.cpp:175
bool readCSV(Table &result, const char delim=',')
Definition: sim_model_delay_steer_map_acc_geared.cpp:64
static bool validateMap(const Map &map, const bool is_col_decent)
Definition: sim_model_delay_steer_map_acc_geared.cpp:92
static bool validateData(const Table &table, const std::string &csv_path)
Definition: sim_model_delay_steer_map_acc_geared.cpp:121
static std::vector< double > getRowIndex(const Table &table)
Definition: sim_model_delay_steer_map_acc_geared.cpp:157
CSVLoader(const std::string &csv_path)
Definition: sim_model_delay_steer_map_acc_geared.cpp:62
static std::vector< double > getColumnIndex(const Table &table)
Definition: sim_model_delay_steer_map_acc_geared.cpp:166
Definition: sim_model_delay_steer_map_acc_geared.hpp:208
AccelerationMap acc_map_
Definition: sim_model_delay_steer_map_acc_geared.hpp:234
~SimModelDelaySteerMapAccGeared()=default
default destructor
SimModelDelaySteerMapAccGeared(double vx_lim, double steer_lim, double vx_rate_lim, double steer_rate_lim, double wheelbase, double dt, double acc_delay, double acc_time_constant, double steer_delay, double steer_time_constant, std::string path)
constructor
Definition: sim_model_delay_steer_map_acc_geared.cpp:189
simple_planning_simulator vehicle model class to calculate vehicle dynamics
Definition: sim_model_interface.hpp:26
Definition: sim_model_delay_steer_map_acc_geared.hpp:32
std::vector< double > validateKeys(const std::vector< double > &base_keys, const std::vector< double > &query_keys)
Definition: sim_model_delay_steer_map_acc_geared.hpp:63
bool isIncreasing(const std::vector< double > &x)
Definition: sim_model_delay_steer_map_acc_geared.hpp:33
bool isNotDecreasing(const std::vector< double > &x)
Definition: sim_model_delay_steer_map_acc_geared.hpp:48
void validateKeysAndValues(const std::vector< double > &base_keys, const std::vector< T > &base_values)
Definition: sim_model_delay_steer_map_acc_geared.hpp:100
Definition: sim_model_delay_steer_map_acc_geared.hpp:124
double lerp(const double src_val, const double dst_val, const double ratio)
Definition: sim_model_delay_steer_map_acc_geared.cpp:22
char const * to_string(const RoutingGraphType &)
Definition: routing_graph_type.cpp:21
std::string string
Definition: junit5.hpp:26
std::vector< std::vector< double > > Map
Definition: sim_model_delay_steer_map_acc_geared.hpp:138
std::vector< std::vector< std::string > > Table
Definition: sim_model_delay_steer_map_acc_geared.hpp:137