首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我遇到麻烦了,j= pygame.joystick.Joystick()

我遇到麻烦了,j= pygame.joystick.Joystick()
EN

Stack Overflow用户
提问于 2017-03-03 20:21:58
回答 1查看 237关注 0票数 0

我的示例代码来自google。

代码语言:javascript
复制
#!/usr/bin/env python

import pygame
from pygame import
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

# Initialise the pygame library
pygame.init()

# Connect to the first JoyStick16:11:10:05:0B:1C
j = pygame.joystick.Joystick(0)
j.init()

print 'Initialized Joystick : %s' % j.get_name()

# Setup the various GPIO values, using the BCM numbers for now
MotorA0 = 16
MotorA1 = 18
MotorAE = 22

MotorB0 = 23
MotorB1 = 21
MotorBE = 19
16:11:10:05:0B:1C
A0 = False
A1 = False
B0 = False
B1 = False



GPIO.setup(MotorA0,GPIO.OUT)
GPIO.setup(MotorA1,GPIO.OUT)
GPIO.setup(MotorAE,GPIO.OUT)

GPIO.setup(MotorB0,GPIO.OUT)
GPIO.setup(MotorB1,GPIO.OUT)
GPIO.setup(MotorBE,GPIO.OUT)

# Set all the Motors to 'off'
GPIO.output(MotorA0, A0)
GPIO.output(MotorA1, A1)
GPIO.output(MotorAE, False)
GPIO.output(MotorBE, False)
GPIO.output(MotorB0, B0)
GPIO.output(MotorB1, B1)


# Only start the motors when the inputs go above the following threshold
threshold = 0.60


LeftTrack = 0
RightTrack = 0

# Configure the motors to match the current settings.

def setmotors():
    GPIO.output(MotorA0, A0)
    GPIO.output(MotorA1, A1)
    GPIO.output(MotorAE, True)
    GPIO.output(MotorBE, True)
    GPIO.output(MotorB0, B0)
    GPIO.output(MotorB1, B1)

# Try and run the main code, and in case of failure we can stop the motors
try:
    # Turn on the motors
    GPIO.output(MotorAE, True)
    GPIO.output(MotorBE, True)

    # This is the main loop
    while True:

    # Check for any queued events and then process each one
    events = pygame.event.get()
    for event in events:
      UpdateMotors = 0

      # Check if one of the joysticks has moved
      if event.type == pygame.JOYAXISMOTION:
        if event.axis == 1:
          LeftTrack = event.value
          UpdateMotors = 1
        elif event.axis == 3:
          RightTrack = event.value
          UpdateMotors = 1

        # Check if we need to update what the motors are doing
        if UpdateMotors:

          # Check how to configure the left motor

          # Move forwards
          if (RightTrack > threshold):
              A0 = False
              A1 = True
          # Move backwards
          elif (RightTrack < -threshold):
              A0 = True
              A1 = False
          # Stopping
          else:
              A0 = False
              A1 = False

          # And do the same for the right motor
          if (LeftTrack > threshold):
              B0 = False
              B1 = True
          # Move backwards
          elif (LeftTrack < -threshold):
              B0 = True
              B1 = False
          # Otherwise stop
          else:
              B0 = False
              B1 = False

          # Now we've worked out what is going on we can tell the
          # motors what they need to do
          setmotors()


except KeyboardInterrupt:
    # Turn off the motors
    GPIO.output(MotorAE, False)
    GPIO.output(MotorBE, False)
    j.quit()#!/usr/bin/env python

GPIO.cleanup()

当我运行这段代码时,我有一个疑难解答。

回溯(最近一次调用):文件"control.py",第13行,j= pygame.joystick.Joystick() TypeError: function恰好接受1个参数(给定0个)

EN

回答 1

Stack Overflow用户

发布于 2017-06-17 21:45:22

代码语言:javascript
复制
Your issue is that the command line j=pygame.joystick.Joystick(0) is calling the first ps3 controller that is available and paired to your raspberry pi (or whatever device you are using). 
With that said, if you do not have a ps3 controller paired with the device, it will never work, because the next command line calls the Ps3controller 0 , and in your case, seems not to be any installed or paired.

您的代码还存在一些其他问题:

第4行:完全删除此行第26行:假设在它之前有一个# hashtag,仅用于表示法。您实际上可以删除此行,因为它是来自其他人的项目的注释。第78行:缩进不正确。它必须遵循它上面的"while True“循环的缩进。因此,要解决这个问题,请确保将第78行中的命令一直退格到左侧,然后按空格键8次,或按TAB键2次,相同的操作。这会解决它的!

现在你的代码可以工作了。实际上还有最后一件事,请确保也安装了pygame库,如果还没有的话,请在Xterminal(我使用raspberry pi和raspbian)中输入以下命令: sudo apt-get install python-pygame

我希望这一切都能有所帮助。我知道你会有更多的问题,这个代码曾经让我很恼火!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42578788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档