我们使用一些必要的Cookie来确保网站正常运行。

我们使用可选的 Cookie,具体详情请参阅我们的Cookie政策,用于记住您的设置并了解您如何使用我们的网站。

Raspberry Pi Pico brings junked joysticks back to life | The MagPi #146

在最新一期的树莓派社区评选这款软件为,今日出版,我们采访了 David Miles,他设计了一个 Raspberry Pi Pico 套件来让废弃的摇杆重新焕发生机。

众多亮点之中,有一项尤为出色的在于EMF Camp 活动是交换摊位帐篷,人们会带来各种物品进行买卖和交换。在我多次光顾时,我发现了一对略有磨损(但分量很足)的摇杆,看起来仿佛曾是某种专业模拟器的一部分。在这篇文章中,我将分享我是如何通过逆向工程将它们改造成完整的飞行模拟器控制器的。在此过程中,我还顺带写了一个 Pico 程序,让你能轻松地将任何输入设备当作 USB 摇杆使用。

The image shows two gimbal joysticks resting on grass. These joysticks are industrial or military-grade, manufactured by "Ultra Electronics" (Electrical Division). They both have black bases with model information labels on them, identifying the joysticks as part of the Ultra Electronics line. Left Joystick: It appears worn, with patches of the original paint chipped away, revealing a lighter color underneath. The grip features several buttons and what looks like a control hat (small thumbstick). Right Joystick: This one appears to be in better condition, with a smoother and more consistent finish. It also has buttons and a control hat, similar to the left joystick, but the design is slightly more compact and less worn. Both joysticks are mounted on black gimbal bases, which allow for multidirectional movement, and each has a label with the Ultra Electronics branding. The labels contain text, including "Gimbal Joystick" and some part or batch numbers, but they are partially obscured or difficult to read due to the angle. These joysticks could be from fighter jets or advanced simulation setups.
图1:我在 EMF Camp 交换摊位找到的摇杆

入门指南

图1展示了我在摊位上找到时的摇杆。Ultra Electronics 是一家为 Ministry of Defence 制造设备的厂商,因此这东西看起来相当有趣。我的目标是设法让它们恢复正常工作,以便我能将其接入一款同样采用此类摇杆的飞机飞行模拟器中。这就意味着我要解决两个难题:

  • 从摇杆中提取数据
  • 制作一个可连接至 PC 作为游戏控制器使用的设备

追踪信号

为了弄清楚如何从摇杆中获取数据,我检查了从它引出的线缆。主控单元接着一根(长度出乎意料的)线缆,末端有两个插头——看起来是一个九针 RS232 串行接口,以及一个十五针的游戏端口接口。副摇杆(在右侧的图片中的那个)配有一个 25-pin 接口,用于插入主控端,这说明它可能仅包含开关电路,主控部分才是运算核心。图1)带有一个 25-pin 连接器,可插入主控端,这暗示其内部可能仅有开关,主控才是核心处理单元。连接器的类型与硬件浓厚的上世纪 90 年代末风格相吻合,也为我们与它通信提供了线索,不过我们也可以拆开内部结构来验证一些推测。

图2展示了设备内部的视图。壳内有四颗螺丝固定着摇杆的上半部分。卸下螺丝后,整个摇杆及球形关节组件便可整体取出,展现在我们面前的是这块电路板。板上的一些元器件为我们提示了其内部的工作原理。

