树莓皮3+温度传感器+伺服m

2024-10-16 20:44:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有树莓皮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分钟和当树莓皮打开?在

谢谢你的帮助!在


Tags: 代码inimportadafruitiftimesysargs
1条回答
网友
1楼 · 发布于 2024-10-16 20:44:38

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

wiringpi.pwmWrite(18,pulse)

请参见以下示例: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-8-using-a-servo-motor?view=all

根据您的伺服,脉冲值100会将伺服一直向左移动(在本例中为关闭),而200则会一直向右移动(在本例中为打开)。您需要通过阅读数据表或通过实验找到这些值。一旦你有了这些,下面是如何设置伺服的位置:

^{pr2}$

现在您可以编写一个函数,将温度转换为最小伺服阀和最大伺服阀之间的伺服位置,也可以使用一个简单的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__':之后添加它们

最后,让脚本在启动时运行是一个很好的主题: Start shell script on Raspberry Pi startup

相关问题 更多 >