Commit e0bec3b9 authored by 957dd's avatar 957dd

更新

parent 9c89baa5
......@@ -51,13 +51,20 @@
/* USER CODE BEGIN Variables */
/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
/* Definitions for main_Task */
osThreadId_t main_TaskHandle;
const osThreadAttr_t main_Task_attributes = {
.name = "main_Task",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for beat_task */
osThreadId_t beat_taskHandle;
const osThreadAttr_t beat_task_attributes = {
.name = "beat_task",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityLow,
};
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
......@@ -65,6 +72,7 @@ const osThreadAttr_t defaultTask_attributes = {
/* USER CODE END FunctionPrototypes */
void StartDefaultTask(void *argument);
void StartTask02(void *argument);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
......@@ -95,8 +103,11 @@ void MX_FREERTOS_Init(void) {
/* USER CODE END RTOS_QUEUES */
/* Create the thread(s) */
/* creation of defaultTask */
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
/* creation of main_Task */
main_TaskHandle = osThreadNew(StartDefaultTask, NULL, &main_Task_attributes);
/* creation of beat_task */
beat_taskHandle = osThreadNew(StartTask02, NULL, &beat_task_attributes);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
......@@ -126,6 +137,24 @@ void StartDefaultTask(void *argument)
/* USER CODE END StartDefaultTask */
}
/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the beat_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void *argument)
{
/* USER CODE BEGIN StartTask02 */
/* Infinite loop */
for(;;)
{
task_2_beatdata();
}
/* USER CODE END StartTask02 */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
......
......@@ -114,8 +114,8 @@ int main(void)
MX_TIM5_Init();
MX_TIM9_Init();
MX_TIM12_Init();
MX_SPI3_Init();
MX_TIM8_Init();
MX_SPI3_Init();
/* USER CODE BEGIN 2 */
RGB_Init();
SERVO_Init_All();
......@@ -134,7 +134,6 @@ int main(void)
printf("MPU6050 Software IIC Initializing...\r\n");
// 初始化MPU6050
MPU6050_Init();
Servo_360_Set_Angle(90);
/* USER CODE END 2 */
/* Init scheduler */
......
......@@ -94,7 +94,7 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
hdma_spi3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_spi3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_spi3_tx.Init.Mode = DMA_NORMAL;
hdma_spi3_tx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_spi3_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_spi3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_spi3_tx) != HAL_OK)
{
......
This diff is collapsed.
......@@ -49,11 +49,11 @@ void motor_right_front(int16_t speed) {
temp = -4200;
if (temp >= 0) {
PWM_Set_Duty(&htim1, TIM_CHANNEL_3, 4200);
PWM_Set_Duty(&htim1, TIM_CHANNEL_4, (4200 - temp));
} else {
PWM_Set_Duty(&htim1, TIM_CHANNEL_3, (4200 + temp));
PWM_Set_Duty(&htim1, TIM_CHANNEL_3, (4200 - temp));
PWM_Set_Duty(&htim1, TIM_CHANNEL_4, 4200);
} else {
PWM_Set_Duty(&htim1, TIM_CHANNEL_3, 4200);
PWM_Set_Duty(&htim1, TIM_CHANNEL_4, (4200 + temp));
}
}
......@@ -66,11 +66,11 @@ void motor_left_back(int16_t speed) {
temp = -4200;
if (temp >= 0) {
PWM_Set_Duty(&htim9, TIM_CHANNEL_1, 4200);
PWM_Set_Duty(&htim9, TIM_CHANNEL_2, (4200 - temp));
} else {
PWM_Set_Duty(&htim9, TIM_CHANNEL_1, (4200 + temp));
PWM_Set_Duty(&htim9, TIM_CHANNEL_1, (4200 - temp));
PWM_Set_Duty(&htim9, TIM_CHANNEL_2, 4200);
} else {
PWM_Set_Duty(&htim9, TIM_CHANNEL_1, 4200);
PWM_Set_Duty(&htim9, TIM_CHANNEL_2, (4200 + temp));
}
}
......@@ -98,6 +98,7 @@ void pwm_control_territory(uint16_t mode, uint16_t value) {
// 将 0-100 的速度映射到实际 PWM
int16_t speed = (int16_t)(value * MAX_PWM / 10);
switch (mode) {
case 0: // 停止
motor_left_front(0); motor_right_front(0);
......
......@@ -10,7 +10,7 @@ void SERVO_Init_All(void) {
}
void Servo_360_Set_Angle(float angle) {
void Servo_270_Set_Angle(float angle) {
// 1. 角度限幅
if (angle < 0.0f) angle = 0.0f;
if (angle > 270.0f) angle = 270.0f;
......
......@@ -2,6 +2,6 @@
#define __SERVO_H
void SERVO_Init_All(void);
void Servo_360_Set_Angle(float angle);
void Servo_270_Set_Angle(float angle);
#endif
......@@ -2,6 +2,7 @@
#include "pwm.h"
#include <string.h>
beatstatus_e beat_status = BEAT_UNKNOWN;
/**
* @brief 启动串口2接收(初始化调用)
*/
......@@ -16,15 +17,15 @@ void USART2_Config_Init(void)
* @param mode: 模式 (0-10)
* @param value: 速度值 (0-100)
*/
void USART2_Send_Command(uint8_t mode, uint8_t value)
void USART2_Send_Command(uint8_t data1, uint8_t data2, uint8_t data3, uint8_t data4, uint8_t data5)
{
uint8_t frame[8];
frame[0] = 0x01; // 帧头
frame[1] = mode; // 数据位1
frame[2] = value; // 数据位2
frame[3] = 0x00; // 预留
frame[4] = 0x00; // 预留
frame[5] = 0x00; // 预留
frame[1] = data1; // 数据位1
frame[2] = data2; // 数据位2
frame[3] = data3; // 预留
frame[4] = data4; // 预留
frame[5] = data5; // 预留
// 生成和校验 (Sum Check)
frame[6] = (uint8_t)((frame[1] + frame[2] + frame[3] + frame[4] + frame[5]) & 0xFF);
......@@ -59,12 +60,18 @@ void USART2_Process_Packet(uint8_t *pData, uint16_t Size)
// 2. 计算接收到的数据位和校验
uint8_t cal_check = (uint8_t)((pData[i+1] + pData[i+2] + pData[i+3] + pData[i+4] + pData[i+5]) & 0xFF);
// 3. 校验比对
if (cal_check == r_check)
{
if(r_check==0x00){
beat_status=BEAT_START;
printf("receive beat start\r\n");
}
printf("receive control start\r\n");
// 校验通过,执行麦轮控制逻辑
pwm_control_territory(r_mode, r_value);
// 如果一包里只有一帧数据,处理完可以直接返回
return;
}
......
......@@ -6,11 +6,18 @@
#include <string.h>
#include "usart_config.h"
// 定义枚举
typedef enum {
BEAT_STOP,
BEAT_START,
BEAT_UNKNOWN
}beatstatus_e;
extern beatstatus_e beat_status;
/* 函数声明 */
void USART2_Config_Init(void);
void USART2_Process_Packet(uint8_t *pData, uint16_t Size);
void USART2_Send_Command(uint8_t mode, uint8_t value);
void USART2_Send_Command(uint8_t data1, uint8_t data2, uint8_t data3, uint8_t data4, uint8_t data5);
#endif
......@@ -11,7 +11,6 @@
#include "servo.h"
int16_t acc[3], gyro[3];
float temperature;
float angle=90;
void task_1_main(void) {
Encoder_Update_All();
......@@ -20,8 +19,7 @@ void task_1_main(void) {
g_encoder[1].total_count, g_encoder[1].speed,
g_encoder[2].total_count, g_encoder[2].speed,
g_encoder[3].total_count, g_encoder[3].speed);
printf("VIN: %fV\r\n",Get_Vin_Voltage());
printf("VIN: %2.fV\r\n",Get_Vin_Voltage());
MPU6050_GetAccData(acc);
printf("acceleration: X=%d, Y=%d, Z=%d\r\n", acc[0], acc[1], acc[2]);
......@@ -30,13 +28,25 @@ void task_1_main(void) {
MPU6050_GetGyroData(gyro);
printf("gyroscope: X=%d, Y=%d, Z=%d\r\n", gyro[0], gyro[1], gyro[2]);
temperature = MPU6050_GetTempValue();
printf("temperature: %.2f\r\n", temperature);
printf("T: %2.f\r\n",MPU6050_GetTempValue());
angle+=30;
if(360<angle)angle=20;
Servo_360_Set_Angle(angle);
if(270<angle)angle=20;
Servo_270_Set_Angle(angle);
osDelay(2000); // 100ms读取一次
}
void task_2_beatdata(void){
int16_t temp_int = (int16_t)(MPU6050_GetTempValue() * 100.0f);
uint8_t temperature_low = (uint8_t)(temp_int & 0xFF); // 0x03
uint8_t temperature_high = (uint8_t)((temp_int >> 8) & 0xFF); // 0x0A
int16_t vin_int = (int16_t)(Get_Vin_Voltage()* 100.0f);
uint8_t vin_low = (uint8_t)(vin_int & 0xFF);
uint8_t vin_high = (uint8_t)((vin_int >> 8) & 0xFF);
if(beat_status == BEAT_START) USART2_Send_Command(0x00,temperature_high,temperature_low,vin_high,vin_low);
osDelay(3000); // 100ms读取一次
}
......@@ -7,5 +7,6 @@
#include "cmsis_os.h"
void task_1_main(void);
void task_2_beatdata(void);
#endif
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -21,15 +21,17 @@ Target DLL: STLink\ST-LINKIII-KEIL_SWO.dll V3.1.0.0
Dialog DLL: TCM.DLL V1.56.4.0
<h2>Project:</h2>
C:\Users\17122\Desktop\״ʦԻ\H60\project\MDK-ARM\project.uvprojx
Project File Date: 12/26/2025
C:\Users\17122\Desktop\robot\H60\project\MDK-ARM\project.uvprojx
Project File Date: 12/31/2025
<h2>Output:</h2>
*** Using Compiler 'V5.06 update 7 (build 960)', folder: 'D:\keil\ARM\ARMCC\Bin'
Build target 'project'
compiling servo.c...
compiling pwm.c...
compiling usart2.c...
compiling task_manage.c...
linking...
Program Size: Code=26468 RO-data=504 RW-data=172 ZI-data=21500
Program Size: Code=26716 RO-data=552 RW-data=176 ZI-data=21496
FromELF: creating hex file...
"..\debug\main.axf" - 0 Error(s), 0 Warning(s).
......@@ -55,7 +57,7 @@ Package Vendor: Keil
* Component: ARM::CMSIS:CORE:5.6.0
Include file: CMSIS/Core/Include/tz_context.h
Build Time Elapsed: 00:00:01
Build Time Elapsed: 00:00:03
</pre>
</body>
</html>
No preview for this file type
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
No preview for this file type
No preview for this file type
This diff is collapsed.
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -22,10 +22,11 @@ Dma.SPI3_TX.0.MemInc=DMA_MINC_ENABLE
Dma.SPI3_TX.0.Mode=DMA_NORMAL
Dma.SPI3_TX.0.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.SPI3_TX.0.PeriphInc=DMA_PINC_DISABLE
Dma.SPI3_TX.0.Priority=DMA_PRIORITY_HIGH
Dma.SPI3_TX.0.Priority=DMA_PRIORITY_LOW
Dma.SPI3_TX.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode
FREERTOS.IPParameters=Tasks01
FREERTOS.Tasks01=defaultTask,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL
FREERTOS.FootprintOK=true
FREERTOS.IPParameters=Tasks01,FootprintOK
FREERTOS.Tasks01=main_Task,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL;beat_task,8,128,StartTask02,Default,NULL,Dynamic,NULL,NULL
File.Version=6
GPIO.groupedBy=
KeepUserPlacement=false
......@@ -252,7 +253,7 @@ ProjectManager.ToolChainLocation=
ProjectManager.UAScriptAfterPath=
ProjectManager.UAScriptBeforePath=
ProjectManager.UnderRoot=false
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,4-MX_CAN1_Init-CAN1-false-HAL-true,5-MX_ADC1_Init-ADC1-false-HAL-true,6-MX_USART1_UART_Init-USART1-false-HAL-true,7-MX_USART2_UART_Init-USART2-false-HAL-true,8-MX_TIM1_Init-TIM1-false-HAL-true,9-MX_TIM2_Init-TIM2-false-HAL-true,10-MX_TIM3_Init-TIM3-false-HAL-true,11-MX_TIM4_Init-TIM4-false-HAL-true,12-MX_TIM5_Init-TIM5-false-HAL-true,13-MX_TIM9_Init-TIM9-false-HAL-true,14-MX_TIM12_Init-TIM12-false-HAL-true,15-MX_SPI3_Init-SPI3-false-HAL-true
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,4-MX_CAN1_Init-CAN1-false-HAL-true,5-MX_ADC1_Init-ADC1-false-HAL-true,6-MX_USART1_UART_Init-USART1-false-HAL-true,7-MX_USART2_UART_Init-USART2-false-HAL-true,8-MX_TIM1_Init-TIM1-false-HAL-true,9-MX_TIM2_Init-TIM2-false-HAL-true,10-MX_TIM3_Init-TIM3-false-HAL-true,11-MX_TIM4_Init-TIM4-false-HAL-true,12-MX_TIM5_Init-TIM5-false-HAL-true,13-MX_TIM9_Init-TIM9-false-HAL-true,14-MX_TIM12_Init-TIM12-false-HAL-true,15-MX_TIM8_Init-TIM8-false-HAL-true,16-MX_SPI3_Init-SPI3-false-HAL-true
RCC.48MHZClocksFreq_Value=84000000
RCC.AHBFreq_Value=168000000
RCC.APB1CLKDivider=RCC_HCLK_DIV4
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment