Skip to content

Instantly share code, notes, and snippets.

@glipR
Last active September 10, 2022 00:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glipR/6cce5e327877b5e8f89da2ec014bfdf4 to your computer and use it in GitHub Desktop.
Save glipR/6cce5e327877b5e8f89da2ec014bfdf4 to your computer and use it in GitHub Desktop.
EV3Dev Python Summary points + links

EV3Dev Python

Setup

Flashing SD Card

SD Card needs to be more than 4GB

Download the ISO here.

Unzip, then open balenaEtcher.

Connecting to Brick

  1. Insert SD Before turning on Brick
  2. Plug in cable, and power on
  3. Go to All Networks > Ensure 'Connected'
  4. Should be visible in VSCode, or SSH-able.

Common Usage

Using VSCode

  1. Find device in EV3Device Browser
  2. Press the upload button to upload your currently opened folder
  3. Two options:

SSH

  1. Right click device, open ssh, and then use ls and cd to locate your file. Then run python3 <filename.py> .

Select from Brick (Required for competitions)

  1. Add shebang #!/usr/bin/env python3
  2. CRLF -> LF
  3. Select file from brick screen

Using Filezilla / PuTTY

  • Host: ev3dev
  • Port: 22
  • Username: robot
  • Password: maker

Motors

from ev3dev2.motor import LargeMotor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C

# Instantiate connection
lm1 = LargeMotor(OUTPUT_A)
lm2 = LargeMotor(OUTPUT_B)
mm = MediumMotor(OUTPUT_C)

# 4 seconds, 70% speed forwards
# Blocking operation
lm1.on_for_seconds(70, 4)

print("LM1 Finished!")

# 2 seconds backwards 30%.
# Both lines execute simulataneously.
lm2.on_for_seconds(-30, 2, block=False)
# On for 40% speed.
mm.on(40)
# If using ev3sim, you should wait_for_tick here.
lm2.wait_while("running")
mm.off()
print("LM2 Finished!")

from ev3dev2.motor import MoveTank, MoveSteering

tank = MoveTank(OUTPUT_A, OUTPUT_B)
# 70% left, 30% right, 2 seconds
tank.on_for_seconds(70, 30, 2)

print("Tank Finished!")

steering = MoveSteering(OUTPUT_A, OUTPUT_B)
# Steering constant 30, speed -40 (So backwards to the left) for 2 seconds
steering.on_for_seconds(30, -40, 2)

print("Steering Finished!")

First party sensors (Color, Ultrasonic)

Specific Sensor Class Documentation

import time
from ev3dev2.sensor.lego import ColorSensor, UltrasonicSensor
from ev3dev2.sensor import INPUT_1, INPUT_2

# Instantiate connection
color = ColorSensor(INPUT_1)
ultrasonic = UltrasonicSensor(INPUT_2)

# Color sensor needs calibration to work optimally.
# Call this function when over a white strip
color.calibrate_white()

while True:
  # ColorSensor stuff
  r, g, b = color.rgb
  c = color.color_name
  intensity = color.reflected_light_intensity
  print("Color Data: R:{}, G:{}. B:{}. Predicted: {}. RLI: {}".format(r, g, b, c, intensity))

  # UltrasonicSensor stuff
  dist = ultrasonic.distance_centimeters
  print("Ultrasonic Data: {}cm away".format(dist))
  
  time.sleep(0.5)

Third party sensors (HiTechnic and others)

Sensor Data

import time
from ev3dev2.sensor import Sensor, INPUT_1, INPUT_2

ir = Sensor(INPUT_1, driver_name="ht-nxt-ir-seek-v2")
ir.mode = "AC-ALL"

compass = Sensor(INPUT_2, driver_name="ht-nxt-compass")

# Do calibration so we are at 0.
compass.COMMAND = "BEGIN-CAL"
compass.COMMAND = "END-CAL"

while True:
  direction = ir.value(0)
  subsensor_values = [
    ir.value(x)
    for x in range(1, 6)
  ] 
  print("Infrared Data: D:{}, V:{}".format(direction, subsensor_values))
  
  bearing = compass.value(0)
  print("Compass Data: B:{}".format(bearing))
  
  time.sleep(0.5)

Miscellaneous Brick Control

Console, Display can be good debugging tools, but in no way vital.

Sound is pretty self explanatory. Make sure related files are also uploaded to the brick.

Leds are super simple debug tools. Some documented functionality does not work well. Best to stick to basic methods.

Buttons

Usage is a bit more complicated.

import time
from ev3dev2.button import Button

btn = Button()

def on_right(pressed):
  if pressed:
    print("Right button pressed!")
  else:
    print("Right button released!")

# Subscribe the event. This alone does nothing, we need to check for events later.
btn.on_right = on_right

# Rather than subscribing, you can wait for events:
print("Waiting for enter...")
btn.wait_for_released(["enter"])
print("Enter got pressed!")

while True:
  # This actually checks the events you've subscribed to.
  btn.process()
  # Just checking whether buttons are currently pressed.
  if btn.check_buttons(["left", "right"]):
    print("Left and right pressed! Exiting...")
    break
  # Same thing
  elif btn.left:
    print("Left button is being held down! (And not right button)")
    print(btn.buttons_pressed, "<- This is the full list of buttons")
  time.sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment