*Nanpy 允许 Raspi 通过 python 拥有一个 Arduino slave
现在我在使用 nanpy Servo 包时遇到了极大的困难,我无法让它正确运行。我用它来制作一个由计算机无线控制的机器人,这就是我的代码:
from nanpy import (ArduinoApi, SerialManager)
import pygame
from nanpy import Servo
import time
pygame.init()
a=0
d=0
window = pygame.display.set_mode((800,600))
pygame.display.set_caption("Console")
try :
connection = SerialManager()
ard = ArduinoApi(connection = connection)
run = True
except:
print("Connection Failed!")
servoA = Servo(2)
servoD = Servo(4)
while run :
for event in pygame.event.get():
keys = pygame.key.get_pressed()
# if (event.type==pygame.KEYDOWN):
if keys[pygame.K_s] and keys[pygame.K_a]:
a=a-1
servoA.write(a)
elif keys[pygame.K_s] and keys[pygame.K_d]:
d=d-1
servoD.write(d)
elif keys[pygame.K_w]:
a=a+1
d=d+1
servoD.write(d)
servoA.write(a)
elif keys[pygame.K_s]:
a=a-1
d=d-1
servoD.write(d)
servoA.write(a)
elif keys[pygame.K_d]:
d=d+1
servoD.write(d)
elif keys[pygame.K_a]:
a=a+1
servoA.write(a)
elif keys[pygame.K_t]:
run=False
pygame.quit()
这是 python shell 抛出的错误:
Traceback (most recent call last):
File "/home/pi/Desktop/nanpy/RobotCode.py", line 28, in <module>
servoA = Servo(2)
File "/usr/local/lib/python3.4/dist-packages/nanpy-0.9.6-py3.4.egg/nanpy/servo.py", line 9, in __init__
self.id = self.call('new', pin)
File "/usr/local/lib/python3.4/dist-packages/nanpy-0.9.6-py3.4.egg/nanpy/arduinoboard.py", line 150, in call
return _call(self.namespace, self.id, args, self.connection)
File "/usr/local/lib/python3.4/dist-packages/nanpy-0.9.6-py3.4.egg/nanpy/arduinoboard.py", line 47, in _call
ret = return_value(connection)
File "/usr/local/lib/python3.4/dist-packages/nanpy-0.9.6-py3.4.egg/nanpy/arduinoboard.py", line 18, in return_value
return serial_manager.readline().replace('\r\n', '')
File "/usr/local/lib/python3.4/dist-packages/nanpy-0.9.6-py3.4.egg/nanpy/serialmanager.py", line 101, in readline
raise SerialManagerError('Serial timeout!')
nanpy.serialmanager.SerialManagerError: Serial timeout!
我该如何解决这个问题,和/或我做错了什么?