App
The app
module is used communicate between hub and app
Sub Modules
Bargraph
The bargraph
module is used make bar graphs in the SPIKE App
To use the bargraph
module simply import the module like so:
from app import bargraph
Functions
change
change(color: int, value: float) -> None
color: int
A color from the color
module
get_value
get_value(color: int) -> Awaitable
color: int
A color from the color
module
set_value
set_value(color: int, value: float) -> None
color: int
A color from the color
module
show
show(fullscreen: bool) -> None
Display
The display
module is used show images in the SPIKE App
To use the display
module simply import the module like so:
from app import display
Functions
image
image(image: int) -> None
image: int
The id of the image to show. The range of available images is 1 to 21. There are consts on the display
module for these
show
show(fullscreen: bool) -> None
app.display Constants
IMAGE_ROBOT_1 = 1
IMAGE_ROBOT_2 = 2
IMAGE_ROBOT_3 = 3
IMAGE_ROBOT_4 = 4
IMAGE_ROBOT_5 = 5
IMAGE_HUB_1 = 6
IMAGE_HUB_2 = 7
IMAGE_HUB_3 = 8
IMAGE_HUB_4 = 9
IMAGE_AMUSEMENT_PARK = 10
IMAGE_BEACH = 11
IMAGE_HAUNTED_HOUSE = 12
IMAGE_CARNIVAL = 13
IMAGE_BOOKSHELF = 14
IMAGE_PLAYGROUND = 15
IMAGE_MOON = 16
IMAGE_CAVE = 17
IMAGE_OCEAN = 18
IMAGE_POLAR_BEAR = 19
IMAGE_PARK = 20
IMAGE_RANDOM = 21
Linegraph
The linegraph
module is used make line graphs in the SPIKE App
To use the linegraph
module simply import the module like so:
from app import linegraph
Functions
clear
clear(color: int) -> None
color: int
A color from the color
module
get_average
get_average(color: int) -> Awaitable
color: int
A color from the color
module
get_last
get_last(color: int) -> Awaitable
color: int
A color from the color
module
get_max
get_max(color: int) -> Awaitable
color: int
A color from the color
module
get_min
get_min(color: int) -> Awaitable
color: int
A color from the color
module
plot
plot(color: int, x: float, y: float) -> None
color: int
A color from the color
module
show
show(fullscreen: bool) -> None
Music
The music
module is used make music in the SPIKE App
To use the music
module simply import the module like so:
from app import music
Functions
play_drum
play_drum(drum: int) -> None
drum: int
The drum name. See all available values in the app.sound module.
play_instrument
play_instrument(instrument: int, note: int, duration: int) -> None
instrument: int
The instrument name. See all available values in the app.music module.
note: int
The midi note to play (0-130)
duration: int
The duration in milliseconds
app.music Constants
DRUM_BASS = 2
DRUM_BONGO = 13
DRUM_CABASA = 15
DRUM_CLAVES = 9
DRUM_CLOSED_HI_HAT = 6
DRUM_CONGA = 14
DRUM_COWBELL = 11
DRUM_CRASH_CYMBAL = 4
DRUM_CUICA = 18
DRUM_GUIRO = 16
DRUM_HAND_CLAP = 8
DRUM_OPEN_HI_HAT = 5
DRUM_SIDE_STICK = 3
DRUM_SNARE = 1
DRUM_TAMBOURINE = 7
DRUM_TRIANGLE = 12
DRUM_VIBRASLAP = 17
DRUM_WOOD_BLOCK = 10
INSTRUMENT_BASS = 6
INSTRUMENT_BASSOON = 14
INSTRUMENT_CELLO = 8
INSTRUMENT_CHOIR = 15
INSTRUMENT_CLARINET = 10
INSTRUMENT_ELECTRIC_GUITAR = 5
INSTRUMENT_ELECTRIC_PIANO = 2
INSTRUMENT_FLUTE = 12
INSTRUMENT_GUITAR = 4
INSTRUMENT_MARIMBA = 19
INSTRUMENT_MUSIC_BOX = 17
INSTRUMENT_ORGAN = 3
INSTRUMENT_PIANO = 1
INSTRUMENT_PIZZICATO = 7
INSTRUMENT_SAXOPHONE = 11
INSTRUMENT_STEEL_DRUM = 18
INSTRUMENT_SYNTH_LEAD = 20
INSTRUMENT_SYNTH_PAD = 21
INSTRUMENT_TROMBONE = 9
INSTRUMENT_VIBRAPHONE = 16
INSTRUMENT_WOODEN_FLUTE = 13
Sound
The sound
module is used play sounds in the SPIKE App
To use the sound
module simply import the module like so:
from app import sound
Functions
play
play(sound_name: str, volume: int = 100, pitch: int = 0, pan: int = 0) -> Awaitable
Play a sound in the SPIKE App
sound_name: str
The sound name as seen in the Word Blocks sound extension
pan: int
The pan effect determines which speaker is emitting the sound, with "-100" being only the left speaker, "0" being normal, and "100" being only the right speaker.
set_attributes
set_attributes(volume: int, pitch: int, pan: int) -> None
pan: int
The pan effect determines which speaker is emitting the sound, with "-100" being only the left speaker, "0" being normal, and "100" being only the right speaker.
Color
The color
module contains all the color constants to use with the color_matrix
, color_sensor
and light
modules.
To use the Color module add the following import statement to your project:
import color
color Constants
BLACK = 0
MAGENTA = 1
PURPLE = 2
BLUE = 3
AZURE = 4
TURQUOISE = 5
GREEN = 6
YELLOW = 7
ORANGE = 8
RED = 9
WHITE = 10
UNKNOWN = -1
Color Matrix
To use the Color Matrix module add the following import statement to your project:
import color_matrix
All functions in the module should be called inside the color_matrix
module as a prefix like so:
color_matrix.set_pixel(port.A, 1, 1, (color.BLUE, 10))
Functions
clear
Turn off all pixels on a Color Matrix
from hub import port
import color_matrix
color_matrix.clear(port.A)
port: int
A port from the port
submodule in the hub
module
get_pixel
get_pixel(port: int, x: int, y: int) -> tuple[int, int]
Retrieve a specific pixel represented as a tuple containing the color and intensity
from hub import port
import color_matrix
print(color_matrix.get_pixel(port.A, 0, 0))
port: int
A port from the port
submodule in the hub
module
y: int
The Y value, range (0 - 2)
set_pixel
set_pixel(port: int, x: int, y: int, pixel: tuple[color: int, intensity: int]) -> None
Change a single pixel on a Color Matrix
from hub import port
import color
import color_matrix
color_matrix.set_pixel(port.A, 0, 0, (color.RED, 10))
print(color_matrix.get_pixel(port.A, 0, 0)[0])
port: int
A port from the port
submodule in the hub
module
y: int
The Y value, range (0 - 2)
pixel: tuple[color: int, intensity: int]
Tuple containing color and intensity, meaning how bright to light up the pixel
show
show(port: int, pixels: list[tuple[int, int]]) -> None
Change all pixels at once on a Color Matrix
from hub import port
import color
import color_matrix
pixels = [(color.BLUE, 10)] * 9
color_matrix.show(port.A, pixels)
port: int
A port from the port
submodule in the hub
module
pixels: list[tuple[int, int]]
A list containing color and intensity value tuples for all 9 pixels.
Color Sensor
The color_sensor
module enables you to write code that reacts to specific colors or the intensity of the reflected light.
To use the Color Sensor module add the following import statement to your project:
import color_sensor
All functions in the module should be called inside the color_sensor
module as a prefix like so:
color_sensor.reflection(port.A)
The Color Sensor can recognize the following colors:
Red
Green
Blue
Magenta
Yellow
Orange
Azure
Black
White
Functions
color
Returns the color value of the detected color. Use the color
module to map the color value to a specific color.
import color_sensor
from hub import port
import color
if color_sensor.color(port.A) is color.RED:
print("Red detected")
port: int
A port from the port
submodule in the hub
module
reflection
reflection(port: int) -> int
Retrieves the intensity of the reflected light (0-100%).
port: int
A port from the port
submodule in the hub
module
rgbi
rgbi(port: int) -> tuple[int, int, int, int]
Retrieves the overall color intensity and intensity of red, green and blue.
Returns tuple[red: int, green: int, blue: int, intensity: int]
port: int
A port from the port
submodule in the hub
module
Device
The device
module enables you to write code to get information about devices plugged into the hub.
To use the Device module add the following import statement to your project:
import device
All functions in the module should be called inside the device
module as a prefix like so:
device.device_id(port.A)
Functions
data
data(port: int) -> tuple[int]
Retrieve the raw LPF-2 data from a device.
port: int
A port from the port
submodule in the hub
module
id
Retrieve the device id of a device. Each device has an id based on its type.
port: int
A port from the port
submodule in the hub
module
get_duty_cycle
get_duty_cycle(port: int) -> int
Retrieve the duty cycle for a device. Returned values is in range 0 to 10000
port: int
A port from the port
submodule in the hub
module
ready
When a device is attached to the hub it might take a short amount of time before it's ready to accept requests.
Use ready
to test for the readiness of the attached devices.
port: int
A port from the port
submodule in the hub
module
set_duty_cycle
set_duty_cycle(port: int, duty_cycle: int) -> None
Set the duty cycle on a device. Range 0 to 10000
port: int
A port from the port
submodule in the hub
module
Distance Sensor
The distance_sensor
module enables you to write code that reacts to specific distances or light up the Distance Sensor in different ways.
To use the Distance Sensor module add the following import statement to your project:
import distance_sensor
All functions in the module should be called inside the distance_sensor
module as a prefix like so:
distance_sensor.distance(port.A)
Functions
clear
Turns off all the lights in the Distance Sensor connected to port
.
port: int
A port from the port
submodule in the hub
module
distance
distance(port: int) -> int
Retrieve the distance in millimeters captured by the Distance Sensor connected to port
. If the Distance Sensor cannot read a valid distance it will return -1.
port: int
A port from the port
submodule in the hub
module
get_pixel
get_pixel(port: int, x: int, y: int) -> int
Retrieve the intensity of a specific light on the Distance Sensor connected to port
.
port: int
A port from the port
submodule in the hub
module
y: int
The Y value, range (0 - 3)
set_pixel
set_pixel(port: int, x: int, y: int, intensity: int) -> None
Changes the intensity of a specific light on the Distance Sensor connected to port
.
port: int
A port from the port
submodule in the hub
module
y: int
The Y value, range (0 - 3)
intensity: int
How bright to light up the pixel
show
show(port: int, pixels: list[int]) -> None
Change all the lights at the same time.
from hub import port
import distance_sensor
pixels = [100] * 4
distance_sensor.show(port.A, pixels)
port: int
A port from the port
submodule in the hub
module
pixels: bytes
A list containing intensity values for all 4 pixels.
Force Sensor
The force_sensor
module contains all functions and constants to use the Force Sensor.
To use the Force Sensor module add the following import statement to your project:
import force_sensor
All functions in the module should be called inside the force_sensor
module as a prefix like so:
force_sensor.force(port.A)
Functions
force
Retrieves the measured force as decinewton. Values range from 0 to 100
from hub import port
import force_sensor
print(force_sensor.force(port.A))
port: int
A port from the port
submodule in the hub
module
pressed
pressed(port: int) -> bool
Tests whether the button on the sensor is pressed. Returns true if the force sensor connected to port is pressed.
from hub import port
import force_sensor
print(force_sensor.pressed(port.A))
port: int
A port from the port
submodule in the hub
module
raw
Returns the raw, uncalibrated force value of the force sensor connected on port port
from hub import port
import force_sensor
print(force_sensor.raw(port.A))
port: int
A port from the port
submodule in the hub
module
Hub
Sub Modules
Button
To use the Button module add the following import statement to your project:
from hub import button
All functions in the module should be called inside the button
module as a prefix like so:
button.pressed(button.LEFT)
Functions
pressed
int pressed(button: int) -> int
This module allows you to react to buttons being pressed on the hub. You must first import the button
module to use the buttons.
from hub import button
left_button_press_duration = 0
while not button.pressed(button.LEFT):
pass
while button.pressed(button.LEFT):
left_button_press_duration = button.pressed(button.LEFT)
print("Left button was pressed for " + str(left_button_press_duration) + " milliseconds")
button: int
A button from the button
submodule in the hub
module
hub.button Constants
LEFT = 1
Left button next to the power button on the SPIKE Prime hub
RIGHT = 2
Right button next to the power button on the SPIKE Prime hub
Light
The light
module includes functions to change the color of the light on the SPIKE Prime hub.
To use the Light module add the following import statement to your project:
from hub import light
All functions in the module should be called inside the light
module as a prefix like so:
light.color(color.RED)
Functions
color
color(light: int, color: int) -> None
Change the color of a light on the hub.
from hub import light
import color
light.color(light.POWER, color.RED)
color: int
A color from the color
module
hub.light Constants
POWER = 0
The power button. On SPIKE Prime it's the button between the left and right buttons.
CONNECT = 1
The light around the Bluetooth connect button on SPIKE Prime.
Light Matrix
To use the Light Matrix module add the following import statement to your project:
from hub import light_matrix
All functions in the module should be called inside the light_matrix
module as a prefix like so:
light_matrix.write("Hello World")
Functions
clear
Switches off all of the pixels on the Light Matrix.
from hub import light_matrix
import time
light_matrix.show_image(2)
time.sleep_ms(2000)
light_matrix.clear()
get_orientation
Retrieve the current orientation of the Light Matrix.
Can be used with the following constants: orientation.UP
, orientation.LEFT
, orientation.RIGHT
, orientation.DOWN
get_pixel
get_pixel(x: int, y: int) -> int
Retrieve the intensity of a specific pixel on the Light Matrix.
from hub import light_matrix
light_matrix.show_image(1)
print(light_matrix.get_pixel(2, 2))
x: int
The X value, range (0 - 4)
y: int
The Y value, range (0 - 4)
set_orientation
set_orientation(top: int) -> int
Change the orientation of the Light Matrix. All subsequent calls will use the new orientation.
Can be used with the following constants: orientation.UP
, orientation.LEFT
, orientation.RIGHT
, orientation.DOWN
top: int
The side of the hub to be the top
set_pixel
set_pixel(x: int, y: int, intensity: int) -> None
Sets the brightness of one pixel (one of the 25 LEDs) on the Light Matrix.
from hub import light_matrix
light_matrix.set_pixel(2, 2, 100)
x: int
The X value, range (0 - 4)
y: int
The Y value, range (0 - 4)
intensity: int
How bright to light up the pixel
show
show(pixels: list[int]) -> None
Change all the lights at the same time.
from hub import light_matrix
pixels = [100] * 25
light_matrix.show(pixels)
pixels: Iterable
A list containing light intensity values for all 25 pixels.
show_image
show_image(image: int) -> None
Display one of the built in images on the display.
from hub import light_matrix
light_matrix.show_image(light_matrix.IMAGE_HAPPY)
image: int
The id of the image to show. The range of available images is 1 to 67. There are consts on the light_matrix
module for these.
write
write(text: str, intensity: int = 100, time_per_character: int = 500) -> Awaitable
Displays text on the Light Matrix, one letter at a time, scrolling from right to left except if there is a single character to show which will not scroll
from hub import light_matrix
light_matrix.write("Hello, world!")
intensity: int
How bright to light up the pixel
time_per_character: int
How long to show each character on the display
hub.light_matrix Constants
IMAGE_HEART = 1
IMAGE_HEART_SMALL = 2
IMAGE_HAPPY = 3
IMAGE_SMILE = 4
IMAGE_SAD = 5
IMAGE_CONFUSED = 6
IMAGE_ANGRY = 7
IMAGE_ASLEEP = 8
IMAGE_SURPRISED = 9
IMAGE_SILLY = 10
IMAGE_FABULOUS = 11
IMAGE_MEH = 12
IMAGE_YES = 13
IMAGE_NO = 14
IMAGE_CLOCK12 = 15
IMAGE_CLOCK1 = 16
IMAGE_CLOCK2 = 17
IMAGE_CLOCK3 = 18
IMAGE_CLOCK4 = 19
IMAGE_CLOCK5 = 20
IMAGE_CLOCK6 = 21
IMAGE_CLOCK7 = 22
IMAGE_CLOCK8 = 23
IMAGE_CLOCK9 = 24
IMAGE_CLOCK10 = 25
IMAGE_CLOCK11 = 26
IMAGE_ARROW_N = 27
IMAGE_ARROW_NE = 28
IMAGE_ARROW_E = 29
IMAGE_ARROW_SE = 30
IMAGE_ARROW_S = 31
IMAGE_ARROW_SW = 32
IMAGE_ARROW_W = 33
IMAGE_ARROW_NW = 34
IMAGE_GO_RIGHT = 35
IMAGE_GO_LEFT = 36
IMAGE_GO_UP = 37
IMAGE_GO_DOWN = 38
IMAGE_TRIANGLE = 39
IMAGE_TRIANGLE_LEFT = 40
IMAGE_CHESSBOARD = 41
IMAGE_DIAMOND = 42
IMAGE_DIAMOND_SMALL = 43
IMAGE_SQUARE = 44
IMAGE_SQUARE_SMALL = 45
IMAGE_RABBIT = 46
IMAGE_COW = 47
IMAGE_MUSIC_CROTCHET = 48
IMAGE_MUSIC_QUAVER = 49
IMAGE_MUSIC_QUAVERS = 50
IMAGE_PITCHFORK = 51
IMAGE_XMAS = 52
IMAGE_PACMAN = 53
IMAGE_TARGET = 54
IMAGE_TSHIRT = 55
IMAGE_ROLLERSKATE = 56
IMAGE_DUCK = 57
IMAGE_HOUSE = 58
IMAGE_TORTOISE = 59
IMAGE_BUTTERFLY = 60
IMAGE_STICKFIGURE = 61
IMAGE_GHOST = 62
IMAGE_SWORD = 63
IMAGE_GIRAFFE = 64
IMAGE_SKULL = 65
IMAGE_UMBRELLA = 66
IMAGE_SNAKE = 67
Motion Sensor
To use the Motion Sensor module add the following import statement to your project:
from hub import motion_sensor
All functions in the module should be called inside the motion_sensor
module as a prefix like so:
motion_sensor.up_face()
Functions
acceleration
acceleration(raw_unfiltered: bool) -> tuple[int, int, int]
Returns a tuple containing x, y & z acceleration values as integers. The values are mili G, so 1 / 1000 G
raw_unfiltered: bool
If we want the data back raw and unfiltered
angular_velocity
angular_velocity(raw_unfiltered: bool) -> tuple[int, int, int]
Returns a tuple containing x, y & z angular velocity values as integers. The values are decidegrees per second
raw_unfiltered: bool
If we want the data back raw and unfiltered
gesture
Returns the gesture recognized.
Possible values are:
motion_sensor.TAPPED
motion_sensor.DOUBLE_TAPPED
motion_sensor.SHAKEN
motion_sensor.FALLING
motion_sensor.UNKNOWN
get_yaw_face
Retrieve the face of the hub that yaw is relative to.
If you put the hub on a flat surface with the face returned pointing up, when you rotate the hub only the yaw will update
motion_sensor.TOP
The SPIKE Prime hub face with the USB charging port.
motion_sensor.FRONT
The SPIKE Prime hub face with the Light Matrix.
motion_sensor.RIGHT
The right side of the SPIKE Prime hub when facing the front hub face.
motion_sensor.BOTTOM
The side of the SPIKE Prime hub where the battery is.
motion_sensor.BACK
The SPIKE Prime hub face where the speaker is.
motion_sensor.LEFT
The left side of the SPIKE Prime hub when facing the front hub face.
quaternion
quaternion() -> tuple[float, float, float, float]
Returns the hub orientation quaternion as a tuple[w: float, x: float, y: float, z: float].
reset_tap_count
reset_tap_count() -> None
Reset the tap count returned by the tap_count
function
reset_yaw
reset_yaw(angle: int) -> None
Change the yaw angle offset.
The angle set will be the new yaw value.
set_yaw_face
set_yaw_face(up: int) -> bool
Change what hub face is used as the yaw face.If you put the hub on a flat surface with this face pointing up, when you rotate the hub only the yaw will update
up: int
The hub face that should be set as the upwards facing hub face.
Available values are:
motion_sensor.TOP
The SPIKE Prime hub face with the USB charging port.
motion_sensor.FRONT
The SPIKE Prime hub face with the Light Matrix.
motion_sensor.RIGHT
The right side of the SPIKE Prime hub when facing the front hub face.
motion_sensor.BOTTOM
The side of the SPIKE Prime hub where the battery is.
motion_sensor.BACK
The SPIKE Prime hub face where the speaker is.
motion_sensor.LEFT
The left side of the SPIKE Prime hub when facing the front hub face.
stable
Whether or not the hub is resting flat.
tap_count
Returns the number of taps recognized since the program started or last time motion_sensor.reset_tap_count()
was called.
tilt_angles
tilt_angles() -> tuple[int, int, int]
Returns a tuple containing yaw pitch and roll values as integers. Values are decidegrees
up_face
Returns the Hub face that is currently facing up
motion_sensor.TOP
The SPIKE Prime hub face with the USB charging port.
motion_sensor.FRONT
The SPIKE Prime hub face with the Light Matrix.
motion_sensor.RIGHT
The right side of the SPIKE Prime hub when facing the front hub face.
motion_sensor.BOTTOM
The side of the SPIKE Prime hub where the battery is.
motion_sensor.BACK
The SPIKE Prime hub face where the speaker is.
motion_sensor.LEFT
The left side of the SPIKE Prime hub when facing the front hub face.
hub.motion_sensor Constants
TAPPED = 0
DOUBLE_TAPPED = 1
SHAKEN = 2
FALLING = 3
UNKNOWN = -1
TOP = 0
The SPIKE Prime hub face with the Light Matrix.
FRONT = 1
The SPIKE Prime hub face where the speaker is.
RIGHT = 2
The right side of the SPIKE Prime hub when facing the front hub face.
BOTTOM = 3
The side of the SPIKE Prime hub where the battery is.
BACK = 4
The SPIKE Prime hub face with the USB charging port.
LEFT = 5
The left side of the SPIKE Prime hub when facing the front hub face.
Port
This module contains constants that enables easy access to the ports on the SPIKE Prime hub. Use the constants in all functions that takes a port
parameter.
To use the Port module add the following import statement to your project:
from hub import port
All functions in the module should be called inside the port
module as a prefix like so:
port.A
hub.port Constants
A = 0
The Port that is labelled ‘A’ on the Hub.
B = 1
The Port that is labelled ‘B’ on the Hub.
C = 2
The Port that is labelled ‘C’ on the Hub.
D = 3
The Port that is labelled ‘D’ on the Hub.
E = 4
The Port that is labelled ‘E’ on the Hub.
F = 5
The Port that is labelled ‘F’ on the Hub.
Sound
To use the Sound module add the following import statement to your project:
from hub import sound
All functions in the module should be called inside the sound
module as a prefix like so:
sound.stop()
Functions
beep
beep(freq: int = 440, duration: int = 500, volume: int = 100, *, attack: int = 0, decay: int = 0, sustain: int = 100, release: int = 0, transition: int = 10, waveform: int = WAVEFORM_SINE, channel: int = DEFAULT) -> Awaitable
Plays a beep sound from the hub
duration: int
The duration in milliseconds
Optional keyword arguments:
attack: int
The time taken for initial run-up of level from nil to peak, beginning when the key is pressed.
decay: int
The time taken for the subsequent run down from the attack level to the designated sustain level.
sustain: int
The level during the main sequence of the sound's duration, until the key is released.
release: int
The time taken for the level to decay from the sustain level to zero after the key is released
transition: int
time in milliseconds to transition into the sound if something is already playing in the channel
waveform: int
The synthesized waveform. Use one of the constants in the hub.sound
module.
channel: int
The desired channel to play on, options are sound.DEFAULT
and sound.ANY
stop
Stops all noise from the hub
volume
volume(volume: int) -> None
hub.sound Constants
ANY = -2
DEFAULT = -1
WAVEFORM_SINE = 1
WAVEFORM_SAWTOOTH = 3
WAVEFORM_SQUARE = 2
WAVEFORM_TRIANGLE = 1
Functions
hardware_id
Retrieve the hardware id.
temperature
Retrieve the hub temperature. Measured in decidegrees celsius (d°C) which is 1 / 10 of a degree celsius (°C)
Motor
To use a Motor add the following import statement to your project:
import motor
All functions in the module should be called inside the motor
module as a prefix like so:
motor.run(port.A, 1000)
Functions
absolute_position
absolute_position(port: int) -> int
Get the absolute position of a Motor
port: int
A port from the port
submodule in the hub
module
get_duty_cycle
get_duty_cycle(port: int) -> int
port: int
A port from the port
submodule in the hub
module
relative_position
relative_position(port: int) -> int
Get the relative position of a Motor
port: int
A port from the port
submodule in the hub
module
reset_relative_position
reset_relative_position(port: int, position: int) -> None
Change the position used as the offset when using the run_to_relative_position
function.
port: int
A port from the port
submodule in the hub
module
run
run(port: int, velocity: int, *, acceleration: int = 1000) -> None
Start a Motor at a constant speed
from hub import port
import motor, time
motor.run(port.A, 1000)
port: int
A port from the port
submodule in the hub
module
velocity: int
The velocity in degrees/sec
Value ranges depends on motor type.
Small motor (essential): -660 to 660
Medium motor: -1110 to 1110
Large motor: -1050 to 1050
Optional keyword arguments:
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
run_for_degrees
run_for_degrees(port: int, degrees: int, velocity: int, *, stop: int = BRAKE, acceleration: int = 1000, deceleration: int = 1000) -> Awaitable
Turn a motor for a specific number of degrees
When awaited returns a status of the movement that corresponds to one of the following constants:
motor.READY
motor.RUNNING
motor.STALLED
motor.CANCELED
motor.ERROR
motor.DISCONNECTED
port: int
A port from the port
submodule in the hub
module
velocity: int
The velocity in degrees/sec
Value ranges depends on motor type.
Small motor (essential): -660 to 660
Medium motor: -1110 to 1110
Large motor: -1050 to 1050
Optional keyword arguments:
stop: int
The behavior of the Motor after it has stopped. Use the constants in the motor
module.
Possible values are
motor.COAST
to make the motor coast until a stop
motor.BREAK
to brake and continue to brake after stop
motor.HOLD
to tell the motor to hold it's position
motor.CONTINUE
to tell the motor to keep running at whatever velocity it's running at until it gets another command
motor.SMART_COAST
to make the motor brake until stop and then coast and compensate for inaccuracies in the next command
motor.SMART_BRAKE
to make the motor brake and continue to brake after stop and compensate for inaccuracies in the next command
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
deceleration: int
The deceleration (deg/sec²) (1 - 10000)
run_for_time
run_for_time(port: int, duration: int, velocity: int, *, stop: int = BRAKE, acceleration: int = 1000, deceleration: int = 1000) -> Awaitable
Run a Motor for a limited amount of time
When awaited returns a status of the movement that corresponds to one of the following constants:
motor.READY
motor.RUNNING
motor.STALLED
motor.ERROR
motor.DISCONNECTED
from hub import port
import runloop
import motor
async def main():
await motor.run_for_time(port.A, 1000, 1000)
await motor_pair.run_for_time(port.A, 1000, 280)
await motor_pair.run_for_time(port.A, 10000, 280, deceleration=10)
runloop.run(main())
port: int
A port from the port
submodule in the hub
module
duration: int
The duration in milliseconds
velocity: int
The velocity in degrees/sec
Value ranges depends on motor type.
Small motor (essential): -660 to 660
Medium motor: -1110 to 1110
Large motor: -1050 to 1050
Optional keyword arguments:
stop: int
The behavior of the Motor after it has stopped. Use the constants in the motor
module.
Possible values are
motor.COAST
to make the motor coast until a stop
motor.BREAK
to brake and continue to brake after stop
motor.HOLD
to tell the motor to hold it's position
motor.CONTINUE
to tell the motor to keep running at whatever velocity it's running at until it gets another command
motor.SMART_COAST
to make the motor brake until stop and then coast and compensate for inaccuracies in the next command
motor.SMART_BRAKE
to make the motor brake and continue to brake after stop and compensate for inaccuracies in the next command
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
deceleration: int
The deceleration (deg/sec²) (1 - 10000)
run_to_absolute_position
run_to_absolute_position(port: int, position: int, velocity: int, *, direction: int = motor.SHORTEST_PATH, stop: int = BRAKE, acceleration: int = 1000, deceleration: int = 1000) -> Awaitable
Turn a motor to an absolute position.
When awaited returns a status of the movement that corresponds to one of the following constants:
motor.READY
motor.RUNNING
motor.STALLED
motor.CANCELED
motor.ERROR
motor.DISCONNECTED
port: int
A port from the port
submodule in the hub
module
velocity: int
The velocity in degrees/sec
Value ranges depends on motor type.
Small motor (essential): -660 to 660
Medium motor: -1110 to 1110
Large motor: -1050 to 1050
Optional keyword arguments:
direction: int
The direction to turn.
Options are:
motor.CLOCKWISE
motor.COUNTERCLOCKWISE
motor.SHORTEST_PATH
motor.LONGEST_PATH
stop: int
The behavior of the Motor after it has stopped. Use the constants in the motor
module.
Possible values are
motor.COAST
to make the motor coast until a stop
motor.BREAK
to brake and continue to brake after stop
motor.HOLD
to tell the motor to hold it's position
motor.CONTINUE
to tell the motor to keep running at whatever velocity it's running at until it gets another command
motor.SMART_COAST
to make the motor brake until stop and then coast and compensate for inaccuracies in the next command
motor.SMART_BRAKE
to make the motor brake and continue to brake after stop and compensate for inaccuracies in the next command
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
deceleration: int
The deceleration (deg/sec²) (1 - 10000)
run_to_relative_position
run_to_relative_position(port: int, position: int, velocity: int, *, stop: int = BRAKE, acceleration: int = 1000, deceleration: int = 1000) -> Awaitable
Turn a motor to a position relative to the current position.
When awaited returns a status of the movement that corresponds to one of the following constants:
motor.READY
motor.RUNNING
motor.STALLED
motor.CANCELED
motor.ERROR
motor.DISCONNECTED
port: int
A port from the port
submodule in the hub
module
velocity: int
The velocity in degrees/sec
Value ranges depends on motor type.
Small motor (essential): -660 to 660
Medium motor: -1110 to 1110
Large motor: -1050 to 1050
Optional keyword arguments:
stop: int
The behavior of the Motor after it has stopped. Use the constants in the motor
module.
Possible values are
motor.COAST
to make the motor coast until a stop
motor.BREAK
to brake and continue to brake after stop
motor.HOLD
to tell the motor to hold it's position
motor.CONTINUE
to tell the motor to keep running at whatever velocity it's running at until it gets another command
motor.SMART_COAST
to make the motor brake until stop and then coast and compensate for inaccuracies in the next command
motor.SMART_BRAKE
to make the motor brake and continue to brake after stop and compensate for inaccuracies in the next command
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
deceleration: int
The deceleration (deg/sec²) (1 - 10000)
set_duty_cycle
set_duty_cycle(port: int, pwm: int) -> None
Start a Motor with a specific pwm
port: int
A port from the port
submodule in the hub
module
pwm: int
The PWM value (-10000-10000)
stop
stop(port: int, *, stop: int = BRAKE) -> None
from hub import port
import motor, time
motor.run(port.A, 1000)
time.sleep_ms(2000)
motor.stop(port.A)
port: int
A port from the port
submodule in the hub
module
Optional keyword arguments:
stop: int
The behavior of the Motor after it has stopped. Use the constants in the motor
module.
Possible values are
motor.COAST
to make the motor coast until a stop
motor.BREAK
to brake and continue to brake after stop
motor.HOLD
to tell the motor to hold it's position
motor.CONTINUE
to tell the motor to keep running at whatever velocity it's running at until it gets another command
motor.SMART_COAST
to make the motor brake until stop and then coast and compensate for inaccuracies in the next command
motor.SMART_BRAKE
to make the motor brake and continue to brake after stop and compensate for inaccuracies in the next command
velocity
velocity(port: int) -> int
Get the velocity (deg/sec) of a Motor
port: int
A port from the port
submodule in the hub
module
motor Constants
READY = 0
RUNNING = 1
STALLED = 2
CANCELLED = 3
ERROR = 4
DISCONNECTED = 5
COAST = 0
BRAKE = 1
HOLD = 2
CONTINUE = 3
SMART_COAST = 4
SMART_BRAKE = 5
CLOCKWISE = 0
COUNTERCLOCKWISE = 1
SHORTEST_PATH = 2
LONGEST_PATH = 3
Motor Pair
The motor_pair
module is used to run motors in a synchronized fashion. This mode is optimal for creating drivebases where you'd want a pair of motors to start and stop at the same time.
To use the motor_pair
module simply import the module like so:
import motor_pair
All functions in the module should be called inside the motor_pair
module as a prefix like so:
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
Functions
move
move(pair: int, steering: int, *, velocity: int = 360, acceleration: int = 1000) -> None
Move a Motor Pair at a constant speed until a new command is given.
from hub import port
import runloop
import motor_pair
async def main():
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
await runloop.sleep_ms(2000)
motor_pair.move(motor_pair.PAIR_1, 0)
await runloop.sleep_ms(2000)
motor_pair.move(motor_pair.PAIR_1, 0, velocity=280)
await runloop.sleep_ms(2000)
motor_pair.move(motor_pair.PAIR_1, 0, velocity=280, acceleration=100)
runloop.run(main())
pair: int
The pair slot of the Motor Pair.
steering: int
The steering (-100 to 100)
Optional keyword arguments:
velocity: int
The velocity in degrees/sec
Value ranges depends on motor type.
Small motor (essential): -660 to 660
Medium motor: -1110 to 1110
Large motor: -1050 to 1050
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
move_for_degrees
move_for_degrees(pair: int, degrees: int, steering: int, *, velocity: int = 360, stop: int = motor.BRAKE, acceleration: int = 1000, deceleration: int = 1000) -> Awaitable
Move a Motor Pair at a constant speed for a specific number of degrees.
When awaited returns a status of the movement that corresponds to one of the following constants from the motor module:
motor.READY
motor.RUNNING
motor.STALLED
motor.CANCELED
motor.ERROR
motor.DISCONNECTED
from hub import port
import runloop
import motor_pair
async def main():
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
await motor_pair.move_for_degrees(motor_pair.PAIR_1, 90, 0)
await motor_pair.move_for_degrees(motor_pair.PAIR_1, 360, 0, velocity=280)
await motor_pair.move_for_degrees(motor_pair.PAIR_1, 360, 0, velocity=280, deceleration=10)
runloop.run(main())
pair: int
The pair slot of the Motor Pair.
steering: int
The steering (-100 to 100)
Optional keyword arguments:
velocity: int
The velocity in degrees/sec
Value ranges depends on motor type.
Small motor (essential): -660 to 660
Medium motor: -1110 to 1110
Large motor: -1050 to 1050
stop: int
The behavior of the Motor after it has stopped. Use the constants in the motor
module.
Possible values are
motor.COAST
to make the motor coast until a stop
motor.BREAK
to brake and continue to brake after stop
motor.HOLD
to tell the motor to hold it's position
motor.CONTINUE
to tell the motor to keep running at whatever velocity it's running at until it gets another command
motor.SMART_COAST
to make the motor brake until stop and then coast and compensate for inaccuracies in the next command
motor.SMART_BRAKE
to make the motor brake and continue to brake after stop and compensate for inaccuracies in the next command
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
deceleration: int
The deceleration (deg/sec²) (1 - 10000)
move_for_time
move_for_time(pair: int, duration: int, steering: int, *, velocity: int = 360, stop: int = motor.BRAKE, acceleration: int = 1000, deceleration: int = 1000) -> Awaitable
Move a Motor Pair at a constant speed for a specific duration.
When awaited returns a status of the movement that corresponds to one of the following constants from the motor module:
motor.READY
motor.RUNNING
motor.STALLED
motor.CANCELED
motor.ERROR
motor.DISCONNECTED
from hub import port
import runloop
import motor_pair
async def main():
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
await motor_pair.move_for_time(motor_pair.PAIR_1, 1000, 0)
await motor_pair.move_for_time(motor_pair.PAIR_1, 1000, 0, velocity=280)
await motor_pair.move_for_time(motor_pair.PAIR_1, 10000, 0, velocity=280, deceleration=10)
runloop.run(main())
pair: int
The pair slot of the Motor Pair.
duration: int
The duration in milliseconds
steering: int
The steering (-100 to 100)
Optional keyword arguments:
velocity: int
The velocity in degrees/sec
Value ranges depends on motor type.
Small motor (essential): -660 to 660
Medium motor: -1110 to 1110
Large motor: -1050 to 1050
stop: int
The behavior of the Motor after it has stopped. Use the constants in the motor
module.
Possible values are
motor.COAST
to make the motor coast until a stop
motor.BREAK
to brake and continue to brake after stop
motor.HOLD
to tell the motor to hold it's position
motor.CONTINUE
to tell the motor to keep running at whatever velocity it's running at until it gets another command
motor.SMART_COAST
to make the motor brake until stop and then coast and compensate for inaccuracies in the next command
motor.SMART_BRAKE
to make the motor brake and continue to brake after stop and compensate for inaccuracies in the next command
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
deceleration: int
The deceleration (deg/sec²) (1 - 10000)
move_tank
move_tank(pair: int, left_velocity: int, right_velocity: int, *, acceleration: int = 1000) -> None
Perform a tank move on a Motor Pair at a constant speed until a new command is given.
from hub import port
import runloop
import motor_pair
async def main():
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
motor_pair.move_tank(motor_pair.PAIR_1, 1000, 1000)
await runloop.sleep_ms(2000)
motor_pair.move_tank(motor_pair.PAIR_1, 0, 1000)
await runloop.sleep_ms(2000)
motor_pair.move_tank(motor_pair.PAIR_1, 1000, -1000)
runloop.run(main())
pair: int
The pair slot of the Motor Pair.
left_velocity: int
The velocity (deg/sec) of the left motor.
right_velocity: int
The velocity (deg/sec) of the right motor.
Optional keyword arguments:
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
move_tank_for_degrees
move_tank_for_degrees(pair: int, degrees: int, left_velocity: int, right_velocity: int, *, stop: int = motor.BRAKE, acceleration: int = 1000, deceleration: int = 1000) -> Awaitable
Perform a tank move on a Motor Pair at a constant speed until a new command is given.
When awaited returns a status of the movement that corresponds to one of the following constants from the motor module:
motor.READY
motor.RUNNING
motor.STALLED
motor.CANCELED
motor.ERROR
motor.DISCONNECTED
from hub import port
import runloop
import motor_pair
async def main():
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
await motor_pair.move_tank_for_degrees(motor_pair.PAIR_1, 360, 1000, 1000)
await motor_pair.move_tank_for_degrees(motor_pair.PAIR_1, 180, 0, 1000)
await motor_pair.move_tank_for_degrees(motor_pair.PAIR_1, 720, 1000, -1000)
runloop.run(main())
pair: int
The pair slot of the Motor Pair.
left_velocity: int
The velocity (deg/sec) of the left motor.
right_velocity: int
The velocity (deg/sec) of the right motor.
Optional keyword arguments:
stop: int
The behavior of the Motor after it has stopped. Use the constants in the motor
module.
Possible values are
motor.COAST
to make the motor coast until a stop
motor.BREAK
to brake and continue to brake after stop
motor.HOLD
to tell the motor to hold it's position
motor.CONTINUE
to tell the motor to keep running at whatever velocity it's running at until it gets another command
motor.SMART_COAST
to make the motor brake until stop and then coast and compensate for inaccuracies in the next command
motor.SMART_BRAKE
to make the motor brake and continue to brake after stop and compensate for inaccuracies in the next command
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
deceleration: int
The deceleration (deg/sec²) (1 - 10000)
move_tank_for_time
move_tank_for_time(pair: int, left_velocity: int, right_velocity: int, duration: int, *, stop: int = motor.BRAKE, acceleration: int = 1000, deceleration: int = 1000) -> Awaitable
Perform a tank move on a Motor Pair at a constant speed for a specific amount of time.
When awaited returns a status of the movement that corresponds to one of the following constants from the motor module:
motor.READY
motor.RUNNING
motor.STALLED
motor.CANCELED
motor.ERROR
motor.DISCONNECTED
from hub import port
import runloop
import motor_pair
async def main():
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
await motor_pair.move_tank_for_time(motor_pair.PAIR_1, 1000, 1000, 1000)
await motor_pair.move_tank_for_time(motor_pair.PAIR_1, 0, 1000, 3000)
await motor_pair.move_tank_for_time(motor_pair.PAIR_1, 1000, -1000, 2000)
runloop.run(main())
pair: int
The pair slot of the Motor Pair.
duration: int
The duration in milliseconds
left_velocity: int
The velocity (deg/sec) of the left motor.
right_velocity: int
The velocity (deg/sec) of the right motor.
Optional keyword arguments:
stop: int
The behavior of the Motor after it has stopped. Use the constants in the motor
module.
Possible values are
motor.COAST
to make the motor coast until a stop
motor.BREAK
to brake and continue to brake after stop
motor.HOLD
to tell the motor to hold it's position
motor.CONTINUE
to tell the motor to keep running at whatever velocity it's running at until it gets another command
motor.SMART_COAST
to make the motor brake until stop and then coast and compensate for inaccuracies in the next command
motor.SMART_BRAKE
to make the motor brake and continue to brake after stop and compensate for inaccuracies in the next command
acceleration: int
The acceleration (deg/sec²) (1 - 10000)
deceleration: int
The deceleration (deg/sec²) (1 - 10000)
pair
pair(pair: int, left_motor: int, right_motor: int) -> None
pair two motors (left_motor
& right_motor
) and store the paired motors in pair
.
Use pair
in all subsequent motor_pair
related function calls.
import motor_pair
from hub import port
motor_pair.pair(motor_pair.PAIR_1, port.A, port.B)
pair: int
The pair slot of the Motor Pair.
left_motor: int
The port of the left motor. Use the port
submodule in the hub
module.
right_motor: int
The port of the right motor. Use the port
submodule in the hub
module.
stop
stop(pair: int, *, stop: int = motor.BRAKE) -> None
import motor_pair
motor_pair.stop(motor_pair.PAIR_1)
pair: int
The pair slot of the Motor Pair.
Optional keyword arguments:
stop: int
The behavior of the Motor after it has stopped. Use the constants in the motor
module.
Possible values are
motor.COAST
to make the motor coast until a stop
motor.BREAK
to brake and continue to brake after stop
motor.HOLD
to tell the motor to hold it's position
motor.CONTINUE
to tell the motor to keep running at whatever velocity it's running at until it gets another command
motor.SMART_COAST
to make the motor brake until stop and then coast and compensate for inaccuracies in the next command
motor.SMART_BRAKE
to make the motor brake and continue to brake after stop and compensate for inaccuracies in the next command
unpair
unpair(pair: int) -> None
import motor_pair
motor_pair.unpair(motor_pair.PAIR_1)
pair: int
The pair slot of the Motor Pair.
motor_pair Constants
PAIR_1 = 0
First Motor Pair
PAIR_2 = 1
Second Motor Pair
PAIR_3 = 2
Third Motor Pair
Orientation
The orientation
module contains all the orientation constants to use with the light_matrix
module.
To use the orientation module add the following import statement to your project:
import orientation
orientation Constants
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
Runloop
The runloop
module contains all functions and constants to use the Runloop.
To use the Runloop module add the following import statement to your project:
import runloop
All functions in the module should be called inside the runloop
module as a prefix like so:
runloop.run(some_async_function())
Functions
run
run(*functions: Awaitable) -> None
Start any number of parallel async
functions. This is the function you should use to create programs with a similar structure to Word Blocks.
sleep_ms
sleep_ms(duration: int) -> Awaitable
Pause the execution of the application for any amount of milliseconds.
from hub import light_matrix
import runloop
async def main():
light_matrix.write("Hi!")
await runloop.sleep_ms(10000)
light_matrix.write("Are you still here?")
runloop.run(main())
duration: int
The duration in milliseconds
until
until(function: Callable[[], bool], timeout: int = 0) -> Awaitable
Returns an awaitable that will return when the condition in the function or lambda passed is True
or when it times out
import color_sensor
import color
from hub import port
import runloop
def is_color_red():
return color_sensor.color(port.A) is color.RED
async def main():
await runloop.until(is_color_red)
print("Red!")
runloop.run(main())
function: Callable[[], bool]
A callable with no parameters that returns either True
or False
.
Callable is anything that can be called, so a def
or a lambda
timeout: int
A timeout for the function in milliseconds.
If the callable does not return True
within the timeout, the until
still resolves after the timeout.
0 means no timeout, in that case it will not resolve until the callable returns True