Class IRObstacleAvoidanceHelper
java.lang.Object
com.opensourcewithslu.inputdevices.IRObstacleAvoidanceHelper
The IRObstacleAvoidance class provides functionality for interfacing with an IR obstacle avoidance module.
This sensor detects obstacles and outputs a LOW signal when an obstacle is detected.
The detection distance can be adjusted using the potentiometer on the module.
-
Constructor Summary
ConstructorsConstructorDescriptionIRObstacleAvoidanceHelper(com.pi4j.io.gpio.digital.DigitalInput digitalInput) Initializes the IR obstacle avoidance module with the specified digital input. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEventListener(com.pi4j.io.gpio.digital.DigitalStateChangeListener function) Registers a listener to be notified when the obstacle detection state changes.com.pi4j.io.gpio.digital.DigitalStategetState()Gets the raw digital state from the sensor.Gets the current obstacle detection status as a human-readable string.booleanChecks if an obstacle is currently detected.voidRemoves the currently registered event listener.voidshutdown()Shuts down the IR obstacle avoidance module and cleans up resources.booleanwaitForObstacle(long timeoutMillis) Waits for an obstacle to be detected with a timeout.
-
Constructor Details
-
IRObstacleAvoidanceHelper
public IRObstacleAvoidanceHelper(com.pi4j.io.gpio.digital.DigitalInput digitalInput) Initializes the IR obstacle avoidance module with the specified digital input.- Parameters:
digitalInput- The Pi4J DigitalInput instance connected to the IR sensor's output pin- Throws:
IllegalArgumentException- if digitalInput is null
-
-
Method Details
-
isObstacleDetected
public boolean isObstacleDetected()Checks if an obstacle is currently detected. The sensor outputs LOW when an obstacle is detected, HIGH when no obstacle is present.- Returns:
- true if an obstacle is detected, false otherwise
-
getState
public com.pi4j.io.gpio.digital.DigitalState getState()Gets the raw digital state from the sensor.- Returns:
- The current DigitalState (HIGH when no obstacle, LOW when obstacle detected)
-
addEventListener
public void addEventListener(com.pi4j.io.gpio.digital.DigitalStateChangeListener function) Registers a listener to be notified when the obstacle detection state changes. Only one listener can be registered at a time. Calling this method again will replace the previous listener.- Parameters:
function- The callback to execute when state changes- Throws:
IllegalArgumentException- if eventConsumer is null
-
removeEventListener
public void removeEventListener()Removes the currently registered event listener. -
waitForObstacle
Waits for an obstacle to be detected with a timeout.- Parameters:
timeoutMillis- Maximum time to wait in milliseconds- Returns:
- true if an obstacle was detected within the timeout, false otherwise
- Throws:
IllegalArgumentException- if timeoutMillis is negativeInterruptedException- if the thread is interrupted while waiting
-
getStatusMessage
Gets the current obstacle detection status as a human-readable string.- Returns:
- "Obstacle detected" or "No obstacle"
-
shutdown
public void shutdown()Shuts down the IR obstacle avoidance module and cleans up resources.
-