The image shows the internal components of an electronic device, likely part of the control mechanism for the joysticks in the previous image. It features a printed circuit board (PCB) with various electronic components and wiring. Key elements visible in the image include: Integrated Circuits (ICs): Several IC chips are mounted on the PCB, including some marked with "H" and what appears to be part numbers like "CD74AC573E" and "1814306." These ICs likely control the logic and processing functions of the device. Dip Switches: A blue section of the board has multiple red dip switches, which are used to configure the device for various settings or functions. Wiring: Color-coded wires connect the board to a nearby connector, which suggests this component interfaces with other parts of a larger system. The wiring is neatly organized and secured in place with black ties. Labelled Component: One chip has a white label with "PRG_OBDO," which could indicate this is a programmable chip or ROM that stores specific instructions or firmware for the device. LEDs or Indicators: Several small red components, possibly LEDs, are mounted near the switches, likely serving as status indicators for the system. This appears to be a custom-built or specialized electronics module, possibly part of a control system for industrial or aviation-related equipment, given the context of the previous images. The wiring and the presence of programmable chips suggest it controls a complex system.
图2:位于摇杆核心的 PCB。请仔细瞧瞧那些经过精密调节并密封的校准电阻!

板上有几颗芯片被标签贴遮盖了。这通常是微控制器的特征,说明该电路不止具备简单的控制功能。印有部分“ADC”字样的芯片进一步印证了这一点——ADC 全称“Analogue to Digital Converter”,用于将模拟信号(如摇杆中电位计的物理位置)转换为输送给计算机的数字信号。

最后,中间位置有一颗芯片上印有“MAX232”字样。这一系列的芯片专门用于将微电子器件中常见的 5v 逻辑电平转换为 RS232 通信标准所使用的 15v 电平。这也进一步佐证了我的判断:那个九针接口极可能是一个标准的 RS232 接口,可以用来直接读取摇杆的数据。

剩下就是那个十五针接口。它是游戏端口吗?在 90 年代,这种接口常被用于将摇杆连接至 PC(令人困惑的是,电脑端往往使用的是声卡上的接口)。传统游戏端口允许你同时连接两个摇杆,每个摇杆支持两个按键。但我们可以看出这个摇杆的按键数量远超四个——粗略数了一下竟有近 30 个,说明它可能被赋予了其他特殊用途。

The image shows the internal view of a joystick, likely the same worn joystick seen in a previous image. The outer casing has been removed to reveal the internal wiring and components. Key elements include: Wiring: A bundle of color-coded wires runs vertically inside the joystick housing. The wires are neatly organized and secured with black ties, indicating they are part of the internal control system that connects the buttons and control mechanisms of the joystick to the external interface. Capacitor: A large black cylindrical component marked "2I649" and "T2" appears to be a capacitor. Capacitors like this are used for energy storage within electrical circuits and are common in joystick designs to manage power surges or smooth out signals. Metal Shaft: A metal rod or shaft is visible running vertically through the center of the joystick, likely part of the gimbal system that allows for multidirectional movement. Green Component: Near the bottom, there's a small green component with what looks like a circular interface or sensor, possibly related to the joystick's motion tracking or button functionality. Buttons: The top of the image shows part of the joystick grip with external buttons still visible, including a red button and a few other black controls. These buttons are connected to the internal components via the wiring. This internal view reveals the intricate electronic design and mechanical structure of the joystick, highlighting its complexity as a precision control device, likely for industrial or aviation purposes.
图 3:所有线缆都采用了独一无二的颜色组合

若拆开接口的后盖,我们会发现仅使用了红黑两根导线连接了两个引脚。查阅游戏端口的电气规格表可知,这两个引脚分别对应接地和 5v。

这与我们的早期推断完全吻合。常规串口无法为外部设备供电,但若结合游戏端口与串口,即可实现向 PC 的连接,并同时完成数据通信与供电传输。

连接设备

既然我们已经摸清了它的工作机制,就可以尝试将其接入电脑。为此,我采用了一个常规的 USB-to-serial 转换模块,并将其供电端连接至一块 USB 充电宝。

接通电源后,监测到稳定的 100ma 功耗,这让我心里有了底。随后我接好串口线,尝试了多种通信配置,看能否收到有意义的信号。最终调试成功,我获得了一条连续的数据流,且在我拨动摇杆或按动按键时数据会随之变化。事实证明,这些摇杆的运行速率为 9600 baud(每秒约 100 个字符),并以 8-bit 格式传输数据。

