From 26db5e14522173c274ac954c867d2ebe5d8ca3ac Mon Sep 17 00:00:00 2001
From: yincheng.zhong <634916154@qq.com>
Date: 星期二, 18 十一月 2025 19:43:53 +0800
Subject: [PATCH] python和STM32数据双向调通,准备下一阶段GPS测试。

---
 STM32H743/FML/SBUS.c |   58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 57 insertions(+), 1 deletions(-)

diff --git a/STM32H743/FML/SBUS.c b/STM32H743/FML/SBUS.c
index 4841598..a957b50 100644
--- a/STM32H743/FML/SBUS.c
+++ b/STM32H743/FML/SBUS.c
@@ -15,6 +15,7 @@
 #include "stm32h7xx_hal_dma.h"
 #include <string.h>
 #include "pwm_ctrol.h"
+#include "PythonLink.h"
 
 /*******************************************************************************
  *                                  Macro                                      *
@@ -48,6 +49,9 @@
 static HIDO_UINT32 g_u32DebugLastDmaRemaining = 0;
 // SBUS failsafe active flag
 static volatile HIDO_UINT8 g_bSBUSFailsafeActive = 0;
+
+// 鎺у埗妯″紡鐘舵�� (0=鎵嬪姩閬ユ帶鍣�, 1=鑷姩Python)
+static volatile HIDO_UINT8 g_u8ControlMode = 0;
 
 /*******************************************************************************
  *                             Local Function                                  *
@@ -106,13 +110,33 @@
     g_stSBUSData.m_u32LastUpdateTick = HAL_GetTick();
     g_stSBUSData.m_u32FrameCount++;
 
+    // 5. 妫�鏌� CH8 (閫氶亾7, 绱㈠紩7) 鍐冲畾鎺у埗妯″紡
+    // CH8 > 1500: 鑷姩妯″紡 (Python鎺у埗)
+    // CH8 < 1500: 鎵嬪姩妯″紡 (閬ユ帶鍣ㄦ帶鍒�)
+    HIDO_UINT16 ch8_value = g_stSBUSData.m_au16Channels[7];
+    if (ch8_value > 1500)
+    {
+        // 鑷姩妯″紡: 涓嶆洿鏂� PWM, 鐢� Python 鎺у埗
+        g_u8ControlMode = 1;
+    }
+    else
+    {
+        // 鎵嬪姩妯″紡: 浣跨敤閬ユ帶鍣ㄤ俊鍙锋帶鍒�
+        g_u8ControlMode = 0;
+    }
+
     // 瑙f瀽鎴愬姛鍚庣珛鍗宠Е鍙� PWM 鎺у埗锛堟渶灏忓欢杩燂級
     // 鍚屾椂娓呴櫎 failsafe 鏍囧織锛堝鏋滀箣鍓嶈Е鍙戣繃锛�
     if (g_bSBUSFailsafeActive)
     {
         g_bSBUSFailsafeActive = 0;
     }
-    SBUS_Control_PWM();
+    
+    // 鍙湁鍦ㄦ墜鍔ㄦā寮忎笅鎵嶆洿鏂� PWM
+    if (g_u8ControlMode == 0)
+    {
+        SBUS_Control_PWM();
+    }
 
     return HIDO_TRUE;
 }
@@ -283,6 +307,28 @@
             g_bSBUSFailsafeActive = 1;
         }
     }
+    
+    // 鑷姩妯″紡: 搴旂敤Python涓嬪彂鐨勬帶鍒跺懡浠�
+    if (g_u8ControlMode == 1)
+    {
+        HIDO_UINT16 steering_pwm = 1500;
+        HIDO_UINT16 throttle_pwm = 1500;
+        HIDO_UINT32 cmd_timestamp = 0;
+        
+        // 鑾峰彇Python鎺у埗鍛戒护
+        if (PythonLink_GetControl(&steering_pwm, &throttle_pwm, &cmd_timestamp) == HIDO_TRUE)
+        {
+            // 搴旂敤鎺у埗鍛戒护鍒癙WM杈撳嚭
+            Set_Steering_Pulse(steering_pwm);
+            Set_Motor_Pulse(throttle_pwm);
+        }
+        else
+        {
+            // 娌℃湁鏈夋晥鐨凱ython鍛戒护鏃讹紝浣跨敤瀹夊叏鐨勪腑浣嶅��
+            Set_Steering_Pulse(1500);
+            Set_Motor_Pulse(1500);
+        }
+    }
 }
 
 /**
@@ -430,3 +476,13 @@
     }
     HIDO_Debug2("====================================\r\n\r\n");
 }
+
+/**
+ * @brief 鍒ゆ柇鏄惁澶勪簬鑷姩妯″紡
+ * @return HIDO_TRUE: 鑷姩妯″紡 (CH8 > 1500, Python鎺у埗)
+ *         HIDO_FALSE: 鎵嬪姩妯″紡 (CH8 < 1500, 閬ユ帶鍣ㄦ帶鍒�)
+ */
+HIDO_BOOL SBUS_IsAutoMode(HIDO_VOID)
+{
+    return (g_u8ControlMode == 1) ? HIDO_TRUE : HIDO_FALSE;
+}

--
Gitblit v1.9.3