首页 > 解决方案 > 咖啡烘焙程序,我如何编写一个程序来对温度读数样本进行排序并只返回最低 10 个的平均值?

问题描述

我正在使用 Python 为要由 Artisan-Scope 烘焙软件运行的程序构建脚本。该程序已经适用于我的设备(Phidg​​ets 1045_1B),但我需要对温度读数进行更多过滤。我希望程序以 32ms 采样并按升序组织每秒 30 个样本。然后,我希望将最低的 10 个样本取平均值并返回到 Artisan 软件进行绘图。

这是我目前所拥有的,但在向 Artisan 提供一个温度读数之前,我需要帮助弄清楚如何组织样本并对其进行平均。

import sys
import time
import traceback

from Phidget22.Devices.TemperatureSensor import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *

try:
    from PhidgetHelperFunctions import *
except ImportError:
    sys.stderr.write("\nCould not find PhidgetHelperFunctions. Either add PhdiegtHelperFunctions.py to your project folder "
                      "or remove the import from your project.")
    sys.stderr.write("\nPress ENTER to end program.")
    readin = sys.stdin.readline()
    sys.exit()


def onAttachHandler(self):

    ph = self
    try:
        #If you are unsure how to use more than one Phidget channel with this event, we recommend going to
        #www.phidgets.com/docs/Using_Multiple_Phidgets for information

        print("\nAttach Event:")


        channelClassName = ph.getChannelClassName()
        serialNumber = ph.getDeviceSerialNumber()
        channel = ph.getChannel()



        ph.setDataInterval(32)


        ph.setTemperatureChangeTrigger(0)



    except PhidgetException as e:
        print("\nError in Attach Event:")
        DisplayError(e)
        traceback.print_exc()
        return


def onDetachHandler(self):

    ph = self
    try:





    except PhidgetException as e:
        print("\nError in Detach Event:")
        DisplayError(e)
        traceback.print_exc()
        return


def onErrorHandler(self, errorCode, errorString):

    sys.stderr.write("[Phidget Error Event] -> " + errorString + " (" + str(errorCode) + ")\n")

"""
* Outputs the TemperatureSensor's most recently reported temperature.
* Fired when a TemperatureSensor channel with onTemperatureChangeHandler registered meets DataInterval and ChangeTrigger criteria
*
* @param self The TemperatureSensor channel that fired the TemperatureChange event
* @param temperature The reported temperature from the TemperatureSensor channel
"""
def onTemperatureChangeHandler(self, temperature):

    #If you are unsure how to use more than one Phidget channel with this event, we recommend going to
    #www.phidgets.com/docs/Using_Multiple_Phidgets for information

    print("[Temperature Event] -> Temperature: " + str(temperature))


"""
* Prints descriptions of how events related to this class work
"""
def PrintEventDescriptions():

    print("\n--------------------\n"
        "\n  | Temperature change events will call their associated function every time new temperature data is received from the device.\n"
        "  | The rate of these events can be set by adjusting the DataInterval for the channel.\n"
        "  | Press ENTER once you have read this message.")
    readin = sys.stdin.readline(1)

    print("\n--------------------")

"""
* Creates, configures, and opens a TemperatureSensor channel.
* Displays Temperature events for 10 seconds
* Closes out TemperatureSensor channel
*
* @return 0 if the program exits successfully, 1 if it exits with errors.
"""
def main():
    try:

        ch = TemperatureSensor()
        ch.setOnAttachHandler(onAttachHandler)

        ch.setDeviceSerialNumber(424909)
        ch.setChannel(0)
        ch.openWaitForAttachment(5000)
        ch.setTemperatureChangeTrigger(0)



        ch.setOnDetachHandler(onDetachHandler)

        ch.setOnErrorHandler(onErrorHandler)

        #This call may be harmlessly removed
        PrintEventDescriptions()

        ch.setOnTemperatureChangeHandler(onTemperatureChangeHandler)


        try:
            ch.openWaitForAttachment(5000)
        except PhidgetException as e:
            PrintOpenErrorMessage(e, ch)
            raise EndProgramSignal("Program Terminated: Open Failed")


        time.sleep(1)

        return 0

    except PhidgetException as e:
        sys.stderr.write("\nExiting with error(s)...")
        DisplayError(e)
        traceback.print_exc()
        print("Cleaning up...")
        ch.close()
        return 1
    except EndProgramSignal as e:
        print(e)
        print("Cleaning up...")
        ch.close()
        return 1
    except RuntimeError as e:
         sys.stderr.write("Runtime Error: \n\t" + e)
         traceback.print_exc()
         return 1
    finally:
        print("Press ENTER to end program.")
        readin = sys.stdin.readline()

main()

标签: pythonphidgets

解决方案


您需要的第一件事是某种缓冲区来保存记录的值,直到您有足够的值来处理它们。例如,您可以使用 python 列表::

# in onAttachHandler:
# init buffer
global buffer
buffer = []

在 onTemperatureChangeHandler 中,将值存储在缓冲区中。一旦缓冲区已满,计算您的平均值,然后传递该值。

# in onTEmperatureChangeHandler
global buffer
buffer.append(temperature)
if len(buffer) > 30:
    buffer.sort()
    mean_temperature = sum(buffer[:10]) / 10.0
    buffer = []
    # Do something with mean_temperature here

也就是说,这里使用的全局变量被认为是不好的风格是有充分理由的。应该通过定义一个类来改进代码,该类具有缓冲区和所有处理程序作为属性。有很多关于此的 Python 教程。


推荐阅读