Skip to content

Commit dc53b74

Browse files
committed
[choreolib] Remove jerk from trajectory sample interpolation
Fixes #1295.
1 parent 50bf2c2 commit dc53b74

File tree

5 files changed

+42
-80
lines changed

5 files changed

+42
-80
lines changed

choreolib/py/choreo/trajectory/__init__.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,8 @@ def f(state, input):
168168
α = (ar - al) / width
169169
return [v * cos(θ), v * sin(θ), ω, al, ar, α]
170170

171-
τ = t - self.timestamp
172171
sample = solve_ivp(f, (self.timestamp, t), initial_state).y
173172

174-
dt = end_value.timestamp - self.timestamp
175-
jl = (end_value.al - self.al) / dt
176-
jr = (end_value.ar - self.ar) / dt
177-
178173
return DifferentialSample(
179174
t,
180175
sample[0, 0],
@@ -183,8 +178,8 @@ def f(state, input):
183178
sample[3, 0],
184179
sample[4, 0],
185180
sample[5, 0],
186-
self.al + jl * τ,
187-
self.ar + jr * τ,
181+
self.al,
182+
self.ar,
188183
lerp(self.fl, end_value.fl, scale),
189184
lerp(self.fr, end_value.fr, scale),
190185
)
@@ -463,30 +458,22 @@ def interpolate(self, end_value: SwerveSample, t: float) -> SwerveSample:
463458
# Δt = tₖ₊₁ − tₖ
464459
# τ = timestamp − tₖ
465460
#
466-
# x(τ) = xₖ + vₖτ + 1/2 aₖτ² + 1/6 jₖτ³
467-
# v(τ) = vₖ + aₖτ + 1/2 jₖτ²
468-
# a(τ) = aₖ + jₖτ
469-
#
470-
# where jₖ = (aₖ₊₁ − aₖ)/Δt
471-
dt = end_value.timestamp - t
461+
# x(τ) = xₖ + vₖτ + 1/2 aₖτ²
462+
# v(τ) = vₖ + aₖτ
472463
τ = t - self.timestamp
473464
τ2 = τ * τ
474-
τ3 = τ * τ * τ
475-
jx = (end_value.ax - self.ax) / dt
476-
jy = (end_value.ay - self.ay) / dt
477-
η = (end_value.alpha - self.alpha) / dt
478465

479466
return SwerveSample(
480467
t,
481-
self.x + self.vx * τ + 0.5 * self.ax * τ2 + 1.0 / 6.0 * jx * τ3,
482-
self.y + self.vy * τ + 0.5 * self.ay * τ2 + 1.0 / 6.0 * jy * τ3,
483-
self.heading + self.omega * τ + 0.5 * self.alpha * τ2 + 1.0 / 6.0 * η * τ3,
484-
self.vx + self.ax * τ + 0.5 * jx * τ2,
485-
self.vy + self.ay * τ + 0.5 * jy * τ2,
486-
self.omega + self.alpha * τ + 0.5 * η * τ2,
487-
self.ax + jx * τ,
488-
self.ay + jy * τ,
489-
self.alpha + η * τ,
468+
self.x + self.vx * τ + 0.5 * self.ax * τ2,
469+
self.y + self.vy * τ + 0.5 * self.ay * τ2,
470+
self.heading + self.omega * τ + 0.5 * self.alpha * τ2,
471+
self.vx + self.ax * τ,
472+
self.vy + self.ay * τ,
473+
self.omega + self.alpha * τ,
474+
self.ax,
475+
self.ay,
476+
self.alpha,
490477
[lerp(self.fx[i], end_value.fx[i], scale) for i in range(len(self.fx))],
491478
[lerp(self.fy[i], end_value.fy[i], scale) for i in range(len(self.fy))],
492479
)

