Skip to content

Commit 0146ccf

Browse files
authored
Update Kloeser2020.mo
1 parent 68b553e commit 0146ccf

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

Kloeser2020.mo

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ model Kloeser2020 "Bicycle model of a model racwe car, from Kloeser2020 paper"
1212
parameter Real cr3 = 5.0;
1313

1414
// Constant curvature (circle)
15-
parameter Real kappa = 1 "Road curvature [1/m] (constant)";
15+
Real kappa "Road curvature [1/m] (constant)";
1616

1717
// --- Inputs ---
1818
input Real D_der(min = -20.0, max = 20.0) "Rate of duty cycle";
@@ -24,6 +24,14 @@ model Kloeser2020 "Bicycle model of a model racwe car, from Kloeser2020 paper"
2424
// --- Outputs ---
2525
output Real acc_long "Longitudinal acceleration";
2626
output Real acc_lat "Lateral acceleration";
27+
28+
output Real p_x "car x coordinate";
29+
output Real p_x "car y coordinate";
30+
31+
output Real c_x "Center line x coordinate";
32+
output Real c_y "Center line y coordinate";
33+
output Real e_n_x "Central path normal vector, projection on world x"
34+
output Real e_n_y "Central path normal vector, projection on world y"
2735

2836
// --- States ---
2937
Real s "Tangential position";
@@ -35,13 +43,42 @@ model Kloeser2020 "Bicycle model of a model racwe car, from Kloeser2020 paper"
3543
Real beta;
3644
Real Fx_d;
3745
Real ds, dn, dalpha, dv;
46+
47+
Real s_mod "s wrapped to [0, 4*pi)";
48+
49+
// ---- Local functions ----
50+
function logsumexp2
51+
"Numerically stable LSE for two arguments with temperature alpha"
52+
input Real a;
53+
input Real b;
54+
input Real alpha=0.1;
55+
output Real y;
56+
protected
57+
Real m;
58+
algorithm
59+
m := if a > b then a else b;
60+
y := m + alpha*Modelica.Math.log(
61+
Modelica.Math.exp((a - m)/alpha)
62+
+ Modelica.Math.exp((b - m)/alpha));
63+
end logsumexp2;
3864

65+
function clip1
66+
"Smooth min(x,1) via -logsumexp([-1,-x]) with temperature alpha"
67+
input Real x;
68+
input Real alpha=0.1;
69+
output Real y;
70+
algorithm
71+
y := -logsumexp2(-1.0, -x, alpha);
72+
end clip1;
73+
3974
equation
4075
// beta (slip-free approximation, small angle)
4176
beta = lr/(lr + lf) * delta;
4277

4378
// Longitudinal force
4479
Fx_d = (cm1 - cm2*v)*D - cr2*v*v - cr0*tanh(cr3*v);
80+
81+
kappa = (-clip1(-clip1(-10*sin(s), alpha_clip), alpha_clip) + 1.0)/2.0;
4582

4683
// Dynamics
4784
ds = v*cos(alpha + beta)/(1 - n*kappa);
@@ -61,4 +98,32 @@ equation
6198
acc_long = Fx_d/m;
6299
acc_lat = v*v/lr*sin(beta) + Fx_d*sin(beta)/m;
63100

101+
// s modulo 4π
102+
s_mod = mod(s, 4*Modelica.Constants.pi);
103+
104+
// Piecewise definition of gamma and normal
105+
if s_mod < Modelica.Constants.pi then
106+
c_x = s_mod;
107+
c_y = 0;
108+
e_n_x = 0;
109+
e_n_y = 1;
110+
elseif s_mod < 2*Modelica.Constants.pi then
111+
c_x = sin(s - Modelica.Constants.pi) + Modelica.Constants.pi;
112+
c_y = 1 - cos(s - Modelica.Constants.pi);
113+
e_n_x = -sin(s - Modelica.Constants.pi);
114+
e_n_y = cos(s - Modelica.Constants.pi);
115+
elseif s_mod < 3*Modelica.Constants.pi then
116+
c_x = 3*Modelica.Constants.pi - s_mod;
117+
c_y = 2;
118+
e_n_x = 0;
119+
e_n_y = -1;
120+
else
121+
c_x = -sin(s - 3*Modelica.Constants.pi);
122+
c_y = 1 + cos(s - 3*Modelica.Constants.pi);
123+
e_n_x = sin(s - 3*Modelica.Constants.pi);
124+
e_n_y = -cos(s - 3*Modelica.Constants.pi);
125+
end if;
126+
127+
p_x = c_x+n*e_n_x;
128+
p_y = c_y+n*e_n_y;
64129
end Kloeser2020;

0 commit comments

Comments
 (0)