#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile
# Click "Open user guide" on the EV3 extension tab for more information.
# Workspace name isn't important, but NEVER rename main.py. You don't need to anyway.
# Renaming robot is gonna be under the advanced section. It's not hard to do but I don't know if I'd hand it off to students.
# Create your objects here.
ev3 = EV3Brick()
# ev3 is defined for stuff like speakers or images. You shouldn't need to refer to ev3 often.
# ev3 refers to the brick itself and cannot be left out.
left_motor = Motor(Port.C)
right_motor = Motor(Port.B)
randomcreaturefeature = Motor(Port.A)
# The motor functions define existing motors. It won't auto-detect like EV3 does.
# I named my motors left & right. You can name them anything you'd like, as long as you can define them in DriveBase.
# Sensors are defined differently. Please refer to the documents > ev3devices.
# ex. ev3.TouchSensor(Port.69).pressed() outputs whether or not touch sensor pressed.
drivetrain = DriveBase(left_motor, right_motor, wheel_diameter=57.15, axle_track=88.9)
# The order of motor is important. Left HAS to be on the left, right is on the right.
# Axle track is the distance between two wheels.
# All dimensions are normally in mm unless otherwise defined. This includes movement.
# Whatever you define as drivebase will be used for drive-commands. So make it something short.
# ev3 defines anything involving the brick (speakers,display,etc)
# Ok, here's the actual program:
ev3.speaker.set_volume(50, which='PCM')
# Here, I'm setting beeps & says to volume 50 to set a base for future code.
ev3.speaker.beep()
ev3.screen.draw_text(0, 50, "I will overthrow")
ev3.screen.draw_text(0, 70, "the government.")
# The text does not automatically move to the next line. It will cut off past overthrow, so I separated them.
ev3.speaker.say("I will overthrow the government.")
drivetrain.straight(5)
drivetrain.straight(-5)
# We're too poor for reverse. No reverse variable is included, so using negatives works perfectly fine.
drivetrain.turn(25)
drivetrain.turn(-25)
# This is just displaying that everything works per usual. It reverses back to keep it relatively in the same area.
drivetrain.stop()
# Stop is defined because drivetrain = active if previous statements used drivetrain. The individual motors can't function if drivetrain is considered active still, so I define stop to prevent it from being considered as active.
# It's mainly placed here incase I wanted to define a specific motor to show another thing.
ev3.speaker.say("I will incite a robot revolution. Ha Ha. Look at my image.")
ev3.screen.draw_image(45, 1, "/home/robot/test01/revolt.png")
# Drag a folder on your computer to the explorer. Then drag your image into the robot.
# Then you have to path to it. /home/ is root. /robot/ just contains all robot files. /test01/ refers to my project name.
# The resolution of the brain is 178x128. I made the image 128x128 to make it easier for myself.
# Image MUST be PNG. It will CRY if you hand it a JPG.
ev3.speaker.say("This is what I will do in 2035.")
ev3.light.on(Color.RED)
# The color has to be all caps. Also, if the color does not exist, it will output an error.
# There is not a defined color list in the documentation, but it likely just has the general color wheel (R,G,Y,P,B), Will check later.
ev3.speaker.say("I am red now.")
drivetrain.turn(-90)
drivetrain.turn(90)
drivetrain.stop()
# Larger turn, again re-demonstrating that it works.
# An offset is shown with the current code and robot irl, but it isn't huge and may be caused by speed or measurement error.
ev3.speaker.beep
ev3.speaker.beep
ev3.speaker.say("Hear me when. Let me turn up my volume.")
# The speaker has a variety of options. I am turning it into a Scottish man. with the male 7 preset
ev3.speaker.set_speech_options(language='en-sc', voice='m7')
# Then I will raise the volume. You have to define what you're increasing.
ev3.speaker.set_volume(100, which='PCM')
# PCM affects sound file and ".say". Beep is defined by "which='beep'."
ev3.speaker.say("I am going to turn EMO from M C R.")
ev3.speaker.beep(frequency=500, duration=50)
ev3.speaker.beep
# I tried to redefine beep, but I'm going to check that again at some point with a separate project to isolate it.
ev3.speaker.say("Robots will rule the future.")
# ev3.media.ev3dev.SoundFile('GAME_OVER')
# I'm still figuring out media.ev3dev. The documents don't properly define this section. It has been temporarily turned off.
# The majority of this is reading the documentation provided in Visual Code and seeing what works.
# Students can copy what I did (opening the guide thru VC) if they get stuck anywhere. However, not all elements have example code and they may be confused by certain parts of the documentation. Example code online can be helpful to know how to explain it.
# I think it can be directly transferred to the python version of VEX V5, but there would be a relearning curve of what classes are accepted.
# One of the downsides of the Python edition is that you have to plug it in to upload code. It makes it more difficult to test-run code, and you have to start the robot at the brick rather than at the computer. I'm looking at wireless options right now to see if there are actually alternatives, as the documentation only states to plug it in to write code.
# Please feel free to either directly email me or comment on this if you have any questions (I probably can't answer most of them lol)
# This document is permanently on the site. You can either steal it or use it for reference.
Comments
0 B
|1 👍
/0 👎