2

カスタム オフロード ビークルのデータ取得システムの作成に取り組んでいます。Raspberry Pi とカスタム タコメーター (テスト済みで動作確認済み) を使用して RPM を測定します。次のコードで割り込みを使用して RPM 値を取得します。

def get_rpm():                                         
    GPIO.wait_for_edge(17, GPIO.FALLING)
    start = time.time()
    GPIO.wait_for_edge(17, GPIO.FALLING)
    end = time.time()
    duration = end - start
    rpm = (1/duration)*60
    return rpm

このコードは、エンジンが稼働中で火花が発生している場合にのみ機能します。火花がない場合、コードはそのエッジを待って待機し、処理を続行しません。を呼び出すときget_rpm()に、コードがエッジを待機している場合、これにより他のプロセスがハングアップします。

これに対する私の意図した回避策は、別のプロセスでエンジンの状態を取得することです。2部構成でうまくいくと思います。

パート 1、別のスレッドで実行 (ループ):

GPIO.wait_for_edge(17, GPIO.RISING)
last = time.time

パート 2、必要に応じて関数として呼び出される実行:

def get_state():
    while time.time - last < .5:
        engine_state = true
    else:
        engine_state = false
    return engine_state

パート 1 をメモリに保存lastし、パート 2 からアクセスできるようにすると、パート 2 は、スパーク プラグが最後に火花を散らした時間に基づいて、車が動いているかどうかを判断します。コンパレータとして使用engine_stateすると、データ取得システムはが true のget_rpm()場合にのみRPM 値を取得して保存します。engine_state

lastパート 2 で変数を使用できるようにパート 1 を実装するにはどうすればよいですか? last非常に急速に変化します。last更新のたびにRaspberry PiのSDカードにテキストファイルとして保存したくありません。lastRAMに保存したい。

本当にありがとう!

4

1 に答える 1

1

これはインスピレーションのためだけです。私はPisを手元に持っていないので、これは盲目的に記憶から書かれています.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
# the line below is important. It'll fire on both rising and falling edges
# and it's non-blocking
GPIO.add_event_detect(17, GPIO.BOTH, callback=callback_func)

length_of_last_high = 0
length_of_last_low = 0

last_rise = 0
last_fall = 0
last_callback = 0

def callback_func(pin):
    # all of these global variables aren't needed 
    # but I left them in for demo purposes
    global last_rise
    global last_fall
    global last_callback
    global length_of_last_high
    global length_of_last_low
    last_callback = time.time()
    if GPIO.input(17):
        print "Pin 17 is rising!"
        length_of_last_high = last_callback - last_rise
        last_rise = last_callback
    else:
        print "Pin 17 is falling!"
        length_of_last_low = last_callback - last_fall 
        last_fall = last_callback


# last passed as parameter (the preferred solution if it works).
# You test it - I can't at the moment
def get_state(last=last_rise):
    engine_state = False
    if time.time() - last < .5:
        engine_state = True
    return engine_state

# last as global variable. 
# This works too but global is bad practice if it can be avoided     
# (namespace littering)
def get_state2():
    global last_rise
    engine_state = False
    if time.time() - last_rise < .5:
        engine_state = True
    return engine_state


def main():
    while True:
        print "Not blocking! This loop could sleep forever. It wouldn't affect the readings"
        time.sleep(1)
        print get_state()

"""
GPIO.cleanup()
"""
于 2016-04-06T00:03:56.060 に答える