Python‎ > ‎EV3 MicroPython‎ > ‎

EV3 Python


BE SURE TO VISIT

Setting Up VS Code EV3 Programming


https://pybricks.github.io/ev3-micropython/startinstall.html

Setup EV3 Python on EV3 brick
  1. Obtain a suitable microSD memory card.
    1. A microSD or microSDHC card (2GB or larger). 
    2. microSDXC is not supported on the EV3. All cards larger than 32GB will not work with the EV3!
  2. Download the latest Linux Debian Stretch ev3dev image. (https://www.ev3dev.org/downloads/)

  3. Download and install Etcher, a free utility that will allow you to flash the ev3dev image to the microSD card.
  4. Use Etcher to flash the image to the card.

  5. Insert the card into the EV3, boot the EV3, do some minor configuring and establish a connection to the computer via USB, WiFi, Bluetooth or Ethernet.

  6. Download and install Microsoft Visual Studio Code (VS Code).
    This is a free multiplatform code editor, compatible with Windows, Mac OS and Linux.

  7. Write and run some non-EV3 Python scripts.
  8. Download and unzip the starter project. 
    From here: https://github.com/ev3dev/vscode-hello-python

  9. Open VS Code, open the starter project folder and install two extensions.
  10. Configure VS Code.
  11. Connect  VS Code to your EV3.
  12. Write and run your first EV3 Python script! 
Hints:
  1. Install latest Python from Python.org on your computer
  2. To allow EV3 Python code to not error during editing on VSCode install the following: (Note: not installing this will not affect operation on EV3 Brick. But helps with syntax checking when developing software)
    1. Open CMD prompt box:
      1. python -m pip install --upgrade pip
      2. pip install python-ev3dev2 --trusted-host pypi.org --trusted-host files.pythonhosted.org


1. Setting Up EV3 using VS Code

Instructions on setting up VS Code to integrate with EV2 Brick

2. How to program EV3 Python


3. Connect to the Lego Brick using SSH



Some basics:

1. Dealing with the Buttons on EV3 Brick


Troubleshooting
Starting: brickrun --directory="/home/robot/ev3test" "/home/robot/ev3test/speak.py"
Started.
----------
/usr/bin/env: 'python3 \r': No such file or directory
----------
Exited with error code 127.
If you encounter an error such as /usr/bin/env: 'python3\r': No such file or directory, you must switch your editor’s “line endings” setting for the file from “CRLF” to just “LF”. This is usually in the status bar at the bottom. For help, see our FAQ page.

At the bottom of the Editor window, click where it says "CRLF" and then choose "LF". Then try running the program again.



Sample MarsRover.py Code:


#!/usr/bin/env python3 from ev3dev.ev3 import * from ev3dev2.sound import Sound from ev3dev2.motor import LargeMotor, MoveTank, MoveSteering, MediumMotor from ev3dev2.motor import OUTPUT_A, OUTPUT_B, OUTPUT_C, OUTPUT_D from ev3dev2.sensor.lego import InfraredSensor, TouchSensor, UltrasonicSensor from ev3dev2.sensor import INPUT_3, INPUT_4 from time import sleep, time, strftime, localtime import os from threading import Thread sound = Sound() spL=70 spR=70 sl=spL sr=spR usL = UltrasonicSensor(INPUT_4) usR = UltrasonicSensor(INPUT_3) #ir = InfraredSensor(INPUT_2) #tsL = TouchSensor(INPUT_2) #tsR = TouchSensor(INPUT_1) mLf = LargeMotor(OUTPUT_B); mLf.stop_action = 'coast' #mLb = MediumMotor(OUTPUT_A); mLb.stop_action = 'coast' mRf = LargeMotor(OUTPUT_C); mRf.stop_action = 'coast' #mRb = MediumMotor(OUTPUT_D); mRb.stop_action = 'coast' os.system('setfont Lat15-TerminusBold32x16') def talk(words): try: if len(words) > 0: sound.speak(words) print(words) words = "" except: sound.speak('error '+words) def checkUS(): global sl, sr, usL first=True while True: words="" if usL.distance_centimeters < 40: if first: first=False words ='Too close in Front' sl = -spL sr = +spR #print(time()) else: if not first: first=True words ='All ok in Front' sl = spL sr = spR #talk(words) sleep (0.01) # Give the CPU a rest def moveT(): ''' Move Tank methods to move the Rover ''' global sl, sr #### MoveTank(leftMotorPort, rightMotorPort) #frontd = MoveTank(OUTPUT_A,OUTPUT_D) backd = MoveTank(OUTPUT_B,OUTPUT_C) # Use a background Thread to check the Ultrasonic Sensor t = Thread(target=checkUS) t.setDaemon(True) t.start() sl=spL sr=spR while True: #frontd.on_for_seconds(sl,sr,1) backd.on(sl,sr) sleep (0.1) # Give the CPU a rest ## MoveS uses MoveSteering to control the rover def moveS(): ''' Move Steering method for driving the Rover ''' #frontd = MoveSteering(OUTPUT_A,OUTPUT_D, motor_class='ev3dev2.motor.MediumMotor') backd = MoveSteering(OUTPUT_B,OUTPUT_C) # t = Thread(target=talk) # t.setDaemon(True) # t.start() d=30 sl=spL sr=spR stg=0 while True: sumd = usL.distance_centimeters + usR.distance_centimeters diffd = usL.distance_centimeters - usR.distance_centimeters #sound.speak(str(round(diffd,1))) # default foward speed and straight steering sl=spL stg=0 if (sumd < 2*d): #if both us distances less than 40 #words = 'About to collide' if (diffd <= 0.2): #if pretty much head on - then reverse and spin round sl=-spL stg=0 backd.on_for_seconds(steering=stg, speed=sl, seconds=2) stg=-100 backd.on_for_seconds(steering=stg, speed=sl, seconds=2) else: # coming at obstacle at an angle - so one US is closer than the other if (usL.distance_centimeters < d or usR.distance_centimeters < d): # or ir.proximity < d*1.4): sl=-spL if (usL.distance_centimeters < usR.distance_centimeters): stg=100 #turn right #words = 'right' elif (usL.distance_centimeters > usR.distance_centimeters): stg = -100 #turn left #words = 'left' else: stg = 0 else: pass #frontd.on(steering=stg,speed=sl) backd.on(steering=stg,speed=sl) sleep (0.1) # Give the CPU a rest ''' def moveI(): # # RC mode - Move individual motors and turn left/right using 2 x touchsensors # Touchsensor returns 0 or 1 depending on being pressed or not. # while True: sr=(spR,-spR)[tsL.is_pressed] #set value based on TUPLE[0|1] sl=(spL,-spL)[tsR.is_pressed] #talk("left {} right {}".format(sl,sr)) mRb.on(sr) mLb.on(sl) sleep (0.01) ''' #---------------------------------------------------------------------------- greeting='Hello James, my name is EV3 Rover!' print(greeting) talk(greeting) #moveT() #moveI() moveS() greeting='Bye Bye Alex' print(greeting) talks(greeting) sleep(1)