ev3dev-lang-python Documentation

repository·ev3dev-stretch·Indexed 19 days ago

https://github.com/ev3dev/ev3dev-lang-python

Python 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.

Tokens
7.2K
Snippets
24
Records
58
Agent score
65%

What's inside ev3dev-lang-python

  1. Control multiple motors with Motor Sets and Movement Classes

    ev3dev-stretch

    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.
  2. Specify motor speed using different unit classes

    ev3dev-stretch

    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)
  3. Use dedicated sensor classes for LEGO sensors

    ev3dev-stretch
    The 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.
  4. Considerations when using RPyC

    ev3dev-stretch

    Pros

    • Lightweight: Only requires an IP connection (no SSH required).
    • Computational Power: Allows offloading heavy calculations to a laptop while controlling the robot.

    Cons

    • Latency: Network connection introduces delay, which may be unsuitable for high-speed reactive robotics.
    • Compatibility: RPyC is only supported by standard Python; it is NOT supported by MicroPython.
  5. Understand the base Device class and device discovery

    ev3dev-stretch

    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.
  6. Manually reconfigure I/O ports using LegoPort

    ev3dev-stretch
    The 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.
  7. Control an ev3dev device remotely using RPyC

    ev3dev-stretch

    You 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()