MotionSensor

Package: Libraries.Robots.Spike · Inherits from: Libraries.Language.Object

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

Variables

  • integer FALLING
  • integer LEFT: The face with the USB charging port
  • integer SHAKEN
  • integer BOTTOM: The right side of the hub when facing the front face
  • integer FRONT: The face with the Light Matrix
  • integer RIGHT: The side of the hub where the speaker is
  • integer DOUBLE_TAPPED
  • integer TOP: The face with the Light Matrix
  • integer UNKOWN
  • integer BACK: The side of the hub where the battery is
  • integer TAPPED

Actions

ChangeYawOffset(integer angle)

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

Parameters

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

Example

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

GetAcceleration(boolean raw_data)

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

Parameters

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

Returns

Libraries.Containers.Array<integer>: 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

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

Returns

Libraries.Containers.Array<integer>: 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.

Returns

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)

Returns

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

GetQuaternion()

Used to get the hub's orientation quaternion

Returns

Libraries.Containers.Array<number>: 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.

Returns

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.

Returns

Libraries.Containers.Array<integer>: 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
use Libraries.Containers.Array
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)

Returns

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.

Returns

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

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

Returns

boolean: A boolean

Example

use Libraries.Robots.Spike.MotionSensor
MotionSensor ms
boolean yaw_face_set = ms:SetYawFace(ms:FRONT)

Inherited Actions

3 actions from Libraries.Language.Object