NextColorDistanceSensor

class NextColorDistanceSensor(colorInitializer: () -> NormalizedColorSensor, distanceInitializer: () -> DistanceSensor? = null)

Combines a color sensor and an optional distance sensor into one class. Call update each loop to read the hardware. Use isColor to check against a dev.nextftc.hardware.sensors.colors.ColorProfile.

Example:

val green = ColorProfile(
space = ColorSpace.HSV,
color = NextColor.HSV(160f, 0.8f, 0.7f),
tolerance = NextColor.HSV(15f, 0.3f, 1f),
)

override fun periodic() {
sensor.update()
if (sensor.isWithinDistance(4.0) && sensor.isColor(green)) { ... }
}

Use debug in telemetry to calibrate dev.nextftc.hardware.sensors.colors.ColorProfiles.

Author

28shettr

Parameters

colorInitializer

Lazily resolves the backing NormalizedColorSensor.

distanceInitializer

Optional lazy distance sensor.

Constructors

Link copied to clipboard
constructor(colorInitializer: () -> NormalizedColorSensor, distanceInitializer: () -> DistanceSensor? = null)
constructor(sensorName: String, hasDistance: Boolean = false)

Properties

Link copied to clipboard

Last cached reading as a NextColor. Black until update is called.

Link copied to clipboard
var gain: Float

Gain applied to the color sensor. Higher values amplify readings for better detection at distance or in low light. Typical range is 1..4.

Link copied to clipboard
val hue: Float

Last cached hue in degrees (0..360).

Link copied to clipboard

Last cached saturation (0..1).

Link copied to clipboard

Last cached value/brightness (0..1).

Functions

Link copied to clipboard
fun debug(): String

Single-line telemetry string showing current HSV and distance. Useful for calibrating ColorProfiles.

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

True if the cached color reading matches profile.

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

True if the cached color reading matches profile and an object is within threshold in the given 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 color sensor (and distance sensor, if present) and refreshes the cache. Call this once per loop, before reading any properties.