Skip to content

Commit 0caa5e2

Browse files
authored
[command] Fix timing issue in RamseteCommand (#2871)
This issue only existed on the initial iteration. When timing is paused and stepped, initialize() and execute() get called with the same timestamp the first time, which would result in a divide by zero. All subsequent steps advance timing and only call execute() so the time deltas are all set correctly.
1 parent f9881a0 commit 0caa5e2

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

cpp/frc2/command/RamseteCommand.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ RamseteCommand::RamseteCommand(
8787
}
8888

8989
void RamseteCommand::Initialize() {
90-
m_prevTime = 0_s;
90+
m_prevTime = -1_s;
9191
auto initialState = m_trajectory.Sample(0_s);
9292
m_prevSpeeds = m_kinematics.ToWheelSpeeds(
9393
frc::ChassisSpeeds{initialState.velocity, 0_mps,
@@ -104,6 +104,16 @@ void RamseteCommand::Execute() {
104104
auto curTime = m_timer.Get();
105105
auto dt = curTime - m_prevTime;
106106

107+
if (m_prevTime < 0_s) {
108+
if (m_usePID)
109+
m_outputVolts(0_V, 0_V);
110+
else
111+
m_outputVel(0_mps, 0_mps);
112+
113+
m_prevTime = curTime;
114+
return;
115+
}
116+
107117
auto targetWheelSpeeds = m_kinematics.ToWheelSpeeds(
108118
m_controller.Calculate(m_pose(), m_trajectory.Sample(curTime)));
109119

@@ -130,9 +140,8 @@ void RamseteCommand::Execute() {
130140
} else {
131141
m_outputVel(targetWheelSpeeds.left, targetWheelSpeeds.right);
132142
}
133-
134-
m_prevTime = curTime;
135143
m_prevSpeeds = targetWheelSpeeds;
144+
m_prevTime = curTime;
136145
}
137146

138147
void RamseteCommand::End(bool interrupted) { m_timer.Stop(); }

0 commit comments

Comments
 (0)