The image shows two dialog boxes from the "Game Controllers" settings interface in a Windows operating system. These settings are used to configure and test game controllers connected to the computer. The controller being used appears to be recognized as a "CircuitPython HID" device. Left Dialog Box: Title: "Game Controllers" This box lists the connected game controllers and their status. One controller is listed: "CircuitPython HID," which has a status of "OK," indicating that the controller is recognized and functioning correctly. Buttons include "Advanced" and "Properties" for further configuration or testing of the device. Right Dialog Box: Title: "CircuitPython HID properties" This is the "Test" tab for the game controller, allowing the user to test and calibrate various inputs of the controller. Axes Section: There are visual indicators for the X Axis/Y Axis, Z Axis, and Z Rotation. The test allows movement of the joystick to see how the axes respond in real time. Buttons Section: A grid of red circles represents buttons on the controller, where each circle lights up when a corresponding button on the joystick is pressed. Currently, all the button indicators are off. Point of View Hat Section: This section represents the directional pad (or "POV hat") found on some controllers, allowing for testing of its functionality. Overall, this image shows the controller has been successfully recognized by the system and is being tested using the built-in Windows game controller settings. The "CircuitPython HID" designation suggests the controller is using the CircuitPython library, commonly used for custom hardware projects like joysticks.
图 4:这是 Windows 系统中识别该控制器的样子。Raspberry Pi 也拥有类似的手柄接口协议。

既然已经获取了数据,下一步就是对其进行解码。我首先编写了一个Python程序来读取摇杆数据并将其输出。通过在PC上先进行搭建,我就能获得一套随后可以在使用CircuitPython的Pico上运行的代码。

梳理摇杆的控制逻辑后,我发现每个按钮对应数据中的一个比特位,而每个轴(操纵杆的X和Y方向移动)在输出中占用一个字节,范围从-128到127,并采用two’s complement编码。

制作游戏控制器

既然我已经能够从摇杆读取数据,接下来就需要将这些数据以标准游戏控制器的形式发送给计算机,以便诸如Microsoft Flight Simulator. 图3展示了我所追求的目标:我希望摇杆的所有功能都能暴露出来,以便在PC上使用。

Raspberry Pi Pico 是此处绝佳的硬件选择,因为它具备出色的IO选项且支持Python开发。我需要将其配置为通过USB向系统宣告自己为游戏控制器,然后调整代码使其能在Pico上运行并向PC发送相应的指令。

幸运的是,CircuitPython 对创建此类自定义设备提供了很好的支持。首先,我们需要定义设备在USB连接时的外观特征,并编写一些Python代码,确保每次Pico通过USB连接时都能正确注册。

The image shows a collection of electronic components and connectors laid out on a red surface, likely part of a hardware setup for interfacing with or powering a device. Key components include: USB Power Supply: A black external battery pack is powering the setup via a USB cable. The USB cable is connected to a small red "USB Power" module with an integrated display showing output values, including 4.94V and 0.18A. This module is likely monitoring the power draw of the connected components. DB9 Connectors: There are two separate DB9 (serial) connectors. One has a grey body and a blue breakout board at the top, while the other appears to be connected to a green terminal block for wiring. These connectors are typically used for RS-232 or other serial communication interfaces, often found in industrial or retro computing hardware. Breakout Boards: The setup includes at least two small breakout boards attached to the serial connectors. These boards seem to have small ICs and wiring that enable communication or power regulation between the connected devices. Wiring: Several wires, color-coded in yellow, black, and red, are connecting various parts of the setup. Some wires are plugged into a pin header at the left, likely for GPIO or serial communication. Overall Setup: The combination of serial connectors, breakout boards, and USB power monitoring suggests this setup is being used for testing or interfacing with an external device, perhaps an older serial-controlled system or custom hardware. The USB power module ensures that the components receive regulated power, while the serial connectors enable communication between devices. This configuration looks like a development or testing setup for a project involving serial communication and low-power electronics.
图5:桌上的系统布局。顶部是由USB电池供电的电源,左侧是通过USB连接到PC的Raspberry Pi Pico,中间是RS232接口,右侧是游戏端口适配器。

