修改move_xr结构
This commit is contained in:
parent
3333d5daa0
commit
9f7d7820ae
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"ads8864.h": "c"
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
17
User/bsp/bsp.h
Normal file
17
User/bsp/bsp.h
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define BSP_OK (0)
|
||||
#define BSP_ERR (-1)
|
||||
#define BSP_ERR_NULL (-2)
|
||||
#define BSP_ERR_INITED (-3)
|
||||
#define BSP_ERR_NO_DEV (-4)
|
||||
|
||||
#define SIGNAL_BSP_USB_BUF_RECV (1u << 0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -0,0 +1,19 @@
|
||||
// /* Includes ----------------------------------------------------------------- */
|
||||
// #include "bsp\can.h"
|
||||
// #include "can.h" // 包含 hcan 的定义
|
||||
|
||||
// /* Private define ----------------------------------------------------------- */
|
||||
// /* Private macro ------------------------------------------------------------ */
|
||||
// /* Private typedef ---------------------------------------------------------- */
|
||||
// /* Private variables -------------------------------------------------------- */
|
||||
// /* Private function -------------------------------------------------------- */
|
||||
|
||||
// /* Exported functions ------------------------------------------------------- */
|
||||
// CAN_HandleTypeDef *BSP_CAN_GetHandle(BSP_CAN_t can) {
|
||||
// switch (can) {
|
||||
// case BSP_CAN:
|
||||
// return &hcan; // 返回全局定义的 hcan 实例
|
||||
// default:
|
||||
// return NULL; // 未知的 CAN 枚举值
|
||||
// }
|
||||
// }
|
@ -0,0 +1,15 @@
|
||||
// #pragma once
|
||||
|
||||
// /* Includes ----------------------------------------------------------------- */
|
||||
// #include <can.h>
|
||||
// #include "bsp/bsp.h"
|
||||
|
||||
// /* Exported constants ------------------------------------------------------- */
|
||||
// /* Exported macro ----------------------------------------------------------- */
|
||||
// /* Exported types ----------------------------------------------------------- */
|
||||
// typedef enum {
|
||||
// BSP_CAN,
|
||||
// } BSP_CAN_t;
|
||||
|
||||
// /* Exported functions prototypes -------------------------------------------- */
|
||||
// CAN_HandleTypeDef *BSP_CAN_GetHandle(BSP_CAN_t can);
|
@ -1,6 +1,5 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "bsp\led.h"
|
||||
// #include "stm32f3xx.h"
|
||||
#include "gpio.h"
|
||||
#include "tim.h"
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
|
@ -27,6 +27,4 @@ typedef enum
|
||||
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
|
||||
int8_t BSP_LED_SET(BSP_LED_Channel_t ch, BSP_LED_Status_t s);
|
||||
int8_t BSP_WS2812_Set(uint8_t red, uint8_t green, uint8_t blue);
|
||||
int8_t BSP_WS2812_Init(void);
|
||||
int8_t BSP_LED_SET(BSP_LED_Channel_t ch, BSP_LED_Status_t s);
|
@ -0,0 +1,26 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "bsp\spi.h"
|
||||
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
/* Private function -------------------------------------------------------- */
|
||||
static BSP_SPI_t SPI_Get(SPI_HandleTypeDef *hspi) {
|
||||
if (hspi->Instance == SPI2)
|
||||
return BSP_SPI_ADC;
|
||||
else
|
||||
return BSP_ERR;
|
||||
}
|
||||
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
SPI_HandleTypeDef *BSP_SPI_GetHandle(BSP_SPI_t spi) {
|
||||
switch (spi) {
|
||||
case BSP_SPI_ADC:
|
||||
return &hspi2;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <spi.h>
|
||||
|
||||
#include "bsp/bsp.h"
|
||||
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
|
||||
/* 要添加使用SPI的新设备,需要先在此添加对应的枚举值 */
|
||||
|
||||
/* SPI实体枚举,与设备对应 */
|
||||
typedef enum {
|
||||
BSP_SPI_ADC,
|
||||
} BSP_SPI_t;
|
||||
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
SPI_HandleTypeDef *BSP_SPI_GetHandle(BSP_SPI_t spi);
|
||||
|
12
User/component/user_math.c
Normal file
12
User/component/user_math.c
Normal file
@ -0,0 +1,12 @@
|
||||
/*
|
||||
自定义的数学运算。
|
||||
*/
|
||||
|
||||
#include "user_math.h"
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
float Adc_to_Distance(uint16_t adc_data) {
|
||||
// return (float)((adc_data * 1000) / 4095);
|
||||
return (float)((adc_data * 1000) / 4095.0f); // 使用浮点数进行计算
|
||||
}
|
20
User/component/user_math.h
Normal file
20
User/component/user_math.h
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
自定义的数学运算。
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846f
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief 用于计算ADC数据对应的距离(mm)
|
||||
* @param adc_data
|
||||
* @return
|
||||
*/
|
||||
float Adc_to_Distance(uint16_t adc_data);
|
@ -1,9 +1,10 @@
|
||||
#include "ads8864.h"
|
||||
#include "spi.h"
|
||||
#include "gpio.h"
|
||||
|
||||
#include "bsp/spi.h"
|
||||
#include <string.h>
|
||||
#include "component/user_math.h"
|
||||
/* Private variables --------------------------------------------------------- */
|
||||
// static Ads8864_t ads8864_data = {0}; // 全局的ADC数据结构体
|
||||
// static Ads8864_t ads8864 = {0}; // 全局的ADC数据结构体
|
||||
|
||||
/* Private function prototypes ----------------------------------------------- */
|
||||
static uint16_t Ads8864_Read_Internal(void);
|
||||
@ -15,30 +16,37 @@ static void Ads8864_Update_Filtered_Data(void);
|
||||
* @note 初始化ADC相关引脚
|
||||
* @retval None
|
||||
*/
|
||||
void ads8864_Init(void) {
|
||||
HAL_GPIO_WritePin(GPIOB, DIN_Pin, GPIO_PIN_SET); // 设置DIN引脚为高电平
|
||||
HAL_GPIO_WritePin(GPIOB, CONVST_Pin, GPIO_PIN_RESET); // 设置CS引脚为低电平
|
||||
// void ads8864_Init(void) {
|
||||
// HAL_GPIO_WritePin(GPIOB, DIN_Pin, GPIO_PIN_SET); // 设置DIN引脚为高电平
|
||||
// HAL_GPIO_WritePin(GPIOB, CONVST_Pin, GPIO_PIN_RESET); // 设置CS引脚为低电平
|
||||
// }
|
||||
int8_t ads8864_Init(Ads8864_t *ads8864) {
|
||||
HAL_GPIO_WritePin(GPIOB, DIN_Pin, GPIO_PIN_SET); // 设置DIN引脚为高电平
|
||||
HAL_GPIO_WritePin(GPIOB, CONVST_Pin, GPIO_PIN_RESET); // 设置CS引脚为低电平
|
||||
memset(ads8864, 0, sizeof(Ads8864_t)); // 初始化结构体
|
||||
return 0; // 初始化成功
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 读取ADS8864 ADC数据
|
||||
* @note 通过SPI接口读取ADC转换结果,并更新滤波数据
|
||||
* @retval uint16_t 返回滤波后的ADC数据
|
||||
*/
|
||||
uint16_t Ads8864_Read(void) {
|
||||
int8_t Ads8864_Read(Ads8864_t *ads8864) {
|
||||
// 读取原始数据
|
||||
uint16_t raw_data = Ads8864_Read_Internal();
|
||||
|
||||
// 更新原始数据缓冲区
|
||||
for (int i = 9; i > 0; i--) {
|
||||
ads8864_data.raw.adc_data[i] = ads8864_data.raw.adc_data[i - 1];
|
||||
ads8864->raw.adc_data[i] = ads8864->raw.adc_data[i - 1];
|
||||
}
|
||||
ads8864_data.raw.adc_data[0] = raw_data;
|
||||
ads8864->raw.adc_data[0] = raw_data;
|
||||
|
||||
// 更新滤波数据
|
||||
Ads8864_Update_Filtered_Data();
|
||||
// 计算距离
|
||||
ads8864->filtered.distance = Adc_to_Distance(ads8864->filtered.adc_data); // 计算距离
|
||||
|
||||
return ads8864_data.filtered.adc_data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Private functions --------------------------------------------------------- */
|
||||
@ -50,9 +58,15 @@ uint16_t Ads8864_Read(void) {
|
||||
static uint16_t Ads8864_Read_Internal(void) {
|
||||
uint8_t rx_data[2] = {0}; // 接收数据缓冲区
|
||||
|
||||
// 获取 SPI 句柄
|
||||
SPI_HandleTypeDef *hspi = BSP_SPI_GetHandle(BSP_SPI_ADC);
|
||||
if (hspi == NULL) {
|
||||
return 0; // 如果句柄为空,返回 0
|
||||
}
|
||||
|
||||
HAL_GPIO_WritePin(GPIOB, CONVST_Pin, GPIO_PIN_RESET); // 设置CS引脚为低电平
|
||||
HAL_Delay(1); // 延时 1 毫秒
|
||||
HAL_SPI_Receive(&hspi2, rx_data, sizeof(rx_data), HAL_MAX_DELAY); // SPI接收数据
|
||||
HAL_SPI_Receive(hspi, rx_data, sizeof(rx_data), HAL_MAX_DELAY); // SPI接收数据
|
||||
HAL_GPIO_WritePin(GPIOB, CONVST_Pin, GPIO_PIN_SET); // 设置CS引脚为高电平
|
||||
|
||||
return (rx_data[0] << 8) | rx_data[1]; // 合并接收数据并返回
|
||||
@ -68,7 +82,7 @@ static void Ads8864_Update_Filtered_Data(void) {
|
||||
|
||||
// 计算原始数据的平均值
|
||||
for (int i = 0; i < 10; i++) {
|
||||
sum += ads8864_data.raw.adc_data[i];
|
||||
sum += ads8864.raw.adc_data[i];
|
||||
}
|
||||
ads8864_data.filtered.adc_data = (uint16_t)(sum / 10);
|
||||
ads8864.filtered.adc_data = (uint16_t)(sum / 10);
|
||||
}
|
@ -15,6 +15,7 @@ typedef struct {
|
||||
// 滤波后的ADC数据
|
||||
typedef struct {
|
||||
uint16_t adc_data;
|
||||
float distance; // 距离(mm)
|
||||
} Ads8864_Filtered_t;
|
||||
|
||||
// ADC数据结构体
|
||||
@ -23,8 +24,8 @@ typedef struct {
|
||||
Ads8864_Filtered_t filtered; // 滤波后的数据
|
||||
} Ads8864_t;
|
||||
|
||||
extern Ads8864_t ads8864_data; // 声明全局变量
|
||||
extern Ads8864_t ads8864; // 声明全局变量
|
||||
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
void ads8864_Init(void);
|
||||
uint16_t Ads8864_Read(void);
|
||||
int8_t ads8864_Init(Ads8864_t *ads8864);
|
||||
int8_t Ads8864_Read(Ads8864_t *ads8864);
|
@ -1,11 +1,52 @@
|
||||
/*
|
||||
CAN总线上的设
|
||||
将所有CAN总线上挂载的设抽象成一设进行配和控制
|
||||
*/
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "can.h"
|
||||
#include "device/can.h"
|
||||
#include "stm32f3xx_hal.h"
|
||||
// CAN 调试结构体实例
|
||||
can_t can_debug = {
|
||||
.TxData = {0},
|
||||
.TxID = 0x023,
|
||||
.TxStatus = 0,
|
||||
.DebugCounter = 0
|
||||
};
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
// 初始化 CAN
|
||||
void CAN_Init(CAN_HandleTypeDef *hcan_Cur)
|
||||
{
|
||||
// 仅初始化CAN,不配置接收过滤器
|
||||
HAL_CAN_Start(hcan_Cur); // 开启CAN
|
||||
}
|
||||
|
||||
// CAN 发送函数,添加超时机制
|
||||
uint8_t CAN_SendData(CAN_HandleTypeDef *hcan_Cur, uint8_t *pData, uint16_t ID)
|
||||
{
|
||||
HAL_StatusTypeDef HAL_RetVal = HAL_ERROR;
|
||||
uint8_t FreeTxNum = 0;
|
||||
CAN_TxHeaderTypeDef TxMessage;
|
||||
|
||||
TxMessage.StdId = ID;
|
||||
TxMessage.DLC = 8; /* 默认一帧传输长度为8 */
|
||||
TxMessage.IDE = CAN_ID_STD;
|
||||
TxMessage.RTR = CAN_RTR_DATA;
|
||||
|
||||
uint32_t timeout = 5000; // 增加超时时间
|
||||
while (FreeTxNum == 0 && timeout > 0)
|
||||
{
|
||||
FreeTxNum = HAL_CAN_GetTxMailboxesFreeLevel(hcan_Cur);
|
||||
if (FreeTxNum > 0) break; // 如果有空闲邮箱,退出循环
|
||||
osDelay(1); // 避免忙等待
|
||||
timeout--;
|
||||
}
|
||||
if (timeout == 0)
|
||||
{
|
||||
return 1; // 超时,发送失败
|
||||
}
|
||||
|
||||
HAL_RetVal = HAL_CAN_AddTxMessage(hcan_Cur, &TxMessage, pData, (uint32_t *)CAN_TX_MAILBOX1);
|
||||
|
||||
if (HAL_RetVal != HAL_OK)
|
||||
{
|
||||
return 1; // 发送失败
|
||||
}
|
||||
|
||||
return 0; // 发送成功
|
||||
}
|
@ -1,19 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <cmsis_os2.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "bsp\can.h"
|
||||
|
||||
|
||||
#define CAN_TX_BUF_SIZE (8)
|
||||
#define CAN_RX_BUF_SIZE (8)
|
||||
|
||||
|
||||
#include "can.h"
|
||||
#include "stm32f3xx_hal.h"
|
||||
#include <stdint.h>
|
||||
// CAN 调试结构体
|
||||
typedef struct {
|
||||
uint16_t adc_data;
|
||||
struct {
|
||||
uint32_t sick;
|
||||
} mailbox;
|
||||
} CAN_t;
|
||||
uint8_t TxData[8]; // 要发送的数据
|
||||
uint16_t TxID; // 发送的CAN ID
|
||||
uint8_t TxStatus; // 发送状态,0: 成功,1: 失败
|
||||
uint32_t DebugCounter; // 调试计数器
|
||||
} can_t;
|
||||
|
||||
// 外部变量声明
|
||||
extern can_t can_debug;
|
||||
|
||||
// 函数声明
|
||||
void CAN_Init(CAN_HandleTypeDef *hcan_Cur);
|
||||
uint8_t CAN_SendData(CAN_HandleTypeDef *hcan_Cur, uint8_t *pData, uint16_t ID);
|
||||
|
@ -1,36 +0,0 @@
|
||||
// #include "device/pc.h"
|
||||
// #include "usart.h" // 包含 USART 驱动头文件
|
||||
// #include <string.h> // 用于 memcpy
|
||||
|
||||
// /* Private variables --------------------------------------------------------- */
|
||||
|
||||
// /* Exported functions -------------------------------------------------------- */
|
||||
|
||||
// /**
|
||||
// * @brief 初始化PC通信
|
||||
// */
|
||||
// void PC_Init(void) {
|
||||
// // MX_USART1_UART_Init(); // 初始化 USART1
|
||||
// pc_data.PC_online = false; // 默认PC离线
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @brief 通过串口发送数据
|
||||
// * @param data 要发送的16位数据
|
||||
// */
|
||||
// void PC_SendData(void) {
|
||||
// // 将 pc_data.to_pc 数据通过串口发送
|
||||
// HAL_UART_Transmit(&huart1, (uint8_t *)&pc_data.to_pc, sizeof(PC_UpPackage_t), HAL_MAX_DELAY);
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @brief 接收串口数据
|
||||
// * @param data 接收缓冲区
|
||||
// * @param length 接收数据长度
|
||||
// */
|
||||
// void PC_ReceiveData(uint8_t *data, uint16_t length) {
|
||||
// if (length == sizeof(PC_DownPackage_t)) {
|
||||
// memcpy(&pc_data.from_pc, data, length); // 将接收到的数据存储到 pc_data.from_pc
|
||||
// pc_data.PC_online = true; // 标记PC在线
|
||||
// }
|
||||
// }
|
@ -1,33 +0,0 @@
|
||||
// #pragma once
|
||||
|
||||
// /* Includes ----------------------------------------------------------------- */
|
||||
|
||||
// #include <stdint.h>
|
||||
// #include <stdbool.h> // 添加此行以定义 bool 类型
|
||||
// /* Exported constants ------------------------------------------------------- */
|
||||
// /* Exported macro ----------------------------------------------------------- */
|
||||
// /* Exported types ----------------------------------------------------------- */
|
||||
|
||||
// typedef struct {
|
||||
// uint8_t notice;
|
||||
// } PC_DownPackage_t;
|
||||
|
||||
// // 滤波后的ADC数据
|
||||
// typedef struct {
|
||||
// uint16_t adc_data; // ADC数据
|
||||
// } PC_UpPackage_t;
|
||||
|
||||
// // ADC数据结构体
|
||||
// typedef struct {
|
||||
// PC_DownPackage_t from_pc; // 从PC接收的数据
|
||||
// PC_UpPackage_t to_pc; // 发送到PC的数据
|
||||
// bool PC_online; // PC在线状态
|
||||
// } PC_t;
|
||||
|
||||
|
||||
// extern PC_t pc_data; // 用于存储PC通信数据
|
||||
|
||||
// /* Exported functions prototypes -------------------------------------------- */
|
||||
// void PC_Init(void);
|
||||
// void PC_SendData(void);
|
||||
// void PC_ReceiveData(uint8_t *data, uint16_t length);
|
@ -1,13 +1,12 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "task/user_task.h"
|
||||
#include "device/ads8864.h"
|
||||
#include "bsp/led.h" // 包含 LED 控制头文件
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private variables --------------------------------------------------------- */
|
||||
Ads8864_t ads8864_data; // 引用全局的ADC数据结构体
|
||||
Ads8864_t ads8864;
|
||||
/* Private function --------------------------------------------------------- */
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
@ -15,21 +14,16 @@ void Task_Adc(void *argument) {
|
||||
(void)argument; // 消除未使用参数的警告
|
||||
|
||||
/* 计算任务运行到指定频率需要等待的tick数 */
|
||||
const uint32_t delay_tick = osKernelGetTickFreq() / 200; // 250Hz
|
||||
|
||||
// osDelay(1000); /* 延时一段时间再开启任务 */
|
||||
|
||||
const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_ADC; // 200Hz
|
||||
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
|
||||
ads8864_Init(); // 初始化 ADC
|
||||
ads8864_Init(&ads8864); // 初始化 ADC
|
||||
while (1) {
|
||||
tick += delay_tick; /* 计算下一个唤醒时刻 */
|
||||
// 读取滤波后的 ADC 数据
|
||||
Ads8864_Read();
|
||||
osMessageQueueReset(adcQueueHandle); // 重置消息队列
|
||||
osMessageQueuePut(adcQueueHandle, &ads8864_data.filtered.adc_data, 0, 0); // 将数据放入消息队列
|
||||
osMessageQueueReset(pcQueueHandle); // 重置消息队列
|
||||
osMessageQueuePut(pcQueueHandle, &ads8864_data.filtered.adc_data, 0, 0); // 将数据放入消息队列
|
||||
// BSP_LED_SET(BSP_LED1, BSP_LED_TAGGLE); // 切换 LED 状态
|
||||
Ads8864_Read(&ads8864);
|
||||
osMessageQueueReset(task_runtime.msgq.adc); // 重置消息队列
|
||||
osMessageQueuePut(task_runtime.msgq.adc, &ads8864.filtered, 0, 0); // 将数据放入消息队列
|
||||
osMessageQueueReset(task_runtime.msgq.pc); // 重置消息队列
|
||||
osMessageQueuePut(task_runtime.msgq.pc, &ads8864.filtered.distance,0, 0); // 将数据放入消息队列
|
||||
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
|
||||
}
|
||||
}
|
@ -8,90 +8,40 @@
|
||||
#include "can.h"
|
||||
#include "task\user_task.h"
|
||||
#include "bsp\led.h"
|
||||
#include "device\ads8864.h"
|
||||
#include "device\can.h"
|
||||
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
typedef struct {
|
||||
uint8_t TxData[8]; // 要发送的数据
|
||||
uint16_t TxID; // 发送的CAN ID
|
||||
uint8_t TxStatus; // 发送状态,0: 成功,1: 失败
|
||||
uint32_t DebugCounter; // 调试计数器
|
||||
} can_t;
|
||||
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
can_t can_debug = {
|
||||
.TxData = {0},
|
||||
.TxID = 0x023,
|
||||
.TxStatus = 0,
|
||||
.DebugCounter = 0
|
||||
};
|
||||
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void CAN_Init(CAN_HandleTypeDef *hcan_Cur)
|
||||
{
|
||||
// 仅初始化CAN,不配置接收过滤器
|
||||
HAL_CAN_Start(hcan_Cur); // 开启CAN
|
||||
}
|
||||
|
||||
/* CAN发送函数 */
|
||||
uint8_t CAN_SendData(CAN_HandleTypeDef *hcan_Cur, uint8_t *pData, uint16_t ID)
|
||||
{
|
||||
HAL_StatusTypeDef HAL_RetVal = HAL_ERROR;
|
||||
uint8_t FreeTxNum = 0;
|
||||
CAN_TxHeaderTypeDef TxMessage;
|
||||
|
||||
TxMessage.StdId = ID;
|
||||
TxMessage.DLC = 8; /* 默认一帧传输长度为8 */
|
||||
TxMessage.IDE = CAN_ID_STD;
|
||||
TxMessage.RTR = CAN_RTR_DATA;
|
||||
|
||||
FreeTxNum = HAL_CAN_GetTxMailboxesFreeLevel(hcan_Cur);
|
||||
|
||||
while (FreeTxNum == 0) // 等待空邮箱,可能会卡死在这里(小BUG)
|
||||
{
|
||||
FreeTxNum = HAL_CAN_GetTxMailboxesFreeLevel(hcan_Cur);
|
||||
}
|
||||
|
||||
HAL_RetVal = HAL_CAN_AddTxMessage(hcan_Cur, &TxMessage, pData, (uint32_t *)CAN_TX_MAILBOX1);
|
||||
|
||||
if (HAL_RetVal != HAL_OK)
|
||||
{
|
||||
return 1; // 发送失败
|
||||
}
|
||||
|
||||
return 0; // 发送成功
|
||||
}
|
||||
|
||||
void Task_Can(void *argument) {
|
||||
(void)argument; // 消除未使用参数的警告
|
||||
const uint32_t delay_tick = osKernelGetTickFreq() / 100; // 50Hz
|
||||
const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_CAN; // 100Hz
|
||||
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
|
||||
|
||||
CAN_Init(&hcan); // 初始化CAN模块
|
||||
uint16_t adc_data = 0; // 用于存储从消息队列读取的 ADC 数据
|
||||
|
||||
// 初始化调试结构体数据
|
||||
for (int i = 0; i < 8; i++) {
|
||||
can_debug.TxData[i] = 0; // 填充测试数据
|
||||
}
|
||||
|
||||
uint16_t adc_data = 0; // 用于存储ADC数据
|
||||
while (1) {
|
||||
tick += delay_tick; /* 计算下一个唤醒时刻 */
|
||||
tick += delay_tick; /* 计算下一个唤醒时刻 */
|
||||
// 周期性发送数据
|
||||
if (osMessageQueueGet(adcQueueHandle, &adc_data, NULL, osWaitForever) == osOK) {
|
||||
// 将 ADC 数据填充到 CAN 数据帧
|
||||
can_debug.TxData[0] = (adc_data >> 8) & 0xFF; // 高字节
|
||||
can_debug.TxData[1] = adc_data & 0xFF; // 低字节
|
||||
if (osMessageQueueGet(task_runtime.msgq.adc, &adc_data, NULL, 100) == osOK) {
|
||||
// 将 ADC 数据填充到 CAN 数据帧
|
||||
can_debug.TxData[0] = (adc_data >> 8) & 0xFF; // 高字节
|
||||
can_debug.TxData[1] = adc_data & 0xFF; // 低字节
|
||||
|
||||
// 发送 CAN 数据
|
||||
can_debug.TxStatus = CAN_SendData(&hcan, can_debug.TxData, can_debug.TxID);
|
||||
can_debug.DebugCounter++; // 调试计数器递增
|
||||
}
|
||||
//每发送10条数据切换一次LED状态
|
||||
// 发送 CAN 数据
|
||||
can_debug.TxStatus = CAN_SendData(&hcan, can_debug.TxData, can_debug.TxID);
|
||||
can_debug.DebugCounter++; // 调试计数器递增
|
||||
}
|
||||
// 每发送10条数据切换一次LED状态
|
||||
if (can_debug.DebugCounter % 10 == 0) {
|
||||
BSP_LED_SET(BSP_LED2, BSP_LED_TAGGLE); // 切换 LED 状态
|
||||
}
|
||||
}
|
||||
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
|
||||
}
|
||||
}
|
@ -6,14 +6,12 @@
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "task\user_task.h"
|
||||
|
||||
#include "device/ads8864.h"
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
|
||||
osMessageQueueId_t adcQueueHandle;
|
||||
osMessageQueueId_t pcQueueHandle;
|
||||
/* Private function --------------------------------------------------------- */
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
|
||||
@ -28,14 +26,16 @@ void Task_Init(void *argument) {
|
||||
osKernelLock(); // 锁定内核,防止任务切换
|
||||
|
||||
// 创建任务,确保任务创建成功
|
||||
|
||||
osThreadId_t monitorTaskHandle = osThreadNew(Task_Monitor, NULL, &attr_monitor);
|
||||
osThreadId_t adcReadTaskHandle = osThreadNew(Task_Adc, NULL, &attr_adc);
|
||||
osThreadId_t canTaskHandle = osThreadNew(Task_Can, NULL, &attr_can);
|
||||
osThreadId_t pcTaskHandle = osThreadNew(Task_PC, NULL, &attr_pc);
|
||||
task_runtime.thread.adc = osThreadNew(Task_Adc, NULL, &attr_adc);
|
||||
task_runtime.thread.can = osThreadNew(Task_Can, NULL, &attr_can);
|
||||
task_runtime.thread.monitor = osThreadNew(Task_Monitor, NULL, &attr_monitor);
|
||||
task_runtime.thread.pc = osThreadNew(Task_PC, NULL, &attr_pc);
|
||||
|
||||
//创建消息队列
|
||||
adcQueueHandle = osMessageQueueNew(2u, sizeof(uint16_t), NULL);
|
||||
pcQueueHandle = osMessageQueueNew(2u, sizeof(uint16_t), NULL);
|
||||
task_runtime.msgq.adc =osMessageQueueNew(2u, sizeof(Ads8864_Filtered_t), NULL);
|
||||
task_runtime.msgq.can = osMessageQueueNew(2u, sizeof(Ads8864_Filtered_t), NULL);
|
||||
task_runtime.msgq.pc = osMessageQueueNew(2u, sizeof(float), NULL);
|
||||
|
||||
osKernelUnlock(); // 解锁内核
|
||||
osThreadTerminate(osThreadGetId()); // 任务完成后结束自身
|
||||
}
|
||||
|
@ -13,14 +13,8 @@
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void Task_Monitor(void *argument) {
|
||||
(void)argument; // 消除未使用参数的警告
|
||||
// BSP_WS2812_Init(); // 初始化 WS2812 LED
|
||||
while (1) {
|
||||
BSP_LED_SET(BSP_LED1, BSP_LED_TAGGLE); // 切换 LED 状态
|
||||
// BSP_WS2812_Set(255, 0, 0); // 设置为红色
|
||||
HAL_Delay(1000); // 延时 1 秒
|
||||
// BSP_WS2812_Set(0, 255, 0); // 设置为绿色
|
||||
// HAL_Delay(1000); // 延时 1 秒
|
||||
// BSP_WS2812_Set(0, 0, 255); // 设置为蓝色
|
||||
// HAL_Delay(1000); // 延时 1 秒
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "task\user_task.h"
|
||||
// #include "device/pc.h"
|
||||
#include "usart.h" // 添加此行以声明 HAL_UART_Receive 和 huart1
|
||||
#include "usart.h"
|
||||
#include "bsp\led.h"
|
||||
|
||||
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
uint16_t adc_data = 0; // 用于存储ADC数据
|
||||
float distance = 0.0f; // 用于存储距离数据单位为米
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void Task_PC(void *argument) {
|
||||
(void)argument; // 消除未使用参数的警告
|
||||
@ -15,11 +15,10 @@ void Task_PC(void *argument) {
|
||||
|
||||
while (1) {
|
||||
tick += delay_tick; /* 计算下一个唤醒时刻 */
|
||||
// 使用 DMA 发送数据
|
||||
if (osMessageQueueGet(pcQueueHandle, &adc_data, NULL, osWaitForever) == osOK) {
|
||||
// 直接发送数字到PC
|
||||
HAL_UART_Transmit_DMA(&huart3, (uint8_t *)&adc_data, sizeof(adc_data));
|
||||
if (osMessageQueueGet(task_runtime.msgq.pc, &distance, NULL, osWaitForever) == osOK) {
|
||||
HAL_UART_Transmit_DMA(&huart3, (uint8_t *)&distance, sizeof(distance)); // 发送数据
|
||||
}
|
||||
|
||||
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
#include "task/user_task.h"
|
||||
|
||||
Task_Runtime_t task_runtime;
|
||||
|
||||
// 定义任务属性
|
||||
const osThreadAttr_t attr_init = {
|
||||
.name = "Task_Init",
|
||||
|
@ -10,12 +10,51 @@ extern "C" {
|
||||
|
||||
// 定义任务运行时结构体
|
||||
typedef struct {
|
||||
uint32_t runtime;
|
||||
} Task_Runtime_t;
|
||||
/* 各任务,也可以叫做线程 */
|
||||
struct {
|
||||
osThreadId_t can; /* CAN任务 */
|
||||
osThreadId_t adc; /* ADC任务 */
|
||||
osThreadId_t pc; /* PC任务 */
|
||||
osThreadId_t monitor; /* 监控任务 */
|
||||
} thread;
|
||||
|
||||
struct {
|
||||
osMessageQueueId_t can; /* CAN消息队列 */
|
||||
osMessageQueueId_t adc; /* ADC消息队列 */
|
||||
osMessageQueueId_t pc; /* PC消息队列 */
|
||||
} msgq;
|
||||
|
||||
|
||||
struct {
|
||||
uint32_t can; /* CAN使用 */
|
||||
uint32_t adc; /* ADC使用 */
|
||||
uint32_t pc; /* PC使用 */
|
||||
uint32_t monitor; /* 监控使用 */
|
||||
} heap_water_mark; /* heap使用 */
|
||||
|
||||
struct {
|
||||
float can; /* CAN任务运行频率 */
|
||||
float adc; /* ADC任务运行频率 */
|
||||
float pc; /* PC任务运行频率 */
|
||||
float monitor; /* 监控任务运行频率 */
|
||||
} freq; /* 任务运行频率 */
|
||||
|
||||
struct {
|
||||
uint32_t can; /* CAN任务运行时间 */
|
||||
uint32_t adc; /* ADC任务运行时间 */
|
||||
uint32_t pc; /* PC任务运行时间 */
|
||||
uint32_t monitor; /* 监控任务运行时间 */
|
||||
} last_up_time; /* 任务最近运行时间 */
|
||||
} Task_Runtime_t;
|
||||
|
||||
|
||||
|
||||
// 任务频率和初始化延时
|
||||
#define TASK_FREQ_CAN (1000u)
|
||||
#define TASK_FREQ_ADC (500u)
|
||||
#define TASK_FREQ_CAN (100u)
|
||||
#define TASK_FREQ_ADC (200u)
|
||||
#define TASK_FREQ_MONITOR (1u)
|
||||
#define TASK_FREQ_PC (100u)
|
||||
|
||||
|
||||
#define TASK_INIT_DELAY_INFO (500u)
|
||||
|
||||
@ -24,20 +63,21 @@ typedef struct {
|
||||
osThreadId_t can;
|
||||
osThreadId_t adc;
|
||||
osThreadId_t monitor;
|
||||
// osThreadId_t init;
|
||||
osThreadId_t pc;
|
||||
} Task_Handles_t;
|
||||
|
||||
extern Task_Runtime_t task_runtime;
|
||||
|
||||
|
||||
extern const osThreadAttr_t attr_init;
|
||||
|
||||
extern const osThreadAttr_t attr_can;
|
||||
extern const osThreadAttr_t attr_adc;
|
||||
extern const osThreadAttr_t attr_monitor;
|
||||
extern const osThreadAttr_t attr_pc;
|
||||
|
||||
extern osMessageQueueId_t adcQueueHandle;
|
||||
extern osMessageQueueId_t pcQueueHandle;
|
||||
|
||||
// extern osMessageQueueId_t adcQueueHandle;
|
||||
// extern osMessageQueueId_t pcQueueHandle;
|
||||
|
||||
void Task_Init(void *argument);
|
||||
void Task_Can(void *argument);
|
||||
|
Loading…
Reference in New Issue
Block a user