在中使用Python时的奇怪控件

2024-04-19 09:20:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在实现Python(2.7.9)作为我的带有操纵杆和几个按钮的Arduino与我的Linux机器之间的接口,以在模拟器上控制Megaman。操纵杆进行x/y运动,按钮进行发射和跳跃。我的代码接收一个格式为(X\u Y\u FIRE\u JUMP)的字符串,并解析出值,然后使用PyUserInput库查看它应该在键盘上输入什么。你知道吗

不过,有一个奇怪的错误正在发生:每当我向右移动时,即使没有按任何一个按钮,Megaman也会疯狂地开火。我已经检查了我的串行输出,看看这是否是硬件方面的,它不是;正在接收的串行字符串是干净的,因为它应该看起来像这样“[X>;510]\uy~510]\u k\t”。所以,X告诉它向右移动,Y实际上什么也不做,k告诉它不要跳 t告诉它不要开火。为什么当我移到右边时,我只会得到一个粗略的射击?你知道吗

Python代码:

import serial
from pykeyboard import PyKeyboard


control = PyKeyboard()


def getxy():
    while True:
        try:
            ab = arduino.readline()
            a, b, c, d = ab.split("_")
            a = int(a)
            a = a - 512
    # This is pure jiggery-pokery and apple sauce. The joystick controller is
    # totally kaput (#german) and I didn't want to mess with the wiring (damn
    # color wires. Don't touch this, it will hurt your family.)
            b = int(b)
            b = (b - 512) * -1
            return a, b, c, d
        except Exception:
            continue
        break


def procxy():
    x, y, s, j = getxy()
    mov = ""
    if (x > 100):
        mov = mov + "r"
    if (x < -100):
        mov = mov + "l"
    if (y > 100):
        mov = mov + "u"
    if (y < -100):
        mov = mov + "d"
    if ("f" in s):
        mov = mov + "f"
    if ("j" in j):
        mov = mov + "j"
    return mov


def doshot(instr):
    if ("f" in instr):
        control.press_key('z')
    if ("f" not in instr):
        control.release_key('z')


def dojump(instr):
    if ("j" in instr):
        control.press_key('s')
    if ("j" not in instr):
        control.release_key('s')


def domove():
    movstr = procxy()
    doshot(movstr)
    dojump(movstr)
    while ("r" in movstr):
        control.press_key(control.right_key)
        movstr = procxy()
        doshot(movstr)
        dojump(movstr)
    control.release_key(control.right_key)
    while ("l" in movstr):
        control.press_key(control.left_key)
        movstr = procxy()
        doshot(movstr)
        dojump(movstr)
    control.release_key(control.left_key)


try:
    arduino = serial.Serial('/dev/ttyACM1', 9600)
except:
    print ("Failed to connect on /dev/ttyACM0")
while True:
    x, y, s, j = getxy()
    domove()
    print ("X = {0}\nY = {1}".format(x, y))

Arduino C代码:

int y = 0;
int x = 0;
int fire = 0;
int jump = 0;
void setup(){
  Serial.begin(9600);
}

void loop(){
  y = analogRead(A0);
  x = analogRead(A1);
  fire = analogRead(A2);
  jump = analogRead(A3);
  String out = "";
  out.concat(x);
  out.concat("_");
  out.concat(y);
  if(fire > 900)
  {
    out.concat("_");
    out.concat("f");
  }
  else
  {
    out.concat("_");
    out.concat("t");
  }
  if (jump > 900)
  {
    out.concat("_");
    out.concat("j");
  }
  else
  {
    out.concat("_");
    out.concat("k");
  }
  out.concat("\n");
  Serial.print(out);
}

Tags: keyinifdefoutcontrolintpress
1条回答
网友
1楼 · 发布于 2024-04-19 09:20:07

结果,这个错误是由于一个坏的电阻导致Arduino的模拟端口有轻微的过电压,从而造成干扰。由于操纵杆是模拟的,所以问题只会在特定轴更活跃时发生,因此缺陷的特殊性只会在角色向右移动时发生。你知道吗

相关问题 更多 >