Skip to content

Commit ebde87f

Browse files
committed
make m_current initalized with 0, 0
1 parent bd78215 commit ebde87f

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

Diff for: wpimath/src/main/java/edu/wpi/first/math/trajectory/TrapezoidProfile.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class TrapezoidProfile {
4343
private int m_direction;
4444

4545
private final Constraints m_constraints;
46-
private State m_current;
46+
private State m_current = new State();
4747

4848
private double m_endAccel;
4949
private double m_endFullSpeed;
@@ -187,7 +187,8 @@ public State calculate(double t, State current, State goal) {
187187
* Returns the time left until a target distance in the profile is reached.
188188
*
189189
* @param target The target distance.
190-
* @return The time left until a target distance in the profile is reached.
190+
* @return The time left until a target distance in the profile is reached, or zero if no goal was
191+
* set.
191192
*/
192193
public double timeLeftUntil(double target) {
193194
double position = m_current.position * m_direction;
@@ -253,7 +254,7 @@ public double timeLeftUntil(double target) {
253254
/**
254255
* Returns the total time the profile takes to reach the goal.
255256
*
256-
* @return The total time the profile takes to reach the goal.
257+
* @return The total time the profile takes to reach the goal, or zero if no goal was set.
257258
*/
258259
public double totalTime() {
259260
return m_endDecel;

Diff for: wpimath/src/main/native/include/frc/trajectory/TrapezoidProfile.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ class TrapezoidProfile {
200200
* Returns the time left until a target distance in the profile is reached.
201201
*
202202
* @param target The target distance.
203-
* @return The time left until a target distance in the profile is reached.
203+
* @return The time left until a target distance in the profile is reached, or
204+
* zero if no goal was set.
204205
*/
205206
constexpr units::second_t TimeLeftUntil(Distance_t target) const {
206207
Distance_t position = m_current.position * m_direction;
@@ -271,7 +272,8 @@ class TrapezoidProfile {
271272
/**
272273
* Returns the total time the profile takes to reach the goal.
273274
*
274-
* @return The total time the profile takes to reach the goal.
275+
* @return The total time the profile takes to reach the goal, or zero if no
276+
* goal was set.
275277
*/
276278
constexpr units::second_t TotalTime() const { return m_endDecel; }
277279

@@ -311,14 +313,14 @@ class TrapezoidProfile {
311313
}
312314

313315
// The direction of the profile, either 1 for forwards or -1 for inverted
314-
int m_direction;
316+
int m_direction = 1;
315317

316318
Constraints m_constraints;
317319
State m_current;
318320

319-
units::second_t m_endAccel;
320-
units::second_t m_endFullSpeed;
321-
units::second_t m_endDecel;
321+
units::second_t m_endAccel = 0_s;
322+
units::second_t m_endFullSpeed = 0_s ;
323+
units::second_t m_endDecel = 0_s;
322324
};
323325

324326
} // namespace frc

Diff for: wpimath/src/test/java/edu/wpi/first/math/trajectory/TrapezoidProfileTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,11 @@ void timingBeforeNegativeGoal() {
246246
}
247247
}
248248
}
249+
250+
@Test
251+
void initalizationOfCurrentState() {
252+
var profile = new TrapezoidProfile(new TrapezoidProfile.Constraints(1, 1));
253+
assertNear(profile.timeLeftUntil(0), 0, 1e-10);
254+
assertNear(profile.totalTime(), 0, 1e-10);
255+
}
249256
}

Diff for: wpimath/src/test/native/cpp/trajectory/TrapezoidProfileTest.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,10 @@ TEST(TrapezoidProfileTest, TimingBeforeNegativeGoal) {
234234
}
235235
}
236236
}
237+
238+
TEST(TrapezoidProfileTest, InitalizationOfCurrentState) {
239+
frc::TrapezoidProfile<units::meter>::Constraints constraints{1_mps, 1_mps_sq};
240+
frc::TrapezoidProfile<units::meter> profile{constraints};
241+
EXPECT_NEAR_UNITS(profile.TimeLeftUntil(0_m), 0_s, 1e-10_s);
242+
EXPECT_NEAR_UNITS(profile.TotalTime(), 0_s, 1e-10_s);
243+
}

0 commit comments

Comments
 (0)