- Introduction and Installation
- Connect and Run
- Single Motor
- Double Motor
- Color Sensor
- Controller
- Combine Single Motor and Color Sensor
- Combine Double Motor and Controller
- Constants
The Controller allows control and monitoring of a controller.
Reading data from the Controller can be done inline within your code or via a callback.
import legoeducation as le
import time
# update these values to match the Connection Card
card_color = le.LEGO_COLOR_AZURE
card_serial = '3683'
# Connect to the Controller
controller = le.Controller()
controller.connect(card_color=card_color, card_serial=card_serial)
# Check connection
if not controller.connected:
print('Error connecting to Controller.')
exit(1) # error connecting
# Print lever positions (%) for five seconds
for i in range(50):
print(f"Left lever: {controller.sensor.leftPercent}, Right lever: {controller.sensor.rightPercent}")
time.sleep(0.1)
# Disconnect
controller.disconnect()
exit(0) # successful executionimport legoeducation as le
import time
# update these values to match the Connection Card
card_color = le.LEGO_COLOR_AZURE
card_serial = '3683'
# Callback for monitoring position
def notification_callback(data):
parsed_items = le.device_notification_parser(data)
for parsed_item in parsed_items:
if isinstance(parsed_item, le.ControllerNotification):
print(f"Left lever: {parsed_item.leftPercent}, Right lever: {parsed_item.rightPercent}")
# Connect to the Controller
controller = le.Controller()
controller.connect(card_color=card_color, card_serial=card_serial)
controller.set_notification_callback(notification_callback) # set callback
# Check connection
if not controller.connected:
print('Error connecting to Controller.')
exit(1) # error connecting
# Wait for 5 seconds (while data is streaming via callback)
time.sleep(5)
# Disconnect
controller.disconnect()
exit(0) # successful executionSee controller.py for an example of interacting with the Controller.
There are other available ways for interacting with the Controller. Here are a few common ones:
Controller data (from the controller, e.g. controller.sensor):
leftPercent
rightPercent
leftAngle
rightAngleFor control of the button light color and sound beeps:
controller.light_color(le.LEGO_COLOR_BLUE, pattern=le.LIGHT_PATTERN_BREATHE, intensity=100)
controller.beep(pattern=le.SOUND_PATTERN_BEEP_SINGLE, frequency=440)For more information about interacting with the Controller through the LEGO® Education Python API, use the Python help() command:
help(le.Controller)
