Skip to content

[wpimath] fix null pointer when calling timeLeftUntill #7894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class TrapezoidProfile {
private int m_direction;

private final Constraints m_constraints;
private State m_current;
private State m_current = new State();

private double m_endAccel;
private double m_endFullSpeed;
Expand Down Expand Up @@ -187,7 +187,8 @@ public State calculate(double t, State current, State goal) {
* Returns the time left until a target distance in the profile is reached.
*
* @param target The target distance.
* @return The time left until a target distance in the profile is reached.
* @return The time left until a target distance in the profile is reached, or zero if no goal was
* set.
*/
public double timeLeftUntil(double target) {
double position = m_current.position * m_direction;
Expand Down Expand Up @@ -253,7 +254,7 @@ public double timeLeftUntil(double target) {
/**
* Returns the total time the profile takes to reach the goal.
*
* @return The total time the profile takes to reach the goal.
* @return The total time the profile takes to reach the goal, or zero if no goal was set.
*/
public double totalTime() {
return m_endDecel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ class TrapezoidProfile {
* Returns the time left until a target distance in the profile is reached.
*
* @param target The target distance.
* @return The time left until a target distance in the profile is reached.
* @return The time left until a target distance in the profile is reached, or
* zero if no goal was set.
*/
constexpr units::second_t TimeLeftUntil(Distance_t target) const {
Distance_t position = m_current.position * m_direction;
Expand Down Expand Up @@ -271,7 +272,8 @@ class TrapezoidProfile {
/**
* Returns the total time the profile takes to reach the goal.
*
* @return The total time the profile takes to reach the goal.
* @return The total time the profile takes to reach the goal, or zero if no
* goal was set.
*/
constexpr units::second_t TotalTime() const { return m_endDecel; }

Expand Down Expand Up @@ -311,14 +313,14 @@ class TrapezoidProfile {
}

// The direction of the profile, either 1 for forwards or -1 for inverted
int m_direction;
int m_direction = 1;

Constraints m_constraints;
State m_current;

units::second_t m_endAccel;
units::second_t m_endFullSpeed;
units::second_t m_endDecel;
units::second_t m_endAccel = 0_s;
units::second_t m_endFullSpeed = 0_s;
units::second_t m_endDecel = 0_s;
};

} // namespace frc
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,11 @@ void timingBeforeNegativeGoal() {
}
}
}

@Test
void initalizationOfCurrentState() {
var profile = new TrapezoidProfile(new TrapezoidProfile.Constraints(1, 1));
assertNear(profile.timeLeftUntil(0), 0, 1e-10);
assertNear(profile.totalTime(), 0, 1e-10);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,10 @@ TEST(TrapezoidProfileTest, TimingBeforeNegativeGoal) {
}
}
}

TEST(TrapezoidProfileTest, InitalizationOfCurrentState) {
frc::TrapezoidProfile<units::meter>::Constraints constraints{1_mps, 1_mps_sq};
frc::TrapezoidProfile<units::meter> profile{constraints};
EXPECT_NEAR_UNITS(profile.TimeLeftUntil(0_m), 0_s, 1e-10_s);
EXPECT_NEAR_UNITS(profile.TotalTime(), 0_s, 1e-10_s);
}
Loading