使用 Python 作为界面时的奇怪控件
Weird controls when using Python as interface
我正在实施 Python (2.7.9) 作为我的带有操纵杆和几个按钮的 Arduino 与我的 Linux 机器之间的接口,以在模拟器上控制 Megaman。操纵杆做 x/y 运动,按钮做射击和跳跃。我的代码接收到一个格式为 (X_Y_FIRE_JUMP) 的字符串,并解析出这些值,然后使用 PyUserInput 库查看它应该在键盘上输入什么。
但出现了一个奇怪的错误:每当我向右移动时,即使没有按下任何一个按钮,洛克人也会疯狂开火。我已经检查了我的串行输出,看看这是否是硬件方面的,但不是;接收到的串行字符串是干净的,因为它应该看起来像这样“[X>510]_[Y~510]_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);
}
事实证明,错误是由于电阻器损坏导致 Arduino 的模拟端口出现轻微过压,从而造成干扰。由于操纵杆是模拟的,因此问题只会在该特定轴更活跃时发生,因此错误的特殊性只会在角色向右移动时发生。
我正在实施 Python (2.7.9) 作为我的带有操纵杆和几个按钮的 Arduino 与我的 Linux 机器之间的接口,以在模拟器上控制 Megaman。操纵杆做 x/y 运动,按钮做射击和跳跃。我的代码接收到一个格式为 (X_Y_FIRE_JUMP) 的字符串,并解析出这些值,然后使用 PyUserInput 库查看它应该在键盘上输入什么。
但出现了一个奇怪的错误:每当我向右移动时,即使没有按下任何一个按钮,洛克人也会疯狂开火。我已经检查了我的串行输出,看看这是否是硬件方面的,但不是;接收到的串行字符串是干净的,因为它应该看起来像这样“[X>510]_[Y~510]_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);
}
事实证明,错误是由于电阻器损坏导致 Arduino 的模拟端口出现轻微过压,从而造成干扰。由于操纵杆是模拟的,因此问题只会在该特定轴更活跃时发生,因此错误的特殊性只会在角色向右移动时发生。