Skip to content

Commit

Permalink
fix: potentially less scuffed signal refresher
Browse files Browse the repository at this point in the history
  • Loading branch information
mimizh2418 committed Jan 27, 2024
1 parent ebc7798 commit 274729a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/main/java/org/team1540/robot2024/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.littletonrobotics.junction.networktables.NT4Publisher;
import org.littletonrobotics.junction.wpilog.WPILOGReader;
import org.littletonrobotics.junction.wpilog.WPILOGWriter;
import org.team1540.robot2024.util.PhoenixTimeSyncSignalRefresher;

/**
* The VM is configured to automatically run this class, and to call the functions corresponding to
Expand Down Expand Up @@ -84,7 +83,6 @@ public void robotInit() {
*/
@Override
public void robotPeriodic() {
if (Constants.currentMode == Constants.Mode.REAL) PhoenixTimeSyncSignalRefresher.refreshSignals();
// Runs the Scheduler. This is responsible for polling buttons, adding
// newly-scheduled commands, running already-scheduled commands, removing
// finished or interrupted commands, and running subsystem periodic() methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public GyroIOPigeon2() {

@Override
public void updateInputs(GyroIOInputs inputs) {
PhoenixTimeSyncSignalRefresher.refreshSignals();
inputs.connected = BaseStatusSignal.isAllGood(yaw, yawVelocity);
inputs.yawPosition = Rotation2d.fromDegrees(yaw.getValueAsDouble());
inputs.yawVelocityRadPerSec = Units.degreesToRadians(yawVelocity.getValueAsDouble());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public ModuleIOTalonFX(SwerveFactory.SwerveModuleHW hw) {

@Override
public void updateInputs(ModuleIOInputs inputs) {
PhoenixTimeSyncSignalRefresher.refreshSignals();
BaseStatusSignal.refreshAll(
driveVelocity,
driveAppliedVolts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.ctre.phoenix6.BaseStatusSignal;
import com.ctre.phoenix6.CANBus;
import edu.wpi.first.wpilibj.Timer;
import org.team1540.robot2024.Constants;

import java.util.Arrays;

Expand All @@ -14,12 +16,22 @@ public class PhoenixTimeSyncSignalRefresher {
private static BaseStatusSignal[] signals = new BaseStatusSignal[0];
private static final boolean isCANFD = CANBus.isNetworkFD(CAN_BUS);

private static double lastRefreshTimestamp = 0;

/**
* Registers status signals to be refreshed when {@link PhoenixTimeSyncSignalRefresher#refreshSignals()}is called
*/
public static void registerSignals(BaseStatusSignal... newSignals) {
signals = Arrays.copyOf(signals, signals.length + newSignals.length);
for (int i = 0; i < newSignals.length; i++) signals[signals.length - 1 - i] = newSignals[i];
}

/**
* Refreshes all registered signals if not already done so in the current loop period
*/
public static void refreshSignals() {
if (Timer.getFPGATimestamp() - lastRefreshTimestamp < Constants.LOOP_PERIOD_SECS) return;
lastRefreshTimestamp = Timer.getFPGATimestamp();
if (isCANFD) BaseStatusSignal.waitForAll(0.01, signals);
else BaseStatusSignal.refreshAll(signals);
}
Expand Down

0 comments on commit 274729a

Please sign in to comment.