用 Python 撰寫程式控制 EV3
EV3 主機的狀態燈
Code
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.tools import wait
from pybricks.parameters import Color
# Initialize the EV3
ev3 = EV3Brick()
# Turn on a red light
ev3.light.on(Color.RED)
# Wait
wait(1000)
# Turn the light off
ev3.light.off()
以下是其他 EV3 Python 範例程式
首先是聲音 EV3 Speaker
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.tools import wait
from pybricks.media.ev3dev import SoundFile
# Initialize the EV3
ev3 = EV3Brick()
# BEEP ########################################################################
# Simple beep
ev3.speaker.beep()
wait(1000)
# Interesting beeps
for f in range(100, 500, 100):
ev3.speaker.beep(f)
wait(1000)
# PLAY NOTES ##################################################################
# Twinkle, Twinkle Little Star
A = [
"C4/4",
"C4/4",
"G4/4",
"G4/4",
"A4/4",
"A4/4",
"G4/2",
"F4/4",
"F4/4",
"E4/4",
"E4/4",
"D4/4",
"D4/4",
"C4/2",
]
B = ["G4/4", "G4/4", "F4/4", "F4/4", "E4/4", "E4/4", "D4/2"] * 2
TWINKLE = A + B + A
ev3.speaker.play_notes(TWINKLE)
wait(1000)
# PLAY FILE #############################################
ev3.speaker.play_file(SoundFile.HELLO)
wait(1000)
# TEXT TO SPEECH #######################################
# Say something in English
ev3.speaker.say("I am am E V 3. Pleased to meet you.")
# Say something in Danish + female
ev3.speaker.set_speech_options(voice="da+f5")
ev3.speaker.say("Leg godt!")
wait(1000)
接著是文字顯示,中文字
Code
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.tools import wait
from pybricks.media.ev3dev import Font
# It takes some time for fonts to load from file, so it is best to only
# load them once at the beginning of the program like this:
tiny_font = Font(size=6)
big_font = Font(size=24, bold=True)
chinese_font = Font(size=24, lang="zh-cn")
# Initialize the EV3
ev3 = EV3Brick()
# Say hello
ev3.screen.print("Hello!")
# Say tiny hello
ev3.screen.set_font(tiny_font)
ev3.screen.print("hello")
# Say big hello
ev3.screen.set_font(big_font)
ev3.screen.print("HELLO")
# Say Chinese hello
ev3.screen.set_font(chinese_font)
ev3.screen.print("你好")
# Wait some time to look at the screen
wait(5000)
還有圖片顯示
Code
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.tools import wait
from pybricks.media.ev3dev import Image, ImageFile
# It takes some time to load images from the SD card, so it is best to load
# them once at the beginning of a program like this:
ev3_img = Image(ImageFile.EV3_ICON)
# Initialize the EV3
ev3 = EV3Brick()
# Show an image
ev3.screen.load_image(ev3_img)
# Wait some time to look at the image
wait(5000)
Draw畫圖功能
Code
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.tools import wait
# Initialize the EV3
ev3 = EV3Brick()
# Draw a rectangle
ev3.screen.draw_box(10, 10, 40, 40)
# Draw a solid rectangle
ev3.screen.draw_box(20, 20, 30, 30, fill=True)
# Draw a rectangle with rounded corners
ev3.screen.draw_box(50, 10, 80, 40, 5)
# Draw a circle
ev3.screen.draw_circle(25, 75, 20)
# Draw a triangle using lines
x1, y1 = 65, 55
x2, y2 = 50, 95
x3, y3 = 80, 95
ev3.screen.draw_line(x1, y1, x2, y2)
ev3.screen.draw_line(x2, y2, x3, y3)
ev3.screen.draw_line(x3, y3, x1, y1)
# Wait some time to look at the shapes
wait(5000)
其他
延伸閱讀:
https://pybricks.com/ev3-micropython/hubs.html
https://docs.pybricks.com/en/v3.0/hubs/ev3brick.html
沒有留言:
張貼留言
探奇歡迎大家留言討論!謝謝分享你的意見。