lpf2.devices

The motors, sensors and lights you can plug into a port.

You don’t create these yourself: ask the port with hub.ports.A.device() and you get the right one for whatever is plugged in, with functions that fit that device.

Classes

accelerometer

The hub's built-in accelerometer (hub.accelerometer): measures pushes and gravity in milli-g (1000 = gravity).

basic_motor

A simple motor without a rotation sensor, like a train motor. You can set its power, but not its exact speed or position.

color_distance_sensor

The BOOST Color & Distance Sensor: sees colors and roughly how far away things are.

color_sensor

The Technic Color Sensor: sees colors and how much light bounces back.

distance_sensor

The Technic Distance Sensor (the one with two "eyes"): measures how far away things are.

encoder_motor

A motor with a rotation sensor (most Technic and BOOST motors).

gyroscope

The hub's built-in gyroscope (hub.gyro): measures how fast the hub spins, in degrees per second.

hub_led

A hub's built-in colored light (hub.led).

port_expander

A Port Expander: plugs into one port and gives you four more.

Functions

registerDefault() → None

Advanced: teach the hub about the standard LEGO devices again.

Package Contents

class lpf2.devices.accelerometer[source]

The hub’s built-in accelerometer (hub.accelerometer): measures pushes and gravity in milli-g (1000 = gravity).

getDeviceType() → int[source]

A number saying what kind of device this is (see lpf2.device_type).

getX() → float[source]

Push along the x direction, in milli-g.

getY() → float[source]

Push along the y direction, in milli-g.

getZ() → float[source]

Push along the z direction (up/down), in milli-g.

name() → str[source]

The device’s name as text, e.g. "Hub Accelerometer".

class lpf2.devices.basic_motor[source]

A simple motor without a rotation sensor, like a train motor. You can set its power, but not its exact speed or position.

getDeviceType() → int[source]

A number saying what kind of device this is (see lpf2.device_type).

name() → str[source]

The device’s name as text, e.g. "DC Motor (dumb)".

startPower(pw: int) → None[source]

Run the motor at a power from -100 to 100. The sign sets the direction; 0 stops.

class lpf2.devices.color_distance_sensor[source]

The BOOST Color & Distance Sensor: sees colors and roughly how far away things are.

getAmbientLight() → int[source]

How bright the surroundings are, 0..100 %.

getColorIdx() → int[source]

The color it sees, as a number from lpf2.color (e.g. compare with color.RED).

getDeviceType() → int[source]

A number saying what kind of device this is (see lpf2.device_type).

getDistance() → float[source]

How far away the nearest object is, in centimeters.

getReflectedLight() → int[source]

How much light bounces back from the surface, 0..100 %. White paper is high, black is low.

getRgb() → tuple[int, int, int][source]

Red, green and blue amounts: r, g, b = sensor.getRgb().

name() → str[source]

The device’s name as text, e.g. "Color Distance Sensor".

setIrTx(value: int) → None[source]

Advanced: send an infrared code, like an old LEGO Power Functions remote.

setLedColor(color: int) → None[source]

Set the sensor’s light to a color from lpf2.color.

setMode(modeNum: int, delta: float = 1.0) → None[source]

Advanced: choose which value the sensor reports (one of the MODE_… numbers).

MODE_AMBI: int = 4[source]

surrounding light.

Type:

Mode number

MODE_COLOR: int = 0[source]

detected color.

Type:

Mode number

MODE_DIST: int = 1[source]

distance.

Type:

Mode number

MODE_IR: int = 7[source]

infrared remote-control signals.

Type:

Mode number

MODE_LED: int = 5[source]

the sensor’s light color.

Type:

Mode number

MODE_REFLT: int = 3[source]

reflected light.

Type:

Mode number

MODE_RGB: int = 6[source]

r, g, b = sensor.getRgb().

Type:

Red, green and blue amounts

class lpf2.devices.color_sensor[source]

The Technic Color Sensor: sees colors and how much light bounces back.

All its readings are kept up to date at the same time, so you can call any of the get… functions whenever you like.

getColorIdx() → int[source]

The color it sees, as a number from lpf2.color (e.g. compare with color.RED).

getDeviceType() → int[source]

A number saying what kind of device this is (see lpf2.device_type).

getHSV() → tuple[int, int, int][source]

The color as hue (0..360, the color wheel angle), saturation (0..100) and value/brightness: h, s, v = sensor.getHSV().

getRGB() → tuple[int, int, int, int][source]

How much red, green and blue it sees, plus overall brightness: r, g, b, i = sensor.getRGB().

getReflectivity() → float[source]

How much light bounces back from the surface, 0..100 %. White paper is high, black is low.

name() → str[source]

The device’s name as text, e.g. "Technic Color Sensor".

setLight(l1: int, l2: int, l3: int) → None[source]

Set the brightness of the sensor’s three lights, each 0..100 %.

setMode(modeNum: int, delta: float = 1.0) → None[source]

