SPIKE 樂高機器人

我的網頁

2022-06-25

EV3 Python 程式範例

用 Python 撰寫程式控制 EV3 

EV3 主機的狀態燈

Code

  1. #!/usr/bin/env pybricks-micropython
  2. from pybricks.hubs import EV3Brick
  3. from pybricks.tools import wait
  4. from pybricks.parameters import Color
  5. # Initialize the EV3
  6. ev3 = EV3Brick()
  7. # Turn on a red light
  8. ev3.light.on(Color.RED)
  9. # Wait
  10. wait(1000)
  11. # Turn the light off
  12. ev3.light.off()


以下是其他 EV3 Python 範例程式

首先是聲音 EV3 Speaker

  1. #!/usr/bin/env pybricks-micropython
  2. from pybricks.hubs import EV3Brick
  3. from pybricks.tools import wait
  4. from pybricks.media.ev3dev import SoundFile
  5. # Initialize the EV3
  6. ev3 = EV3Brick()
  7. # BEEP ########################################################################
  8. # Simple beep
  9. ev3.speaker.beep()
  10. wait(1000)
  11. # Interesting beeps
  12. for f in range(100, 500, 100):
  13. ev3.speaker.beep(f)
  14. wait(1000)
  15. # PLAY NOTES ##################################################################
  16. # Twinkle, Twinkle Little Star
  17. A = [
  18. "C4/4",
  19. "C4/4",
  20. "G4/4",
  21. "G4/4",
  22. "A4/4",
  23. "A4/4",
  24. "G4/2",
  25. "F4/4",
  26. "F4/4",
  27. "E4/4",
  28. "E4/4",
  29. "D4/4",
  30. "D4/4",
  31. "C4/2",
  32. ]
  33. B = ["G4/4", "G4/4", "F4/4", "F4/4", "E4/4", "E4/4", "D4/2"] * 2
  34. TWINKLE = A + B + A
  35. ev3.speaker.play_notes(TWINKLE)
  36. wait(1000)
  37. # PLAY FILE #############################################
  38. ev3.speaker.play_file(SoundFile.HELLO)
  39. wait(1000)
  40. # TEXT TO SPEECH #######################################
  41. # Say something in English
  42. ev3.speaker.say("I am am E V 3. Pleased to meet you.")
  43. # Say something in Danish + female
  44. ev3.speaker.set_speech_options(voice="da+f5")
  45. ev3.speaker.say("Leg godt!")

  46. wait(1000)


接著是文字顯示,中文字

Code

  1. #!/usr/bin/env pybricks-micropython
  2. from pybricks.hubs import EV3Brick
  3. from pybricks.tools import wait
  4. from pybricks.media.ev3dev import Font
  5. # It takes some time for fonts to load from file, so it is best to only
  6. # load them once at the beginning of the program like this:
  7. tiny_font = Font(size=6)
  8. big_font = Font(size=24, bold=True)
  9. chinese_font = Font(size=24, lang="zh-cn")
  10. # Initialize the EV3
  11. ev3 = EV3Brick()
  12. # Say hello
  13. ev3.screen.print("Hello!")
  14. # Say tiny hello
  15. ev3.screen.set_font(tiny_font)
  16. ev3.screen.print("hello")
  17. # Say big hello
  18. ev3.screen.set_font(big_font)
  19. ev3.screen.print("HELLO")
  20. # Say Chinese hello
  21. ev3.screen.set_font(chinese_font)
  22. ev3.screen.print("你好")
  23. # Wait some time to look at the screen
  24. wait(5000)

還有圖片顯示

Code

  1. #!/usr/bin/env pybricks-micropython
  2. from pybricks.hubs import EV3Brick
  3. from pybricks.tools import wait
  4. from pybricks.media.ev3dev import Image, ImageFile
  5. # It takes some time to load images from the SD card, so it is best to load
  6. # them once at the beginning of a program like this:
  7. ev3_img = Image(ImageFile.EV3_ICON)
  8. # Initialize the EV3
  9. ev3 = EV3Brick()
  10. # Show an image
  11. ev3.screen.load_image(ev3_img)
  12. # Wait some time to look at the image
  13. wait(5000)

Draw畫圖功能

Code

  1. #!/usr/bin/env pybricks-micropython
  2. from pybricks.hubs import EV3Brick
  3. from pybricks.tools import wait
  4. # Initialize the EV3
  5. ev3 = EV3Brick()
  6. # Draw a rectangle
  7. ev3.screen.draw_box(10, 10, 40, 40)
  8. # Draw a solid rectangle
  9. ev3.screen.draw_box(20, 20, 30, 30, fill=True)
  10. # Draw a rectangle with rounded corners
  11. ev3.screen.draw_box(50, 10, 80, 40, 5)
  12. # Draw a circle
  13. ev3.screen.draw_circle(25, 75, 20)
  14. # Draw a triangle using lines
  15. x1, y1 = 65, 55
  16. x2, y2 = 50, 95
  17. x3, y3 = 80, 95
  18. ev3.screen.draw_line(x1, y1, x2, y2)
  19. ev3.screen.draw_line(x2, y2, x3, y3)
  20. ev3.screen.draw_line(x3, y3, x1, y1)
  21. # Wait some time to look at the shapes
  22. wait(5000)

其他



延伸閱讀:

https://pybricks.com/ev3-micropython/hubs.html

https://docs.pybricks.com/en/v3.0/hubs/ev3brick.html


沒有留言:

張貼留言

探奇歡迎大家留言討論!謝謝分享你的意見。