rm_balance/User/device/mrobot.h
2026-02-04 13:03:11 +08:00

247 lines
7.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file mrobot.h
* @brief MRobot CLI - 基于 FreeRTOS CLI 的嵌入式调试命令行系统
*
* 功能特性:
* - 设备注册与监控IMU、电机、传感器等
* - 类 Unix 文件系统命令cd, ls, pwd
* - htop 风格的任务监控
* - 自定义命令扩展
* - 线程安全设计
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "component/ahrs.h"
#include "device/device.h"
#include "bsp/uart.h"
/* Configuration ------------------------------------------------------------ */
/* 可在编译时通过 -D 选项覆盖这些默认值 */
#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 {
MROBOT_OK = 0, /* 成功 */
MROBOT_ERR_FULL = -1, /* 容量已满 */
MROBOT_ERR_NULL_PTR = -2, /* 空指针 */
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;
/* CLI 运行状态 */
typedef enum {
MROBOT_STATE_IDLE, /* 空闲状态,等待输入 */
MROBOT_STATE_HTOP, /* htop 模式 */
MROBOT_STATE_PROCESSING, /* 正在处理命令 */
} MRobot_State_t;
/* 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);
/* Device structure --------------------------------------------------------- */
typedef struct {
char name[MROBOT_DEVICE_NAME_LEN]; /* 设备名称 */
MRobot_DeviceType_t type; /* 设备类型 */
void *data; /* 设备数据指针 */
MRobot_PrintCallback_t print_cb; /* 打印回调函数 */
} MRobot_Device_t;
/* 标准设备数据结构 ---------------------------------------------------------- */
/**
* @brief IMU 设备数据结构(标准模板)
*/
typedef struct {
DEVICE_Header_t header;
AHRS_Accl_t accl; /* 加速度 m/s² */
AHRS_Gyro_t gyro; /* 角速度 rad/s */
AHRS_Eulr_t euler; /* 欧拉角 rad */
AHRS_Quaternion_t quat; /* 四元数 */
float temp; /* 温度 °C */
} DEVICE_IMU_t;
/**
* @brief 电机反馈数据结构
*/
typedef struct {
float rotor_abs_angle; /* 转子绝对角度 */
float rotor_speed; /* 实际转子转速 */
float torque_current; /* 转矩电流 */
float temp; /* 温度 */
} DEVICE_MOTOR_Feedback_t;
/**
* @brief 电机设备数据结构(标准模板)
*/
typedef struct {
DEVICE_Header_t header;
bool reverse; /* 是否反装 */
DEVICE_MOTOR_Feedback_t feedback;
} DEVICE_MOTOR_t;
/* Public API --------------------------------------------------------------- */
/**
* @brief 初始化 MRobot CLI 系统
* @note 必须在 FreeRTOS 调度器启动后调用
*/
void MRobot_Init(void);
/**
* @brief 反初始化 MRobot CLI 系统,释放资源
*/
void MRobot_DeInit(void);
/**
* @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 imu_device 指向 DEVICE_IMU_t 结构的指针
* @return MRobot_Error_t 错误码
*/
MRobot_Error_t MRobot_RegisterIMU(const char *name, DEVICE_IMU_t *imu_device);
/**
* @brief 注册电机设备(使用内置打印函数)
* @param name 设备名称
* @param motor 指向 MOTOR_t 或兼容结构的指针
* @return MRobot_Error_t 错误码
*/
MRobot_Error_t MRobot_RegisterMotor(const char *name, void *motor);
/**
* @brief 注册自定义命令
* @param command 命令名称
* @param help_text 帮助文本
* @param callback 命令回调函数
* @param param_count 参数个数(-1 表示可变参数)
* @return MRobot_Error_t 错误码
*/
MRobot_Error_t MRobot_RegisterCommand(const char *command, const char *help_text,
MRobot_CommandCallback_t callback, int8_t param_count);
/**
* @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);
/**
* @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
}
#endif