对于一个学校的项目,我需要做一辆车,用一个传感器连接到一个树莓派零瓦上的GPIO来检查颜色零售物价指数图书馆。同时我还想用gpiozero库用四个gpio引脚来驱动这辆车。运动的控制通过python套接字服务器发送到pi-zero。我把所有这些过程放在不同的线程中,但现在看来我不能开车了。好像Gpiozero被禁用了零售物价指数只起作用。在
颜色传感器代码
import RPi.GPIO as GPIO
import time
S2 = 26 #select color / S2
S3 = 21 #select color / S3
wave = 20 #sine wave proportional to light frequency / OUT
pulses = 1000 #pulses to check, more pulses increase accuracy but decreases speed
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(wave, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(S2, GPIO.OUT)
GPIO.setup(S3, GPIO.OUT)
def getFrequency(color):
if (color == 'red'):
GPIO.output(S2, GPIO.LOW)
GPIO.output(S3, GPIO.LOW)
if (color == 'green'):
GPIO.output(S2, GPIO.HIGH)
GPIO.output(S3, GPIO.HIGH)
if (color == 'blue'):
GPIO.output(S2, GPIO.LOW)
GPIO.output(S3, GPIO.HIGH)
if (color == 'clear'):
GPIO.output(S2, GPIO.HIGH)
GPIO.output(S3, GPIO.LOW)
startTime = time.time()
for pulseCount in range(pulses):
GPIO.wait_for_edge(wave, GPIO.FALLING)
duration = time.time() - startTime
frequency = wave / duration
return frequency
def endProgram():
GPIO.cleanup()
setup()
移动代码
^{pr2}$
在另一个文件中我有这些函数
def listen():
print("Listening to commands")
while True:
msg = socket.receive()
print("received: " + msg)
try:
if(msg == something):
movement.forward() #ect
except:
print(msg)
listening = Thread(target=listen)
listening.start()
我让颜色传感器的代码在主线程中运行
我希望一切都能正常工作,汽车向前行驶,颜色传感器读取正确的值
目前没有回答
相关问题 更多 >
编程相关推荐