0

我正在尝试使用一个模块从传感器板上读取数据,将该数据放入一个类中,而不是在另一个模块中打印该数据。我不确定我是否在这里遵循最佳实践,或者是否有更好的方法来做到这一点。目前我收到以下错误,我不知道如何更正。

Traceback (most recent call last):
  File "app/conductor.py", line 15, in <module>
    accel_x, accel_y, accel_z = nineDOF.Acceleration()
TypeError: cannot unpack non-iterable NoneType object

导体.py:

import time
import datetime
import dht11
import max31865
import lsm9ds1

while True:
    externalReading = max31865.temp()
    print("External Temp: {0:0.3f}C".format(externalReading))

    internalTemp, internalHumidity = dht11.readings()
    print("Internal Temp 1: {:.3f}C  Humidity: {}% ".format(internalTemp, internalHumidity))

    nineDOF = lsm9ds1.get()
    accel_x, accel_y, accel_z = nineDOF.Acceleration()
    mag_x, mag_y, mag_z = nineDOF.Magnetometer()
    gyro_x, gyro_y, gyro_z = nineDOF.Gyroscope()
    internalTemp2 = nineDOF.temperature()
    print(
        "Acceleration (m/s^2): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
            accel_x, accel_y, accel_z
        )
    )
    print(
        "Magnetometer (gauss): ({0:0.3f},{1:0.3f},{2:0.3f})".format(mag_x, mag_y, mag_z)
    )
    print(
        "Gyroscope (degrees/sec): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
            gyro_x, gyro_y, gyro_z
        )
    )
    print("Internal Temp 2: {0:0.3f}C".format(internalTemp2))

lsm9ds1.py:

import board
import busio
import adafruit_lsm9ds1

# SPI connection:
from digitalio import DigitalInOut, Direction

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
csag = DigitalInOut(board.D5)
csag.direction = Direction.OUTPUT
csag.value = True
csm = DigitalInOut(board.D6)
csm.direction = Direction.OUTPUT
csm.value = True
sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi, csag, csm)


class Readings:
    def __init__(self):
        pass

    def Acceleration(self):
        x, y, z = sensor.acceleration

    def Magnetometer(self):
        x, y, z = sensor.magnetic

    def Gyroscope(self):
        x, y, z = sensor.gyro

    def temperature(self):
        x = sensor.temperature


def get():
    return Readings()

4

1 回答 1

1

您的直接问题是Readings类中的方法没有返回值,并且没有显式 return语句时 Python 的默认返回值是None. 在一些语言中,函数中的最后一个值也是返回值——我想到了 R、Perl 和 Bourne Shell——这可能是这里造成混淆的原因。有讨论这个回报,回报None,根本不回报?

该方法的隐式None返回值Acceleration无法解压缩为三元素元组。如果您return x, y, z在该方法的末尾添加 a 它应该可以工作。

就课程和程序结构而言,这取决于您的目标是什么。

于 2020-07-15T11:32:28.890 回答