(四)Jetson Agx Xavier 手柄使用(xbox joystick ubuntu python)

0、无线的手柄,打算先做个遥控的功能,方便后期连接线控底盘

淘宝亚博智能旗舰店,价格89,官方提供ROS和jupyter的代码,技术客服不提供python脚本,需要自己去写,先用自己的虚拟机ubuntu做了一边测试。

 

1、测试

直接访问Gamepad Tester,插上接收器,按一下图中start开关,连接成功后即可看到数据,记一下自己需要的按键和摇杆对应的名字 。摇杆是axis,按键是b。

2、通过pygame.joystick进行读取

官方提供了ROS和jupyder交互式的页面(import ipywidgets.widgets as widgets),但是没有python脚本,这里采用pygame.joystick进行开发。

首先安装pygame,没有pip3的需要先安装pip3

pip3 install pygame

也可以安装joystick先测试下

sudo apt-get install joystick

终端通过joystick测试手柄

sudo jstest /dev/input/js0

 通过py脚本进行读取

import pygame
import time


pygame.init()

joystick = pygame.joystick.Joystick(0) 
joystick.init()
key=joystick.get_init()
print('ready____________________________')
print(key)
name = joystick.get_name()
print('name____________________________')
print(name)
axes = joystick.get_numaxes()
print('axes________________________')
print(axes)

buttons = joystick.get_numbuttons() #获得 Joystick 上按钮的数量
print('buttons_______________________')
print(buttons)


while 1:
	for event_ in pygame.event.get():
		if event_.type == pygame.JOYBUTTONDOWN or event_.type == pygame.JOYBUTTONUP:
			if joystick.get_button(0)==1:
				print('A')
			if joystick.get_button(1)==1: 
				print('B')
			if joystick.get_button(5)==1: 
				print('R')
		if event_.type == pygame.JOYAXISMOTION:
			print(joystick.get_axis(1),joystick.get_axis(3),joystick.get_button(5))

(1)、pygame 初始化 pygame.init()

(2)、创建joystick对象,一般只有一个手柄joystick = pygame.joystick.Joystick(0) 

(3)、初始化手柄 joystick.init()

(4)、简单的返回:是否初始化成功、名字、axes数量,按键数量

(5)、循环。判断事件 按下或者摇杆移动 通过get方法获取值

3、后期填坑

1、这个必须移动才能返回数据、或者按键触发

2、通过遥感数据控制小车方向和速度

3、断线识别问题

猜你喜欢

转载自blog.csdn.net/qq_53086461/article/details/127121377