【问题标题】:Comparing the same variable against itself?将同一个变量与自身进行比较?
【发布时间】:2021-06-09 02:38:12
【问题描述】:

提前感谢您阅读并花时间与我一起解决此问题!

让我先从重要的东西开始:

目标设备:

  • 树莓派 4B
  • 电子沙龙接力帽

开发环境:

  • macOS 卡塔利娜
  • PyCharm
  • Python 2.7.10

在我的家里,我有一个为我家供水的泉水。我想出的解决办法是为了防止由于恶劣的阴雨天气导致地面土壤松动而导致脏水进入我的水箱,方法是关闭阀门并等待大约 12 小时让水重新清理干净。然后我打开阀门,清水流入我的蓄水池,为我的家提供水,这个解决方案非常有效。

我最近得出了一个结论,即我想用一个常开螺线管使这个过程自动化。我购买了 Raspberry Pi、Relay Hat 和 Ambient Weather 气象站。

我希望用 Python 2.7.10 做的是在分配的时间后检查同一个变量。在此示例中,我正在检查相对于自身的相对大气压力,并且我希望在该变量中寻找显着的负变化。

即“变量 A 有什么?好的,现在等待 3 秒。A 现在有什么?它改变了多少?”

这是我到目前为止所遇到的问题,我该如何改进?谢谢。

起初我在想也许我应该用数据绘制图表并比较两个绘图点之间的差异,但我不确定如何使用 Matplotlib。

# This is the executing script for the Smart Valve.
# This project is powered by raspberry pi and 120 angry pixies.
import time,imp
from ambient_api.ambientapi import AmbientAPI

# This code will pull the data from the weather computer

api = AmbientAPI()
devices = api.get_devices()
device = devices[0]
time.sleep(1) #pause for a second to avoid API limits


# The code below is meant for telling the python interpreter to behave normally whether or not it's in a RPi env or a
# developer env

try:
    imp.find_module('RPi.GPIO')
    import RPi.GPIO as GPIO
except ImportError:
    """
    import FakeRPi.GPIO as GPIO
    OR
    import FakeRPi.RPiO as RPiO
    """

    import FakeRPi.GPIO as GPIO

# this code compares the rate of change for the barometric pressure over time and checks if rate is negative
a1 = None
a2 = None

while True:
    weatherData = device.get_data()
    data = dict(weatherData[0])
    pressure = data[u'baromrelin']
    wind = data[u'windspeedmph']
    rain = data[u'hourlyrainin']
    a1 = pressure
    time.sleep(30)
    a2 = pressure
    print("A1 is equal to " + str(a1))
    print("A2 is equal to " + str(a2))
    if a1 > a2:
        print("we should close the valve, it'll rain soon")
        continue
    elif a1 == a2:
        print("It's all hunky dory up here!")
        break

【问题讨论】:

    标签: python matplotlib raspberry-pi pycharm


    【解决方案1】:

    一般的设计模式是:

    oldval = 0
    while 1:
        newval = read_the_value()
        if oldval != newval:
            take_action()
            oldval = newval
        sleep(xxx)
    

    我认为这对你有用。

    【讨论】:

      【解决方案2】:

      首先,我会避免使用 Python 2。2020 年是它的最后一年。现在一直是 Python 3。你也不需要u'text',因为一切都是unicode。

      你的代码的问题是你引用了同一个压力变量两次。

      原因如下。等待后,您无需进行任何更改。其有效:

      pressure = 123.4
      
      first = pressure # 123.4
      thread.sleep(30)
      second = pressure # 123.4
      
      second == first 
      # True
      

      我会避免同时设置两个测量值。我将使用 previous 和 current 作为名称,因为它比 a1 或 first 更有意义。

      没有循环。

      prev = None
      
      # Pass 1
      
      weatherData = device.get_data()
      data = dict(weatherData[0])
      pressure = data[u'baromrelin']
      curr = pressure # e.g. 123.4
      
      if prev is None or curr > prev:
          print("Pressure is rising!")
      # Current iteration's current pressure will become the next iteration's previous pressure. You could even then see curr = None because we about to overwrite on next iteration.
      prev = curr # 123.4
      
      time.sleep(30)
      
      # Pass 2
      
      weatherData = device.get_data()
      data = dict(weatherData[0])
      pressure = data[u'baromrelin']
      curr = pressure # e.g. 234.5
      
      if prev is None or curr > prev:
          print("Pressure is rising!")
          # 234.5 > 123.4
      
      time.sleep(30)
      

      有一个循环:

      WAIT = 30
      
      
      prev = None
      
      while True:
          weatherData = device.get_data()
          data = dict(weatherData[0])
          pressure = data[u'baromrelin']
          curr = pressure 
      
          if prev is None or curr > prev:
              print("Pressure is rising!")
              break
      
           print(f"Nothing usual. Waiting {WAIT} seconds...")
           time.sleep(WAIT)
      

      【讨论】:

        猜你喜欢
        • 2021-12-17
        • 1970-01-01
        • 2015-01-24
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 1970-01-01
        相关资源
        最近更新 更多