coxbox变换 python_coxbox变换 python_怎样用Python实现对Xbox手柄的控制?

sudo apt install xboxdrv

sudo apt install python3-pygame

使用手柄控制小车

使用serial控制

fe 轮子编号 f0 速度

如 fe 01 f0 128 第一个轮子 向前 速度为128

如 fe 01 fb 128 第一个轮子 后退 速度为128

– coding: utf-8 -*

import serial

import time

import binascii

import sys

import pygame

import RPi.GPIO as GPIO

打开串口

jetson-nano

ser = serial.Serial(“/dev/ttyTHS1”, 9600)

raspberry 3b +

ser = serial.Serial(“/dev/ttyUSB0”, 9600)

def init():

ser = serial.Serial(“/dev/ttyUSB0”, 9600)

模块初始化

pygame.init()

pygame.joystick.init()

done = False

clock = pygame.time.Clock()

joystick_count = pygame.joystick.get_count()

print(joystick_count);

joystick = pygame.joystick.Joystick(0)

joystick.init()

name = joystick.get_name()

return done,name ,clock,joystick_count,joystick

def swClockwise(serial_object,wid,speed):

speed = 20 + (speed/1.25)

serial_object.write((0xfe).to_bytes(1, byteorder=’big’));

serial_object.write((wid).to_bytes(1, byteorder=’big’));

serial_object.write((0xf0).to_bytes(1, byteorder=’big’));

