Libraries.Robots.Spike.DistanceSensor Documentation

The distance sensor is an external device that is connected to the hub through a port. The distance sensor is capable of providing light via the front facing LEDs on the device, and can retrieve the distance in millimeters of a nearby object it is pointed towards.

Example Code

use Libraries.Robots.Spike.DistanceSensor
use Libraries.Robots.Spike.Port
Port port
DistanceSensor ds
ds:SetPort(port:A)
integer currentDistance = ds:GetDistance

Inherits from: Libraries.Language.Object

Actions Documentation

ClearLights()

Shuts off all lights on the distance sensor's light display

Example

use Libraries.Robots.Spike.DistanceSensor
use Libraries.Robots.Spike.Port
Port port
DistanceSensor ds
ds:SetPort(port:A)
ds:ClearLights()

Compare(Libraries.Language.Object object)

This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.

Parameters

Return

integer: The Compare result, Smaller, Equal, or Larger.

Example

Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)

Equals(Libraries.Language.Object object)

This action determines if two objects are equal based on their hash code values.

Parameters

Return

boolean: True if the hash codes are equal and false if they are not equal.

Example

use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)

GetDistance()

Returns the distance in millimeters captured from the distance sensor

Return

integer: The distance in millimeters from the distance sensor, returns -1 if the sensor cannot read a valid distance

Example

use Libraries.Robots.Spike.DistanceSensor
use Libraries.Robots.Spike.Port
Port port
DistanceSensor ds
ds:SetPort(port:A)
integer currentDistance = ds:GetDistance

GetHashCode()

This action gets the hash code for an object.

Return

integer: The integer hash code of the object.

Example

Object o
integer hash = o:GetHashCode()

GetPixel(integer x, integer y)

Retrieves the intensity of a specific pixel on the distance sensor's light display

Parameters

  • integer x: the x coordinate from 0 to 1
  • integer y: the y coordinate from 0 to 1

Return

integer: Intensity, a value between 0 and 100 for the requested pixel

Example

use Libraries.Robots.Spike.DistanceSensor
use Libraries.Robots.Spike.Port
Port port
DistanceSensor ds
ds:SetPort(port:A)
integer firstPixel = ds:GetPixel(0, 0)

GetPort()

Returns the port number of the distance sensor

Return

integer: Port number

Example

use Libraries.Robots.Spike.DistanceSensor
use Libraries.Robots.Spike.Port
Port port
DistanceSensor ds
ds:SetPort(port:A)
integer distanceSensorPort = ds:GetPort()

SetAllLights(integer intensity)

Sets all lights on the display to the provided intensity

Parameters

  • integer intensity: The intensity of the lights from 0 to 100

Example

use Libraries.Robots.Spike.DistanceSensor
use Libraries.Robots.Spike.Port
Port port
DistanceSensor ds
ds:SetPort(port:A)
ds:SetAllLights(50)

SetPixel(integer x, integer y, integer intensity)

Sets the intensity of a specific pixel on the distance sensor's light display

Parameters

  • integer x: The x coordinate from 0 to 1
  • integer y: The y coordinate from 0 to 1
  • integer intensity: The intensity of the provided pixel from 0 to 100

Example

use Libraries.Robots.Spike.DistanceSensor
use Libraries.Robots.Spike.Port
Port port
DistanceSensor ds
ds:SetPort(port:A)
ds:SetPixel(0, 0, 100)

SetPort(integer port)

Sets the port number of the distance sensor object

Parameters

  • integer port: The port (0 through 5) the distance sensor is attached to

Example

use Libraries.Robots.Spike.DistanceSensor
use Libraries.Robots.Spike.Port
Port port
DistanceSensor ds
ds:SetPort(port:A)
ds:SetPort(0)

Show(Libraries.Containers.Array<integer> pixels)

Sets the light intensity of the display pixel by pixel based on the values in a length 4 array of integers from 0 to 100

Parameters

Example

use Libraries.Robots.Spike.DistanceSensor
use Libraries.Containers.Array
use Libraries.Robots.Spike.Port
Port port
DistanceSensor ds
ds:SetPort(port:A)
Array<integer> pixels
pixels:Add(0)
pixels:Add(25)
pixels:Add(50)
pixels:Add(100)
ds:Show(pixels)