-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add (experimental) multithreading of sensor reading
- Loading branch information
1 parent
974809d
commit 58f2694
Showing
2 changed files
with
57 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package frc.robot.feeder; | ||
|
||
import edu.wpi.first.wpilibj.DigitalSource; | ||
import edu.wpi.first.wpilibj.Notifier; | ||
import edu.wpi.first.wpilibj.SynchronousInterrupt; | ||
import java.util.function.BooleanSupplier; | ||
|
||
public class SensorThread { | ||
private final double kUpdateFrequencyHz = 200.0; | ||
private final Notifier m_thread; | ||
private final SynchronousInterrupt m_synchronousInterrupt; | ||
private double m_lastRisingEdgeTimestamp = 0.0; | ||
private double m_lastFallingEdgeTimestamp = 0.0; | ||
private boolean m_sensorTripped = false; | ||
public final BooleanSupplier getSensorTripped = () -> m_sensorTripped; | ||
|
||
public SensorThread(DigitalSource digitalSource) { | ||
m_thread = new Notifier(this::periodic); | ||
m_thread.setName("SensorThread"); | ||
m_synchronousInterrupt = new SynchronousInterrupt(digitalSource); | ||
m_synchronousInterrupt.setInterruptEdges(true, true); | ||
} | ||
|
||
public void start() { | ||
m_thread.startPeriodic(1.0 / kUpdateFrequencyHz); | ||
} | ||
|
||
private void periodic() { | ||
final double latestRisingEdgeTimestamp = m_synchronousInterrupt.getRisingTimestamp(); | ||
final double latestFallingEdgeTimestamp = m_synchronousInterrupt.getFallingTimestamp(); | ||
final boolean newRisingEdge = latestRisingEdgeTimestamp > m_lastRisingEdgeTimestamp; | ||
if (newRisingEdge) m_lastRisingEdgeTimestamp = latestRisingEdgeTimestamp; | ||
final boolean newFallingEdge = latestFallingEdgeTimestamp > m_lastFallingEdgeTimestamp; | ||
if (newFallingEdge) m_lastFallingEdgeTimestamp = latestFallingEdgeTimestamp; | ||
if (latestFallingEdgeTimestamp > latestRisingEdgeTimestamp && newFallingEdge) | ||
m_sensorTripped = true; | ||
else if (latestRisingEdgeTimestamp > latestFallingEdgeTimestamp && newRisingEdge) | ||
m_sensorTripped = false; | ||
} | ||
} |