Class UltrasonicDistanceSensorComponent

java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.UltrasonicDistanceSensorComponent

public class UltrasonicDistanceSensorComponent extends Component
Implementation of the CrowPi ultrasonic distance sensor (HC-SR04) using GPIO with Pi4J
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final int
     
    protected static final int
    Default BCM pins of ultrasonic distance sensor for CrowPi
    protected static final long
    Default period in milliseconds of button state poller.
    protected static final double
    Default temperature setting to calculate distances

    Fields inherited from class com.pi4j.crowpi.components.Component

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    UltrasonicDistanceSensorComponent(com.pi4j.context.Context pi4j)
    Creates a new ultrasonic distance sensor component using the default setup.
    UltrasonicDistanceSensorComponent(com.pi4j.context.Context pi4j, int triggerAddress, int echoAddress)
    Creates a new ultrasonic distance sensor component with custom GPIO addresses
  • Method Summary

    Modifier and Type
    Method
    Description
    protected com.pi4j.io.gpio.digital.DigitalInputConfig
    buildDigitalInputConfig(com.pi4j.context.Context pi4j, int address)
    Builds a new DigitalInput instance for the ultrasonic distance sensor.
    protected com.pi4j.io.gpio.digital.DigitalOutputConfig
    buildDigitalOutputConfig(com.pi4j.context.Context pi4j, int address)
    Builds a new instance for the ultrasonic distance sensor.
    protected double
    calculateDistance(double pulseLength, double temperature)
    Calculates measured distance from pulse length with temperature compensation.
    com.pi4j.io.gpio.digital.DigitalInput
    Get the instance of the echo input
    com.pi4j.io.gpio.digital.DigitalOutput
    Get the instance of the trigger output
    protected ScheduledFuture<?>
    Returns the internal scheduled future for the poller thread or null if currently stopped.
    double
    Start a measurement with default temperature setting
    double
    measure(double temperature)
    Start a measurement with custom temperature setting.
    protected double
    Triggers the ultrasonic sensor to start a measurement.
    void
    Sets or disables the handler for the object disappear recognition.
    void
    Sets or disables the handler for the object found recognition.
    void
    setDetectionRange(double minRange, double maxRange)
    With this Method the range where a object should be recognized is defined.
    void
    setMeasurementTemperature(double temperature)
    Sets the currently used measurement temperature by this ultrasonic sensor.
    protected void
    startPoller(long pollerPeriodMs)
    (Re-)starts the poller with the desired time period in milliseconds.
    protected void
    Stops the poller immediately, therefore causing the button states to be no longer refreshed.

    Methods inherited from class com.pi4j.crowpi.components.Component

    sleep, triggerSimpleEvent

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_PIN_TRIGGER

      protected static final int DEFAULT_PIN_TRIGGER
      Default BCM pins of ultrasonic distance sensor for CrowPi
      See Also:
    • DEFAULT_PIN_ECHO

      protected static final int DEFAULT_PIN_ECHO
      See Also:
    • DEFAULT_TEMPERATURE

      protected static final double DEFAULT_TEMPERATURE
      Default temperature setting to calculate distances
      See Also:
    • DEFAULT_POLLER_PERIOD_MS

      protected static final long DEFAULT_POLLER_PERIOD_MS
      Default period in milliseconds of button state poller. The poller will be run in a separate thread and executed every X milliseconds.
      See Also:
  • Constructor Details

    • UltrasonicDistanceSensorComponent

      public UltrasonicDistanceSensorComponent(com.pi4j.context.Context pi4j)
      Creates a new ultrasonic distance sensor component using the default setup.
      Parameters:
      pi4j - Pi4J context
    • UltrasonicDistanceSensorComponent

      public UltrasonicDistanceSensorComponent(com.pi4j.context.Context pi4j, int triggerAddress, int echoAddress)
      Creates a new ultrasonic distance sensor component with custom GPIO addresses
      Parameters:
      pi4j - Pi4J context
      triggerAddress - GPIO address of trigger output
      echoAddress - GPIO address of echo input
  • Method Details

    • setDetectionRange

      public void setDetectionRange(double minRange, double maxRange)
      With this Method the range where a object should be recognized is defined.
      Parameters:
      minRange - Minimal distance to the object in cm
      maxRange - Maximal distance to the object in cm
    • setMeasurementTemperature

      public void setMeasurementTemperature(double temperature)
      Sets the currently used measurement temperature by this ultrasonic sensor.
      Parameters:
      temperature - Temperature the sensor is operating at in [°C].
    • startPoller

      protected void startPoller(long pollerPeriodMs)
      (Re-)starts the poller with the desired time period in milliseconds. If the poller is already running, it will be cancelled and rescheduled with the given time. The first poll happens immediately in a separate thread and does not get delayed.
      Parameters:
      pollerPeriodMs - Polling period in milliseconds
    • stopPoller

      protected void stopPoller()
      Stops the poller immediately, therefore causing the button states to be no longer refreshed. If the poller is already stopped, this method will silently return and do nothing.
    • getPoller

      protected ScheduledFuture<?> getPoller()
      Returns the internal scheduled future for the poller thread or null if currently stopped.
      Returns:
      Active poller instance or null
    • measure

      public double measure()
      Start a measurement with default temperature setting
      Returns:
      Measured distance [cm]
    • measure

      public double measure(double temperature)
      Start a measurement with custom temperature setting. Use this to have a temperature compensation.
      Parameters:
      temperature - Current environment temperature the ultra sonic sensor is working in. Range -20°C to 40°C
      Returns:
      Measured distance [cm]
    • onObjectFound

      public void onObjectFound(SimpleEventHandler handler)
      Sets or disables the handler for the object found recognition. This event gets triggered whenever a object comes into the specified range Only a single event handler can be registered at once.
      Parameters:
      handler - Event handler to call or null to disable
    • onObjectDisappeared

      public void onObjectDisappeared(SimpleEventHandler handler)
      Sets or disables the handler for the object disappear recognition. This event gets triggered whenever a object leaves the specified range Only a single event handler can be registered at once.
      Parameters:
      handler - Event handler to call or null to disable
    • measurePulse

      protected double measurePulse()
      Triggers the ultrasonic sensor to start a measurement. Measures the time until the ECHO is recognized.
      Returns:
      Time which the ultrasonic signal needs to travel to the next object and return to the sensor
    • calculateDistance

      protected double calculateDistance(double pulseLength, double temperature)
      Calculates measured distance from pulse length with temperature compensation.
      Parameters:
      pulseLength - pulse duration in milliseconds
      temperature - temperature during the measurement. Range -20°C to 40°C
      Returns:
      distance in centimeters
    • getDigitalInputEcho

      public com.pi4j.io.gpio.digital.DigitalInput getDigitalInputEcho()
      Get the instance of the echo input
      Returns:
      A digital input configuration
    • getDigitalOutputTrigger

      public com.pi4j.io.gpio.digital.DigitalOutput getDigitalOutputTrigger()
      Get the instance of the trigger output
      Returns:
      A digital output configuration
    • buildDigitalInputConfig

      protected com.pi4j.io.gpio.digital.DigitalInputConfig buildDigitalInputConfig(com.pi4j.context.Context pi4j, int address)
      Builds a new DigitalInput instance for the ultrasonic distance sensor.
      Parameters:
      pi4j - Pi4J context
      address - GPIO address of ultrasonic distance sensor echo input
      Returns:
      DigitalInput configuration
    • buildDigitalOutputConfig

      protected com.pi4j.io.gpio.digital.DigitalOutputConfig buildDigitalOutputConfig(com.pi4j.context.Context pi4j, int address)
      Builds a new instance for the ultrasonic distance sensor.
      Parameters:
      pi4j - Pi4J context
      address - GPIO address of ultrasonic distance sensor trigger output
      Returns:
      DigitalOutput configuration