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>
22 #include <rclcpp/logging.hpp>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 
28 #include "eigen3/Eigen/Core"
29 #include "eigen3/Eigen/LU"
30 
33 {
34 inline bool isIncreasing(const std::vector<double> & x)
35 {
36  if (x.empty()) {
37  throw std::invalid_argument("Points is empty.");
38  }
39 
40  for (size_t i = 0; i < x.size() - 1; ++i) {
41  if (x.at(i) >= x.at(i + 1)) {
42  return false;
43  }
44  }
45 
46  return true;
47 }
48 
49 inline bool isNotDecreasing(const std::vector<double> & x)
50 {
51  if (x.empty()) {
52  throw std::invalid_argument("Points is empty.");
53  }
54 
55  for (size_t i = 0; i < x.size() - 1; ++i) {
56  if (x.at(i) > x.at(i + 1)) {
57  return false;
58  }
59  }
60 
61  return true;
62 }
63 
64 inline std::vector<double> validateKeys(
65  const std::vector<double> & base_keys, const std::vector<double> & query_keys)
66 {
67  // when vectors are empty
68  if (base_keys.empty() || query_keys.empty()) {
69  throw std::invalid_argument("Points is empty.");
70  }
71 
72  // when size of vectors are less than 2
73  if (base_keys.size() < 2) {
74  throw std::invalid_argument(
75  "The size of points is less than 2. base_keys.size() = " + std::to_string(base_keys.size()));
76  }
77 
78  // when indices are not sorted
79  if (!isIncreasing(base_keys) || !isNotDecreasing(query_keys)) {
80  throw std::invalid_argument("Either base_keys or query_keys is not sorted.");
81  }
82 
83  // when query_keys is out of base_keys (This function does not allow exterior division.)
84  constexpr double epsilon = 1e-3;
85  if (
86  query_keys.front() < base_keys.front() - epsilon ||
87  base_keys.back() + epsilon < query_keys.back()) {
88  throw std::invalid_argument("query_keys is out of base_keys");
89  }
90 
91  // NOTE: Due to calculation error of double, a query key may be slightly out of base keys.
92  // Therefore, query keys are cropped here.
93  auto validated_query_keys = query_keys;
94  validated_query_keys.front() = std::max(validated_query_keys.front(), base_keys.front());
95  validated_query_keys.back() = std::min(validated_query_keys.back(), base_keys.back());
96 
97  return validated_query_keys;
98 }
99 
100 template <class T>
102  const std::vector<double> & base_keys, const std::vector<T> & base_values)
103 {
104  // when vectors are empty
105  if (base_keys.empty() || base_values.empty()) {
106  throw std::invalid_argument("Points is empty.");
107  }
108 
109  // when size of vectors are less than 2
110  if (base_keys.size() < 2 || base_values.size() < 2) {
111  throw std::invalid_argument(
112  "The size of points is less than 2. base_keys.size() = " + std::to_string(base_keys.size()) +
113  ", base_values.size() = " + std::to_string(base_values.size()));
114  }
115 
116  // when sizes of indices and values are not same
117  if (base_keys.size() != base_values.size()) {
118  throw std::invalid_argument("The size of base_keys and base_values are not the same.");
119  }
120 }
121 } // namespace interpolation_utils
122 
124 namespace interpolation
125 {
126 double lerp(const double src_val, const double dst_val, const double ratio);
127 
128 std::vector<double> lerp(
129  const std::vector<double> & base_keys, const std::vector<double> & base_values,
130  const std::vector<double> & query_keys);
131 
132 double lerp(
133  const std::vector<double> & base_keys, const std::vector<double> & base_values,
134  const double query_key);
135 
136 } // namespace interpolation
137 
138 using Table = std::vector<std::vector<std::string>>;
139 using Map = std::vector<std::vector<double>>;
140 
143 {
144 public:
145  explicit CSVLoader(const std::string & csv_path);
146 
147  bool readCSV(Table & result, const char delim = ',');
148  static bool validateData(const Table & table, const std::string & csv_path);
149  static bool validateMap(const Map & map, const bool is_col_decent);
150  static Map getMap(const Table & table);
151  static std::vector<double> getRowIndex(const Table & table);
152  static std::vector<double> getColumnIndex(const Table & table);
153  static double clampValue(
154  const double val, const std::vector<double> & ranges, const std::string & name);
155 
156 private:
157  std::string csv_path_;
158 };
159 
161 {
162 public:
164  {
165  CSVLoader csv(csv_path);
166  std::vector<std::vector<std::string>> table;
167  if (!csv.readCSV(table)) {
168  RCLCPP_ERROR_STREAM(
169  rclcpp::get_logger("SimModelDelaySteerMapAccGeared"),
170  "failed to read acceleration map from " << csv_path);
171  return false;
172  }
173 
174  vehicle_name_ = table[0][0];
175  vel_index_ = CSVLoader::getRowIndex(table);
176  acc_index_ = CSVLoader::getColumnIndex(table);
178 
179  RCLCPP_INFO_STREAM(
180  rclcpp::get_logger("SimModelDelaySteerMapAccGeared"),
181  "success to read acceleration map from " << csv_path);
182  return true;
183  }
184 
185  double getAcceleration(const double acc_des, const double vel) const
186  {
187  std::vector<double> interpolated_acc_vec;
188  const double clamped_vel = CSVLoader::clampValue(vel, vel_index_, "acc: vel");
189 
190  // (throttle, vel, acc) map => (throttle, acc) map by fixing vel
191  for (const auto & acc_vec : acceleration_map_) {
192  interpolated_acc_vec.push_back(interpolation::lerp(vel_index_, acc_vec, clamped_vel));
193  }
194  // calculate throttle
195  // When the desired acceleration is smaller than the throttle area, return min acc
196  // When the desired acceleration is greater than the throttle area, return max acc
197  const double clamped_acc = CSVLoader::clampValue(acc_des, acc_index_, "acceleration: acc");
198  const double acc = interpolation::lerp(acc_index_, interpolated_acc_vec, clamped_acc);
199 
200  return acc;
201  }
202  std::vector<std::vector<double>> acceleration_map_;
203 
204 private:
205  std::string vehicle_name_;
206  std::vector<double> vel_index_;
207  std::vector<double> acc_index_;
208 };
209 
211 {
212 public:
228  double vx_lim, double steer_lim, double vx_rate_lim, double steer_rate_lim, double wheelbase,
229  double dt, double acc_delay, double acc_time_constant, double steer_delay,
230  double steer_time_constant, std::string path);
231 
236 
238 
239 private:
240  const double MIN_TIME_CONSTANT;
241 
242  enum IDX {
243  X = 0,
244  Y,
245  YAW,
246  VX,
247  STEER,
248  ACCX,
249  };
250  enum IDX_U {
251  ACCX_DES = 0,
252  STEER_DES,
253  DRIVE_SHIFT,
254  };
255 
256  const double vx_lim_;
257  const double vx_rate_lim_;
258  const double steer_lim_;
259  const double steer_rate_lim_;
260  const double wheelbase_;
261 
262  std::deque<double> acc_input_queue_;
263  std::deque<double> steer_input_queue_;
264  const double acc_delay_;
265  const double acc_time_constant_;
266  const double steer_delay_;
267  const double steer_time_constant_;
268  const std::string path_;
269 
274  void initializeInputQueue(const double & dt);
275 
279  double getX() override;
280 
284  double getY() override;
285 
289  double getYaw() override;
290 
294  double getVx() override;
295 
299  double getVy() override;
300 
304  double getAx() override;
305 
309  double getWz() override;
310 
314  double getSteer() override;
315 
320  void update(const double & dt) override;
321 
327  Eigen::VectorXd calcModel(const Eigen::VectorXd & state, const Eigen::VectorXd & input) override;
328 
336  void updateStateWithGear(
337  Eigen::VectorXd & state, const Eigen::VectorXd & prev_state, const uint8_t gear,
338  const double dt);
339 };
340 
341 #endif // SIMPLE_PLANNING_SIMULATOR__VEHICLE_MODEL__SIM_MODEL_DELAY_STEER_MAP_ACC_GEARED_HPP_
Definition: sim_model_delay_steer_map_acc_geared.hpp:161
bool readAccelerationMapFromCSV(const std::string &csv_path)
Definition: sim_model_delay_steer_map_acc_geared.hpp:163
double getAcceleration(const double acc_des, const double vel) const
Definition: sim_model_delay_steer_map_acc_geared.hpp:185
std::vector< std::vector< double > > acceleration_map_
Definition: sim_model_delay_steer_map_acc_geared.hpp:202
Definition: sim_model_delay_steer_map_acc_geared.hpp:143
static Map getMap(const Table &table)
Definition: sim_model_delay_steer_map_acc_geared.cpp:148
static double clampValue(const double val, const std::vector< double > &ranges, const std::string &name)
Definition: sim_model_delay_steer_map_acc_geared.cpp:179
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:123
static std::vector< double > getRowIndex(const Table &table)
Definition: sim_model_delay_steer_map_acc_geared.cpp:161
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:170
Definition: sim_model_delay_steer_map_acc_geared.hpp:211
AccelerationMap acc_map_
Definition: sim_model_delay_steer_map_acc_geared.hpp:237
~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:194
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:33
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:64
bool isIncreasing(const std::vector< double > &x)
Definition: sim_model_delay_steer_map_acc_geared.hpp:34
bool isNotDecreasing(const std::vector< double > &x)
Definition: sim_model_delay_steer_map_acc_geared.hpp:49
void validateKeysAndValues(const std::vector< double > &base_keys, const std::vector< T > &base_values)
Definition: sim_model_delay_steer_map_acc_geared.hpp:101
Definition: sim_model_delay_steer_map_acc_geared.hpp:125
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:139
std::vector< std::vector< std::string > > Table
Definition: sim_model_delay_steer_map_acc_geared.hpp:138