Advanced: choose which value the sensor reports (one of the MODE_… numbers).

MODE_AMBI: int = 2[source]

surrounding light (0..100 %).

Type:

Mode number

MODE_COLOR: int = 0[source]

detected color.

Type:

Mode number

MODE_HSV: int = 6[source]

hue, saturation and value.

Type:

Mode number

MODE_LIGHT: int = 3[source]

the sensor’s own lights.

Type:

Mode number

MODE_REFLT: int = 1[source]

reflected light (0..100 %).

Type:

Mode number

MODE_RGB: int = 5[source]

red, green, blue and brightness.

Type:

Mode number

class lpf2.devices.distance_sensor[source]

The Technic Distance Sensor (the one with two “eyes”): measures how far away things are.

getDeviceType() → int[source]

A number saying what kind of device this is (see lpf2.device_type).

getDistance() → float[source]

How far away the nearest object is, in centimeters.

name() → str[source]

The device’s name as text, e.g. "Technic Distance Sensor".

setLight(l1: int, l2: int, l3: int, l4: int) → None[source]

Set the brightness of the four lights around the eyes, each 0..100 %.

setMode(modeNum: int, delta: float = 1.0) → None[source]

Advanced: choose which value the sensor reports (one of the MODE_… numbers).

MODE_LIGHT: int = 5[source]

the lights around the eyes.

Type:

Mode number

class lpf2.devices.encoder_motor[source]

A motor with a rotation sensor (most Technic and BOOST motors).

It can hold a steady speed, turn an exact number of degrees, or go to a position.

getDeviceType() → int[source]

A number saying what kind of device this is (see lpf2.device_type).

gotoAbsPosition(absPos: int, speed: int = 100, maxPower: int = 100, endState: int = 0, useProfile: int = 0) → None[source]

Turn to position absPos degrees, then stop.

Position 0 is set with presetEncoder(). endState: 0 = coast, 127 = brake, 126 = hold position.

name() → str[source]

The device’s name as text, e.g. "Motor with Encoder".

presetEncoder(pos: int) → None[source]

Stop the motor and call its current position pos degrees (usually 0).

setAccTime(time: int, profile: int) → None[source]

How long (ms) the motor takes to speed up, stored in slot profile (0..15).

setDecTime(time: int, profile: int) → None[source]

How long (ms) the motor takes to slow down, stored in slot profile (0..15).

startPower(pw: int) → None[source]

Run the motor at a power from -100 to 100. The sign sets the direction; 0 stops.

startSpeed(speed: int = 100, maxPower: int = 100, useProfile: int = 0) → None[source]

Keep turning at speed (-100..100) until told otherwise. maxPower (0..100) limits how hard it pushes.

startSpeedForDegrees(degrees: int, speed: int = 100, maxPower: int = 100, endState: int = 0, useProfile: int = 0) → None[source]

Turn by degrees (360 = one full turn), then stop.

Make speed negative to turn the other way. endState: 0 = coast, 127 = brake, 126 = hold position.

startSpeedForTime(time: int, speed: int = 100, maxPower: int = 100, endState: int = 0, useProfile: int = 0) → None[source]

Turn at speed for time milliseconds, then stop.

endState: 0 = coast, 127 = brake, 126 = hold position.

class lpf2.devices.gyroscope[source]

The hub’s built-in gyroscope (hub.gyro): measures how fast the hub spins, in degrees per second.

getDeviceType() → int[source]

A number saying what kind of device this is (see lpf2.device_type).

getX() → float[source]

Spin speed around the x direction, degrees per second.

getY() → float[source]

Spin speed around the y direction, degrees per second.

getZ() → float[source]

Spin speed around the z direction, degrees per second.

name() → str[source]

The device’s name as text, e.g. "Hub Gyroscope".

class lpf2.devices.hub_led[source]

A hub’s built-in colored light (hub.led).

getDeviceType() → int[source]

A number saying what kind of device this is (see lpf2.device_type).

name() → str[source]

The device’s name as text, e.g. "Hub LED".

setColor(r: int, g: int, b: int) → None[source]

Set the light to any color by mixing red, green and blue, each 0..255. setColor(0, 0, 0) is off.

setColorIdx(color: int) → None[source]

Set the light to a named color from lpf2.color, e.g. hub.led.setColorIdx(color.GREEN).

class lpf2.devices.port_expander[source]

A Port Expander: plugs into one port and gives you four more.

getDeviceType() → int[source]

A number saying what kind of device this is (see lpf2.device_type).

getPort(port_num: int) → lpf2.port[source]

One of the expander’s four ports, numbered 0..3 (see lpf2.port_expander.port_num).

It works just like hub.ports.A.

name() → str[source]

The device’s name as text, e.g. "Port Expander".

lpf2.devices.registerDefault() → None[source]

Advanced: teach the hub about the standard LEGO devices again.

This already happens automatically; you never need to call it.