You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino Pro Micro飞行外设代码求助:适配X-Plane 11独立运行

飞行模拟外设Arduino代码调试与优化需求

背景

本人为航空专业学生,编程能力有限,基于Arduino Pro Micro及配套传感器/开关制作飞行模拟外设,目标是让该外设独立于摇杆、踏板等其他设备与PC交互,主要适配X-Plane 11,同时希望兼容微软飞行模拟器。

现有硬件包括3个10kΩ滑动电位器,需要将它们的输出转换为百分比值,用于绑定油门(Throttle)、螺旋桨转速(Prop)、油气混合比(Mixture)等模拟器控制项;此前通过绑定键盘按键实现开关及旋钮功能,虽能正常运行但方案不够优雅,现寻求对以下代码的调试与优化支持。

当前代码

#include <Arduino.h>
#include <Joystick.h>   // From the NicoHood HID library
#include <Bounce2.h>    // For debouncing toggle and rotary switches

// Create a Joystick instance
Joystick_ Joystick;

// ----- Pin Definitions -----
// Sliding potentiometers (acting as voltage dividers)
// Throttle (100mm) on A0, Angle-of-Attack (75mm) on A1, Mixture on A2
const int throttlePin = A0;
const int aoaPin      = A1;
const int mixturePin  = A2;

// Calibration values for the throttle potentiometer
// (Assuming 0 ohm corresponds to an analog read of 0 and 10kΩ corresponds to 1023)
const int throttleAnalogMin = 0;
const int throttleAnalogMax = 1023;

// Three toggle switches (wired as on/off switches, ON when connected to GND)
// 1. Master Switch (Airplane Power)
// 2. Comm/Nav Equipment
// 3. PFD/ATX (example like the Xbox "X" button)
// Connected to digital pins D2, D3, and D4 respectively.
const int togglePins[3] = {2, 3, 4};
Bounce toggleDebouncers[3];

// Ignition rotary switch with 5 positions (only one output active at a time)
// Positions are connected to digital pins D5, D6, D7, D8, and D9.
const int ignitionPins[5] = {5, 6, 7, 8, 9};
Bounce ignitionDebouncers[5];

void setup() {
  // Initialize the USB HID joystick interface
  Joystick.begin();

  // Set up toggle switches with internal pull-ups and debouncing
  for (int i = 0; i < 3; i++) {
    pinMode(togglePins[i], INPUT_PULLUP);
    toggleDebouncers[i].attach(togglePins[i]);
    toggleDebouncers[i].interval(25); // 25 ms debounce interval
  }
  
  // Set up ignition rotary switch pins with internal pull-ups and debouncing
  for (int i = 0; i < 5; i++) {
    pinMode(ignitionPins[i], INPUT_PULLUP);
    ignitionDebouncers[i].attach(ignitionPins[i]);
    ignitionDebouncers[i].interval(25);
  }
}

void loop() {
  // --- Process Throttle Potentiometer ---
  int throttleVal = analogRead(throttlePin);
  // Map the raw analog value (0 to 1023) to a percentage (0% to 100%)
  int throttlePercent = map(throttleVal, throttleAnalogMin, throttleAnalogMax, 0, 100);
  throttlePercent = constrain(throttlePercent, 0, 100);
  // Send the throttle percentage to the joystick X-axis
  Joystick.setXAxis(throttlePercent);

  // --- Process Angle-of-Attack and Mixture (raw values; adjust as needed) ---
  int aoaVal     = analogRead(aoaPin);
  int mixtureVal = analogRead(mixturePin);
  Joystick.setYAxis(aoaVal);   // e.g., map AoA to the Y-axis
  Joystick.setZAxis(mixtureVal); // e.g., map Mixture to the Z-axis
  
  // --- Process Toggle Switches ---
  // With internal pull-ups enabled, a LOW reading indicates the switch is ON.
  // These are mapped to joystick buttons 0, 1, and 2.
  for (int i = 0; i < 3; i++) {
    toggleDebouncers[i].update();
    bool toggleState = (toggleDebouncers[i].read() == LOW);
    Joystick.setButton(i, toggleState);
  }
  
  // --- Process Ignition Rotary Switch ---
  // Map the 5 positions as follows:
  // Position 1 (Off)    → Hat = -1 (centered)
  // Position 2 (Left)   → Hat = 270°
  // Position 3 (Right)  → Hat = 90°
  // Position 4 (Both)   → Hat = 180°
  // Position 5 (Starter)→ Hat = 0°
  int hatValue = -1;  // Default to Off (centered)
  for (int i = 0; i < 5; i++) {
    ignitionDebouncers[i].update();
    if (ignitionDebouncers[i].read() == LOW) { // Active state detected
      if (i == 0) {
        hatValue = -1;    // Off
      } else if (i == 1) {
        hatValue = 270;   // Left
      } else if (i == 2) {
        hatValue = 90;    // Right
      } else if (i == 3) {
        hatValue = 180;   // Both
      } else if (i == 4) {
        hatValue = 0;     // Starter
      }
      break; // Only one position should be active at a time
    }
  }
  Joystick.setHat(hatValue);
  
  delay(20);  // Short delay to limit USB report rate
}

核心优化需求

  • 将3个10kΩ滑动电位器的输出统一转换为0-100%的百分比值,分别对应油门、螺旋桨转速、油气混合比等模拟控制项
  • 优化开关与旋钮的HID适配逻辑:3个拨动开关、5档点火旋转开关的输入处理需更可靠,替代原键盘按键绑定方案
  • 确保外设可独立于其他飞行模拟外设(如摇杆、踏板)与PC建立HID连接,稳定适配X-Plane 11,同时兼容微软飞行模拟器

注:引脚布局图片仅作参考

内容的提问来源于stack exchange,提问作者hetot he

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:24:52