ESP32与Xbox手柄的UART通信测试
1. 说明
这个项目的目标是实现使用手柄来控制ESP32。最近正在进行无人机项目,但是由于没有适合的遥控器来控制四轴,画板子也有些占用时间,所以比较有效的方法就是基于手头有的Xbox手柄来进行一个DIY,在手柄与ESP32之间建立串口通信。此处使用PC作为中继,可能速度有些慢,但是基于目前需求,速度已经足够了。下图说明了无人机项目的通信方式,红框部分为本次涉及部分。

2. 环境
这里我使用主要Ubuntu 18作为开发环境,Win10下也能正常运行。python版本为3.9,所需库为pygame与pyserial。
3. 手柄与PC之间的通信测试
手柄与PC之间通过Pygame建立通信,以下提供了两个测试程序,第一个测试程序是一个简单的终端输出,如果手柄工作正常,就会看到六轴的输出。
import pygame
import time
pygame.init()
pygame.joystick.init()
done=False
while (done != True):
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
joystick_count = pygame.joystick.get_count()
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
axes = joystick.get_numaxes()
print('================')
time.sleep(0.1)
for i in range(axes):
axis = joystick.get_axis(i)
print(axis)
以下测试程序提供了一个简单的GUI来对每个按键进行测试
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vi:ts=4 sw=4 et
#
# This tool runs fine on both Python 2 and Python 3.
#
# https://github.com/denilsonsa/pygame-joystick-test
from __future__ import division
from __future__ import print_function
import sys
import pygame
from pygame.locals import *
class joystick_handler(object):
def __init__(self, id):
self.id = id
self.joy = pygame.joystick.Joystick(id)
self.name = self.joy.get_name()
self.joy.init()
self.numaxes = self.joy.get_numaxes()
self.numballs = self.joy.get_numballs()
self.numbuttons = self.joy.get_numbuttons()
self.numhats = self.joy.get_numhats()
self.axis = []
for i in range(self.numaxes):
self.axis.append(self.joy.get_axis(i))
self.ball = []
for i in range(self.numballs):
self.ball.append(self.joy.get_ball(i))
self.button = []
for i in range(self.numbuttons):
self.button.append(self.joy.get_button(i))
self.hat = []
for i in range(self.numhats):
self.hat.append(self.joy.get_hat(i))
class input_test(object):
class program:
"Program metadata"
name = "Pygame Joystick Test"
version = "0.2"
author = "Denilson Figueiredo de Sá Maia"
nameversion = name + " " + version
class default:
"Program constants"
fontnames = [
# Bold, Italic, Font name
(0, 0, "Bitstream Vera Sans Mono"),
(0, 0, "DejaVu Sans Mono"),
(0, 0, "Inconsolata"),
(0, 0, "LucidaTypewriter"),
(0, 0, "Lucida Typewriter"),
(0, 0, "Terminus"),
(0, 0, "Luxi Mono"),
(1, 0, "Monospace"),
(1, 0, "Courier New"),
(1, 0, "Courier"),
]
# TODO: Add a command-line parameter to change the size.
# TODO: Maybe make this program flexible, let the window height define
# the actual font/circle size.
fontsize = 20
circleheight = 10
resolution = (640, 480)
def load_the_fucking_font(self):
# The only reason for this function is that pygame can find a font
# but gets an IOError when trying to load it... So I must manually
# workaround that.
# self.font = pygame.font.SysFont(self.default.fontnames, self.default.fontsize)
for bold, italic, f in self.default.fontnames:
try:
filename = pygame.font.match_font(f, bold, italic)
if filename:
self.font = pygame.font.Font(filename, self.default.fontsize)
# print("Successfully loaded font: %s (%s)" % (f, filename))
break
except IOError as e:
# print("Could not load font: %s (%s)" % (f, filename))
pass
else:
self.font = pygame.font.Font(None, self.default.fontsize)
# print("Loaded the default fallback font: %s" % pygame.font.get_default_font())
def pre_render_circle_image(self):
size = self.default.circleheight
self

本文详细描述了如何通过Python与ESP32搭建Xbox手柄与无人机的串口通信链路,包括PC作为中继的测试,以及手柄与ESP32直接通信的实现过程。重点在于手柄数据的处理和ESP32的接收与响应。
最低0.47元/天 解锁文章
585

被折叠的 条评论
为什么被折叠?



