【发布时间】:2018-11-23 16:38:41
【问题描述】:
编辑:多亏了拔掉插头再插上所有东西的古老传统,我让它工作了;我刷新了 WiPy 模块并重新安装了 Pymakr 插件。
我目前正在为 micropython 编写一个 PID 控制器(我们必须创建一个 linefollower)。我已经将 microPython 中的 PID 算法作为一个类实现了。
我将使用 Pycom 的 WiPy 模块来控制我的 linefollower。
目前实际程序并没有做太多,我只是想测试程序是否相当快,因此我有一些随机数作为输入和 K 值,而输出函数只是留空。
但是,每当我尝试运行一个小测试脚本来创建对象并计算 PID 100 次时,我会在 PID 类的 init 中得到一个属性错误。这是给我带来麻烦的方法:
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")
这是我运行文件时得到的输出:
>>> 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
【问题讨论】:
标签: python pid attributeerror micropython