用 Python 撰寫程式控制 EV3
EV3 主機的狀態燈
Code
#!/usr/bin/env pybricks-micropythonfrom pybricks.hubs import EV3Brickfrom pybricks.tools import waitfrom pybricks.parameters import Color# Initialize the EV3ev3 = EV3Brick()# Turn on a red lightev3.light.on(Color.RED)# Waitwait(1000)# Turn the light offev3.light.off()
以下是其他 EV3 Python 範例程式
首先是聲音 EV3 Speaker
#!/usr/bin/env pybricks-micropythonfrom pybricks.hubs import EV3Brickfrom pybricks.tools import waitfrom pybricks.media.ev3dev import SoundFile# Initialize the EV3ev3 = EV3Brick()# BEEP ######################################################################### Simple beepev3.speaker.beep()wait(1000)# Interesting beepsfor f in range(100, 500, 100):ev3.speaker.beep(f)wait(1000)# PLAY NOTES ################################################################### Twinkle, Twinkle Little StarA = ["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"] * 2TWINKLE = A + B + Aev3.speaker.play_notes(TWINKLE)wait(1000)# PLAY FILE #############################################ev3.speaker.play_file(SoundFile.HELLO)wait(1000)# TEXT TO SPEECH ######################################## Say something in Englishev3.speaker.say("I am am E V 3. Pleased to meet you.")# Say something in Danish + femaleev3.speaker.set_speech_options(voice="da+f5")ev3.speaker.say("Leg godt!")wait(1000)
接著是文字顯示,中文字
Code
#!/usr/bin/env pybricks-micropythonfrom pybricks.hubs import EV3Brickfrom pybricks.tools import waitfrom 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 EV3ev3 = EV3Brick()# Say helloev3.screen.print("Hello!")# Say tiny helloev3.screen.set_font(tiny_font)ev3.screen.print("hello")# Say big helloev3.screen.set_font(big_font)ev3.screen.print("HELLO")# Say Chinese helloev3.screen.set_font(chinese_font)ev3.screen.print("你好")# Wait some time to look at the screenwait(5000)
還有圖片顯示
Code
#!/usr/bin/env pybricks-micropythonfrom pybricks.hubs import EV3Brickfrom pybricks.tools import waitfrom 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 EV3ev3 = EV3Brick()# Show an imageev3.screen.load_image(ev3_img)# Wait some time to look at the imagewait(5000)
Draw畫圖功能
Code
#!/usr/bin/env pybricks-micropythonfrom pybricks.hubs import EV3Brickfrom pybricks.tools import wait# Initialize the EV3ev3 = EV3Brick()# Draw a rectangleev3.screen.draw_box(10, 10, 40, 40)# Draw a solid rectangleev3.screen.draw_box(20, 20, 30, 30, fill=True)# Draw a rectangle with rounded cornersev3.screen.draw_box(50, 10, 80, 40, 5)# Draw a circleev3.screen.draw_circle(25, 75, 20)# Draw a triangle using linesx1, y1 = 65, 55x2, y2 = 50, 95x3, y3 = 80, 95ev3.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 shapeswait(5000)
其他
延伸閱讀:
https://pybricks.com/ev3-micropython/hubs.html
https://docs.pybricks.com/en/v3.0/hubs/ev3brick.html
沒有留言:
張貼留言
探奇歡迎大家留言討論!謝謝分享你的意見。