Python PyQt 定时器固件
我对 python 很陌生,并且正在尝试使用 fimata 来玩弄 arduino 。
这就是我想要发生的事情:
- 将 arduino 设置为 LED 数字输出
将电位器设置为模拟 0
将 PyQt 定时器设置为更新 电位器位置
应用程序在 PyQt 中设置一个阈值来打开 LED 亮起(模拟输入有 1024 位 分辨率,所以说 800 作为
Threshold)
我正在使用这个firmata库:链接
这是我遇到问题的代码:
import sys 从 PyQt4 导入 QtCore、QtGui fromfirmata import *
# Arduino setup
self.a = Arduino('COM3')
self.a.pin_mode(13, firmata.OUTPUT)
# Create timer
self.appTimer = QtCore.QTimer(self)
self.appTimer.start(100)
self.appTimer.event(self.updateAppTimer())
def updateAppTimer(self):
self.analogPosition = self.a.analog_read(self, 0)
self.ui.lblPositionValue.setNum()
我收到错误消息:
回溯(最近一次调用最后一次): 文件“D:\Programming\Eclipse\IO Demo\src\control.py”,第 138 行,位于 myapp = 主窗口() 文件“D:\Programming\Eclipse\IO Demo\src\control.py”,第 56 行,位于 init 中 self.appTimer.event(self.updateAppTimer()) 文件“D:\Programming\Eclipse\IO Demo\src\control.py”,第 60 行,在 updateAppTimer 中 self.analogPosition = self.a.analog_read(self, 0) 类型错误:analog_read() 恰好需要 2 个参数(给定 3 个参数)
如果我取出“self”,我会收到相同的错误消息,但只给出 1 个参数
我不知道 python 隐式在做什么?
块引用
I am pretty new to python and working with firmata I am trying to play around with an arduino .
Here is what I want to happen:
- Set arduino up with an LED as a
digital out Set potentiometer to analog 0
Set PyQt timer up to update
potentiometer position in
applicationSet a threshold in PyQt to turn
LED on (Analog in has 1024bit
resolution, so say 800 as the
threshold)
I am using this firmata library : Link
Here is the code that I am having trouble with:
import sys
from PyQt4 import QtCore, QtGui
from firmata import *
# Arduino setup
self.a = Arduino('COM3')
self.a.pin_mode(13, firmata.OUTPUT)
# Create timer
self.appTimer = QtCore.QTimer(self)
self.appTimer.start(100)
self.appTimer.event(self.updateAppTimer())
def updateAppTimer(self):
self.analogPosition = self.a.analog_read(self, 0)
self.ui.lblPositionValue.setNum()
I am getting the error message:
Traceback (most recent call last):
File "D:\Programming\Eclipse\IO Demo\src\control.py", line 138, in
myapp = MainWindow()
File "D:\Programming\Eclipse\IO Demo\src\control.py", line 56, in init
self.appTimer.event(self.updateAppTimer())
File "D:\Programming\Eclipse\IO Demo\src\control.py", line 60, in updateAppTimer
self.analogPosition = self.a.analog_read(self, 0)
TypeError: analog_read() takes exactly 2 arguments (3 given)
If I take 'self' out I get the same error message but that only 1 argument is given
What is python doing implicitly that I am not aware of?
Blockquote
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的代码中,“a”是类实例,因此绑定到它的所有方法都已经将自指针作为第一个参数传递。
欢迎使用 python,总有一天你会喜欢它:)
相反,你可以调用任何未绑定的方法(我确信你在任何派生类的每个构造函数中都这样做)。语法是:
In your code 'a' is the class instance, so all methods, bound to it, already have self pointers passed as first params.
Welcome to python, someday you'd like it :)
In contra, you can call any method as unbound (and I'm sure you do it in every constructor of any derived class). Syntax is:
自我不需要被传递。我不知道为什么它第一次失败,或者为什么 self 已经包含在内。
Self didn't need to be passed. I have no clue why it failed the first time, or why self is included already.