简体   繁体   English

Nanpy伺服控制

[英]Nanpy Servo Control

*Nanpy allows for a Raspi to have an Arduino slave through python *Nanpy 允许 Raspi 通过 python 拥有一个 Arduino slave

Right now I'm having extreme difficulty in using the nanpy Servo package, and I cant get it to run correctly.现在我在使用 nanpy Servo 包时遇到了极大的困难,我无法让它正确运行。 I'm using it to make a robot that is wirelessly controlled from a computer, this is what I have for code:我正在用它来制作一个由计算机无线控制的机器人,这就是我的代码:

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()

This is the error the python shell throws:这是 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!

How can I fix this, and/or what am I doing wrong?我该如何解决这个问题,和/或我做错了什么?

You need to specify the port of the Arduino.您需要指定 Arduino 的端口。 So could you try:那你可以试试:

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

or whatever port you are using.或您使用的任何端口。

update the configuration file to use the servo and upload it to arduino using the IDE then run this program.更新配置文件以使用伺服并使用IDE将其上传到arduino,然后运行该程序。

you can find the cfg.h file in nanpy-firmware/nanpy folder.您可以在 nanpy-firmware/nanpy 文件夹中找到 cfg.h 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM