我有树莓派3并试图创建智能温室模型。如果温度过高,该模型应打开窗口。
我是用Python编写的新代码,发现了几个例子:1。用于温度传感器和2.用于伺服电机旋转。
有人可以帮助我使用伺服电机吗?如果温度为20°C,我想将伺服移动到30°,如果是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)
关于伺服电机的部分例子我在互联网上找到了。我需要将“while”替换为“if”。我自己试过,但转子一直旋转到相同的角度。 任何人都可以帮我解决这部分代码吗?
第二个问题,我怎样才能在每10分钟自动覆盖莓pi上的终端“sudo python servo.py 11 17”中运行这个命令?当树莓派开启时?
感谢您的帮助!
答案 0 :(得分:0)
脉冲是您在代码中控制伺服位置的方式:
wiringpi.pwmWrite(18,pulse)
查看此示例: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-8-using-a-servo-motor?view=all
根据您的伺服,脉冲值100一直向左移动伺服(本例中为关闭),200向右移动(本例中为打开)。您需要通过阅读数据表或实验来找到这些值。一旦你有了这些,这里是你如何设置伺服的位置:
min_servo_val = 100
max_servo_val = 200
wiringpi.pwmWrite(18, min_servo_val) # Move all the way left
time.sleep(1) # Wait a second
wiringpi.pwmWrite(18, max_servo_val) # Move all the way right
现在您可以编写一个将温度转换为min_servo_val和max_servo_val之间的伺服位置的函数,或者使用简单的if语句。以下是将temp转换为脉冲(伺服位置)的函数示例:
def get_servo_position(temp):
min_servo_val = 100 # Pulse value at which window is all the way closed closed
max_servo_val = 200 # Pulse value at which window is all the way open
full_closed_temp = 20.0 # Temperature at which window is completely closed
full_open_temp = 26.0 # Temperature at which window is completely open
if temp <= full_closed_temp:
return min_servo_val
elif temp >= full_open_temp:
return max_servo_val
else:
return ((temp - full_closed_temp) / (full_open_temp - full_closed_temp)) * (max_servo_val - min_servo_val) + min_servo_val
现在你可以做到:
print get_servo_position(19) # 100 all the way closed
print get_servo_position(22) # 133.3, or 33% of the way between min_servo_val and max_servo_val
print get_servo_position(25) # 183.3, or 83% of the way between min_servo_val and max_servo_val
print get_servo_position(27) # 200 all the way open
现在你需要一个每10分钟检查一次温度的循环并调整伺服位置。像这样:
while True:
humidity, temp = Adafruit_DHT.read_retry(sensor, pin) # Get temperature.
position = get_servo_position(temp) # Get the servo position
wiringpi.pwmWrite(18,position) # Move to the position
time.sleep(10*60) # Wait 10 minutes
总而言之,你的脚本应该是这样的:
import sys
import Adafruit_DHT
import time
import wiringpi
dht_pin = 17 # GPIO conencted to DHT
servo_pin = 18 # GPIO connected to servo
dht_sensor = Adafruit_DHT.DHT11 # Put your sensor here, or set it from command line args
min_servo_val = 100 # Pulse value at which window is all the way closed closed
max_servo_val = 200 # Pulse value at which window is all the way open
full_closed_temp = 20.0 # Temperature at which window is completely closed
full_open_temp = 26.0 # Temperature at which window is completely open
def get_servo_position(temp):
if temp <= full_closed_temp:
return min_servo_val
elif temp >= full_open_temp:
return max_servo_val
else:
return ((temp - full_closed_temp) / (full_open_temp - full_closed_temp)) * (max_servo_val - min_servo_val) + min_servo_val
def main_loop():
while True:
humidity, temp = Adafruit_DHT.read_retry(dht_sensor, dht_pin) # Get temperature.
position = get_servo_position(temp) # Get the servo position
wiringpi.pwmWrite(18,position) # Move to the position
time.sleep(10*60) # Wait 10 minutes
if __name__ == '__main__':
# If you need to get command line arguments, do it below, otherwise just set the pins and other settings at the top of this script.
# For example...
# dht_pin = sys.argv[1]
# servo_pin = sys.argv[2]
# Set up servo
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(18,wiringpi.GPIO.PWM_OUTPUT)
wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS)
wiringpi.pwmSetClock(192)
wiringpi.pwmSetRange(2000)
# Enter main loop
main_loop()
请注意,我省略了命令行arg解析。如果您需要,可以在if __name__ == '__main__':
最后,在启动时运行脚本是一个很好的主题: Start shell script on Raspberry Pi startup