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

基于Phyphox的F-16战斗机座舱模拟Python代码窗口短暂弹出即关闭问题排查请求

Troubleshooting Your F-16 Cockpit Simulation Crash

Let's break down why your window closes immediately and fix the issues one by one:

1. Critical Syntax Error (Immediate Crash Cause)

The most obvious culprit is a typo in your HUD print line that breaks valid Python syntax:

print(f" SPD: {speed_knots:3.0f} KTS | V-ANGLE: {data['pitch'] consultancy:+2.0f} | ALT: {alt_ft:5.0f} FT")

The random consultancy word is misplaced here—this will cause an instant syntax error that crashes your script before anything renders. Fix it to:

print(f" SPD: {speed_knots:3.0f} KTS | V-ANGLE: {data['pitch']:+2.0f} | ALT: {alt_ft:5.0f} FT")

2. Stop Swallowing Errors (Debug Visibility)

Your get_data function uses a bare except: which hides all errors—you can't tell if there's a connection issue, missing sensor data, or other problem. Replace it with specific exception handling to see what's going wrong:

def get_data():
    try:
        url = f"{PHYPHOX_URL}/get?acc&att&light&location"
        r = requests.get(url, timeout=0.5).json()['buffer']
        return {
            "acc": r['acc']['buffer'][0],
            "pitch": r['att']['buffer'][0],
            "roll": r['att']['buffer'][1],
            "lux": r['light']['buffer'][0],
            "lat": r['location']['buffer'][0] if 'location' in r else 0.0,
            "lon": r['location']['buffer'][1] if 'location' in r else 0.0,
            "alt_gps": r['location']['buffer'][2] if 'location' in r else 0.0,
            "speed_gps": r['location']['buffer'][3] if 'location' in r else 0.0
        }
    except requests.exceptions.RequestException as e:
        print(f"Connection error: {e}")
        return None
    except KeyError as e:
        print(f"Missing sensor data: {e}")
        return None
    except Exception as e:
        print(f"Unexpected error: {e}")
        return None

This way, you'll get clear feedback if something fails during data fetching.

3. Run from a Terminal (Not Double-Click)

If you're launching the script by double-clicking the .py file, any errors will close the window instantly without showing you details. Instead:

  • Open a command prompt/terminal
  • Navigate to your script's folder
  • Run it with python your_script_name.py
    This keeps the window open so you can see error messages or the cockpit display.

4. Verify Phyphox Connection & Permissions

  • Ensure your phone (running Phyphox) and computer are on the same Wi-Fi network
  • Double-check PHYPHOX_URL matches the IP shown in Phyphox's web server settings
  • Confirm Phyphox has permissions to access all requested sensors (location, accelerometer, etc.)
  • Test the URL directly in your browser: http://192.168.1.55:8080/get?acc&att&light&location—you should see JSON data if everything is working.

5. Minor: Fix AOA Calculation (Placeholder Note)

Right now you're using pitch for Angle of Attack (AOA), which isn't accurate (AOA is the angle between the wing chord and oncoming air). For now, add a note to remind yourself this is a placeholder, or plan to calculate it using accelerometer/gyro data later.

Final Fixed Code

Here's the script with the syntax error fixed and improved error handling:

import requests
import time
import os

# --- CONFIGURATION ---
PHYPHOX_URL = "http://192.168.1.55:8080"

def get_data():
    try:
        url = f"{PHYPHOX_URL}/get?acc&att&light&location"
        r = requests.get(url, timeout=0.5).json()['buffer']
        return {
            "acc": r['acc']['buffer'][0],
            "pitch": r['att']['buffer'][0],
            "roll": r['att']['buffer'][1],
            "lux": r['light']['buffer'][0],
            "lat": r['location']['buffer'][0] if 'location' in r else 0.0,
            "lon": r['location']['buffer'][1] if 'location' in r else 0.0,
            "alt_gps": r['location']['buffer'][2] if 'location' in r else 0.0,
            "speed_gps": r['location']['buffer'][3] if 'location' in r else 0.0
        }
    except requests.exceptions.RequestException as e:
        print(f"Connection error: {e}")
        return None
    except KeyError as e:
        print(f"Missing sensor data: {e}")
        return None
    except Exception as e:
        print(f"Unexpected error: {e}")
        return None

def main():
    while True:
        data = get_data()
        if data:
            # Convert units
            speed_knots = data['speed_gps'] * 1.94384
            alt_ft = data['alt_gps'] * 3.28084
            
            # Render cockpit
            os.system('cls' if os.name == 'nt' else 'clear')
            print(f"\033[92m") # Green HUD color
            print(f" SPD: {speed_knots:3.0f} KTS | V-ANGLE: {data['pitch']:+2.0f} | ALT: {alt_ft:5.0f} FT")
            print(f" " + "—"*50)
            print(f" Pitch: {data['pitch']:+4.1f}°")
            print(f" Roll: {data['roll']:+4.1f}°")
            print(f" AOA: {data['pitch']:4.1f}°  # Placeholder, not actual AOA")
            print(f" " + "—"*50)
            # MFD Section
            print(f"\n [ G-LOAD ] [ AMBIENT ] [ NAVIGATION ]")
            print(f" ---------- ----------- --------------")
            print(f" {data['acc']/9.81:4.2f} G {data['lux']:5.0f} LUX LAT: {data['lat']:.4f}")
            print(f" LON: {data['lon']:.4f}")
            print(f" ---------- ----------- --------------")
            print(f"\033[0m") # Reset color
        time.sleep(0.1)

if __name__ == "__main__":
    main()

Try these fixes, run the script from a terminal, and you should either see your cockpit display or get clear errors to debug further.

内容的提问来源于stack exchange,提问作者theguy inurcloset

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 06:40:09