17 #define CTRL_LOG_LEVEL_NONE 0
18 #define CTRL_LOG_LEVEL_ERROR 1
19 #define CTRL_LOG_LEVEL_WARNING 2
20 #define CTRL_LOG_LEVEL_INFO 3
21 #define CTRL_LOG_LEVEL_DEBUG 4
23 #ifndef CTRL_LOG_LEVEL
24 #define CTRL_LOG_LEVEL CTRL_LOG_LEVEL_WARNING
27 #if CTRL_LOG_LEVEL >= CTRL_LOG_LEVEL_ERROR
28 #define ctrl_loge (std::cout << "[E][" __FILE__ ":" << __LINE__ << "]\t")
30 #define ctrl_loge std::ostream(0)
33 #if CTRL_LOG_LEVEL >= CTRL_LOG_LEVEL_WARNING
34 #define ctrl_logw (std::cout << "[W][" __FILE__ ":" << __LINE__ << "]\t")
36 #define ctrl_logw std::ostream(0)
39 #if CTRL_LOG_LEVEL >= CTRL_LOG_LEVEL_INFO
40 #define ctrl_logi (std::cout << "[I][" __FILE__ ":" << __LINE__ << "]\t")
42 #define ctrl_logi std::ostream(0)
45 #if CTRL_LOG_LEVEL >= CTRL_LOG_LEVEL_DEBUG
46 #define ctrl_logd (std::cout "[D][" << __FILE__ ":" << __LINE__ << "]\t")
48 #define ctrl_logd std::ostream(0)
73 AccelCurve(
const float j_max,
const float a_max,
const float v_start,
92 void reset(
const float j_max,
const float a_max,
const float v_start,
95 am = (
v_end > v_start) ? a_max : -a_max;
96 jm = (
v_end > v_start) ? j_max : -j_max;
103 const auto tc = a_max / j_max;
105 const auto tm = (
v3 -
v0) /
am - tc;
114 x1 =
x0 +
v0 * tc +
am * tc * tc / 6;
119 const auto tcp = std::sqrt((
v3 -
v0) /
jm);
123 x1 =
x2 =
x0 +
v1 * tcp +
jm * tcp * tcp * tcp / 6;
132 float j(
const float t)
const {
149 float a(
const float t)
const {
153 return jm * (t -
t0);
157 return -
jm * (t -
t3);
166 float v(
const float t)
const {
170 return v0 +
jm / 2 * (t -
t0) * (t -
t0);
172 return v1 +
am * (t -
t1);
174 return v3 -
jm / 2 * (t -
t3) * (t -
t3);
183 float x(
const float t)
const {
185 return x0 +
v0 * (t -
t0);
187 return x0 +
v0 * (t -
t0) +
jm / 6 * (t -
t0) * (t -
t0) * (t -
t0);
189 return x1 +
v1 * (t -
t1) +
am / 2 * (t -
t1) * (t -
t1);
191 return x3 +
v3 * (t -
t3) -
jm / 6 * (t -
t3) * (t -
t3) * (t -
t3);
193 return x3 +
v3 * (t -
t3);
232 void printCsv(std::ostream& os,
const float t_interval = 1e-3f)
const {
233 for (
float t =
t0; t <
t_end(); t += t_interval) {
234 os << t <<
"," <<
j(t) <<
"," <<
a(t) <<
"," <<
v(t) <<
"," <<
x(t)
243 os <<
"\tvs: " << obj.
v0;
244 os <<
"\tve: " << obj.
v3;
245 os <<
"\tt0: " << obj.
t0;
246 os <<
"\tt1: " << obj.
t1;
247 os <<
"\tt2: " << obj.
t2;
248 os <<
"\tt3: " << obj.
t3;
249 os <<
"\td: " << obj.
x3 - obj.
x0;
264 const float vs,
const float vt,
267 const auto tc = a_max / j_max;
269 const auto am = (vt > vs) ? a_max : -a_max;
270 const auto jm = (vt > vs) ? j_max : -j_max;
272 const auto d_triangle = (vs +
am * tc / 2) * tc;
273 const auto v_triangle =
jm /
am * d - vs;
276 if (d * v_triangle > 0 && std::abs(d) > std::abs(d_triangle)) {
278 ctrl_logd <<
"v: curve - straight - curve" << std::endl;
280 const auto amtc =
am * tc;
281 const auto D = amtc * amtc - 4 * (amtc * vs - vs * vs - 2 *
am * d);
282 const auto sqrtD = std::sqrt(D);
283 return (-amtc + (d > 0 ? sqrtD : -sqrtD)) / 2;
288 const auto a = std::abs(vs);
289 const auto b = (d > 0 ? 1 : -1) *
jm * d * d;
290 const auto aaa_27 =
a *
a *
a / 27;
291 const auto cr = 8 * aaa_27 + b / 2;
292 const auto ci_b = 8 * aaa_27 / b + 1.0f / 4;
295 ctrl_logd <<
"v: curve - curve (accel)" << std::endl;
296 const auto c = std::cbrt(cr + std::abs(b) * std::sqrt(ci_b));
297 return (d > 0 ? 1 : -1) * (c + 4 *
a *
a / c / 9 -
a / 3);
300 ctrl_logd <<
"v: curve - curve (decel)" << std::endl;
301 const auto ci = std::abs(b) * std::sqrt(-ci_b);
302 const auto r = std::hypot(cr, ci);
303 const auto th = std::atan2(ci, cr);
304 return (d > 0 ? 1 : -1) * (2 * std::cbrt(r) * std::cos(th / 3) -
a / 3);
317 const float vs,
const float ve,
320 const auto tc = a_max / j_max;
321 const auto am = (d > 0) ? a_max : -a_max;
323 const auto amtc =
am * tc;
324 const auto D = amtc * amtc - 2 * (vs + ve) * amtc + 4 *
am * d +
325 2 * (vs * vs + ve * ve);
328 ctrl_loge <<
"Error! D = " << D <<
" < 0" << std::endl;
331 ctrl_loge <<
"Invalid Input! vs: " << vs <<
", ve: " << ve << std::endl;
334 const auto sqrtD = std::sqrt(D);
335 return (-amtc + (d > 0 ? sqrtD : -sqrtD)) / 2;
350 const auto ve_minus_vs =
v_end - v_start;
352 const auto am = (ve_minus_vs > 0) ? a_max : -a_max;
353 const auto jm = (ve_minus_vs > 0) ? j_max : -j_max;
355 const auto tc = a_max / j_max;
357 const auto tm = ve_minus_vs /
am - tc;
360 (tm > 0) ? (tc + tm + tc) : (2 * std::sqrt(ve_minus_vs /
jm));
361 return (v_start +
v_end) / 2 * t_all;
#define ctrl_logd
Definition: accel_curve.h:48
#define ctrl_loge
Definition: accel_curve.h:28
走行距離拘束のない曲線加速の軌道を生成するクラス
Definition: accel_curve.h:64
float x3
位置定数 [m]
Definition: accel_curve.h:369
float am
加速度定数 [m/s/s]
Definition: accel_curve.h:366
float a(const float t) const
任意の時刻 t [s] における加速度 a [m/s/s] を返す関数
Definition: accel_curve.h:149
float t2
Definition: accel_curve.h:367
float t1
Definition: accel_curve.h:367
float t_2() const
等加速度直線運動の終了時刻 [s]
Definition: accel_curve.h:218
float x0
Definition: accel_curve.h:369
static float calcReachableVelocityMax(const float j_max, const float a_max, const float vs, const float ve, const float d)
走行距離の拘束から達しうる最大速度を算出する関数
Definition: accel_curve.h:316
float t_3() const
曲線加速の終了時刻 [s]
Definition: accel_curve.h:222
float v1
Definition: accel_curve.h:368
friend std::ostream & operator<<(std::ostream &os, const AccelCurve &obj)
情報の表示
Definition: accel_curve.h:241
float t_1() const
等加速度直線運動の開始時刻 [s]
Definition: accel_curve.h:214
float jm
躍度定数 [m/s/s/s]
Definition: accel_curve.h:365
float x_end() const
終点位置 [m]
Definition: accel_curve.h:206
float v2
Definition: accel_curve.h:368
void printCsv(std::ostream &os, const float t_interval=1e-3f) const
std::ostream に軌道のcsvを出力する関数
Definition: accel_curve.h:232
float x(const float t) const
任意の時刻 t [s] における位置 x [m] を返す関数
Definition: accel_curve.h:183
void reset(const float j_max, const float a_max, const float v_start, const float v_end)
引数の拘束条件から曲線を生成する関数
Definition: accel_curve.h:92
float t3
時刻定数 [s]
Definition: accel_curve.h:367
float t_end() const
終点時刻 [s]
Definition: accel_curve.h:198
AccelCurve()
とりあえずインスタンス化を行う空のコンストラクタ
Definition: accel_curve.h:81
const std::array< float, 4 > getTimeStamps() const
境界のタイムスタンプをまとめて取得する関数
Definition: accel_curve.h:226
float j(const float t) const
任意の時刻 t [s] における躍度 j [m/s/s/s] を返す関数
Definition: accel_curve.h:132
float v3
速度定数 [m/s]
Definition: accel_curve.h:368
float v(const float t) const
任意の時刻 t [s] における速度 v [m/s] を返す関数
Definition: accel_curve.h:166
static float calcReachableVelocityEnd(const float j_max, const float a_max, const float vs, const float vt, const float d)
走行距離の拘束から達しうる終点速度を算出する関数
Definition: accel_curve.h:263
AccelCurve(const float j_max, const float a_max, const float v_start, const float v_end)
初期化付きのコンストラクタ
Definition: accel_curve.h:73
float v0
Definition: accel_curve.h:368
float x2
Definition: accel_curve.h:369
float x1
Definition: accel_curve.h:369
float v_end() const
終点速度 [m/s]
Definition: accel_curve.h:202
float t_0() const
曲線加速の開始時刻 [s]
Definition: accel_curve.h:210
static float calcDistanceFromVelocityStartToEnd(const float j_max, const float a_max, const float v_start, const float v_end)
速度差の拘束から達しうる変位を算出する関数
Definition: accel_curve.h:345
float t0
Definition: accel_curve.h:367
制御関係の名前空間
Definition: accel_curve.h:54