diff --git a/wpimath/src/main/java/edu/wpi/first/math/trajectory/Trajectory.java b/wpimath/src/main/java/edu/wpi/first/math/trajectory/Trajectory.java index f716dc0e94..f7120be7df 100644 --- a/wpimath/src/main/java/edu/wpi/first/math/trajectory/Trajectory.java +++ b/wpimath/src/main/java/edu/wpi/first/math/trajectory/Trajectory.java @@ -30,9 +30,15 @@ public class Trajectory { * Constructs a trajectory from a vector of states. * * @param states A vector of states. + * @throws IllegalArgumentException if the vector of states is empty. */ public Trajectory(final List states) { m_states = states; + + if (m_states.isEmpty()) { + throw new IllegalArgumentException("Trajectory manually created with no states."); + } + m_totalTimeSeconds = m_states.get(m_states.size() - 1).timeSeconds; } @@ -92,8 +98,13 @@ public class Trajectory { * * @param timeSeconds The point in time since the beginning of the trajectory to sample. * @return The state at that point in time. + * @throws IllegalStateException if the trajectory has no states. */ public State sample(double timeSeconds) { + if (m_states.isEmpty()) { + throw new IllegalStateException("Trajectory cannot be sampled if it has no states."); + } + if (timeSeconds <= m_states.get(0).timeSeconds) { return m_states.get(0); } diff --git a/wpimath/src/main/native/cpp/trajectory/Trajectory.cpp b/wpimath/src/main/native/cpp/trajectory/Trajectory.cpp index de47547276..5393f8318c 100644 --- a/wpimath/src/main/native/cpp/trajectory/Trajectory.cpp +++ b/wpimath/src/main/native/cpp/trajectory/Trajectory.cpp @@ -5,6 +5,7 @@ #include "frc/trajectory/Trajectory.h" #include +#include #include #include @@ -54,10 +55,20 @@ Trajectory::State Trajectory::State::Interpolate(State endValue, } Trajectory::Trajectory(const std::vector& states) : m_states(states) { + if (m_states.empty()) { + throw std::invalid_argument( + "Trajectory manually initialized with no states."); + } + m_totalTime = states.back().t; } Trajectory::State Trajectory::Sample(units::second_t t) const { + if (m_states.empty()) { + throw std::runtime_error( + "Trajectory cannot be sampled if it has no states."); + } + if (t <= m_states.front().t) { return m_states.front(); } diff --git a/wpimath/src/main/native/include/frc/trajectory/Trajectory.h b/wpimath/src/main/native/include/frc/trajectory/Trajectory.h index f5ee79b65a..58264a2d63 100644 --- a/wpimath/src/main/native/include/frc/trajectory/Trajectory.h +++ b/wpimath/src/main/native/include/frc/trajectory/Trajectory.h @@ -66,6 +66,8 @@ class WPILIB_DLLEXPORT Trajectory { /** * Constructs a trajectory from a vector of states. + * + * @throws std::invalid_argument if the vector of states is empty. */ explicit Trajectory(const std::vector& states); @@ -77,6 +79,7 @@ class WPILIB_DLLEXPORT Trajectory { /** * Return the states of the trajectory. + * * @return The states of the trajectory. */ const std::vector& States() const { return m_states; } @@ -86,6 +89,7 @@ class WPILIB_DLLEXPORT Trajectory { * * @param t The point in time since the beginning of the trajectory to sample. * @return The state at that point in time. + * @throws std::runtime_error if the trajectory has no states. */ State Sample(units::second_t t) const;