优化mr
This commit is contained in:
parent
8712efa6f2
commit
20afc1a656
1086
User/device/mrobot.c
1086
User/device/mrobot.c
File diff suppressed because it is too large
Load Diff
@ -1,25 +1,13 @@
|
|||||||
/*基于freertos_cli实现的虚拟命令行
|
/**
|
||||||
自身作为一个设备存在,通过串口和上位机进行交互,获得类似命令行的体验,
|
* @file mrobot.h
|
||||||
例如输入help可以获得帮助
|
* @brief MRobot CLI - 基于 FreeRTOS CLI 的嵌入式调试命令行系统
|
||||||
输出htop 可以获得freertos任务状态等信息
|
*
|
||||||
|
* 功能特性:
|
||||||
通过cd可以切换目录
|
* - 设备注册与监控(IMU、电机、传感器等)
|
||||||
目录结构是
|
* - 类 Unix 文件系统命令(cd, ls, pwd)
|
||||||
/ (root)
|
* - htop 风格的任务监控
|
||||||
|-- /dev
|
* - 自定义命令扩展
|
||||||
|-- bmi088
|
* - 线程安全设计
|
||||||
|-- chassis_motor1
|
|
||||||
|-- /modules
|
|
||||||
|-- balance_chassis
|
|
||||||
|-- gimbal
|
|
||||||
|-- shoot
|
|
||||||
在dev里可以用show命令查看设备信息
|
|
||||||
modules暂无实现
|
|
||||||
|
|
||||||
至于dev里的设备有哪些,需要通过一个函数MRobot_RegisterDevices(设备名(字符串),结构体指针)来注册设备,我可以在任何一个任务里调用这个函数。
|
|
||||||
然后我在我的一个单独的线程里打印和发送他们。
|
|
||||||
例如我在atti_esit任务里注册了bmi088和他的结构体,我在cli线程里需要能够打印bmi088的数据。
|
|
||||||
然后我在show的时候就可以看到imu的数据了,
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
@ -31,38 +19,103 @@ extern "C" {
|
|||||||
/* Includes ----------------------------------------------------------------- */
|
/* Includes ----------------------------------------------------------------- */
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include "component/ahrs.h"
|
#include "component/ahrs.h"
|
||||||
#include "device/device.h"
|
#include "device/device.h"
|
||||||
|
#include "bsp/uart.h"
|
||||||
|
|
||||||
/* Exported constants ------------------------------------------------------- */
|
/* Configuration ------------------------------------------------------------ */
|
||||||
#define MROBOT_MAX_DEVICES 32
|
/* 可在编译时通过 -D 选项覆盖这些默认值 */
|
||||||
#define MROBOT_MAX_CMD_LEN 128
|
|
||||||
#define MROBOT_MAX_OUTPUT_LEN 512
|
|
||||||
#define MROBOT_RX_BUFFER_SIZE 256
|
|
||||||
|
|
||||||
/* Exported types ----------------------------------------------------------- */
|
#ifndef MROBOT_MAX_DEVICES
|
||||||
|
#define MROBOT_MAX_DEVICES 32 /* 最大设备数 */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MROBOT_MAX_CUSTOM_COMMANDS
|
||||||
|
#define MROBOT_MAX_CUSTOM_COMMANDS 16 /* 最大自定义命令数 */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MROBOT_CMD_BUFFER_SIZE
|
||||||
|
#define MROBOT_CMD_BUFFER_SIZE 128 /* 命令缓冲区大小 */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MROBOT_OUTPUT_BUFFER_SIZE
|
||||||
|
#define MROBOT_OUTPUT_BUFFER_SIZE 512 /* 输出缓冲区大小 */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MROBOT_DEVICE_NAME_LEN
|
||||||
|
#define MROBOT_DEVICE_NAME_LEN 32 /* 设备名最大长度 */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MROBOT_PATH_MAX_LEN
|
||||||
|
#define MROBOT_PATH_MAX_LEN 64 /* 路径最大长度 */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MROBOT_HTOP_REFRESH_MS
|
||||||
|
#define MROBOT_HTOP_REFRESH_MS 200 /* htop 刷新间隔 (ms) */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MROBOT_UART_PORT
|
||||||
|
#define MROBOT_UART_PORT BSP_UART_VOFA /* 默认 UART 端口 */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Error codes -------------------------------------------------------------- */
|
||||||
typedef enum {
|
typedef enum {
|
||||||
MROBOT_DEVICE_TYPE_IMU,
|
MROBOT_OK = 0, /* 成功 */
|
||||||
MROBOT_DEVICE_TYPE_MOTOR,
|
MROBOT_ERR_FULL = -1, /* 容量已满 */
|
||||||
MROBOT_DEVICE_TYPE_SENSOR,
|
MROBOT_ERR_NULL_PTR = -2, /* 空指针 */
|
||||||
MROBOT_DEVICE_TYPE_CUSTOM,
|
MROBOT_ERR_INVALID_ARG = -3, /* 无效参数 */
|
||||||
|
MROBOT_ERR_NOT_FOUND = -4, /* 未找到 */
|
||||||
|
MROBOT_ERR_ALLOC = -5, /* 内存分配失败 */
|
||||||
|
MROBOT_ERR_BUSY = -6, /* 设备忙 */
|
||||||
|
MROBOT_ERR_NOT_INIT = -7, /* 未初始化 */
|
||||||
|
} MRobot_Error_t;
|
||||||
|
|
||||||
|
/* Device types ------------------------------------------------------------- */
|
||||||
|
typedef enum {
|
||||||
|
MROBOT_DEVICE_TYPE_IMU, /* IMU 设备 */
|
||||||
|
MROBOT_DEVICE_TYPE_MOTOR, /* 电机设备 */
|
||||||
|
MROBOT_DEVICE_TYPE_SENSOR, /* 传感器设备 */
|
||||||
|
MROBOT_DEVICE_TYPE_CUSTOM, /* 自定义设备 */
|
||||||
|
MROBOT_DEVICE_TYPE_NUM /* 设备类型总数 */
|
||||||
} MRobot_DeviceType_t;
|
} MRobot_DeviceType_t;
|
||||||
|
|
||||||
/* 设备打印回调函数类型 */
|
/* CLI 运行状态 */
|
||||||
typedef void (*MRobot_PrintCallback_t)(void *device_data, char *buffer, uint16_t buffer_size);
|
typedef enum {
|
||||||
|
MROBOT_STATE_IDLE, /* 空闲状态,等待输入 */
|
||||||
|
MROBOT_STATE_HTOP, /* htop 模式 */
|
||||||
|
MROBOT_STATE_PROCESSING, /* 正在处理命令 */
|
||||||
|
} MRobot_State_t;
|
||||||
|
|
||||||
/* 命令处理回调函数类型(与 pdCOMMAND_LINE_CALLBACK 相同) */
|
/* Callback types ----------------------------------------------------------- */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 设备打印回调函数类型
|
||||||
|
* @param device_data 设备数据指针
|
||||||
|
* @param buffer 输出缓冲区
|
||||||
|
* @param buffer_size 缓冲区大小
|
||||||
|
* @return 实际写入的字节数
|
||||||
|
*/
|
||||||
|
typedef int (*MRobot_PrintCallback_t)(const void *device_data, char *buffer, size_t buffer_size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 命令处理回调函数类型(与 FreeRTOS CLI 兼容)
|
||||||
|
*/
|
||||||
typedef long (*MRobot_CommandCallback_t)(char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString);
|
typedef long (*MRobot_CommandCallback_t)(char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString);
|
||||||
|
|
||||||
/* 注册的设备结构 */
|
/* Device structure --------------------------------------------------------- */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char name[32];
|
char name[MROBOT_DEVICE_NAME_LEN]; /* 设备名称 */
|
||||||
MRobot_DeviceType_t type;
|
MRobot_DeviceType_t type; /* 设备类型 */
|
||||||
void *data; /* 指向实际设备数据的指针 */
|
void *data; /* 设备数据指针 */
|
||||||
MRobot_PrintCallback_t print_callback; /* 打印设备信息的回调函数 */
|
MRobot_PrintCallback_t print_cb; /* 打印回调函数 */
|
||||||
} MRobot_Device_t;
|
} MRobot_Device_t;
|
||||||
|
|
||||||
/* 设备模板结构体 - 使用 AHRS 标准结构 */
|
/* 标准设备数据结构 ---------------------------------------------------------- */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief IMU 设备数据结构(标准模板)
|
||||||
|
*/
|
||||||
typedef struct {
|
typedef struct {
|
||||||
DEVICE_Header_t header;
|
DEVICE_Header_t header;
|
||||||
AHRS_Accl_t accl; /* 加速度 m/s² */
|
AHRS_Accl_t accl; /* 加速度 m/s² */
|
||||||
@ -72,6 +125,9 @@ typedef struct {
|
|||||||
float temp; /* 温度 °C */
|
float temp; /* 温度 °C */
|
||||||
} DEVICE_IMU_t;
|
} DEVICE_IMU_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 电机反馈数据结构
|
||||||
|
*/
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float rotor_abs_angle; /* 转子绝对角度 */
|
float rotor_abs_angle; /* 转子绝对角度 */
|
||||||
float rotor_speed; /* 实际转子转速 */
|
float rotor_speed; /* 实际转子转速 */
|
||||||
@ -79,62 +135,113 @@ typedef struct {
|
|||||||
float temp; /* 温度 */
|
float temp; /* 温度 */
|
||||||
} DEVICE_MOTOR_Feedback_t;
|
} DEVICE_MOTOR_Feedback_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 电机设备数据结构(标准模板)
|
||||||
|
*/
|
||||||
typedef struct {
|
typedef struct {
|
||||||
DEVICE_Header_t header;
|
DEVICE_Header_t header;
|
||||||
bool reverse; /* 是否反装 true表示反装 */
|
bool reverse; /* 是否反装 */
|
||||||
DEVICE_MOTOR_Feedback_t feedback;
|
DEVICE_MOTOR_Feedback_t feedback;
|
||||||
} DEVICE_MOTOR_t;
|
} DEVICE_MOTOR_t;
|
||||||
|
|
||||||
/* Exported functions ------------------------------------------------------- */
|
/* Public API --------------------------------------------------------------- */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 初始化 MRobot 命令行系统(包含 UART 初始化)
|
* @brief 初始化 MRobot CLI 系统
|
||||||
|
* @note 必须在 FreeRTOS 调度器启动后调用
|
||||||
*/
|
*/
|
||||||
void MRobot_Init(void);
|
void MRobot_Init(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 注册设备到 MRobot 系统
|
* @brief 反初始化 MRobot CLI 系统,释放资源
|
||||||
* @param name 设备名称
|
|
||||||
* @param type 设备类型
|
|
||||||
* @param data 指向设备数据结构的指针
|
|
||||||
* @param print_callback 打印设备信息的回调函数
|
|
||||||
* @retval 0 成功, -1 失败
|
|
||||||
*/
|
*/
|
||||||
int8_t MRobot_RegisterDevice(const char *name, MRobot_DeviceType_t type,
|
void MRobot_DeInit(void);
|
||||||
void *data, MRobot_PrintCallback_t print_callback);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 注册 IMU 设备到 MRobot(使用通用打印函数)
|
* @brief 获取当前 CLI 状态
|
||||||
|
* @return MRobot_State_t 当前状态
|
||||||
|
*/
|
||||||
|
MRobot_State_t MRobot_GetState(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 注册设备到 MRobot 系统
|
||||||
|
* @param name 设备名称(会被截断到 MROBOT_DEVICE_NAME_LEN-1)
|
||||||
|
* @param type 设备类型
|
||||||
|
* @param data 设备数据指针(不能为 NULL)
|
||||||
|
* @param print_cb 打印回调函数(可为 NULL,但无法用 show 命令查看)
|
||||||
|
* @return MRobot_Error_t 错误码
|
||||||
|
*/
|
||||||
|
MRobot_Error_t MRobot_RegisterDevice(const char *name, MRobot_DeviceType_t type,
|
||||||
|
void *data, MRobot_PrintCallback_t print_cb);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 注销设备
|
||||||
|
* @param name 设备名称
|
||||||
|
* @return MRobot_Error_t 错误码
|
||||||
|
*/
|
||||||
|
MRobot_Error_t MRobot_UnregisterDevice(const char *name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 注册 IMU 设备(使用内置打印函数)
|
||||||
* @param name 设备名称
|
* @param name 设备名称
|
||||||
* @param imu_device 指向 DEVICE_IMU_t 结构的指针
|
* @param imu_device 指向 DEVICE_IMU_t 结构的指针
|
||||||
* @retval 0 成功, -1 失败
|
* @return MRobot_Error_t 错误码
|
||||||
*/
|
*/
|
||||||
int8_t MRobot_RegisterIMU(const char *name, void *imu_device);
|
MRobot_Error_t MRobot_RegisterIMU(const char *name, DEVICE_IMU_t *imu_device);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 注册电机设备到 MRobot(使用通用打印函数)
|
* @brief 注册电机设备(使用内置打印函数)
|
||||||
* @param name 设备名称
|
* @param name 设备名称
|
||||||
* @param motor 指向 MOTOR_t 结构的指针
|
* @param motor 指向 MOTOR_t 或兼容结构的指针
|
||||||
* @retval 0 成功, -1 失败
|
* @return MRobot_Error_t 错误码
|
||||||
*/
|
*/
|
||||||
int8_t MRobot_RegisterMotor(const char *name, void *motor);
|
MRobot_Error_t MRobot_RegisterMotor(const char *name, void *motor);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 注册自定义命令到 MRobot CLI
|
* @brief 注册自定义命令
|
||||||
* @param command 命令名称(例如 "test")
|
* @param command 命令名称
|
||||||
* @param help_text 帮助文本(例如 "test: 测试命令\r\n")
|
* @param help_text 帮助文本
|
||||||
* @param callback 命令处理回调函数
|
* @param callback 命令回调函数
|
||||||
* @param param_count 参数个数(0=无参数,-1=可变参数)
|
* @param param_count 参数个数(-1 表示可变参数)
|
||||||
* @retval 0 成功, -1 失败
|
* @return MRobot_Error_t 错误码
|
||||||
*/
|
*/
|
||||||
int8_t MRobot_RegisterCommand(const char *command, const char *help_text,
|
MRobot_Error_t MRobot_RegisterCommand(const char *command, const char *help_text,
|
||||||
MRobot_CommandCallback_t callback, int8_t param_count);
|
MRobot_CommandCallback_t callback, int8_t param_count);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief MRobot 主循环,在 CLI 任务中调用
|
* @brief 获取已注册设备数量
|
||||||
|
* @return 设备数量
|
||||||
|
*/
|
||||||
|
uint8_t MRobot_GetDeviceCount(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 根据名称查找设备
|
||||||
|
* @param name 设备名称
|
||||||
|
* @return 设备指针,未找到返回 NULL
|
||||||
|
*/
|
||||||
|
const MRobot_Device_t *MRobot_FindDevice(const char *name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief MRobot 主循环,在 CLI 任务中周期性调用
|
||||||
|
* @note 建议调用周期 10ms
|
||||||
*/
|
*/
|
||||||
void MRobot_Run(void);
|
void MRobot_Run(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 发送字符串到 CLI 终端(线程安全)
|
||||||
|
* @param str 要发送的字符串
|
||||||
|
* @return MRobot_Error_t 错误码
|
||||||
|
*/
|
||||||
|
MRobot_Error_t MRobot_Print(const char *str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 格式化输出到 CLI 终端(线程安全)
|
||||||
|
* @param fmt 格式字符串
|
||||||
|
* @param ... 可变参数
|
||||||
|
* @return 实际输出的字符数,失败返回负数
|
||||||
|
*/
|
||||||
|
int MRobot_Printf(const char *fmt, ...);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user