Class IRObstacleAvoidanceHelper

java.lang.Object
com.opensourcewithslu.inputdevices.IRObstacleAvoidanceHelper

@Primary public class IRObstacleAvoidanceHelper extends Object
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

    Constructors
    Constructor
    Description
    IRObstacleAvoidanceHelper(com.pi4j.io.gpio.digital.DigitalInput digitalInput)
    Initializes the IR obstacle avoidance module with the specified digital input.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addEventListener(com.pi4j.io.gpio.digital.DigitalStateChangeListener function)
    Registers a listener to be notified when the obstacle detection state changes.
    com.pi4j.io.gpio.digital.DigitalState
    Gets the raw digital state from the sensor.
    Gets the current obstacle detection status as a human-readable string.
    boolean
    Checks if an obstacle is currently detected.
    void
    Removes the currently registered event listener.
    void
    Shuts down the IR obstacle avoidance module and cleans up resources.
    boolean
    waitForObstacle(long timeoutMillis)
    Waits for an obstacle to be detected with a timeout.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public boolean waitForObstacle(long timeoutMillis) throws InterruptedException
      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 negative
      InterruptedException - if the thread is interrupted while waiting
    • getStatusMessage

      public String 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.