https://pybricks.github.io/ev3-micropython/startinstall.html Setup EV3 Python on EV3 brick
Hints:
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 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) |
Python > EV3 MicroPython >