这需要深入钻研USB HID描述符的世界。HID代表Human Interface Device,涵盖了一系列可通过USB连接的设备,包括鼠标、键盘和游戏控制器,甚至延伸到音量控制和健身器材。

这些描述符包含了关于设备的详细信息,例如其类别以及它将向PC发送哪些类型的数据。默认情况下,CircuitPython仅支持鼠标和键盘。我们需要发送一个包含该控制器所支持的各种轴、按键和hats的描述符。

该规范的完整文档位于官方www.usb.org网站,但我们可以扩展Adafruit提供的示例,将按键数量增加到32,并为摇杆的hats添加额外的条目。

配置 boot.py

在构建CircuitPython项目时,你通常会在code.py文件中编写代码。该文件中的代码在Pico初始化USB之后才会运行,此时再修改设备属性就已经太晚了。要实现此目的,我们需要关注那个不太为人所知的boot.py文件。该文件在CircuitPython首次启动时运行,且在USB配置之前,这意味着你可以在该阶段修改已注册的USB设备属性。

The image shows the interior of what appears to be an aircraft cockpit, with a focus on two joysticks. These joysticks are positioned in front of a set of displays and control panels. Key elements in the image: Joysticks: The foreground joystick is similar to the one seen in previous images, with visible paint wear and a weathered appearance, revealing a lighter color underneath. It has multiple buttons and controls, including a prominent red button and a thumbstick on top. The background joystick is in better condition, with less visible wear and a more uniform appearance. It has similar controls, including buttons and knobs. Cockpit Displays: Behind the joysticks, there are large digital displays, some of which show what appears to be radar or navigation information. The green display on the left is likely a radar or targeting screen, while the right display shows a similar visual. Control Panels: The cockpit is filled with various buttons, switches, and dials, typical of a fighter jet or advanced aircraft. These controls are likely used for navigation, communication, and weapon systems. This image suggests the joysticks are part of an aircraft control system, likely in a military or aviation simulation environment. The worn joystick in the foreground might be an older or heavily used unit, while the cockpit setup and display screens indicate advanced avionics and targeting systems common in modern fighter jets or flight simulators.
图6:EMF摇杆与其虚拟对应的对照图。外观看起来与实物非常接近。

位于code.py文件中的程序负责从摇杆读取数据包,对数据进行解码以获取数值,并发送与摇杆状态相对应的USB消息。当前版本分为两个部分。第一部分将数据读取到缓冲区中,第二部分则从该缓冲区提取数值并发送USB消息。

整合所有模块

最后,我们需要将从PC串口读取数据的集成项目与我们的游戏控制器代码结合起来。由于Pico仅支持5v逻辑电平,我们可以使用自己的MAX232将RS232电压转换为Pico能够处理的电平。为此,我使用了一个集成模块,一端带有DB-9连接器,另一端为针脚排母,并将其连接到Pico。此外,我还可以直接从Pico的5v电源为摇杆供电。

既然我已经让项目的两部分协同工作了,接下来就是逐一检查设备上的按钮,并将它们映射到控制器定义的控制功能上。这是项目中比较繁琐的部分之一,但也是个确保一切正常运行的好机会。

