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/pwm_ctrol.c | 221 +++++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 205 insertions(+), 16 deletions(-)
diff --git a/STM32H743/FML/pwm_ctrol.c b/STM32H743/FML/pwm_ctrol.c
index a28c8b3..263c44c 100644
--- a/STM32H743/FML/pwm_ctrol.c
+++ b/STM32H743/FML/pwm_ctrol.c
@@ -2,15 +2,29 @@
#include "stdarg.h"
#include "string.h"
#include "pwm_ctrol.h"
-#include "mainex.h"
#include "bluetooth.h"
+#include "stm32h7xx_it.h"
+#include "stm32h7xx_hal.h"
+#include "main.h"
+#include "mainex.h"
+#include "DBG.h"
+#include "Uart.h"
+#include "HIDO_Util.h"
+#include "SBUS.h"
+
+#define STATE_WAIT_RISING 0
+#define STATE_WAIT_FALLING 1
+uint32_t rising_time = 0;
+uint32_t falling_time = 0;
+uint32_t pulse_width_us = 0;
+uint8_t capture_state = STATE_WAIT_RISING;;
-
-// 设置所有电机到指定占空比
-void set_all_pwm(uint16_t duty) {
+// 锟斤拷锟斤拷锟斤拷锟叫碉拷锟斤拷锟街革拷锟秸硷拷毡锟�
+void set_all_pwm(uint16_t duty)
+{
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, duty);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, duty);
__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, duty);
@@ -21,18 +35,193 @@
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, duty);
}
-// 向左:前轮左转,后轮右转
-void set_pwm_left() {
- __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 1000); // 前左
- __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 2000); // 前右
- __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 2000); // 后左
- __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 1000); // 后右
+// 锟斤拷锟斤拷前锟斤拷锟斤拷转锟斤拷锟斤拷锟斤拷锟斤拷转
+void set_pwm_left()
+{
+ __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 1000); // 前锟斤拷
+ __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 2000); // 前锟斤拷
+ __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 2000); // 锟斤拷锟斤拷
+ __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 1000); // 锟斤拷锟斤拷
}
-// 向右:前轮右转,后轮左转
-void set_pwm_right() {
- __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 2000); // 前左
- __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 1000); // 前右
- __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1000); // 后左
- __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 2000); // 后右
+// 锟斤拷锟揭o拷前锟斤拷锟斤拷转锟斤拷锟斤拷锟斤拷锟斤拷转
+void set_pwm_right()
+{
+ __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 2000); // 前锟斤拷
+ __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 1000); // 前锟斤拷
+ __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1000); // 锟斤拷锟斤拷
+ __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 2000); // 锟斤拷锟斤拷
}
+
+
+// 映锟戒函锟斤拷锟斤拷锟斤拷 [-100,100] 映锟戒到 [1000,2000]
+uint32_t Map(int16_t input, int16_t in_min, int16_t in_max, uint32_t out_min, uint32_t out_max)
+{
+ return (input - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+// 锟斤拷锟矫碉拷锟� PWM锟斤拷前锟斤拷/锟斤拷锟剿o拷
+void Set_Motor_PWM(int16_t speed)
+{
+ static HIDO_UINT8 l_Motor[50];
+ uint32_t pulse = Map(speed, -100, 100, 1000, 2000); // -100~100 锟斤拷 1000~2000
+ __HAL_TIM_SetCompare(&MOTOR_TIM, MOTOR_CHANNEL, pulse);
+ HIDO_UtilSnprintf((HIDO_CHAR *)l_Motor, sizeof(l_Motor), "Motor cortrol:speed=%d,pulse=%d\r\n", speed,pulse);
+ Uart_Send(UART_ID_DBG, (HIDO_UINT8 *)l_Motor, strlen(l_Motor));
+}
+
+// 锟斤拷锟斤拷转锟斤拷 PWM锟斤拷锟斤拷转/锟斤拷转锟斤拷
+void Set_Steering_PWM(int16_t steer)
+{
+ static HIDO_UINT8 l_Steering[50];
+ uint32_t pulse = Map(steer, -100, 100, 1000, 2000); // -100~100 锟斤拷 1000~2000
+ __HAL_TIM_SetCompare(&STEERING_TIM, STEERING_CHANNEL, pulse);
+ HIDO_UtilSnprintf((HIDO_CHAR *)l_Steering, sizeof(l_Steering), "Steering cortrol:steer=%d,pulse=%d\r\n", steer,pulse);
+ Uart_Send(UART_ID_DBG, (HIDO_UINT8 *)l_Steering, strlen(l_Steering));
+
+}
+
+void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
+{
+ static HIDO_UINT8 l_pulse_width[20];
+
+ if (htim->Instance == TIM4)
+ {
+ uint32_t current_time = htim->Instance->CCR1;
+
+ if (capture_state == STATE_WAIT_RISING)
+ {
+ // 锟斤拷前锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷 锟斤拷录锟斤拷锟叫伙拷锟斤拷锟饺达拷锟铰斤拷锟斤拷
+ rising_time = current_time;
+ // 锟叫伙拷为锟铰斤拷锟截达拷锟斤拷
+ htim->Instance->CCER &= ~TIM_CCER_CC1P; // 锟斤拷锟斤拷锟斤拷
+ htim->Instance->CCER |= TIM_CCER_CC1NP; // 锟斤拷锟斤拷 NP 锟斤拷示锟角凤拷锟洁?实锟斤拷应使锟矫硷拷锟皆匡拷锟狡猴拷锟斤拷
+
+ // 锟斤拷锟狡硷拷使锟斤拷 HAL 锟斤拷锟斤拷锟斤拷锟矫硷拷锟斤拷
+ __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING);
+
+ capture_state = STATE_WAIT_FALLING;
+ }
+ else if (capture_state == STATE_WAIT_FALLING)
+ {
+ // 锟斤拷前锟斤拷锟铰斤拷锟斤拷 锟斤拷 锟斤拷锟斤拷叩锟狡斤拷锟斤拷锟�
+ uint32_t pulse_width = current_time - rising_time;
+
+ if (pulse_width > 65535) // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值
+ {
+ pulse_width = 0; // 锟斤拷锟竭憋拷锟轿拷锟叫�
+ }
+
+ //printf("High Pulse Width: %lu 锟斤拷s\n", pulse_width);
+ HIDO_UtilSnprintf((HIDO_CHAR *)l_pulse_width, sizeof(l_pulse_width), "pulse_width=%d\r\n", pulse_width);
+ Uart_Send(UART_ID_DBG, (HIDO_UINT8 *)l_pulse_width, strlen(l_pulse_width));
+ // 锟叫伙拷锟斤拷锟斤拷锟截达拷锟斤拷
+ __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
+ capture_state = STATE_WAIT_RISING;
+ }
+
+ // 锟斤拷锟斤拷卸媳锟街�
+ __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
+ }
+}
+uint32_t steering_pulse,motor_pulse;
+void SBUS_Control_PWM(void)
+{
+ ST_SBUSData sbusData;
+
+ // 鑾峰彇 SBUS 鏁版嵁
+ if (SBUS_GetData(&sbusData) != HIDO_TRUE)
+ {
+ return; // 娌℃湁鏈夋晥鏁版嵁
+ }
+
+ // CH0: 杞悜閫氶亾 (200-1800) -> (1000-2000us)
+ uint16_t ch0 = sbusData.m_au16Channels[0];
+ if (ch0 < 200) ch0 = 200;
+ if (ch0 > 1800) ch0 = 1800;
+ steering_pulse = ((ch0 - 200) * 1000) / 1600 + 1000;
+
+ // CH1: 琛岃繘閫氶亾 (200-1800) -> (1000-2000us)
+ uint16_t ch1 = sbusData.m_au16Channels[1];
+ if (ch1 < 200) ch1 = 200;
+ if (ch1 > 1800) ch1 = 1800;
+ motor_pulse = ((ch1 - 200) * 1000) / 1600 + 1000;
+
+ // 璁剧疆 PWM 杈撳嚭
+ __HAL_TIM_SetCompare(&STEERING_TIM, STEERING_CHANNEL, steering_pulse);
+ __HAL_TIM_SetCompare(&MOTOR_TIM, MOTOR_CHANNEL, motor_pulse);
+}
+
+// 鐩存帴璁剧疆鑴夊锛堝井绉掞級
+void Set_Steering_Pulse(uint32_t pulse_us)
+{
+ steering_pulse = pulse_us;
+ __HAL_TIM_SetCompare(&STEERING_TIM, STEERING_CHANNEL, steering_pulse);
+}
+
+void Set_Motor_Pulse(uint32_t pulse_us)
+{
+ motor_pulse = pulse_us;
+ __HAL_TIM_SetCompare(&MOTOR_TIM, MOTOR_CHANNEL, motor_pulse);
+}
+#if 0
+/**
+ * @brief Update Callback (for overflow protection)
+ */
+void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
+{
+ if (htim->Instance == TIM4)
+ {
+ // 只锟节筹拷时锟斤拷锟斤拷锟斤拷应时锟斤拷锟斤拷锟斤拷状态
+ static uint32_t last_reset_ms = 0;
+
+ if (HAL_GetTick() - last_reset_ms > 100) // 100ms 锟斤拷时
+ {
+ capture_state = 0;
+ __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
+ last_reset_ms = HAL_Get_Tick();
+ printf("TIM4 Overflow Reset (timeout)\r\n");
+ }
+ }
+}
+
+#endif
+#if 0
+void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
+{
+ if (htim->Instance == TIM4 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
+ {
+ uint32_t current_value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
+
+ if (capture_state == 0)
+ {
+ // 锟斤拷锟斤拷锟截o拷锟斤拷录锟斤拷始时锟斤拷
+ rising_time = current_value;
+ printf("Rising Edge: %lu\r\n", rising_time);
+
+ // 锟叫伙拷为锟铰斤拷锟截硷拷锟�
+ __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING);
+ capture_state = 1;
+ }
+ else if (capture_state == 1)
+ {
+ // 锟铰斤拷锟截o拷锟斤拷锟斤拷锟斤拷锟斤拷
+ falling_time = current_value;
+ pulse_width_us = falling_time - rising_time;
+
+ // 锟斤拷止锟斤拷锟斤拷锟斤拷锟斤拷止锟斤拷锟斤拷锟�
+ if (pulse_width_us > 65535) // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值
+ {
+ pulse_width_us = 0; // 锟斤拷锟竭憋拷锟轿拷锟叫�
+ }
+
+ printf("Falling Edge: %lu\r\n", falling_time);
+ printf("Pulse Width: %lu 锟斤拷s\r\n", pulse_width_us);
+
+ // 锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟截o拷锟饺达拷锟斤拷一锟斤拷锟斤拷锟斤拷
+ __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
+ capture_state = 0;
+ }
+ }
+}
+#endif
+
--
Gitblit v1.9.3