NextDistanceSensor

class NextDistanceSensor(initializer: () -> DistanceSensor)

Lightweight wrapper for a distance sensor that caches the last reading. Call update in periodic to read the hardware.

Use isWithinDistance to check if an object is close enough.

Example:

override fun periodic() {
sensor.update()
if (sensor.isWithinDistance(6.7)) { ... }
}

Author

28shettr

Parameters

initializer

Lazily resolves the backing DistanceSensor.

Constructors

Link copied to clipboard
constructor(initializer: () -> DistanceSensor)
constructor(name: String)

Functions

Link copied to clipboard
fun getDistance(unit: DistanceUnit = DistanceUnit.CM): Double

Returns the last cached distance converted to the requested unit.

Link copied to clipboard
fun isWithinDistance(threshold: Double, unit: DistanceUnit = DistanceUnit.CM): Boolean

True if a distance sensor senses an object within threshold in the given unit.

Link copied to clipboard
fun update()

Reads the distance sensor and refreshes the cache. Call this once per loop, before reading any properties.