Skip to content

Commit 019559b

Browse files
authored
[choreolib] Remove jerk from trajectory sample interpolation (#1296)
1 parent 50bf2c2 commit 019559b

File tree

5 files changed

+42
-83
lines changed

5 files changed

+42
-83
lines changed

choreolib/py/choreo/trajectory/__init__.py

Lines changed: 13 additions & 27 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
)
@@ -460,33 +455,24 @@ def interpolate(self, end_value: SwerveSample, t: float) -> SwerveSample:
460455
# interpolating the state gives an inaccurate result if the accelerations
461456
# are changing between states
462457
#
463-
# Δt = tₖ₊₁ − tₖ
464458
# τ = timestamp − tₖ
465459
#
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
460+
# x(τ) = xₖ + vₖτ + 1/2 aₖτ²
461+
# v(τ) = vₖ + aₖτ
472462
τ = t - self.timestamp
473463
τ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
478464

479465
return SwerveSample(
480466
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 + η * τ,
467+
self.x + self.vx * τ + 0.5 * self.ax * τ2,
468+
self.y + self.vy * τ + 0.5 * self.ay * τ2,
469+
self.heading + self.omega * τ + 0.5 * self.alpha * τ2,
470+
self.vx + self.ax * τ,
471+
self.vy + self.ay * τ,
472+
self.omega + self.alpha * τ,
473+
self.ax,
474+
self.ay,
475+
self.alpha,
490476
[lerp(self.fx[i], end_value.fx[i], scale) for i in range(len(self.fx))],
491477
[lerp(self.fy[i], end_value.fy[i], scale) for i in range(len(self.fy))],
492478
)

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 & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,32 +159,23 @@ public SwerveSample interpolate(SwerveSample endValue, double timestamp) {
159159
// interpolating the state gives an inaccurate result if the accelerations are changing between
160160
// states
161161
//
162-
// Δt = tₖ₊₁ − tₖ
163162
// τ = timestamp − tₖ
164163
//
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;
164+
// x(τ) = xₖ + vₖτ + 1/2 aₖτ²
165+
// v(τ) = vₖ + aₖτ
171166
double τ = timestamp - this.t;
172167
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;
177168
return new SwerveSample(
178169
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 + η * τ,
170+
this.x + this.vx * τ + 0.5 * this.ax * τ2,
171+
this.y + this.vy * τ + 0.5 * this.ay * τ2,
172+
this.heading + this.omega * τ + 0.5 * this.alpha * τ2,
173+
this.vx + this.ax * τ,
174+
this.vy + this.ay * τ,
175+
this.omega + this.alpha * τ,
176+
this.ax,
177+
this.ay,
178+
this.alpha,
188179
interp_fx,
189180
interp_fy);
190181
}

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 & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -192,34 +192,24 @@ class SwerveSample {
192192
// interpolating the state gives an inaccurate result if the accelerations
193193
// are changing between states
194194
//
195-
// Δt = tₖ₊₁ − tₖ
196195
// τ = timestamp − tₖ
197196
//
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;
197+
// x(τ) = xₖ + vₖτ + 1/2 aₖτ²
198+
// v(τ) = vₖ + aₖτ
204199
auto τ = t - timestamp;
205200
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};
201+
return SwerveSample{wpi::Lerp(timestamp, endValue.timestamp, scale),
202+
x + vx * τ + 0.5 * ax * τ2,
203+
y + vy * τ + 0.5 * ay * τ2,
204+
heading + omega * τ + 0.5 * alpha * τ2,
205+
vx + ax * τ,
206+
vy + ay * τ,
207+
omega + alpha * τ,
208+
ax,
209+
ay,
210+
alpha,
211+
interpolatedForcesX,
212+
interpolatedForcesY};
223213
}
224214

225215
/**

0 commit comments

Comments
 (0)