我正在實現Python(2.7.9)作爲我的Arduino與操縱桿和幾個按鈕以及我的Linux機器之間的接口,以在模擬器上控制Megaman。操縱桿執行x/y動作,按鈕可以啓動並跳躍。我的代碼接收格式爲(X_Y_FIRE_JUMP)的字符串,然後解析出這些值,然後使用PyUserInput庫查看它應該在鍵盤上輸入的內容。當使用Python作爲接口時奇怪的控件
雖然發生了一個奇怪的錯誤:無論我何時向右移動,即使沒有按下其中一個按鈕,曼佳美都會以自由方式進行發射。我檢查了我的串行輸出,看看這是否是硬件方面,而不是;被接收的串行字符串是乾淨的,因爲它應該看起來像這樣「[X> 510] _ [Y〜510] _k_t」。所以,X正在告訴它向右移動,Y並沒有真正做任何事情,K告訴它不要跳,並且告訴它不要開火。爲什麼我仍然粗暴的意外射擊只有當我移動到右邊?
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);
}
爲什麼analogRead而不是digitalRead for button?你如何檢查你的串行輸出? –
@Ôrel我正在使用模擬閱讀,因爲我的按鈕正在彎曲和通斷。串行輸出正在GetXY函數的第一行上進行檢查。 –
我的意思是,你怎麼確定輸出是好的,基於我們的代碼,你應該在一秒鐘內收到幾行。你檢查每一條收到的電話嗎? –