choreolib/src/main/java/choreo/trajectory/DifferentialSample.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,6 @@ public DifferentialSample interpolate(DifferentialSample endValue, double timest
152152
double τ = timestamp - this.t;
153153
var sample = NumericalIntegration.rkdp(f, initialState, VecBuilder.fill(al, ar), τ);
154154

155-
double dt = endValue.t - this.t;
156-
double jl = (endValue.al - this.al) / dt;
157-
double jr = (endValue.ar - this.ar) / dt;
158-
159155
return new DifferentialSample(
160156
MathUtil.interpolate(this.t, endValue.t, scale),
161157
sample.get(0, 0),
@@ -164,8 +160,8 @@ public DifferentialSample interpolate(DifferentialSample endValue, double timest
164160
sample.get(3, 0),
165161
sample.get(4, 0),
166162
sample.get(5, 0),
167-
this.al + jl * τ,
168-
this.ar + jr * τ,
163+
this.al,
164+
this.ar,
169165
MathUtil.interpolate(this.fl, endValue.fl, scale),
170166
MathUtil.interpolate(this.fr, endValue.fr, scale));
171167
}

choreolib/src/main/java/choreo/trajectory/SwerveSample.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,29 +162,21 @@ public SwerveSample interpolate(SwerveSample endValue, double timestamp) {
162162
// Δt = tₖ₊₁ − tₖ
163163
// τ = timestamp − tₖ
164164
//
165-
// x(τ) = xₖ + vₖτ + 1/2 aₖτ² + 1/6 jₖτ³
166-
// v(τ) = vₖ + aₖτ + 1/2 jₖτ²
167-
// a(τ) = aₖ + jₖτ
168-
//
169-
// where jₖ = (aₖ₊₁ − aₖ)/Δt
170-
double dt = endValue.t - this.t;
165+
// x(τ) = xₖ + vₖτ + 1/2 aₖτ²
166+
// v(τ) = vₖ + aₖτ
171167
double τ = timestamp - this.t;
172168
double τ2 = τ * τ;
173-
double τ3 = τ * τ * τ;
174-
double jx = (endValue.ax - this.ax) / dt;
175-
double jy = (endValue.ay - this.ay) / dt;
176-
double η = (endValue.alpha - this.alpha) / dt;
177169
return new SwerveSample(
178170
timestamp,
179-
this.x + this.vx * τ + 0.5 * this.ax * τ2 + 1.0 / 6.0 * jx * τ3,
180-
this.y + this.vy * τ + 0.5 * this.ay * τ2 + 1.0 / 6.0 * jy * τ3,
181-
this.heading + this.omega * τ + 0.5 * this.alpha * τ2 + 1.0 / 6.0 * η * τ3,
182-
this.vx + this.ax * τ + 0.5 * jx * τ2,
183-
this.vy + this.ay * τ + 0.5 * jy * τ2,
184-
this.omega + this.alpha * τ + 0.5 * η * τ2,
185-
this.ax + jx * τ,
186-
this.ay + jy * τ,
187-
this.alpha + η * τ,
171+
this.x + this.vx * τ + 0.5 * this.ax * τ2,
172+
this.y + this.vy * τ + 0.5 * this.ay * τ2,
173+
this.heading + this.omega * τ + 0.5 * this.alpha * τ2,
174+
this.vx + this.ax * τ,
175+
this.vy + this.ay * τ,
176+
this.omega + this.alpha * τ,
177+
this.ax,
178+
this.ay,
179+
this.alpha,
188180
interp_fx,
189181
interp_fy);
190182
}

choreolib/src/main/native/include/choreo/trajectory/DifferentialSample.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ class DifferentialSample {
169169
auto sample =
170170
frc::RKDP(f, initialState, Eigen::Vector<double, 2>(al, ar), τ);
171171

172-
auto dt = endValue.timestamp - timestamp;
173-
auto jl = (endValue.al - al) / dt;
174-
auto jr = (endValue.ar - ar) / dt;
175-
176172
return DifferentialSample{
177173
wpi::Lerp(timestamp, endValue.timestamp, scale),
178174
units::meter_t{sample(0, 0)},
@@ -181,8 +177,8 @@ class DifferentialSample {
181177
units::meters_per_second_t{sample(3, 0)},
182178
units::meters_per_second_t{sample(4, 0)},
183179
units::radians_per_second_t{sample(5, 0)},
184-
al + jl * τ,
185-
ar + jr * τ,
180+
al,
181+
ar,
186182
wpi::Lerp(fl, endValue.fl, scale),
187183
wpi::Lerp(fr, endValue.fr, scale),
188184
};

choreolib/src/main/native/include/choreo/trajectory/SwerveSample.h

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -195,31 +195,22 @@ class SwerveSample {
195195
// Δt = tₖ₊₁ − tₖ
196196
// τ = timestamp − tₖ
197197
//
198-
// x(τ) = xₖ + vₖτ + 1/2 aₖτ² + 1/6 jₖτ³
199-
// v(τ) = vₖ + aₖτ + 1/2 jₖτ²
200-
// a(τ) = aₖ + jₖτ
201-
//
202-
// where jₖ = (aₖ₊₁ − aₖ)/Δt
203-
auto dt = endValue.timestamp - timestamp;
198+
// x(τ) = xₖ + vₖτ + 1/2 aₖτ²
199+
// v(τ) = vₖ + aₖτ
204200
auto τ = t - timestamp;
205201
auto τ2 = τ * τ;
206-
auto τ3 = τ * τ * τ;
207-
auto jx = (endValue.ax - ax) / dt;
208-
auto jy = (endValue.ay - ay) / dt;
209-
auto η = (endValue.alpha - alpha) / dt;
210-
return SwerveSample{
211-
wpi::Lerp(timestamp, endValue.timestamp, scale),
212-
x + vx * τ + 0.5 * ax * τ2 + 1.0 / 6.0 * jx * τ3,
213-
y + vy * τ + 0.5 * ay * τ2 + 1.0 / 6.0 * jy * τ3,
214-
heading + omega * τ + 0.5 * alpha * τ2 + 1.0 / 6.0 * η * τ3,
215-
vx + ax * τ + 0.5 * jx * τ2,
216-
vy + ay * τ + 0.5 * jy * τ2,
217-
omega + alpha * τ + 0.5 * η * τ2,
218-
ax + jx * τ,
219-
ay + jy * τ,
220-
alpha + η * τ,
221-
interpolatedForcesX,
222-
interpolatedForcesY};
202+
return SwerveSample{wpi::Lerp(timestamp, endValue.timestamp, scale),
203+
x + vx * τ + 0.5 * ax * τ2,
204+
y + vy * τ + 0.5 * ay * τ2,
205+
heading + omega * τ + 0.5 * alpha * τ2,
206+
vx + ax * τ,
207+
vy + ay * τ,
208+
omega + alpha * τ,
209+
ax,
210+
ay,
211+
alpha,
212+
interpolatedForcesX,
213+
interpolatedForcesY};
223214
}
224215

225216
/**

0 commit comments

Comments
 (0)