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
解决方案
我只记得增加时间会做出更准确的预测。这段代码是我的工作:)
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
推荐阅读
- sql - 我们可以在选择查询中使用什么而不是游标
- reactjs - Antd 无法将数据输入动态输入字段
- python - 基于动态事件的 Python 算法构建 n 个 OHLC 柱
- c - 是否有任何有效的方法(库或函数或算法)以压缩稀疏行格式删除或附加特定的行和列组件?
- jsf - f:validateRegex 在将 jsf2.1 项目部署到 weblogic12c 时始终检查
- jenkins-pipeline - 如果上传失败,Nexus Artifact Upload Plugin 不会失败管道
- scala - 无法从 spark scala 中的列名中删除空格
- reactjs - 如何在reactJs中的网格内分屏
- postgresql - 从该网络内的容器检查 docker 网络中打开的端口
- node.js - 具有多个查找和管道的聚合仅返回最后一个元素