The image shows a combination of a physical hardware setup and a simulation displayed on a monitor. Key elements: Monitor Display: The monitor displays a realistic aircraft cockpit, likely from a flight simulator. The cockpit features multiple digital screens with radar, navigation, and flight information, including a map, similar to what might be seen in a modern fighter jet. The design and layout of the cockpit are highly detailed, suggesting a realistic simulation environment, perhaps for military or aviation training or entertainment. Hardware Setup: In front of the monitor, there is a physical setup on a red desk, which includes: USB Power Module: A red USB power module similar to what was seen in previous images, connected to the external power source (a black battery pack) and other components. DB9 Serial Connectors and Breakout Boards: The serial connectors, wiring, and breakout boards are also present, part of an electronic interface setup, likely interfacing with the joystick and/or the simulator. Green Microcontroller Board: A small green board with headers and connections, possibly a microcontroller like a Raspberry Pi or an Arduino, is also connected to the system, likely controlling the joystick or interfacing with the simulator. Joystick: In the foreground, someone is holding the joystick (the same one seen in previous images). The person appears to be interacting with the simulation displayed on the monitor, suggesting that the joystick is being used as a control input for the flight simulator. The person is wearing gloves, which might be to protect the equipment or for comfort when handling the joystick during extended use. Context: This image seems to show a setup where a custom-built joystick (and related electronics) is being used to control a flight simulator. The combination of the joystick, the hardware setup on the desk, and the detailed simulator on the monitor points to a project involving custom hardware integration for flight control, possibly as part of a DIY or experimental setup. The physical electronics appear to enable the connection between the joystick and the simulator, allowing for real-time interaction with the simulated aircraft environment.
图7:操纵杆的实际操作效果

最后,我已经具备了使用操纵杆控制PC端飞机的所有条件。在这里,我使用它来控制包含在Flight Simulator,该机摇杆与此类似,只是有两个帽子开关位置互换了。我仍不确定这款操纵杆具体是基于哪款飞机(如果有的话)。AV-8 Harrier 的帽子开关顺序是正确的,但在操纵杆正面这两个开关下方多了一个按钮。

我四处都没能找到与第二种操纵杆相似的图片,所以如果你知道它可能来自哪里,请告诉我!

阅读完整故事,包括制作者 David Miles 提供的一些额外建议,请参阅The MagPi 第146期.

手持电脑

您现在就可以从特易购、森宝利、阿斯达、WHSmith等连锁超市以及其他报刊亭购买最新一期,包括Raspberry Pi Store在剑桥。它也可以在……获得我们的网上商店,可配送至全球各地。您也可以通过我们的应用程序获取安卓或者好文章,我们永远无法满足于寻址RGB事物 :D.

The image you provided is the cover of "The MagPi" magazine, issue 146, from October 2024. This magazine is dedicated to Raspberry Pi enthusiasts. The cover design is orange with black and white elements, featuring a retro horror theme. Some of the key elements on the cover include: The main headline, "PLAY RETRO HORROR CLASSICS ON RASPBERRY PI 5," likely highlighting a feature on retro horror games. The text "Police Line Do Not Cross" in several places, adding to the spooky, horror theme, possibly in reference to crime or mystery-themed games. The imagery of a crow, a spooky-looking house, a cassette tape, and various retro gaming motifs, reinforcing the horror and retro gaming aesthetic. Additional highlights like "LEGO Card Shuffler," "Top 10 Spooky Projects," and "Recycle a Fighter Jet Joystick," suggesting other tech and DIY projects featured in this issue. The bottom of the cover mentions "TURN IT UP TO 11 WITH AUDIO UPGRADES," hinting at content related to enhancing audio experiences. The overall theme seems focused on retro horror gaming and tech projects for Raspberry Pi.

您也可以订阅寄送至《The MagPi》纸质版。我们不仅提供全球配送,凡订阅六个月或十二个月纸质版的读者,还可免费获赠一台 Raspberry Pi Pico W!

3条评论

thagrol avatar

“由于Pico仅支持5v逻辑电平”。
不,其实不是。它是用于GPIO的3.3v设备。

Ashley Whittaker avatar

非常棒的指正。我们已在杂志的数字版中进行了修改。

TonkaF3 avatar

看起来像是 Tornado F3 操纵杆的前部(大)后部(小)

评论已关闭