初始化中的microPython属性错误__

初始化中的microPython属性错误__,python,pid,attributeerror,micropython,Python,Pid,Attributeerror,Micropython,编辑:多亏了一个古老的传统,我把所有的东西都拔掉,重新插上电源,我让它工作起来了;我刷新了WiPy模块并重新安装了Pymakr插件。 我目前正在为micropython编写一个PID控制器(我们必须制作一个linefollower)。我已经在microPython中作为一个类实现了PID算法 我将使用Pycom的WiPy模块来控制我的linefollower 现在实际的程序做的不多,我只是想测试一下程序是否相当快,比如我有一些随机数作为输入和K值,而输出函数是空的 然而,每当我尝试运行一个小测试

编辑:多亏了一个古老的传统,我把所有的东西都拔掉,重新插上电源,我让它工作起来了;我刷新了WiPy模块并重新安装了Pymakr插件。

我目前正在为micropython编写一个PID控制器(我们必须制作一个linefollower)。我已经在microPython中作为一个类实现了PID算法

我将使用Pycom的WiPy模块来控制我的linefollower

现在实际的程序做的不多,我只是想测试一下程序是否相当快,比如我有一些随机数作为输入和K值,而输出函数是空的

然而,每当我尝试运行一个小测试脚本,在其中创建对象并计算PID a 100次时,我就会在PID类的init中得到一个attributeError。这就是给我带来麻烦的方法:

def __init__(self, input_func, output_func, P_factor=1.0, I_factor=0.0,
             D_factor=0.0):

    ... previous variable declarations ...

    # Initializer for the time. This will form the basis for the timebased
    # calculations. This time will mark the start of a time-block.
    self.last_calculate_time = utime.ticks_ms()
最后一行是给我带来麻烦的那一行。主程序中包含以下内容:

def motorOutput(PID_output):
    """
    Function for driving the two motors depending on the PID output.
    """
    pass

def generateInput():
    """
    Return a random float
    """
    return 2.5


if __name__ == "__main__":

    print("creating PID controller")
    PID_controller = PID.PID(generateInput, motorOutput, 1.0, 0.5, 1.5)
    counter = 0
    print("starting loop")
    while counter < 1000:
        counter += 1
        PID_controller.calculate()

    print("finished loop")
def电机输出(PID_输出): """ 根据PID输出驱动两个电机的功能。 """ 通过 def generateInput(): """ 返回一个随机浮点数 """ 返回2.5 如果名称=“\uuuuu main\uuuuuuuu”: 打印(“创建PID控制器”) PID_控制器=PID.PID(发电机输入、电机输出、1.0、0.5、1.5) 计数器=0 打印(“起始循环”) 当计数器<1000时: 计数器+=1 PID_控制器。计算() 打印(“完成的循环”) 这是我在运行文件时得到的输出:

>>> Running main.py

>>>
>>> creating PID controller
╝Traceback (most recent call last):
File "<stdin>", line 60, in <module>
File "/flash/lib/PID.py", line 95, in __init__
AttributeError: 'PID' object has no attribute 'last_calculate_time'
╝>
MicroPython v1.8.6-621-g17ee404e on 2017-05-25; WiPy with ESP32
>>运行main.py
>>>
>>>创建PID控制器
╝回溯(最近一次呼叫最后一次):
文件“”,第60行,在
文件“/flash/lib/PID.py”,第95行,在__
AttributeError:“PID”对象没有“last\u calculate\u time”属性
╝>
2017年5月25日,MicroPython v1.8.6-621-g17ee404e;用ESP32擦拭

您收到此错误是因为您试图分配一个尚未声明的属性,我假设您使用的是python 2的 经典的课堂风格。在PID类中添加
last\u calculate\u time
的定义,例如
last\u calculate\u time=None
,然后它应该按预期工作

更合适的方法是将
对象
作为参数传递给类定义,如下所示,从而将其视为新样式类:

class PID(object):
    def __init__(self):
        self.last_calculate_time = utime.ticks_ms()

更多信息请访问:

谢谢您的快速回复!我已经把对象作为类参数了。我尝试先将变量初始化为None,然后赋值。然而现在我得到了同样的错误,但在实际的计算方法,而不是init@Pieter-Jancasiman为认为这就是问题所在而道歉!嗯,您是否对
utime.ticks\u ms()
的输出进行了一些测试?也许暂时硬编码该值,看看是否有帮助?啊哈!我们知道了!ticks_ms()确实是个问题。它表示模块没有属性ticks_ms()。这很奇怪,因为这是文档告诉我要使用的…刚刚使用在线micropythoncli对其进行了测试,并为我工作。你对utime使用的导入语句是什么?我使用的是“导入utime”,但我刚刚删除了所有插件并重新安装了它。我还将固件刷新到主板上,这似乎解决了问题。