Python version compatibility for ev3dev-lang-python
ev3dev-stretchThis library is compatible only with:
- Python 3
- MicroPython
It is not compatible with Python 2.x.
repository·ev3dev-stretch·Indexed 19 days ago
https://github.com/ev3dev/ev3dev-lang-pythonPython language bindings for ev3dev, providing an interface to control LEGO EV3 hardware including motors, sensors, LEDs, and sound using Python 3 and MicroPython. The library includes classes for interacting with the LCD console and display, handling physical buttons, and managing various motor types such as Tacho, Large, Medium, and Servo motors.
This library is compatible only with:
It is not compatible with Python 2.x.
RemoteControl and BeaconSeeker classes have been removed. All infrared-based functionality is now handled by the InfraredSensor class. Note that many properties on InfraredSensor have also been renamed for clarity.ev3dev2.wheel module, all units for the Wheel class are measured in millimeters. To ensure accurate movement and distance calculations, you must provide the correct diameter and width for your specific LEGO wheels. A useful resource for finding these dimensions is http://wheels.sariel.pl/.To control groups of motors simultaneously (e.g., for a robot chassis), use the following abstractions:
MotorSet: A collection of motors that can be controlled together.MoveTank: Controls two motors independently (tank drive style).MoveSteering: Controls two motors to achieve a specific steering angle.MoveJoystick: Controls motors based on joystick input.MoveDifferential: Controls motors using differential drive logic.Sound class has been re-designed in version 2. The names and interfaces of several methods have changed compared to version 1.Most motor methods accept a speed argument. Instead of using a raw integer (which represents a percentage of max speed), you can use specific unit classes to define speed in more intuitive terms. This makes code more readable and precise.
Available unit classes include:
SpeedPercent: Percentage of maximum speed.SpeedRPS: Rotations per second.SpeedRPM: Rotations per minute.SpeedDPS: Degrees per second.SpeedDPM: Degrees per minute.SpeedNativeUnits: Native units.SpeedValue: Generic speed value.from ev3dev2.motor import SpeedRPM
# rotates the motor at 200 RPM (rotations-per-minute) for five seconds.
my_motor.on_for_seconds(SpeedRPM(200), 5)ev3dev2.sensor.lego module provides specialized classes for common LEGO sensors. These classes inherit from the base Sensor class and include helper functions and property accessors tailored to the specific sensor type (e.g., TouchSensor, ColorSensor, UltrasonicSensor). Using these dedicated classes is preferred over the generic Sensor class when working with supported LEGO hardware.All hardware-specific classes in the ev3dev2 module inherit from the ev3dev2.Device base class. To interact with the hardware, you can use the following utility functions to discover connected devices on the EV3 brick:
ev3dev2.list_devices(): Returns a list of all available device objects.ev3dev2.list_device_names(): Returns a list of names for all available devices.ev3dev2.motor.list_motors(): Returns a list of all connected motors.ev3dev2.sensor.list_sensors(): Returns a list of all connected sensors.ev3dev2.port.LegoPort class is used for manually reconfiguring input/output ports. This is primarily required when working on the BrickPi platform. For other platforms like the EV3, manual port reconfiguration is typically unnecessary and most users can ignore this class.To ensure your script runs using the MicroPython interpreter instead of the standard Python 3 interpreter, update the shebang line at the very top of your .py file to use micropython instead of python3.
#!/usr/bin/env micropythonYou can use RPyC to run Python code on a powerful machine (like a laptop) that controls an ev3dev device over a network. This is useful for CPU-intensive tasks like Rubik's cube solvers.
To use the ev3dev2 API remotely, connect via rpyc.classic.connect(), then access the remote modules through the conn.modules attribute. This allows you to instantiate remote classes like LargeMotor or TouchSensor as if they were local.
import rpyc
# Connect to the remote ev3dev device via IP
conn = rpyc.classic.connect('X.X.X.X')
# Access remote ev3dev2 modules
ev3dev2_motor = conn.modules['ev3dev2.motor']
ev3dev2_sensor = conn.modules['ev3dev2.sensor']
ev3dev2_sensor_lego = conn.modules['ev3dev2.sensor.lego']
# Instantiate remote hardware objects
motor = ev3dev2_motor.LargeMotor(ev3dev2_motor.OUTPUT_A)
ts = ev3dev2_sensor_lego.TouchSensor(ev3dev2_sensor.INPUT_1)
# Control hardware
while True:
ts.wait_for_pressed()
motor.run_forever(speed_sp=200)
ts.wait_for_released()
motor.stop()