mirror of
https://github.com/pybricks/pybricks-projects.git
synced 2026-09-15 11:03:15 +00:00
sets/ev3/home_bonus: add Wack3m program
This commit is contained in:
committed by
laurensvalk
parent
57e2c479a9
commit
5c6a2af6d9
@@ -0,0 +1,3 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
venv/
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
|
||||
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
|
||||
|
||||
// List of extensions which should be recommended for users of this workspace.
|
||||
"recommendations": [
|
||||
"lego-education.ev3-micropython"
|
||||
],
|
||||
|
||||
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
|
||||
"unwantedRecommendations": [
|
||||
"ms-python.python"
|
||||
]
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Download and Run",
|
||||
"type": "ev3devBrowser",
|
||||
"request": "launch",
|
||||
"program": "/home/robot/${workspaceRootFolderName}/main.py",
|
||||
"interactiveTerminal": false
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Place your settings in this file to overwrite default and user settings.
|
||||
{
|
||||
"files.eol": "\n",
|
||||
"debug.openDebug": "neverOpen",
|
||||
"editor.rulers": [79],
|
||||
"python.linting.enabled": true
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
# Example LEGO® MINDSTORMS® EV3 Wack3m Program
|
||||
|
||||
This program requires LEGO® EV3 MicroPython v2.0 downloadable at https://education.lego.com/en-us/support/mindstorms-ev3/python-for-ev3.
|
||||
|
||||
Building instructions can be found at https://www.lego.com/en-us/themes/mindstorms/buildarobot.
|
||||
|
||||
How to play: Press the Touch Sensor to start a game. Each game has ten rounds of whacking, and your average response time is measured.
|
||||
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env pybricks-micropython
|
||||
|
||||
|
||||
from wack3m import Wack3m
|
||||
|
||||
|
||||
wack3m = Wack3m()
|
||||
|
||||
wack3m.start_up()
|
||||
wack3m.play()
|
||||
@@ -0,0 +1,179 @@
|
||||
from pybricks.hubs import EV3Brick
|
||||
from pybricks.ev3devices import Motor, TouchSensor, InfraredSensor
|
||||
from pybricks.media.ev3dev import ImageFile, SoundFile
|
||||
from pybricks.parameters import Direction, Port, Stop, Color
|
||||
from pybricks.tools import wait
|
||||
|
||||
from time import sleep, time
|
||||
from random import randint, uniform
|
||||
|
||||
|
||||
class Wack3m:
|
||||
N_WHACK_TIMES = 10
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
left_motor_port: str = Port.B, right_motor_port: str = Port.C,
|
||||
middle_motor_port: str = Port.A,
|
||||
touch_sensor_port: str = Port.S1, ir_sensor_port: str = Port.S4):
|
||||
self.ev3_brick = EV3Brick()
|
||||
|
||||
self.left_motor = Motor(port=left_motor_port,
|
||||
positive_direction=Direction.CLOCKWISE)
|
||||
self.right_motor = Motor(port=right_motor_port,
|
||||
positive_direction=Direction.CLOCKWISE)
|
||||
self.middle_motor = Motor(port=middle_motor_port,
|
||||
positive_direction=Direction.CLOCKWISE)
|
||||
|
||||
self.touch_sensor = TouchSensor(port=touch_sensor_port)
|
||||
|
||||
self.ir_sensor = InfraredSensor(port=ir_sensor_port)
|
||||
|
||||
def start_up(self):
|
||||
self.ev3_brick.light.on(color=Color.RED)
|
||||
|
||||
self.ev3_brick.screen.print('WACK3M')
|
||||
|
||||
self.left_motor.run_time(
|
||||
speed=-1000,
|
||||
time=1000,
|
||||
then=Stop.HOLD,
|
||||
wait=True)
|
||||
|
||||
self.left_motor.reset_angle(angle=0)
|
||||
|
||||
self.middle_motor.run_time(
|
||||
speed=-1000,
|
||||
time=1000,
|
||||
then=Stop.HOLD,
|
||||
wait=True)
|
||||
|
||||
self.middle_motor.reset_angle(angle=0)
|
||||
|
||||
self.right_motor.run_time(
|
||||
speed=-1000,
|
||||
time=1000,
|
||||
then=Stop.HOLD,
|
||||
wait=True)
|
||||
|
||||
self.right_motor.reset_angle(angle=0)
|
||||
|
||||
def play(self):
|
||||
while True:
|
||||
self.ev3_brick.speaker.play_file(file=SoundFile.START)
|
||||
|
||||
self.ev3_brick.screen.load_image(ImageFile.TARGET)
|
||||
|
||||
self.ev3_brick.light.on(color=Color.ORANGE)
|
||||
|
||||
while not self.touch_sensor.pressed():
|
||||
wait(10)
|
||||
|
||||
self.ev3_brick.speaker.play_file(file=SoundFile.GO)
|
||||
|
||||
self.ev3_brick.light.on(color=Color.GREEN)
|
||||
|
||||
total_response_time = 0
|
||||
|
||||
sleep(1)
|
||||
|
||||
for _ in range(self.N_WHACK_TIMES):
|
||||
self.ev3_brick.light.on(color=Color.GREEN)
|
||||
|
||||
self.ev3_brick.screen.load_image(ImageFile.EV3_ICON)
|
||||
|
||||
sleep(uniform(0.1, 3))
|
||||
|
||||
which_motor = randint(1, 3)
|
||||
|
||||
if which_motor == 1:
|
||||
self.left_motor.run_angle(
|
||||
speed=1000,
|
||||
rotation_angle=90,
|
||||
then=Stop.COAST,
|
||||
wait=True)
|
||||
|
||||
start_time = time()
|
||||
|
||||
self.ev3_brick.screen.load_image(ImageFile.MIDDLE_LEFT)
|
||||
|
||||
self.left_motor.run_time(
|
||||
speed=-1000,
|
||||
time=500,
|
||||
then=Stop.HOLD,
|
||||
wait=True)
|
||||
|
||||
proximity = self.ir_sensor.distance()
|
||||
while abs(self.ir_sensor.distance() - proximity) <= 4:
|
||||
wait(10)
|
||||
|
||||
elif which_motor == 2:
|
||||
self.middle_motor.run_angle(
|
||||
speed=1000,
|
||||
rotation_angle=210,
|
||||
then=Stop.COAST,
|
||||
wait=True)
|
||||
|
||||
start_time = time()
|
||||
|
||||
self.ev3_brick.screen.load_image(ImageFile.NEUTRAL)
|
||||
|
||||
self.middle_motor.run_time(
|
||||
speed=-1000,
|
||||
time=500,
|
||||
then=Stop.COAST,
|
||||
wait=True)
|
||||
|
||||
proximity = self.ir_sensor.distance()
|
||||
while abs(self.ir_sensor.distance() - proximity) <= 5:
|
||||
wait(10)
|
||||
|
||||
else:
|
||||
self.right_motor.run_angle(
|
||||
speed=1000,
|
||||
rotation_angle=90,
|
||||
then=Stop.COAST,
|
||||
wait=True)
|
||||
|
||||
start_time = time()
|
||||
|
||||
self.ev3_brick.screen.load_image(ImageFile.MIDDLE_RIGHT)
|
||||
|
||||
self.right_motor.run_time(
|
||||
speed=-1000,
|
||||
time=500,
|
||||
then=Stop.HOLD,
|
||||
wait=True)
|
||||
|
||||
proximity = self.ir_sensor.distance()
|
||||
while abs(self.ir_sensor.distance() - proximity) <= 5:
|
||||
wait(10)
|
||||
|
||||
response_time = time() - start_time
|
||||
|
||||
self.ev3_brick.screen.load_image(ImageFile.DIZZY)
|
||||
|
||||
self.ev3_brick.screen.print(response_time)
|
||||
|
||||
self.ev3_brick.light.on(color=Color.RED)
|
||||
|
||||
self.ev3_brick.speaker.play_file(file=SoundFile.BOING)
|
||||
|
||||
total_response_time += response_time
|
||||
|
||||
average_response_time = total_response_time / self.N_WHACK_TIMES
|
||||
|
||||
self.ev3_brick.screen.clear()
|
||||
self.ev3_brick.screen.print(
|
||||
'Avg. Time: {:.1f}s'.format(average_response_time))
|
||||
|
||||
self.ev3_brick.speaker.play_file(
|
||||
file=SoundFile.FANTASTIC
|
||||
if average_response_time <= 1
|
||||
else SoundFile.GOOD_JOB)
|
||||
|
||||
self.ev3_brick.speaker.play_file(file=SoundFile.GAME_OVER)
|
||||
|
||||
self.ev3_brick.light.on(color=Color.RED)
|
||||
|
||||
sleep(4)
|
||||
Reference in New Issue
Block a user