-
Notifications
You must be signed in to change notification settings - Fork 8
Hub Programming API
Everything here is from guesswork and playing around with the micropython environment's help() command over the console. You can access the console via putty or on Linux with screen /dev/ttyACM0
. The baud rate is 115200 if needed.
If you discover anything that I am missing, please open a Github issue so that it can be added.
import hub
hub.battery
function hub.battery.info()
(json) --> Displays info about the battery
hub.battery.capacity_left()
(int?) --> Returns battery capacity as pct
hub.button.BUTTON.COMMAND()
Reads input from a brick button.
BUTTON is "left", "right", or "center"
COMMAND can be:
-
.is_pressed()
--> checks current state of center button (boolean) -
.was_pressed()
--> checks if the center button has been pressed since last checked (boolean) -
.presses()
--> returns number of presses since last call of function (integer)
hub.motion
-
hub.motion.accelerometer()
--> tuple with (x,y,z) axis -
hub.motion.gyroscope()
--> tuple with (x,y,z) axis in degrees -
hub.motion.position()
--> tuple with (x,y,z) axis in degrees --> seems similar to gyroscope but not the same values? -
hub.motion.orientation()
--> string with gesture -
hub.motion.callback(XXX)
--> ? -
hub.motion.gesture('GESTURE')
--> boolean --> is currently GESTURE -
hub.motion.was_gesture('GESTURE')
--> boolean --> was GESTURE since last call
possible gestures:
- leftside
- rightside
- down
- up
- front
- back
- tapped
- doubletapped
- shake
- freefall
hub.Image.IMAGENAME
Built in images
- Image.HEART
- Image.HEART_SMALL
- Image.HAPPY
- Image.SMILE
- Image.SAD
- Image.CONFUSED
- Image.ANGRY
- Image.ASLEEP
- Image.SURPRISED
- Image.SILLY
- Image.FABULOUS
- Image.MEH
- Image.YES
- Image.NO
- Image.CLOCK12, Image.CLOCK11, Image.CLOCK10, Image.CLOCK9, Image.CLOCK8, Image.CLOCK7, Image.CLOCK6, Image.CLOCK5, Image.CLOCK4, Image.CLOCK3, Image.CLOCK2, Image.CLOCK1
- Image.ARROW_N, Image.ARROW_NE, Image.ARROW_E, Image.ARROW_SE, Image.ARROW_S, Image.ARROW_SW, Image.ARROW_W, Image.ARROW_NW
- Image.TRIANGLE
- Image.TRIANGLE_LEFT
- Image.CHESSBOARD
- Image.DIAMOND
- Image.DIAMOND_SMALL
- Image.SQUARE
- Image.SQUARE_SMALL
- Image.RABBIT
- Image.COW
- Image.MUSIC_CROTCHET
- Image.MUSIC_QUAVER
- Image.MUSIC_QUAVERS
- Image.PITCHFORK
- Image.XMAS
- Image.PACMAN
- Image.TARGET
- Image.TSHIRT
- Image.ROLLERSKATE
- Image.DUCK
- Image.HOUSE
- Image.TORTOISE
- Image.BUTTERFLY
- Image.STICKFIGURE
- Image.GHOST
- Image.SWORD
- Image.GIRAFFE
- Image.SKULL
- Image.UMBRELLA
- Image.SNAKE
- Finally, related collections of images have been grouped together:
- Image.ALL_CLOCKS
- Image.ALL_ARROWS
Displaying images:
hub.display.show(image)
You can define images:
image = hub.Image("90004\n06090\n00900\n09090\n90009")
or
image = hub.Image('90009:90009:99999:09640:00900')
The \n are separators by line. Each number is for each pixel/block on the screen. Each number, 0-9, sets the brightness of that pixel via pwm.
hub.led
Control the LED light on the center button.
hub.led(255, 0, 0) # red (r,g,b)
hub.led(3) # blue (colors 0 - 10)
hub.led
for i in range(11):
hub.led(i)
time.sleep(1)
hub.port.PORT.motor.COMMAND
PORT is from A-E
hub.port.A.motor.mode(1)
(stop mode?)
Set stop mode of motors:
hub.port.A.motor.float()
hub.port.A.motor.brake()
-
hub.port.A.motor.pwm(100)
--> set raw pwm control -
hub.port.A.motor.run_at_speed(speed = 50, max_power = 100, acceleration = 100, deceleration = 100, stall = False)
--> just run at a speed until stopped. If it does not work, try removing the XXX= (i.e. remove speed=, max_power=....) -
hub.port.A.motor.run_for_degrees(90, 50)
--> run for degrees,speed (-100-100) -
hub.port.A.motor.run_to_position(90, 50)
(takes in degrees, speed) appears to have the same effect as run_for_degrees -
hub.port.A.motor.default()
(?) -
hub.port.A.motor.run_for_time(100, 50)
--> run for time (ms),speed
Synchronize two motors:
p = hub.port.A.motor.pair(hub.port.B.motor)
p.run_for_time(200,40,-40)
Motor encoder:
hub.port.A.device.get()
--> returns and integer array with [0,relative position,absolute position,0].
The relative position resets whenever you reboot the robot or reset it programmatically. The value ranges from -infinity to infinity. The absolute position is based of the tick mark on the motor axle whole and is from -180 to 180. Both are in degrees.
Color sensor:
hub.port.F.device.get()
--> returns integer array [reflected light, color id]; color id can be type None
Touch sensor:
hub.port.E.device.get()
--> returns integer array [(raw) pressure, pressed, (SI) pressure?];
The first entry ranges from 0-10 and appears to be the pressure in Newtons. The second entry is 0 or 1 for released or pressed. The third entry appears to also be pressure, but ranging from 382-687. It is labelled as SI_PRESSURE, but does not appear to be in Newtons or grams.
Ultrasonic sensor:
hub.port.F.device.get()
--> returns integer array [distance (cm)] with a single entry (distance in cm)
import ubluetooth
Standard micropython bluetooth library available as of the SPIKE firmware 3.0.1. Usage?
import uasyncio
Micropython asyncio.core available in old firmware. Parts still remain in the hub? Usage?