From b6167fa10e72fa909692d10b1572e54cd693fa86 Mon Sep 17 00:00:00 2001 From: Odin Grav Date: Fri, 5 Sep 2025 13:21:23 +0200 Subject: [PATCH 1/2] =?UTF-8?q?bruker=20threading=20for=20=C3=A5=20kj?= =?UTF-8?q?=C3=B8re=20startStop=20funksjonen=20og=20runRobot=20funksjonen?= =?UTF-8?q?=20samtidig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oving_2/main.py | 61 ++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/oving_2/main.py b/oving_2/main.py index 44ec493..06c5ccf 100644 --- a/oving_2/main.py +++ b/oving_2/main.py @@ -7,6 +7,9 @@ from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile +import threading +import sys + # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. @@ -30,31 +33,41 @@ isPressed = False -next_turn = 0 - -while True: - if(startBtn.pressed() and isPressed == False): - ev3.speaker.say("Excercise 2") - wait(500) - isPressed = True - next_turn = 1 +def startStrop(): + global isPressed + while True: + if(startBtn.pressed()): + if isPressed == False: + ev3.speaker.say("Excercise 2") + wait(500) + isPressed = True + next_turn = 1 + else: + isPressed = False + robotDriveBase.stop() + ev3.speaker.say("Excercise done") + + break - elif(isPressed == True): - if startBtn.pressed(): - isPressed = False - break - - elif(usSensor.distance() >= 75): - robotDriveBase.drive(-250, 0) - else: - robotDriveBase.turn(90 * next_turn) - robotDriveBase.straight(-150) - robotDriveBase.turn(90 * next_turn) - next_turn *= -1 - -robotDriveBase.stop() -ev3.speaker.say("Excercise done") + print("The end..") + sys.exit() +def runRobot() : + next_turn = 0 + + while True: + if(isPressed == True): + if(usSensor.distance() >= 75): + robotDriveBase.drive(-250, 0) + else: + robotDriveBase.turn(90 * next_turn) + robotDriveBase.straight(-150) + robotDriveBase.turn(90 * next_turn) + next_turn *= -1 + +t1 = threading.Thread(target=startStrop) +t2 = threading.Thread(target=runRobot) -print("The end..") \ No newline at end of file +t1.start() +t2.start() \ No newline at end of file From 9d595034a9816d2d99cdadef2c1355ed03023537 Mon Sep 17 00:00:00 2001 From: Martin Aronsen Date: Wed, 17 Sep 2025 11:00:02 +0200 Subject: [PATCH 2/2] Fjerna multi thread --- oving_2/main.py | 63 ++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/oving_2/main.py b/oving_2/main.py index 06c5ccf..8b2a59a 100644 --- a/oving_2/main.py +++ b/oving_2/main.py @@ -7,9 +7,6 @@ from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile -import threading -import sys - # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. @@ -26,48 +23,38 @@ leftMotor = Motor(port=Port.A) rightMotor = Motor(port=Port.C) -robotDriveBase = DriveBase(leftMotor, rightMotor, wheel_diameter=56, axle_track=110) +robotDriveBase = DriveBase(leftMotor, rightMotor, wheel_diameter=56, axle_track=160) startBtn = TouchSensor(port=Port.S1) usSensor = UltrasonicSensor(port=Port.S3) isPressed = False -def startStrop(): - global isPressed - while True: - if(startBtn.pressed()): - if isPressed == False: - ev3.speaker.say("Excercise 2") - wait(500) - isPressed = True - next_turn = 1 - else: - isPressed = False - robotDriveBase.stop() - ev3.speaker.say("Excercise done") - - break +next_turn = 0 + +while True: + if(startBtn.pressed() and isPressed == False): + ev3.speaker.say("Excercise 2") + wait(500) + isPressed = True + next_turn = 1 - print("The end..") - sys.exit() + elif(isPressed == True): + if startBtn.pressed(): + isPressed = False + break + + elif(usSensor.distance() >= 150): + robotDriveBase.drive(-250, 0) + else: + robotDriveBase.turn(90 * next_turn) + robotDriveBase.straight(-150) + robotDriveBase.turn(90 * next_turn) + next_turn *= -1 + +robotDriveBase.stop() +ev3.speaker.say("Excercise done") -def runRobot() : - next_turn = 0 - - while True: - if(isPressed == True): - if(usSensor.distance() >= 75): - robotDriveBase.drive(-250, 0) - else: - robotDriveBase.turn(90 * next_turn) - robotDriveBase.straight(-150) - robotDriveBase.turn(90 * next_turn) - next_turn *= -1 - -t1 = threading.Thread(target=startStrop) -t2 = threading.Thread(target=runRobot) -t1.start() -t2.start() \ No newline at end of file +print("The end..") \ No newline at end of file