Motor

LEGO® TECHNIC™ motors from the LEGO® Education SPIKE™ portfolio have an integrated rotation sensor (encoder) and can be positioned with 1-degree accuracy. The encoders can be queried to find the current position of the motor with respect to a ‘zero’ mark shown on the motor itself.

Motors with encoders

LEGO® Large angular motor 45602

BrickLink item 45602

The LEGO Large Angular Motor

LEGO® Medium angular motor 45603

BrickLink item 45603

The LEGO Medium Angular Motor

LEGO® Small angular motor 45607

The LEGO Small Angular Motor

Other motors without encoders will report a 0 value if queried.

Motors without encoders

LEGO® Medium Linear motor 88008

BrickLink item 88008

The LEGO medium linear 88008 Motor

Technic™ Large Motor 88013

BrickLink item 88013

The LEGO large linear 88013 Motor

Technic™ XL Motor 88014

BrickLink item 88014

The LEGO XL linear 88014 Motor

class buildhat.Motor(port)

Motor device

Parameters:

port – Port of device

Raises:

DeviceError – Occurs if there is no motor attached to port

bias(bias)

Bias motor

Parameters:

bias – Value 0 to 1

Raises:

MotorError – Occurs if invalid bias value passed

Deprecated since version 0.6.0.

callback(func)

Set callback function

Parameters:

func – Callback function

coast()

Coast motor

property connected

Whether device is connected or not

Returns:

Connection status

static desc_for_id(typeid)

Translate integer type id to something more descriptive than the device name

Parameters:

typeid – Type of device

Returns:

Description of device

property description

Device on port info

Returns:

Device description

deselect()

Unselect data from mode

float()

Float motor

get()

Extract information from device

Returns:

Data from device

Raises:

DeviceError – Occurs if device not in valid mode

get_aposition()

Get absolute position of motor

Returns:

Absolute position of motor from -180 to 180

Return type:

int

get_position()

Get position of motor with relation to preset position (can be negative or positive)

Returns:

Position of motor in degrees from preset position

Return type:

int

get_speed()

Get speed of motor

Returns:

Speed of motor

Return type:

int

property interval

Interval between data points in milliseconds

Getter:

Gets interval

Setter:

Sets interval

Returns:

Device interval

Return type:

int

isconnected()

Whether it is connected or not

Raises:

DeviceError – Occurs if device no longer the same

mode(modev)

Set combimode or simple mode

Parameters:

modev – List of tuples for a combimode, or integer for simple mode

property name

Determine name of device on port

Returns:

Device name

static name_for_id(typeid)

Translate integer type id to device name (python class)

Parameters:

typeid – Type of device

Returns:

Name of device

off()

Turn off sensor

on()

Turn on sensor

plimit(plimit)

Limit power

Parameters:

plimit – Value 0 to 1

Raises:

MotorError – Occurs if invalid plimit value passed

pwm(pwmv)

PWM motor

Parameters:

pwmv – Value -1 to 1

Raises:

MotorError – Occurs if invalid pwm value passed

pwmparams(pwmthresh, minpwm)

PWM thresholds

Parameters:
  • pwmthresh – Value 0 to 1, threshold below, will switch from fast to slow, PWM

  • minpwm – Value 0 to 1, threshold below which it switches off the drive altogether

Raises:

MotorError – Occurs if invalid values are passed

property release

Determine if motor is released after running, so can be turned by hand

Getter:

Returns whether motor is released, so can be turned by hand

Setter:

Sets whether motor is released, so can be turned by hand

Returns:

Whether motor is released, so can be turned by hand

Return type:

bool

reverse()

Reverse polarity

run_for_degrees(degrees, speed=None, blocking=True)

Run motor for N degrees

Speed of 1 means 1 revolution / second

Parameters:
  • degrees – Number of degrees to rotate

  • speed – Speed ranging from -100 to 100

  • blocking – Whether call should block till finished

Raises:

MotorError – Occurs if invalid speed passed

run_for_rotations(rotations, speed=None, blocking=True)

Run motor for N rotations

Parameters:
  • rotations – Number of rotations

  • speed – Speed ranging from -100 to 100

  • blocking – Whether call should block till finished

Raises:

MotorError – Occurs if invalid speed passed

run_for_seconds(seconds, speed=None, blocking=True)

Run motor for N seconds

Parameters:
  • seconds – Time in seconds

  • speed – Speed ranging from -100 to 100

  • blocking – Whether call should block till finished

Raises:

MotorError – Occurs when invalid speed specified

run_to_position(degrees, speed=None, blocking=True, direction='shortest')

Run motor to position (in degrees)

Parameters:
  • degrees – Position in degrees from -180 to 180

  • speed – Speed ranging from 0 to 100

  • blocking – Whether call should block till finished

  • direction – shortest (default)/clockwise/anticlockwise

Raises:

MotorError – Occurs if invalid speed or angle passed

select()

Request data from mode

Raises:

DeviceError – Occurs if device not in valid mode

set_default_speed(default_speed)

Set the default speed of the motor

Parameters:

default_speed – Speed ranging from -100 to 100

Raises:

MotorError – Occurs if invalid speed passed

set_speed_unit_rpm(rpm=False)

Set whether to use RPM for speed units or not

Parameters:

rpm – Boolean to determine whether to use RPM for units

start(speed=None)

Start motor

Parameters:

speed – Speed ranging from -100 to 100

Raises:

MotorError – Occurs when invalid speed specified

stop()

Stop motor

property typeid

Type ID of device

Returns:

Type ID

property typeidcur

Type ID currently present

Returns:

Type ID

property when_rotated

Handle rotation events

Getter:

Returns function to be called when rotated

Setter:

Sets function to be called when rotated

Returns:

Callback function

Example

"""Example driving motors"""

import time

from buildhat import Motor

motor = Motor('A')
motorb = Motor('B')


def handle_motor(speed, pos, apos):
    """Motor data

    :param speed: Speed of motor
    :param pos: Position of motor
    :param apos: Absolute position of motor
    """
    print("Motor", speed, pos, apos)


motor.when_rotated = handle_motor
motor.set_default_speed(50)

print("Run for degrees 360")
motor.run_for_degrees(360)
time.sleep(3)

print("Run for degrees -360")
motor.run_for_degrees(-360)
time.sleep(3)

print("Start motor")
motor.start()
time.sleep(3)
print("Stop motor")
motor.stop()
time.sleep(1)

print("Run for degrees - 180")
motor.run_for_degrees(180)
time.sleep(3)

print("Run for degrees - 90")
motor.run_for_degrees(90)
time.sleep(3)

print("Run for rotations - 2")
motor.run_for_rotations(2)
time.sleep(3)

print("Run for seconds - 5")
motor.run_for_seconds(5)
time.sleep(3)

print("Run both")
motor.run_for_seconds(5, blocking=False)
motorb.run_for_seconds(5, blocking=False)
time.sleep(10)

print("Run to position -90")
motor.run_to_position(-90)
time.sleep(3)

print("Run to position 90")
motor.run_to_position(90)
time.sleep(3)

print("Run to position 180")
motor.run_to_position(180)
time.sleep(3)