0

我有树莓派 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”。我自己试过,但转子一直旋转到相同的角度。任何人都可以帮助我处理这部分代码吗?

第二个问题,如何在树莓派上的终端“sudo pythonservo.py 11 17”中每 10 分钟自动运行一次这个命令,并且当树莓派打开时?

谢谢你的帮助!

4

1 回答 1

1

脉冲是您在代码中控制伺服位置的方式:

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 语句。这是将温度转换为脉冲(伺服位置)的函数示例:

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

现在您需要一个循环,每十分钟检查一次温度并调整伺服位置。像这样的东西:

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__':

最后,让脚本在启动时运行是一个很好的话题: 在 Raspberry Pi 启动时启动 shell 脚本

于 2017-06-08T20:03:10.597 回答