我有树莓皮3,并试图创建智能绿色房子模型。如果温度太高,这个型号的应该打开窗户。在
我是新用Python编写代码的,发现了几个例子:1。温度传感器和2。用于伺服电机旋转。在
谁能帮我弄一下伺服马达吗?我想移动伺服,例如30°C,如果温度是20°C,如果是21°C,移动40°伺服,依此类推。在
我有Python代码:
import sys
import Adafruit_DHT
import time
import wiringpi
sensor_args = { '11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 }
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
sensor = sensor_args[sys.argv[1]]
pin = sys.argv[2]
else:
print('usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#')
print('example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302
connected to GPIO #4')
sys.exit(1)
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity))
else:
print('Failed to get reading. Try again!')
sys.exit(1)
temp=temperature
text_file = open("output.txt", "w")
text_file.write("%s" %(temp))
text_file.close()
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(18,wiringpi.GPIO.PWM_OUTPUT)
wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS)
wiringpi.pwmSetClock(192)
wiringpi.pwmSetRange(2000)
delay_period = 0.01
while temp==20.0:
for pulse in range(50,250,1):
wiringpi.pwmWrite(18,50)
time.sleep(delay_period)
for pulse in range(250,50,-1):
wiringpi.pwmWrite(18,pulse)
time.sleep(delay_period)
我在网上找到的关于伺服电机ir的例子。我要把while改成if。我自己试过了,但转子一直旋转到同一个角度。 有人能帮我完成这一小部分代码吗?在
第二个问题,如何在终端“sudopython”中运行这个命令伺服.py11 17“在树莓皮自动每10分钟和当树莓皮打开?在
谢谢你的帮助!在
脉冲就是你在代码中控制伺服位置的方式:
请参见以下示例: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-8-using-a-servo-motor?view=all
根据您的伺服,脉冲值100会将伺服一直向左移动(在本例中为关闭),而200则会一直向右移动(在本例中为打开)。您需要通过阅读数据表或通过实验找到这些值。一旦你有了这些,下面是如何设置伺服的位置:
^{pr2}$现在您可以编写一个函数,将温度转换为最小伺服阀和最大伺服阀之间的伺服位置,也可以使用一个简单的if语句。以下是将温度转换为脉冲(伺服位置)的函数示例:
现在您可以:
现在你需要一个循环,每十分钟检查一次温度并调整伺服位置。像这样:
综合起来,你的脚本应该是这样的:
注意,我省略了命令行arg解析。如果您需要它们,可以在
if __name__ == '__main__':
之后添加它们最后,让脚本在启动时运行是一个很好的主题: Start shell script on Raspberry Pi startup
相关问题 更多 >
编程相关推荐