Skip to content

Commit

Permalink
Fix concurrent variable editing
Browse files Browse the repository at this point in the history
  • Loading branch information
viggy96 committed Feb 21, 2025
1 parent f5b84e6 commit 97f367b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 22 additions & 6 deletions src/main/java/org/lasarobotics/hardware/PurpleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
public class PurpleManager {
private static double GARBAGE_COLLECTION_SEC = 5.0;
private static Map<LoggableHardware, ScheduledFuture<?>> m_hardware = new HashMap<>();
private static List<Monitorable> m_monitored = new ArrayList<>();
private static List<Runnable> m_callbacks = new ArrayList<>();
private static List<Runnable> m_simCallbacks = new ArrayList<>();
private static VisionSystemSim m_visionSim = new VisionSystemSim("PurpleManager");
private static volatile List<Monitorable> m_monitored = new ArrayList<>();
private static volatile List<Runnable> m_callbacks = new ArrayList<>();
private static volatile List<Runnable> m_simCallbacks = new ArrayList<>();
private static volatile VisionSystemSim m_visionSim = new VisionSystemSim("PurpleManager");
private static Supplier<Pose2d> m_poseSupplier = null;
private static Timer m_garbageTimer = new Timer();
private static ScheduledExecutorService m_inputUpdater = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
Expand All @@ -62,6 +62,16 @@ public Thread newThread(Runnable r) {
return t;
}
});
private static ScheduledExecutorService m_healthMonitor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();

@Override
public Thread newThread(Runnable r) {
Thread t = defaultThreadFactory.newThread(r);
t.setPriority(Thread.MAX_PRIORITY - 2); // Input thread and pose estimation are higher priority
return t;
}
});

/**
* Monitor health of components
Expand Down Expand Up @@ -200,6 +210,13 @@ public static void initialize(LoggedRobot robot,
SignalLogger.start();
}

// Start health monitor thread
m_healthMonitor.scheduleAtFixedRate(PurpleManager::monitorHealth,
0,
(long)GlobalConstants.ROBOT_LOOP_HZ.asPeriod().in(Units.Microseconds),
java.util.concurrent.TimeUnit.MICROSECONDS
);

// Start logging! No more data receivers, replay sources, or metadata values may be added.
Logger.start();
}
Expand Down Expand Up @@ -282,8 +299,7 @@ public static void update() {
// Run garbage collector regularly
if (m_garbageTimer.advanceIfElapsed(GARBAGE_COLLECTION_SEC)) System.gc();

// Monitor health and run periodic logic
monitorHealth();
// Run periodic logic
m_hardware.keySet().stream().forEach((device) -> device.periodic());
m_callbacks.stream().forEach(Runnable::run);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public static class SparkInputs {
}

private static final String LOG_TAG = "Spark";
private static final int CAN_TIMEOUT_MS = 2000;
private static final int MAX_ATTEMPTS = 20;
private static final int CAN_TIMEOUT_MS = 200;
private static final int MAX_ATTEMPTS = 50;
private static final int SPARK_MAX_MEASUREMENT_PERIOD = 16;
private static final int SPARK_FLEX_MEASUREMENT_PERIOD = 32;
private static final int SPARK_MAX_AVERAGE_DEPTH = 2;
Expand Down

0 comments on commit 97f367b

Please sign in to comment.