0

*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!

我该如何解决这个问题,和/或我做错了什么?

4

2 回答 2

0

更新配置文件以使用伺服并使用 IDE 将其上传到 arduino,然后运行此程序。

您可以在 nanpy-firmware/nanpy 文件夹中找到 cfg.h 文件。

于 2021-04-05T06:51:37.517 回答
0

您需要指定 Arduino 的端口。那你可以试试:

connection = SerialManager(device='/dev/ttyACM0')

或您使用的任何端口。

于 2020-04-30T11:28:37.347 回答