serial_object.write((speed).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

time.sleep(0.3);

return

def swCounterclockwise(serial_object,wid,speed):

speed = 20 + (speed/1.25)

serial_object.write((0xfe).to_bytes(1, byteorder=’big’));

serial_object.write((wid).to_bytes(1, byteorder=’big’));

serial_object.write((0xba).to_bytes(1, byteorder=’big’));

serial_object.write((speed).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

time.sleep(0.3);

return

def swStop(serial_object,wid):

serial_object.write((0xfe).to_bytes(1, byteorder=’big’))

serial_object.write((wid).to_bytes(1, byteorder=’big’))

serial_object.write((0xf0).to_bytes(1, byteorder=’big’))

serial_object.write((0x00).to_bytes(1, byteorder=’big’))

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

serial_object.write((0x00).to_bytes(1, byteorder=’big’));

time.sleep(0.3);

return

def forward(speed):

swClockwise(ser,1,speed)

swClockwise(ser,2,speed)

return

def backoff(speed):

swCounterclockwise(ser,1,speed)

swCounterclockwise(ser,2,speed)

return

def groundleftrun(speed):

swCounterclockwise(ser,2,0)

swCounterclockwise(ser,1,speed)

def groundrightrun(speed):

swCounterclockwise(ser,2,speed)

swCounterclockwise(ser,1,0)

def stop():

swStop(ser,1)

swStop(ser,2)

return

def testSw():

while(True):

time.sleep(1)

forward(128)

forward(128)

time.sleep(1)

backoff(128)

backoff(128)

time.sleep(1)

stop()

return

def testGPIO():

GPIO.cleanup()

pin = 4

GPIO.setmode(GPIO.BCM) #GPIO.BOARD

GPIO.setup(pin,GPIO.OUT)

GPIO.cleanup()

return

def default():

print(“aicar”)

def BHLonClick(axis):

print(“BHL”)

def BHRonClick(axis):

print(“BHR”)

def chazhi(in_num,in_max=1.0000,d_num=0.0001,out_max=255.0):

in_num = float(in_num)

in_max = float(in_max)

d_num = float(d_num)

out_max = float(out_max)

retn = (in_max/d_num)

print(retn)

retn = (out_max/retn)

print(retn)

retn = retn *in_num

print(retn)

retn = int(retn)

print(retn)

return retn

def keyup_A():

print(“A_UP“)

return

def keydown_A():

print(“A_DOWN“)

return

def keyup_B():

print(“B_UP“)

return

def keydown_B():

print(“B_DOWN“)

return

def keyup_X():

print(“X_UP“)

return

def keydown_X():

print(“X_DOWN“)

return

def keyup_Y():

print(“Y_UP“)

return

def keydown_Y():

print(“Y_DOWN“)

return

def keyup_SELECT():

print(“SELETC_UP“)

return

def keydown_SELECT():

print(“SELECT_DOWN“)

return

def keyup_START():

print(“START_UP“)

return

def keydown_START():

print(“START_DOWN“)

return

def keyup_L1():

print(“L1_UP“)

return

def keydown_L1():

print(“L1_DOWN“)

return

def keyup_R1():

print(“R1_UP“)

return

def keydown_R1():

print(“R1_DOWN“)

return

def up(num):

print (“up : “+str(num))

return

def down(num):

print (“down : “+str(num))

return

def left(num):

print (“left : “+str(num))

return

def right(num):

print (“right : “+str(num))

return

def main():

done,name ,clock,joystick_count,joystick=init()

stop()

while not done:

for event_ in pygame.event.get():

print(name)

Quit Event

if event_.type == pygame.QUIT:

done=True

elif event_.type == pygame.JOYBUTTONDOWN or event_.type == pygame.JOYBUTTONUP:

buttons = joystick.get_numbuttons()

for i in range(buttons):

button = joystick.get_button(i)

print(“button “+ str(i) + ” : ” + str(button))

if i == 0 and button == 1:

keydown_A()

pass

elif i == 0 and button == 0:

keyup_A()

pass

elif i == 1 and button == 1:

keydown_B()

pass

elif i == 1 and button == 0:

keyup_B()

pass

elif i == 3 and button == 1:

keydown_X()

pass

elif i == 3 and button == 0:

keyup_X()

pass

elif i == 4 and button == 1:

keydown_Y()

pass

elif i == 4 and button == 0:

keyup_Y()

pass

elif i == 10 and button == 1:

keydown_SELECT()

pass

elif i == 10 and button == 0:

keyup_SELECT()

pass

elif i == 11 and button == 1:

keydown_START()

pass

elif i == 11 and button == 0:

keyup_START()

pass

elif i == 6 and button == 1:

keydown_L1()

pass

elif i == 6 and button == 0:

keyup_L1()

pass

elif i == 7 and button == 1:

keydown_R1()

pass

elif i == 7 and button == 0:

keyup_R1()

pass

pass

elif event_.type == pygame.JOYAXISMOTION :

axes = joystick.get_numaxes()

for i in range(axes):

axis = joystick.get_axis(i)

if i == 4 and axis != -1:

BHLonClick(axis)

pass

if i == 5 and axis != -1:

BHRonClick(axis)

pass

if i == 1 and axis < 0:

up(chazhi(axis))

elif i == 1 and axis > 0:

down(chazhi(axis))

elif i == 0 and axis < 0:

left(chazhi(axis))

elif i == 0 and axis > 0:

right(chazhi(axis))

elif i == 0 and axis == 0:

pass

print (“axis “+str(i) + “: “+str(axis))

pass

elif event_.type == pygame.JOYHATMOTION:

hats = joystick.get_numhats()

for i in range(hats):

hat = joystick.get_hat(i)

if(hat == (0,0)):

stop()

elif(hat == (0,1)):

forward(128)

pass

elif hat == (0,-1):

backoff(128)

pass

elif hat == (-1,0):

groundleftrun(100)

pass

elif hat == (1,0):

groundrightrun(100)

pass

pass

print(“hat “+str(i)+ “: “+str(hat))

print(hat)

pass

pass

pass

pass

if ser != None:

ser.close()

pass

return

if name == ‘main‘:

main()

Original: https://blog.csdn.net/weixin_42522676/article/details/114491992
Author: 芒果绵绵冰
Title: coxbox变换 python_coxbox变换 python_怎样用Python实现对Xbox手柄的控制?

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783383/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球