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.
LEGO® Small angular motor 45607 |
Other motors without encoders will report a 0 value if queried.
- 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
- 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
- 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
- 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
- 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)