如何检测布尔变量的摆动状态

时间:2019-01-15 05:40:23

标签: python

我有从反射镜传感器读取的随机布尔变量。 我尝试找到一种方法来检查条件(在mode_wait.txt中)是否存在如下逻辑波动。

RR=swing -> Test (Not as condition) I want to 1
R1=swing -> Test (Not as condition) I want to 1
1R=swing -> Test (Not as condition) I want to 1
11=1 -> Test OK
10=0 -> Test OK
01=0 -> Test OK
00=0 -> Test OK
R0=0 -> Test OK
0R=0 -> Test OK

mode_wait.txt

RR

detect_swing_logic_(virtual_logic).py

from _thread import start_new_thread
import time, copy, random

sensor_top = 0
sensor_low = 0

def random_logic_top():
    global sensor_top
    while True:
        time.sleep(1/random.randint(8,8))
        sensor_top = random.randint(0,1)

def random_logic_low():
    global sensor_low
    while True:
        time.sleep(1/random.randint(5,15))
        sensor_low = random.randint(0,1)

def test_logic():
    global RTD
    global sensor_top
    global sensor_low

    f = open("mode_wait.txt", "r")
    mode_wait = list(f.read().strip())

    if mode_wait[0] == "R":
        sensor_top = sensor_top
    if mode_wait[0] == "1":
        sensor_top = 1
    if mode_wait[0] == "0":
        sensor_top = 0

    if mode_wait[1] == "R":
        sensor_low = sensor_low
    if mode_wait[1] == "1":
        sensor_low = 1
    if mode_wait[1] == "0":
        sensor_low = 0

    if sensor_top == 0 or sensor_low == 0:
        group = 0
    else:
        group = 1

    return group


start_new_thread(random_logic_top,())
start_new_thread(random_logic_low,())

while True:
    #time.sleep(0.025)
    sample = []
    for x in range(3):
        sample.append(test_logic())
        time.sleep(0.001)
    sums = sum(sample)

    if sums < 0 and sums < 3:
        print "swing"
    else:
        # True read logic
        if sums == 3:
            print 1
        else:
            print 0

1 个答案:

答案 0 :(得分:0)

我刚刚记得,仅仅增加时间就能做出更准确的预测。这段代码是我的工作:)

from _thread import start_new_thread
import time, copy, random

sensor_top = 0
sensor_low = 0

def random_logic_top():
    global sensor_top
    while True:
        time.sleep(1/random.randint(8,10))
        sensor_top = random.randint(0,1)

def random_logic_low():
    global sensor_low
    while True:
        time.sleep(1/random.randint(5,15))
        sensor_low = random.randint(0,1)

def test_logic():
    global RTD
    global sensor_top
    global sensor_low

    f = open("mode_wait.txt", "r")
    mode_wait = list(f.read().strip())

    if mode_wait[0] == "R":
        sensor_top = sensor_top
    if mode_wait[0] == "1":
        sensor_top = 1
    if mode_wait[0] == "0":
        sensor_top = 0

    if mode_wait[1] == "R":
        sensor_low = sensor_low
    if mode_wait[1] == "1":
        sensor_low = 1
    if mode_wait[1] == "0":
        sensor_low = 0

    if sensor_top == 0 or sensor_low == 0:
        group = 0
    else:
        group = 1

    return group


start_new_thread(random_logic_top,())
start_new_thread(random_logic_low,())
#start_new_thread(test_logic,())

while True:
    #time.sleep(0.025)
    sample = []
    for x in range(100):
        sample.append(test_logic())
        time.sleep(0.0005)
    sums = sum(sample)
    if sums == 0:
        print 0
    elif sums == 200:
        print 1
    else:
        print 1