2017-06-08 11 views
0

私はラズベリーパイ3を持っていて、スマートグリーンハウスモデルを作成しようとしています。このモデルは、温度が高すぎるとウィンドウを開くはずです。ラズベリーパイ3 +温度センサー+サーボモーター

私はPythonで新しいコードを書いていますが、いくつかの例があります:1.温度センサー用、2.サーボ・モーター用。

サーボモータで助けてもらえますか?私はサーボが20°Cの場合は30°に、21°Cの場合は40°サーボのように移動したいと考えています。サーボモータのIR例について

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) 

パートIは、インターネット上で見つかった:

私はPythonコードを持っています。 "while"を "if"に置き換える必要があります。私は自分で試しましたが、ローターは常に同じ角度に回転します。 誰もこの小さなコードを手伝ってくれますか?

2番目の質問ですが、10分ごとに自動的にラズベリーパイの端末「sudo python servo.py 11 17」でこのコマンドを実行するとラズベリーパイがオンになっていますか?

ありがとうございました!あなたのサーボ、100移動サーボのパルス値に応じて、 https://learn.adafruit.com/adafruits-raspberry-pi-lesson-8-using-a-servo-motor?view=all

wiringpi.pwmWrite(18,pulse) 

は、この例を参照してください:

+0

あなたは、コードの何かを試してみてください、その後、あなたが成し遂げ投稿してください。 ifのwhileループを変更するように頼んだら、プログラミングについては何も知りません。 –

答えて

0

パルスは、あなたのコード内のサーボの位置を制御する方法であります左に(この例では閉じて)、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文の単純なを使用することができます。

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分ごとにチェックするループを必要と:今、あなたが行うことができます

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 

:ここでは、パルス(サーボ位置)への一時を翻訳する機能の一例ですサーボ位置を調整します。このような何か:私は、コマンドライン引数の解析を残し

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() 

注:

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 

はすべて一緒にそれを置く、あなたのスクリプトは次のようになります。右if __name__ == '__main__':

後最後に、起動時に実行するスクリプトを作成することもカバーされ話題となって、あなたがそれらを必要とする場合は、それらを追加することができます Start shell script on Raspberry Pi startup

関連する問題