Libraries.Robots.Spike.MotionSensor Documentation

The motion sensor is a function of the SPIKE Hub which allows for the detection of movements, orientaion, and environmental interactions.

Example Code

use Libraries.Robots.Spike.MotionSensor
MotionSensor ms
integer taps = ms:GetTapCount()

Inherits from: Libraries.Language.Object

Variables Table

VariablesDescription
integer BACKThe side of the hub where the battery is
integer TOPThe face with the Light Matrix
integer RIGHTThe side of the hub where the speaker is
integer SHAKEN
integer FRONTThe face with the Light Matrix
integer UNKOWN
integer DOUBLE_TAPPED
integer BOTTOMThe right side of the hub when facing the front face
integer FALLING
integer TAPPED
integer LEFTThe face with the USB charging port

Actions Documentation

ChangeYawOffset(integer angle)

Changes the Hub's yaw angle offset to the angle specified.

Parameters

  • integer angle: The angle to set as the Hub's yaw offset

Example

use Libraries.Robots.Spike.MotionSensor
MotionSensor ms
ms:ChangeYawOffset(30)

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)

GetAcceleration(boolean raw_data)

Returns the acceleration in all 3 directions in units of mili G (1/1000 G)

Parameters

  • boolean raw_data: A boolean value which determines if the function will return the acceleration as raw unfiltered data (true) or not (false)

Return

Libraries.Containers.Array: An Array of length three, containing information for the x (index 0), y (index 1), and z (index 2) accelerations

Example

use Libraries.Robots.Spike.MotionSensor
use Libraries.Containers.Array
MotionSensor ms
Array<integer> acceleration
acceleration = ms:GetAcceleration(false)

GetAngularVelocity(boolean raw_data)

Returns the angular velocity in all 3 directions in units of decidegrees per second

Parameters

  • boolean raw_data: A boolean value which determines if the function will return the angular velocity as raw unfiltered data (true) or not (false)

Return

Libraries.Containers.Array: An Array of length three, containing information for the x (index 0), y (index 1), and z (index 2) angular velocity

Example

use Libraries.Robots.Spike.MotionSensor
use Libraries.Containers.Array
MotionSensor ms
Array<integer> angular_velocity
angular_velocity = ms:GetAngularVelocity(false)

GetCurrentUpFace()

Returns the hub face that is currently facing up.

Return

integer: An integer for the Hub face which is currently facing up, which corresponds to one of the constants in the MotionSensor class (TOP, FRONT, RIGHT, BOTTOM, BACK, LEFT)

Example

use Libraries.Robots.Spike.MotionSensor
MotionSensor ms
integer up_face = ms:GetCurrentUpFace()

GetGesture()

Retrieves the recognized gesture from 1 of 5 possible values listed as constants in the MotionSensor class (TAPPED, DOUBLE_TAPPED, SHAKEN, FALLING, UNKNOWN)

Return

integer: The recognized gesture from 1 of 5 possible values listed as constants in the MotionSensor class (TAPPED, DOUBLE_TAPPED, SHAKEN, FALLING, UNKNOWN)

Example

use Libraries.Robots.Spike.MotionSensor
MotionSensor ms
integer gesture = ms:GetGesture()

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()

GetQuaternion()

Used to get the hub's orientation quaternion

Return

Libraries.Containers.Array: An Array of length 4 containing the hub orientation quaternion with w at index 0, x at index 1, y at index 2, and z at index 3

Example

use Libraries.Robots.Spike.MotionSensor
use Libraries.Containers.Array
MotionSensor ms
Array<number> quaternion
quaternion = ms:GetQuaternion()

GetTapCount()

Retrieves the amount of taps recognized since the last time ResetTapCount() was called.

Return

integer: An integer stating the amount of taps detected since the count was last reset.

Example

use Libraries.Robots.Spike.MotionSensor
MotionSensor ms
integer taps = ms:GetTapCount()

GetTiltAngles()

Retrieves the angle in all 3 axes of the current orientation of the SPIKE Hub.

Return

Libraries.Containers.Array: An Array of length 3 containing the yaw (index 0), pitch (index 1), and roll (index 2) of the hub.

Example

use Libraries.Robots.Spike.MotionSensor
Array<integer> angles
MotionSensor ms
angles = ms:GetTiltAngles()

GetYawFace()

Retrieves the face of the hub that yaw is relative to from 1 of 6 possible faces listed as constants in the MotionSensor class (TOP, FRONT, RIGHT, BOTTOM, BACK, LEFT)

Return

integer: The recognized yaw_face from 1 of possible faces listed as constants in the MotionSensor class (TOP, FRONT, RIGHT, BOTTOM, BACK, LEFT)

Example

use Libraries.Robots.Spike.MotionSensor
MotionSensor ms
integer yaw_face = ms:GetYawFace()

IsHubStable()

Checks if the hub is in stable, returns true if it is, false if it's not.

Return

boolean: A boolean value that returns true if the hub is stable, and false if it isn't.

Example

use Libraries.Robots.Spike.MotionSensor
MotionSensor ms
if ms:IsHubStable()
    // The Hub is stable!
else
    // The Hub isn't stable
end

ResetTapCount()

Resets the Hub's tap counter to 0

Example

use Libraries.Robots.Spike.MotionSensor
MotionSensor ms
ms:ResetTapCount()

SetYawFace(integer upface)

Sets the face of the hub that yaw is relative to from 1 of 6 possible faces listed as constants in the MotionSensor class (TOP, FRONT, RIGHT, BOTTOM, BACK, LEFT)

Parameters

  • integer upface: The desired yaw face from 1 of possible faces listed as constants in the MotionSensor class (TOP, FRONT, RIGHT, BOTTOM, BACK, LEFT)

Return

boolean: A boolean

Example

use Libraries.Robots.Spike.MotionSensor
MotionSensor ms
integer yaw_face = ms:SetYawFace(ms:FRONT)