This commit is contained in:
Robofish 2026-02-04 15:24:56 +08:00
parent 6422b1e512
commit 05dae2219e
3 changed files with 31 additions and 28 deletions

View File

@ -7,6 +7,7 @@
#include "device/mrobot.h" #include "device/mrobot.h"
#include "component/freertos_cli.h" #include "component/freertos_cli.h"
#include "bsp/uart.h" #include "bsp/uart.h"
#include "bsp/mm.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -523,7 +524,7 @@ void MRobot_DeInit(void) {
/* 释放自定义命令内存 */ /* 释放自定义命令内存 */
for (uint8_t i = 0; i < ctx.custom_cmd_count; i++) { for (uint8_t i = 0; i < ctx.custom_cmd_count; i++) {
if (ctx.custom_cmds[i] != NULL) { if (ctx.custom_cmds[i] != NULL) {
vPortFree(ctx.custom_cmds[i]); BSP_Free(ctx.custom_cmds[i]);
ctx.custom_cmds[i] = NULL; ctx.custom_cmds[i] = NULL;
} }
} }
@ -616,7 +617,7 @@ MRobot_Error_t MRobot_RegisterCommand(const char *command, const char *help_text
} }
/* 动态分配命令结构体 */ /* 动态分配命令结构体 */
CLI_Command_Definition_t *cmd_def = pvPortMalloc(sizeof(CLI_Command_Definition_t)); CLI_Command_Definition_t *cmd_def = BSP_Malloc(sizeof(CLI_Command_Definition_t));
if (cmd_def == NULL) { if (cmd_def == NULL) {
return MROBOT_ERR_ALLOC; return MROBOT_ERR_ALLOC;
} }

View File

@ -25,20 +25,15 @@
* // 2. 实现打印回调 * // 2. 实现打印回调
* static int print_imu(const void *data, char *buf, size_t size) { * static int print_imu(const void *data, char *buf, size_t size) {
* const MyIMU_t *imu = (const MyIMU_t *)data; * const MyIMU_t *imu = (const MyIMU_t *)data;
* char ax[16], ay[16], az[16], r[16], p[16], y[16], t[16]; * return MRobot_Snprintf(buf, size,
* MRobot_FormatFloat(ax, 16, imu->accl[0], 3);
* MRobot_FormatFloat(ay, 16, imu->accl[1], 3);
* MRobot_FormatFloat(az, 16, imu->accl[2], 3);
* MRobot_FormatFloat(r, 16, imu->euler[0], 2);
* MRobot_FormatFloat(p, 16, imu->euler[1], 2);
* MRobot_FormatFloat(y, 16, imu->euler[2], 2);
* MRobot_FormatFloat(t, 16, imu->temp, 1);
* return snprintf(buf, size,
* " Status: %s\r\n" * " Status: %s\r\n"
* " Accel : X=%s Y=%s Z=%s\r\n" * " Accel : X=%.3f Y=%.3f Z=%.3f\r\n"
* " Euler : R=%s P=%s Y=%s\r\n" * " Euler : R=%.2f P=%.2f Y=%.2f\r\n"
* " Temp : %s C\r\n", * " Temp : %.1f C\r\n",
* imu->online ? "Online" : "Offline", ax, ay, az, r, p, y, t); * imu->online ? "Online" : "Offline",
* imu->accl[0], imu->accl[1], imu->accl[2],
* imu->euler[0], imu->euler[1], imu->euler[2],
* imu->temp);
* } * }
* *
* // 3. 注册设备 * // 3. 注册设备
@ -58,16 +53,13 @@
* *
* static int print_motor(const void *data, char *buf, size_t size) { * static int print_motor(const void *data, char *buf, size_t size) {
* const MyMotor_t *m = (const MyMotor_t *)data; * const MyMotor_t *m = (const MyMotor_t *)data;
* char ang[16], spd[16], cur[16]; * return MRobot_Snprintf(buf, size,
* MRobot_FormatFloat(ang, 16, m->angle, 2);
* MRobot_FormatFloat(spd, 16, m->speed, 1);
* MRobot_FormatFloat(cur, 16, m->current, 3);
* return snprintf(buf, size,
* " Status : %s\r\n" * " Status : %s\r\n"
* " Angle : %s deg\r\n" * " Angle : %.2f deg\r\n"
* " Speed : %s RPM\r\n" * " Speed : %.1f RPM\r\n"
* " Current: %s A\r\n", * " Current: %.3f A\r\n",
* m->online ? "Online" : "Offline", ang, spd, cur); * m->online ? "Online" : "Offline",
* m->angle, m->speed, m->current);
* } * }
* *
* // 注册 4 个电机 * // 注册 4 个电机
@ -87,20 +79,20 @@
* const char *param = FreeRTOS_CLIGetParameter(cmd, 1, NULL); * const char *param = FreeRTOS_CLIGetParameter(cmd, 1, NULL);
* *
* if (param == NULL) { * if (param == NULL) {
* return snprintf(buf, size, "Usage: cali <gyro|accel|save>\r\n"); * return MRobot_Snprintf(buf, size, "Usage: cali <gyro|accel|save>\r\n");
* } * }
* if (strncmp(param, "gyro", 4) == 0) { * if (strncmp(param, "gyro", 4) == 0) {
* // 采集 1000 次陀螺仪数据求平均 * // 采集 1000 次陀螺仪数据求平均
* snprintf(buf, size, "Calibrating gyro... keep IMU still!\r\n"); * MRobot_Snprintf(buf, size, "Calibrating gyro... keep IMU still!\r\n");
* // ... 校准逻辑 ... * // ... 校准逻辑 ...
* return 0; * return 0;
* } * }
* if (strncmp(param, "save", 4) == 0) { * if (strncmp(param, "save", 4) == 0) {
* // 保存到 Flash * // 保存到 Flash
* snprintf(buf, size, "Calibration saved to flash.\r\n"); * MRobot_Snprintf(buf, size, "Calibration saved to flash.\r\n");
* return 0; * return 0;
* } * }
* return snprintf(buf, size, "Unknown: %s\r\n", param); * return MRobot_Snprintf(buf, size, "Unknown: %s\r\n", param);
* } * }
* *
* // 注册命令 * // 注册命令

View File

@ -18,6 +18,15 @@
/* USER STRUCT END */ /* USER STRUCT END */
/* Private function --------------------------------------------------------- */ /* Private function --------------------------------------------------------- */
/**
* @brief hello
* @note 0 (pdFALSE) 0
*/
static long Cli_Hello(char *buffer, size_t size, const char *cmd) {
(void)cmd; /* 未使用cmd消除警告 */
MRobot_Snprintf(buffer, size, "Ciallo(∠・ω< )⌒★\r\n");
return 0; /* 返回0表示命令执行完成 */
}
/* Exported functions ------------------------------------------------------- */ /* Exported functions ------------------------------------------------------- */
void Task_cli(void *argument) { void Task_cli(void *argument) {
(void)argument; /* 未使用argument消除警告 */ (void)argument; /* 未使用argument消除警告 */
@ -28,6 +37,7 @@ void Task_cli(void *argument) {
/* USER CODE INIT BEGIN */ /* USER CODE INIT BEGIN */
/* 初始化 MRobot CLI 系统 */ /* 初始化 MRobot CLI 系统 */
MRobot_Init(); MRobot_Init();
MRobot_RegisterCommand("hello", " --hello: 打印问候语\r\n", Cli_Hello, -1);
/* USER CODE INIT END */ /* USER CODE INIT END */
while (1) { while (1) {