我正在尝试为我的 EV3 积木编写一些代码。我真的很难让多线程工作。我正在使用线程模块,它不会引发任何错误,但应该在另一个线程上运行的函数根本不会运行。我的代码:
#!/usr/bin/env pybricks
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
import threading
# Create your objects here.
ev3 = EV3Brick()
def color_thread():
print("color_thread")
ev3.light.on(Color.ORANGE)
wait(100)
ev3.light.on(Color.GREEN)
wait(100)
def speaker_thread():
print("speaker_thread")
ev3.speaker.beep(200,100)
wait(100)
ev3.speaker.beep(500,100)
wait(100)
t1 = threading.Thread(target=color_thread)
t2 = threading.Thread(target=speaker_thread)
t1.start()
t2.start()
所以预期的输出是:
color_thread
speaker_thread
和一些可见和可听的指示(改变灯光和发出声音),但输出是:
Starting: brickrun --directory="/home/robot/olgojChorchoj_Stropochod" "/home/robot/olgojChorchoj_Stropochod"
----------
----------
Completed successfully.
并且没有其他迹象表明这两个功能正在运行。
你能告诉我我做错了什么吗?谢谢