ロータリーエンコーダ処理のプログラムをpythonで書いています。
raspberrypiを使っています。
sample2.py:37: SyntaxWarning: name 'position' is assigned to before global declaration
global position, moved
sample2.py:37: SyntaxWarning: name 'moved' is assigned to before global declaration
global position, moved
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "sample2.py", line 26, in run
i = (int(i, 2) << 2) + ((GPIO.input(port_a) << 1) + (GPIO.input(port_b)) & 3)
TypeError: int() can't convert non-string with explicit base
グローバル変数に関するエラーとスレッドについてのエラーが解決しません。
Python初心者でよく分かっていないところも多いので、教えていただけると嬉しいです。
14 class StateThread(threading.Thread):
15
16 def init(self):
17 threading.Thread.init(self)
18
19 def run(self):
20 global position, moved
21 i = 0
22 GPIO.setup(port_a, GPIO.IN, GPIO.PUD_OFF)
23 GPIO.setup(port_b, GPIO.IN, GPIO.PUD_OFF)
24 table = (0, 1, -1, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, -1, 1, 0)
25 while True:
26 i = (int(i, 2) << 2) + ((GPIO.input(port_a) << 1) + (GPIO.input(port_b)) & 3)
27 print 'run'
28 if table[i & 15]:
29 position = position + table[i & 15]
30 moved = True
31 print 'in table'
32 else:
33 moved = False
34
35 if name == "main":
36
37 global position, moved
38 GPIO.setmode(GPIO.BCM)
39 th = StateThread()
40 th.setDaemon(True)
41 th.start()
42 try:
43 while True:
44 if moved == True:
45 print position
46 except KeyboardInterrupt:
47 print '\nbreak'
48 GPIO.cleanup()
49 th.join()
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/08/18 07:48
退会済みユーザー
2015/08/18 13:57