Skip to content

Commit 91f0448

Browse files
committed
dyn_bike: the lag factors and floors in the plant's own precision; iters in the golden
A double plant is now double throughout (the lags' exp, the slew limit, the determinant floor and the friction ellipse's factor were float), so GRL-SNAM's torch twin reproduces it to 1e-9: next state and VJP on all seven golden cases (tests/test_dyn_bike_parity.py there).
1 parent f562e82 commit 91f0448

3 files changed

Lines changed: 31 additions & 30 deletions

File tree

‎inc/cvc/nav/detail/dyn_bike.h‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ template <class S> CVC_HD inline S dyn_sat(S F, S cap) {
202202
// friction ellipse with the longitudinal force first, sqrt(cap^2 - (1 - e^2) Fx^2), so a locked
203203
// or spinning axle keeps e = 5% of its grip sideways and the root stays smooth.
204204
template <class S> CVC_HD inline S dyn_lateral_cap(S Fx, S cap) {
205-
return s_sqrt(cap * cap - S(0.9975f) * Fx * Fx);
205+
return s_sqrt(cap * cap - S(0.9975) * Fx * Fx);
206206
}
207207

208208
template <class S> CVC_HD inline S dyn_clamp(S x, S lo, S hi) {
@@ -239,15 +239,16 @@ CVC_HD inline void dyn_plant(const dyn_bike_params &p, const bike_veh &v, const
239239
const S m = S(p.m), Iz = S(p.Iz), lf = S(p.lf), lr = S(p.lr), h = S(p.h), g = S(p.g);
240240
const S mu = S(p.mu), Cp = S(p.C), L = lf + lr;
241241

242-
// 1. Actuators.
243-
const S k_steer = S(p.tau_steer > 0.0f ? 1.0f - expf(-v.hdt / p.tau_steer) : 1.0f);
242+
// 1. Actuators. (Computed in S, so a double plant is double throughout and a twin can match it
243+
// to rounding.)
244+
const S k_steer = p.tau_steer > 0.0f ? one - s_exp(-hdt / S(p.tau_steer)) : one;
244245
S step = (delta_cmd - s.delta) * k_steer;
245246
if (p.steer_rate > 0.0f) {
246-
const S R = S(p.steer_rate * v.hdt);
247+
const S R = S(p.steer_rate) * hdt;
247248
step = R * s_tanh(step / R);
248249
}
249250
const S delta1 = s.delta + step;
250-
const S k_drive = S(p.tau_drive > 0.0f ? 1.0f - expf(-v.hdt / p.tau_drive) : 1.0f);
251+
const S k_drive = p.tau_drive > 0.0f ? one - s_exp(-hdt / S(p.tau_drive)) : one;
251252
const S a1 = s.a + (a_cmd - s.a) * k_drive;
252253

253254
// 2. Loads.
@@ -302,8 +303,8 @@ CVC_HD inline void dyn_plant(const dyn_bike_params &p, const bike_veh &v, const
302303
const S J22 = (-Sf * lf * lf - Sr * lr * lr) / Iz;
303304
const S A11 = one - hdt * J11, A12 = -hdt * J12, A21 = -hdt * J21, A22 = one - hdt * J22;
304305
S det = A11 * A22 - A12 * A21;
305-
if (s_val(det) < s_val(S(0.05f)))
306-
det = S(0.05f);
306+
if (s_val(det) < s_val(S(0.05)))
307+
det = S(0.05);
307308
const S b1 = s.vy + hdt * f_vy - vy1, b2 = s.r + hdt * f_r - r1;
308309
const S dvy = (A22 * b1 - A12 * b2) / det, dr = (A11 * b2 - A21 * b1) / det;
309310
vy1 = vy1 + dvy;
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
{
22
"format": "cvc-dyn-bike-golden-1",
33
"order": ["x", "y", "psi", "vx", "vy", "r", "delta", "a"],
4-
"params": {"m": 2676, "Iz": 6283.69384765625, "lf": 1.5369999408721924, "lr": 1.7630000114440918, "h": 0.74000000953674316, "g": 9.8066501617431641, "mu": 0.89999997615814209, "B_f": 36.121696472167969, "B_r": 42.496116638183594, "C": 1.2021652460098267, "front_drive": 0.5, "front_brake": 0.70588237047195435, "drag": 1.5006250143051147, "c_rr": 0.014999999664723873, "tau_steer": 0.05000000074505806, "steer_rate": 0.34999999403953552, "tau_drive": 0.10000000149011612, "v_blend": 1, "v_width": 0.30000001192092896, "eps": 0.5, "fz_min": 0.05000000074505806},
4+
"params": {"m": 2676, "Iz": 6283.69384765625, "lf": 1.5369999408721924, "lr": 1.7630000114440918, "h": 0.74000000953674316, "g": 9.8066501617431641, "mu": 0.89999997615814209, "B_f": 36.121696472167969, "B_r": 42.496116638183594, "C": 1.2021652460098267, "front_drive": 0.5, "front_brake": 0.70588237047195435, "drag": 1.5006250143051147, "c_rr": 0.014999999664723873, "tau_steer": 0.05000000074505806, "steer_rate": 0.34999999403953552, "tau_drive": 0.10000000149011612, "v_blend": 1, "v_width": 0.30000001192092896, "eps": 0.5, "fz_min": 0.05000000074505806, "iters": 3},
55
"g_out": [0.69999999999999996, -0.29999999999999999, 1.1000000000000001, 0.40000000000000002, -0.90000000000000002, 0.25, 0.59999999999999998, -0.14999999999999999],
66
"cases": [
77
{"state": [0, 0, 0, 12, 0.10000000000000001, 0.050000000000000003, 0.02, 0.5], "delta_cmd": 0.050000000000000003, "a_cmd": 1, "grade": 0,
88
"limits": {"L": 3.2999999523162842, "vmax": 40, "hdt": 0.059999998658895493, "allow_reverse": 0},
9-
"next": [0.72161232470560766, 0.0030915364976945637, 0.0061584453592970299, 12.026961595331935, 0.15841392237198784, 0.10264075828248356, 0.035978411732090335, 0.725594162940979],
10-
"g_state": [0.69999999999999996, -0.29999999999999999, 0.88135222703993166, 0.43908416095879343, -0.30332956256131693, 0.1177815525358758, -1.2584661367997847, -0.067429379176129373],
11-
"g_delta_cmd": -0.52467945705565022, "g_a_cmd": -0.055434951806790311},
9+
"next": [0.72161232476023529, 0.0030915364619978677, 0.006158445274966143, 12.026961596240618, 0.15841392030776888, 0.10264075687696875, 0.035978411112368727, 0.72559417581953878],
10+
"g_state": [0.69999999999999996, -0.29999999999999999, 0.881352227048531, 0.43908416096250991, -0.30332956174135622, 0.11778155321430109, -1.2584661505628023, -0.067429376024266241],
11+
"g_delta_cmd": -0.52467945254116677, "g_a_cmd": -0.05543495498191852},
1212
{"state": [1, -2, 0.29999999999999999, 15, -1.2, 0.59999999999999998, 0.25, 0], "delta_cmd": 0.29999999999999999, "a_cmd": 0, "grade": 0.02,
1313
"limits": {"L": 3.2999999523162842, "vmax": 40, "hdt": 0.059999998658895493, "allow_reverse": 0},
14-
"next": [1.8862789801811681, -1.8347158945998598, 0.33547355793214212, 14.854786313705493, -1.219456688874011, 0.59122597875063221, 0.269545337063756, 0],
15-
"g_state": [0.69999999999999996, -0.29999999999999999, 0.71841743216555154, 0.46493637589269232, -0.92038287845047939, 1.1426390353017404, 0.45635706252895575, -0.077760094217633857],
16-
"g_delta_cmd": 0.047047706292320919, "g_a_cmd": -0.063928025559695636},
14+
"next": [1.8862789801906317, -1.8347158945971369, 0.33547355793444861, 14.854786313864151, -1.2194566888495753, 0.59122597878907412, 0.26954533644854789, 0],
15+
"g_state": [0.69999999999999996, -0.29999999999999999, 0.71841743216080634, 0.46493637589482706, -0.92038287845088973, 1.1426390353048035, 0.4563570614107727, -0.077760090566516074],
16+
"g_delta_cmd": 0.04704770737140862, "g_a_cmd": -0.063928029207825282},
1717
{"state": [-3, 4, -1.1000000000000001, 14, 0.80000000000000004, 0.40000000000000002, 0.14999999999999999, -4], "delta_cmd": 0.14999999999999999, "a_cmd": -6, "grade": -0.029999999999999999,
1818
"limits": {"L": 3.2999999523162842, "vmax": 40, "hdt": 0.059999998658895493, "allow_reverse": 0},
19-
"next": [-2.6139870109800127, 3.2746823455082117, -1.0709796531246019, 13.693204349324315, 0.70559921373590384, 0.48367245873422476, 0.14999999999999999, -4.902376651763916],
20-
"g_state": [0.69999999999999996, -0.29999999999999999, 1.4919184614382557, 0.44997106711464691, -0.51421353073012122, 0.22798404290176852, 0.03773939316186864, -0.076678060470892734],
21-
"g_delta_cmd": 0.087559813437037615, "g_a_cmd": -0.063038465410443223},
19+
"next": [-2.6139870110986414, 3.2746823456478453, -1.070979653140653, 13.693204346336563, 0.7055992128641444, 0.48367245846670809, 0.14999999999999999, -4.9023767032781551],
20+
"g_state": [0.69999999999999996, -0.29999999999999999, 1.4919184613761005, 0.44997106717466218, -0.51421353207102205, 0.22798404545675632, 0.037739398844601396, -0.076678056992082619],
21+
"g_delta_cmd": 0.087559812311952701, "g_a_cmd": -0.063038469107698336},
2222
{"state": [2, 2, 2.5, 10, 2.5, 1.1000000000000001, 0.40000000000000002, 1], "delta_cmd": 0.45000000000000001, "a_cmd": 2, "grade": 0,
2323
"limits": {"L": 3.2999999523162842, "vmax": 40, "hdt": 0.059999998658895493, "allow_reverse": 0},
24-
"next": [1.4901741448832595, 2.3297160166793294, 2.5727797258183669, 10.119081134187299, 2.0853344683062338, 1.2129954574186745, 0.41954533706375602, 1.451188325881958],
25-
"g_state": [0.69999999999999996, -0.29999999999999999, 1.0221465448594917, 0.36806557804809409, -0.4105335960760112, -0.071966288133226974, 0.0064366148676435753, -0.068908100695486141],
26-
"g_delta_cmd": 0.00066357681446086586, "g_a_cmd": -0.056650636381715552},
24+
"next": [1.4901741447892105, 2.3297160167472559, 2.5727797258014662, 10.119081136118659, 2.0853344678711072, 1.2129954571369974, 0.41954533644854791, 1.4511883516390776],
25+
"g_state": [0.69999999999999996, -0.29999999999999999, 1.0221465448401579, 0.3680655773155822, -0.4105335944971768, -0.071966286436913274, 0.0064365975211407855, -0.068908097463021359],
26+
"g_delta_cmd": 0.00066357504298502484, "g_a_cmd": -0.056650639617034933},
2727
{"state": [0.5, 0.5, 0.20000000000000001, -2, 0.050000000000000003, -0.10000000000000001, 0.20000000000000001, -0.5], "delta_cmd": 0.25, "a_cmd": -0.80000000000000004, "grade": 0,
2828
"limits": {"L": 3.2999999523162842, "vmax": 40, "hdt": 0.059999998658895493, "allow_reverse": 1},
29-
"next": [0.38234157511518047, 0.47820894214127474, 0.19215389326207336, -1.9942410235285486, -0.2125505385461175, -0.13076844855501363, 0.21954533706375601, -0.63535649776458736],
30-
"g_state": [0.69999999999999996, -0.29999999999999999, 1.1505512679665537, 0.37186240073418236, -0.12784613767012254, 0.017428115581688448, 1.2345138802280493, -0.07080295543678379],
31-
"g_delta_cmd": 0.12727105860682825, "g_a_cmd": -0.058208431849331083},
29+
"next": [0.38234157507615946, 0.47820894213237564, 0.19215389327768181, -1.9942410241949737, -0.21255053807775098, -0.13076844829487239, 0.2195453364485479, -0.63535650549172329],
30+
"g_state": [0.69999999999999996, -0.29999999999999999, 1.1505512679844894, 0.37186240076506216, -0.12784613682710022, 0.017428116952459605, 1.2345138787580188, -0.07080295209586783],
31+
"g_delta_cmd": 0.12727106168621422, "g_a_cmd": -0.058208435157532337},
3232
{"state": [0, 0, 0, 0.59999999999999998, 0.02, 0.050000000000000003, 0.29999999999999999, 0.29999999999999999], "delta_cmd": 0.34999999999999998, "a_cmd": 0.5, "grade": 0,
3333
"limits": {"L": 3.2999999523162842, "vmax": 40, "hdt": 0.059999998658895493, "allow_reverse": 0},
34-
"next": [0.035991441391833912, 8.995724254765162e-05, 0.0036135230264716027, 0.59985887130707083, 0.10550903857615097, 0.060225385120668967, 0.31954533706375599, 0.39023766517639158],
35-
"g_state": [0.69999999999999996, -0.29999999999999999, 1.0891395975126665, 0.29781652300634986, 0.050398345432618334, 0.081666464208064213, 0.26493307772732771, -0.072170291011612667],
36-
"g_delta_cmd": 0.027313028879102919, "g_a_cmd": -0.059332543959222522},
34+
"next": [0.035991441415742745, 8.9957242432581056e-05, 0.003613523021362957, 0.5998588717055453, 0.10550903842574866, 0.060225385035524874, 0.31954533644854788, 0.39023767032781553],
35+
"g_state": [0.69999999999999996, -0.29999999999999999, 1.0891395975055744, 0.29781652332996111, 0.050398345344285118, 0.081666464100695529, 0.26493307713883441, -0.072170287614980372],
36+
"g_delta_cmd": 0.027313029511808661, "g_a_cmd": -0.059332547338548272},
3737
{"state": [0, 0, 0, 0, 0, 0, 0, 0], "delta_cmd": 0.10000000000000001, "a_cmd": 2, "grade": 0,
3838
"limits": {"L": 3.2999999523162842, "vmax": 40, "hdt": 0.059999998658895493, "allow_reverse": 0},
39-
"next": [0.0032481981403485543, 5.312353074796162e-08, 2.0623739818348296e-05, 0.05413663688927093, 0.00060576313142626846, 0.00034372900465541356, 0.020946007224209414, 0.90237665176391602],
40-
"g_state": [0.69999999999999996, -0.29999999999999999, 1.099025503371424, 0.42620268024003993, 0.0018837906558572179, 0.0068176135894011582, 0.57682704060703349, -0.068034402715674916],
41-
"g_delta_cmd": 0.0020775377091722634, "g_a_cmd": -0.055932352956948816}
39+
"next": [0.0032481983257981982, 5.3123536200254369e-08, 2.062374045329597e-05, 0.054136639980098399, 0.0006057631500759013, 0.00034372901523787501, 0.020946006673345115, 0.90237670327815533],
40+
"g_state": [0.69999999999999996, -0.29999999999999999, 1.0990255033157852, 0.42620268045264031, 0.0018837905264509067, 0.0068176134156984324, 0.57682703919426437, -0.068034399515581628],
41+
"g_delta_cmd": 0.0020775379242710104, "g_a_cmd": -0.055932356144165696}
4242
]
4343
}

‎src/cvc/tests/nav_dyn_bike_test.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ TEST(DynBike, Golden) {
587587
js << (first ? "" : ", ") << "\"" << kv.first << "\": " << num(kv.second);
588588
first = false;
589589
}
590-
js << "},\n \"g_out\": " << arr(g_out, 8) << ",\n \"cases\": [\n";
590+
js << ", \"iters\": " << p.iters << "},\n \"g_out\": " << arr(g_out, 8) << ",\n \"cases\": [\n";
591591
for (std::size_t c = 0; c < sizeof cases / sizeof cases[0]; ++c) {
592592
const gcase &gc = cases[c];
593593
const bike_veh lim = plant_limits(p, gc.reverse);

0 commit comments

Comments
 (0)