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

140 lines
4.5 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.

/*基于freertos_cli实现的虚拟命令行
自身作为一个设备存在,通过串口和上位机进行交互,获得类似命令行的体验,
例如输入help可以获得帮助
输出htop 可以获得freertos任务状态等信息
通过cd可以切换目录
目录结构是
/ (root)
|-- /dev
|-- bmi088
|-- chassis_motor1
|-- /modules
|-- balance_chassis
|-- gimbal
|-- shoot
在dev里可以用show命令查看设备信息
modules暂无实现
至于dev里的设备有哪些需要通过一个函数MRobot_RegisterDevices(设备名(字符串),结构体指针)来注册设备,我可以在任何一个任务里调用这个函数。
然后我在我的一个单独的线程里打印和发送他们。
例如我在atti_esit任务里注册了bmi088和他的结构体我在cli线程里需要能够打印bmi088的数据。
然后我在show的时候就可以看到imu的数据了
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
#include <stdbool.h>
#include "component/ahrs.h"
#include "device/device.h"
/* Exported constants ------------------------------------------------------- */
#define MROBOT_MAX_DEVICES 32
#define MROBOT_MAX_CMD_LEN 128
#define MROBOT_MAX_OUTPUT_LEN 512
#define MROBOT_RX_BUFFER_SIZE 256
/* Exported types ----------------------------------------------------------- */
typedef enum {
MROBOT_DEVICE_TYPE_IMU,
MROBOT_DEVICE_TYPE_MOTOR,
MROBOT_DEVICE_TYPE_SENSOR,
MROBOT_DEVICE_TYPE_CUSTOM,
} MRobot_DeviceType_t;
/* 设备打印回调函数类型 */
typedef void (*MRobot_PrintCallback_t)(void *device_data, char *buffer, uint16_t buffer_size);
/* 命令处理回调函数类型(与 pdCOMMAND_LINE_CALLBACK 相同) */
typedef long (*MRobot_CommandCallback_t)(char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString);
/* 注册的设备结构 */
typedef struct {
char name[32];
MRobot_DeviceType_t type;
void *data; /* 指向实际设备数据的指针 */
MRobot_PrintCallback_t print_callback; /* 打印设备信息的回调函数 */
} MRobot_Device_t;
/* 设备模板结构体 - 使用 AHRS 标准结构 */
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;
typedef struct {
float rotor_abs_angle; /* 转子绝对角度 */
float rotor_speed; /* 实际转子转速 */
float torque_current; /* 转矩电流 */
float temp; /* 温度 */
} DEVICE_MOTOR_Feedback_t;
typedef struct {
DEVICE_Header_t header;
bool reverse; /* 是否反装 true表示反装 */
DEVICE_MOTOR_Feedback_t feedback;
} DEVICE_MOTOR_t;
/* Exported functions ------------------------------------------------------- */
/**
* @brief 初始化 MRobot 命令行系统(包含 UART 初始化)
*/
void MRobot_Init(void);
/**
* @brief 注册设备到 MRobot 系统
* @param name 设备名称
* @param type 设备类型
* @param data 指向设备数据结构的指针
* @param print_callback 打印设备信息的回调函数
* @retval 0 成功, -1 失败
*/
int8_t MRobot_RegisterDevice(const char *name, MRobot_DeviceType_t type,
void *data, MRobot_PrintCallback_t print_callback);
/**
* @brief 注册 IMU 设备到 MRobot使用通用打印函数
* @param name 设备名称
* @param imu_device 指向 DEVICE_IMU_t 结构的指针
* @retval 0 成功, -1 失败
*/
int8_t MRobot_RegisterIMU(const char *name, void *imu_device);
/**
* @brief 注册电机设备到 MRobot使用通用打印函数
* @param name 设备名称
* @param motor 指向 MOTOR_t 结构的指针
* @retval 0 成功, -1 失败
*/
int8_t MRobot_RegisterMotor(const char *name, void *motor);
/**
* @brief 注册自定义命令到 MRobot CLI
* @param command 命令名称(例如 "test"
* @param help_text 帮助文本(例如 "test: 测试命令\r\n"
* @param callback 命令处理回调函数
* @param param_count 参数个数0=无参数,-1=可变参数)
* @retval 0 成功, -1 失败
*/
int8_t MRobot_RegisterCommand(const char *command, const char *help_text,
MRobot_CommandCallback_t callback, int8_t param_count);
/**
* @brief MRobot 主循环,在 CLI 任务中调用
*/
void MRobot_Run(void);
#ifdef __cplusplus
}
#endif