Browse Source

paint_robot_new【编译通过】电机消息中心和独立线程已实现

master
Lizongdi 2 weeks ago
parent
commit
793bf2b466
  1. 2
      CMakeLists.txt
  2. 12
      RBcore/BHBF.c
  3. 6
      RBcore/CMakeLists.txt
  4. 16
      RBcore/drv_interface.c
  5. 44
      RBcore/include/BHBF.h
  6. 378
      RBcore/msg_example.c
  7. 6
      motor/CMakeLists.txt
  8. 2
      motor/include/motor_manager.h
  9. 159
      motor/motor_example.c
  10. 1
      motor/msp_motor_leisai.c
  11. 2
      optional/msp_MK32.c
  12. 2
      optional/msp_TI5MOTOR.c
  13. 12
      project/paint_robot_new/CMakeLists.txt
  14. 2
      project/paint_robot_new/include/paint_robot_new.h
  15. 74
      project/paint_robot_new/paint_robot_new.c

2
CMakeLists.txt

@ -117,8 +117,8 @@ add_module(ringbuffer library/ringbuffer OFF OFF)
add_module(peripheral peripheral OFF ON)
add_module(lua lua OFF ON)
add_module(bspMCU bspMCU OFF OFF)
add_module(motor motor OFF OFF)
add_module(RBcore RBcore OFF OFF)
add_module(motor motor OFF OFF)
add_module(optional optional OFF OFF)
add_module(Spoolend project/Spoolend OFF OFF)
add_module(paint_robot_new project/paint_robot_new OFF OFF)

12
RBcore/BHBF_robot.c → RBcore/BHBF.c

@ -3,7 +3,7 @@
(C), 2018-2099, Radkil
******************************************************************************
: BHBF_robot.c
: BHBF.c
: 稿
: radkil
: 202668
@ -16,11 +16,9 @@
:
******************************************************************************/
#include "BHBF_robot.h"
#include "BHBF.h"
#include "common.h"
#include "cmsis_os.h"
#include "FreeRTOS.h"
#include "task.h"
/*----------------------------------------------*
@ -73,15 +71,13 @@ void GF_Dispatch(void *argument)
{
while(1)
{
GF_CMD GFCmd = MK32_Task(GV.m_iWorkMode);
GF_FsmStart(GFCmd);
//GF_CMD GFCmd = MK32_Task(GV.m_iWorkMode);
GF_FsmStart(0);
}
}
void GF_Init(void)
{
Drv_InterfaceInit();
// const osThreadAttr_t GF_Dispatch_attributes = {
// .name = "GF_Task",
// .stack_size = 1024,

6
RBcore/CMakeLists.txt

@ -9,8 +9,8 @@ else()
message(FATAL_ERROR "Cannot find common build logic at ${COMMON_CMAKE_PATH}")
endif()
if(TARGET motor)
target_link_libraries(${TARGET_NAME} PUBLIC motor)
if(TARGET bspMCU)
target_link_libraries(${TARGET_NAME} PUBLIC bspMCU)
else()
message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'motor' not found.")
message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'bspMCU' not found.")
endif()

16
RBcore/drv_interface.c

@ -16,11 +16,12 @@
:
******************************************************************************/
#include "BHBF_robot.h"
#include "BHBF.h"
#include "cmsis_os.h"
#include "FreeRTOS.h"
#include "msg_center.h"
/*----------------------------------------------*
* *
*----------------------------------------------*/
@ -255,12 +256,9 @@ void SendAll_Task(void *argument)
}
extern void MotorInit(void);
void MotorTask(void *argument);
void Drv_InterfaceInit(void)
{
//MK32_Init();
const osThreadAttr_t Read_RS485_1_attributes = {
.name = "RS485_1",
.stack_size = 512,
@ -275,14 +273,6 @@ void Drv_InterfaceInit(void)
};
(void)osThreadNew(SendAll_Task, NULL, &send_task_attributes);
// 初始化电机
MsgCenter_Init();
MotorInit();
// 创建电机任务
const osThreadAttr_t motor_task_attributes = {
.name = "motor_task",
.stack_size = 1024,
.priority = (osPriority_t) osPriorityHigh5,
};
(void)osThreadNew(MotorTask, NULL, &motor_task_attributes);
}

44
RBcore/include/BHBF_robot.h → RBcore/include/BHBF.h

@ -3,12 +3,12 @@
(C), 2018-2099, Radkil
******************************************************************************
: BHBF_robot.h
: BHBF.h
: 稿
: radkil
: 202668
:
: BHBF_robot.c
: BHBF.c
:
1. : 202668
@ -45,8 +45,8 @@
* *
*----------------------------------------------*/
#ifndef __BHBF_ROBOT_H__
#define __BHBF_ROBOT_H__
#ifndef __BHBF_H__
#define __BHBF_H__
#ifdef __cplusplus
@ -61,11 +61,41 @@ extern "C"{
*----------------------------------------------*/
#include "drv_interface.h"
#include "rd_time.h"
#include "msg_center.h"
#include "cmsis_os.h"
#include "FreeRTOS.h"
/*==============================================*
* constants or macros define *
*----------------------------------------------*/
#define MODULE_NAME_MOTOR "motor"
typedef enum {
MOTOR_CMD_SET_SPEED = 0x01, // 设置速度
MOTOR_CMD_SET_POSITION = 0x02, // 设置位置
MOTOR_CMD_STOP = 0x03, // 停止
MOTOR_CMD_GET_STATUS = 0x04, // 获取状态
MOTOR_CMD_SET_HOME = 0x05, // 设置零点
MOTOR_CMD_SPEED_MODE = 0x06, // 速度模式
MOTOR_CMD_POSITION_MODE = 0x07, // 位置模式
} Motor_Cmd_e;
// 电机命令数据结构
typedef struct {
uint8_t m_ucMotorIndex; // 电机索引(0=左轮,1=右轮)
int32_t m_iValue; // 值(速度/位置等)
} Motor_CmdData_t;
// 电机状态数据结构
typedef struct {
uint8_t m_ucMotorIndex;
int32_t m_iPosition;
int32_t m_iVelocity;
uint32_t m_uiFaultCode;
} Motor_StatusData_t;
#define MODULE_NAME_RBCORE "RBcore"
typedef enum {
GF_CMD_DEFAULT_NULL = 0,
GF_CMD_STOP_ALL,
@ -93,10 +123,6 @@ typedef struct _GV_struct_define
GV_define *GetGV(void);
void SetGV(GV_define *_pGV);
// 待实现接口
void MK32_Init(void);
GF_CMD MK32_Task(int mode);
void MoveWheel_Init(void);
void MoveWheel_Task(int32_t Target_Velcity[]);
#ifdef __cplusplus
#if __cplusplus

378
RBcore/msg_example.c

@ -1,378 +0,0 @@
/******************************************************************************
(C), 2018-2099, Radkil
******************************************************************************
: msg_example.c
: 稿
: radkil
: 2026713
:
: 使
:
1. : 2026713
: radkil
:
******************************************************************************/
#include "BHBF_robot.h"
#include "msg_center.h"
#include "motor_manager.h"
#include "msp_motor_leisai.h"
#include "cmsis_os.h"
#include "FreeRTOS.h"
/*==============================================*
* *
*==============================================*/
// 模块名称定义
#define MODULE_NAME_MOTOR "motor"
#define MODULE_NAME_CHASSIS "chassis"
#define MODULE_NAME_UI "ui"
/*==============================================*
* *
*==============================================*/
typedef enum {
MOTOR_CMD_SET_SPEED = 0x01, // 设置速度
MOTOR_CMD_SET_POSITION = 0x02, // 设置位置
MOTOR_CMD_STOP = 0x03, // 停止
MOTOR_CMD_GET_STATUS = 0x04, // 获取状态
MOTOR_CMD_SET_HOME = 0x05, // 设置零点
MOTOR_CMD_SPEED_MODE = 0x06, // 速度模式
MOTOR_CMD_POSITION_MODE = 0x07, // 位置模式
} Motor_Cmd_e;
// 电机命令数据结构
typedef struct {
uint8_t m_ucMotorIndex; // 电机索引(0=左轮,1=右轮)
int32_t m_iValue; // 值(速度/位置等)
} Motor_CmdData_t;
// 电机状态数据结构
typedef struct {
uint8_t m_ucMotorIndex;
int32_t m_iPosition;
int32_t m_iVelocity;
uint32_t m_uiFaultCode;
} Motor_StatusData_t;
/*==============================================*
* *
*==============================================*/
typedef enum {
CHASSIS_CMD_MOVE = 0x01, // 移动
CHASSIS_CMD_STOP = 0x02, // 停止
CHASSIS_CMD_SET_SPEED = 0x03, // 设置速度
} Chassis_Cmd_e;
typedef struct {
int32_t m_iSpeedX; // X方向速度
int32_t m_iSpeedY; // Y方向速度
int32_t m_iSpeedW; // 旋转速度
} Chassis_MoveData_t;
/*==============================================*
* *
*==============================================*/
static uint32_t g_uiMotorModuleID = 0;
static uint32_t g_uiChassisModuleID = 0;
static uint32_t g_uiUIModuleID = 0;
static Motor_Instance_t *g_apstMotors[2] = {NULL, NULL};
/*==============================================*
* *
*==============================================*/
static void Motor_ModuleHandler(const Msg_t *pstMsg)
{
if (NULL == pstMsg)
{
return;
}
switch (pstMsg->m_uiMsgID)
{
case MOTOR_CMD_SET_SPEED:
{
if (pstMsg->m_uiDataLen >= sizeof(Motor_CmdData_t))
{
Motor_CmdData_t *pstData = (Motor_CmdData_t *)pstMsg->m_aucData;
if (pstData->m_ucMotorIndex < 2 && g_apstMotors[pstData->m_ucMotorIndex] != NULL)
{
MotorMgr_SetTargetSpeed(g_apstMotors[pstData->m_ucMotorIndex], pstData->m_iValue);
}
}
break;
}
case MOTOR_CMD_STOP:
{
for (int i = 0; i < 2; i++)
{
if (g_apstMotors[i] != NULL)
{
MotorMgr_SetTargetSpeed(g_apstMotors[i], 0);
}
}
break;
}
case MOTOR_CMD_SET_HOME:
{
if (pstMsg->m_uiDataLen >= sizeof(Motor_CmdData_t))
{
Motor_CmdData_t *pstData = (Motor_CmdData_t *)pstMsg->m_aucData;
if (pstData->m_ucMotorIndex < 2 && g_apstMotors[pstData->m_ucMotorIndex] != NULL)
{
MotorMgr_SetHome(g_apstMotors[pstData->m_ucMotorIndex]);
}
}
break;
}
case MOTOR_CMD_GET_STATUS:
{
// 回复状态给请求者
Motor_StatusData_t stStatus;
for (int i = 0; i < 2; i++)
{
if (g_apstMotors[i] != NULL)
{
const Motor_Data_t *pstData = MotorMgr_GetData(g_apstMotors[i]);
if (pstData != NULL)
{
stStatus.m_ucMotorIndex = i;
stStatus.m_iPosition = pstData->m_iRealPosition;
stStatus.m_iVelocity = pstData->m_iRealVelocity;
stStatus.m_uiFaultCode = pstData->m_uiFaultCode;
// 回复给请求者
MsgCenter_SendTo("ui", 0x10, &stStatus, sizeof(stStatus));
}
}
}
break;
}
default:
break;
}
}
/*==============================================*
* *
*==============================================*/
static void Chassis_ModuleHandler(const Msg_t *pstMsg)
{
if (NULL == pstMsg)
{
return;
}
switch (pstMsg->m_uiMsgID)
{
case CHASSIS_CMD_MOVE:
{
if (pstMsg->m_uiDataLen >= sizeof(Chassis_MoveData_t))
{
Chassis_MoveData_t *pstData = (Chassis_MoveData_t *)pstMsg->m_aucData;
// 转换为左右轮速度(差速模型)
int32_t iLeftSpeed = pstData->m_iSpeedX - pstData->m_iSpeedW;
int32_t iRightSpeed = pstData->m_iSpeedX + pstData->m_iSpeedW;
// 发送给电机模块
Motor_CmdData_t stMotorCmd;
stMotorCmd.m_ucMotorIndex = 0;
stMotorCmd.m_iValue = iLeftSpeed;
MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_SET_SPEED, &stMotorCmd, sizeof(stMotorCmd));
stMotorCmd.m_ucMotorIndex = 1;
stMotorCmd.m_iValue = iRightSpeed;
MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_SET_SPEED, &stMotorCmd, sizeof(stMotorCmd));
}
break;
}
case CHASSIS_CMD_STOP:
{
MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_STOP, NULL, 0);
break;
}
default:
break;
}
}
/*==============================================*
* UI模块消息处理 *
*==============================================*/
static void UI_ModuleHandler(const Msg_t *pstMsg)
{
if (NULL == pstMsg)
{
return;
}
// 处理来自其他模块的回复
switch (pstMsg->m_uiMsgID)
{
case 0x10: // 电机状态回复
{
if (pstMsg->m_uiDataLen >= sizeof(Motor_StatusData_t))
{
Motor_StatusData_t *pstStatus = (Motor_StatusData_t *)pstMsg->m_aucData;
RD_PRINTF("Motor[%d] Status: Pos=%ld, Vel=%ld, Fault=%ld\n",
pstStatus->m_ucMotorIndex,
pstStatus->m_iPosition,
pstStatus->m_iVelocity,
pstStatus->m_uiFaultCode);
}
break;
}
default:
break;
}
}
/*==============================================*
* *
*==============================================*/
// 电机模块任务
void Motor_ModuleTask(void *argument)
{
// 初始化电机
MotorMgr_Init();
Motor_Config_t stConfig = {
.m_ucMotorID = 1,
.m_uiPulsePerRound = 10000,
.m_uiReductionRatio = 70,
.m_fWheelDiameter = 0.26f
};
g_apstMotors[0] = MotorMgr_Create(&stConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL);
stConfig.m_ucMotorID = 2;
g_apstMotors[1] = MotorMgr_Create(&stConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL);
// 初始化电机
for (int i = 0; i < 2; i++)
{
if (g_apstMotors[i] != NULL)
{
MotorMgr_ResetAll(g_apstMotors[i]);
MotorMgr_ActivateAll(g_apstMotors[i]);
MotorMgr_SpeedModeInit(g_apstMotors[i]);
}
}
while (1)
{
// 处理消息
MsgCenter_ProcessWait(g_uiMotorModuleID, 2);
// 周期性读取电机状态
for (int i = 0; i < 2; i++)
{
if (g_apstMotors[i] != NULL)
{
MotorMgr_RequestPosition(g_apstMotors[i]);
MotorMgr_RequestVelocity(g_apstMotors[i]);
MotorMgr_RequestFault(g_apstMotors[i]);
}
}
}
}
// 底盘模块任务
void Chassis_ModuleTask(void *argument)
{
while (1)
{
// 处理消息
MsgCenter_ProcessWait(g_uiChassisModuleID, 10);
}
}
// UI模块任务
void UI_ModuleTask(void *argument)
{
while (1)
{
// 处理消息
MsgCenter_ProcessWait(g_uiUIModuleID, 100);
// 定期请求电机状态
MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_GET_STATUS, NULL, 0);
}
}
/*==============================================*
* *
*==============================================*/
void MsgExample_Init(void)
{
// 初始化消息中心
MsgCenter_Init();
// 注册模块
g_uiMotorModuleID = MsgCenter_Register(MODULE_NAME_MOTOR, Motor_ModuleHandler);
g_uiChassisModuleID = MsgCenter_Register(MODULE_NAME_CHASSIS, Chassis_ModuleHandler);
g_uiUIModuleID = MsgCenter_Register(MODULE_NAME_UI, UI_ModuleHandler);
// 创建模块任务
const osThreadAttr_t motor_task_attr = {
.name = "motor_task",
.stack_size = 1024,
.priority = (osPriority_t) osPriorityHigh5,
};
(void)osThreadNew(Motor_ModuleTask, NULL, &motor_task_attr);
const osThreadAttr_t chassis_task_attr = {
.name = "chassis_task",
.stack_size = 512,
.priority = (osPriority_t) osPriorityHigh4,
};
(void)osThreadNew(Chassis_ModuleTask, NULL, &chassis_task_attr);
const osThreadAttr_t ui_task_attr = {
.name = "ui_task",
.stack_size = 512,
.priority = (osPriority_t) osPriorityNormal3,
};
(void)osThreadNew(UI_ModuleTask, NULL, &ui_task_attr);
}
/*==============================================*
* *
*==============================================*/
// 从任何线程调用这些函数即可跨线程控制电机
void Example_SetMotorSpeed(uint8_t ucMotorIndex, int32_t iSpeed)
{
Motor_CmdData_t stData;
stData.m_ucMotorIndex = ucMotorIndex;
stData.m_iValue = iSpeed;
MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_SET_SPEED, &stData, sizeof(stData));
}
void Example_StopAllMotors(void)
{
MsgCenter_SendTo(MODULE_NAME_MOTOR, MOTOR_CMD_STOP, NULL, 0);
}
void Example_ChassisMove(int32_t iSpeedX, int32_t iSpeedW)
{
Chassis_MoveData_t stData;
stData.m_iSpeedX = iSpeedX;
stData.m_iSpeedY = 0;
stData.m_iSpeedW = iSpeedW;
MsgCenter_SendTo(MODULE_NAME_CHASSIS, CHASSIS_CMD_MOVE, &stData, sizeof(stData));
}

6
motor/CMakeLists.txt

@ -9,8 +9,8 @@ else()
message(FATAL_ERROR "Cannot find common build logic at ${COMMON_CMAKE_PATH}")
endif()
if(TARGET bspMCU)
target_link_libraries(${TARGET_NAME} PUBLIC bspMCU)
if(TARGET RBcore)
target_link_libraries(${TARGET_NAME} PUBLIC RBcore)
else()
message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'bspMCU' not found.")
message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'RBcore' not found.")
endif()

2
motor/include/motor_manager.h

@ -34,7 +34,7 @@ extern "C"{
/*==============================================*
* constants or macros define *
*----------------------------------------------*/
#define MOTOR_INSTANCE_MAX 8 //支持最大电机实例数
/*==============================================*
* data structures *
*----------------------------------------------*/

159
project/paint_robot_new/motor_example.c → motor/motor_example.c

@ -16,8 +16,7 @@
:
******************************************************************************/
#include "BHBF_robot.h"
#include "BHBF.h"
#include "motor_manager.h"
#include "msp_motor_leisai.h"
@ -40,7 +39,8 @@
/*----------------------------------------------*
* *
*----------------------------------------------*/
static Motor_Instance_t *g_apstMotors[MOTOR_INSTANCE_MAX] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
static uint32_t g_uiMotorModuleID = 0;
/*----------------------------------------------*
* *
*----------------------------------------------*/
@ -49,6 +49,7 @@
* *
*----------------------------------------------*/
#if 0
/*****************************************************************************
: MotorExample_LeiSai
: 使
@ -179,3 +180,155 @@ void MotorExample_MultiProtocol(void)
RD_PRINTF("Found motor: %s\n", pstFound->m_szName);
}
}
#endif
void MotorTask(void *argument)
{
// 等待系统稳定
Rd_Delay(1000);
// 复位并激活所有电机
for (int i = 0; i < MOTOR_INSTANCE_MAX; i++)
{
if (g_apstMotors[i] != NULL)
{
MotorMgr_ResetAll(g_apstMotors[i]);
Rd_Delay(500);
MotorMgr_ActivateAll(g_apstMotors[i]);
Rd_Delay(500);
MotorMgr_SetHome(g_apstMotors[i]);
Rd_Delay(100);
MotorMgr_SpeedModeInit(g_apstMotors[i]);
}
}
while(1)
{
// 处理消息
MsgCenter_ProcessWait(g_uiMotorModuleID, 2);
for (int i = 0; i < MOTOR_INSTANCE_MAX; i++)
{
if (g_apstMotors[i] != NULL)
{
// 读取位置、速度、故障
MotorMgr_RequestPosition(g_apstMotors[i]);
MotorMgr_RequestFault(g_apstMotors[i]);
MotorMgr_RequestVelocity(g_apstMotors[i]);
}
}
Rd_Delay(2); // 2ms周期
}
}
/*==============================================*
* *
*==============================================*/
static void Motor_ModuleHandler(const Msg_t *pstMsg)
{
if (NULL == pstMsg)
{
return;
}
switch (pstMsg->m_uiMsgID)
{
case MOTOR_CMD_SET_SPEED:
{
if (pstMsg->m_uiDataLen >= sizeof(Motor_CmdData_t))
{
Motor_CmdData_t *pstData = (Motor_CmdData_t *)pstMsg->m_aucData;
if (pstData->m_ucMotorIndex < 2 && g_apstMotors[pstData->m_ucMotorIndex] != NULL)
{
MotorMgr_SetTargetSpeed(g_apstMotors[pstData->m_ucMotorIndex], pstData->m_iValue);
}
}
break;
}
case MOTOR_CMD_STOP:
{
for (int i = 0; i < 2; i++)
{
if (g_apstMotors[i] != NULL)
{
MotorMgr_SetTargetSpeed(g_apstMotors[i], 0);
}
}
break;
}
case MOTOR_CMD_SET_HOME:
{
if (pstMsg->m_uiDataLen >= sizeof(Motor_CmdData_t))
{
Motor_CmdData_t *pstData = (Motor_CmdData_t *)pstMsg->m_aucData;
if (pstData->m_ucMotorIndex < 2 && g_apstMotors[pstData->m_ucMotorIndex] != NULL)
{
MotorMgr_SetHome(g_apstMotors[pstData->m_ucMotorIndex]);
}
}
break;
}
case MOTOR_CMD_GET_STATUS:
{
// 回复状态给请求者
Motor_StatusData_t stStatus;
for (int i = 0; i < 2; i++)
{
if (g_apstMotors[i] != NULL)
{
const Motor_Data_t *pstData = MotorMgr_GetData(g_apstMotors[i]);
if (pstData != NULL)
{
stStatus.m_ucMotorIndex = i;
stStatus.m_iPosition = pstData->m_iRealPosition;
stStatus.m_iVelocity = pstData->m_iRealVelocity;
stStatus.m_uiFaultCode = pstData->m_uiFaultCode;
// 回复给请求者
MsgCenter_SendTo(MODULE_NAME_RBCORE, 0x10, &stStatus, sizeof(stStatus));
}
}
}
break;
}
default:
break;
}
}
void MotorInit(void)
{
// 初始化电机管理模块
MotorMgr_Init();
// 配置左轮电机
Motor_Config_t stLeftMotorConfig = {
.m_ucMotorID = 1,
.m_uiPulsePerRound = 10000,
.m_uiReductionRatio = 70,
.m_fWheelDiameter = 0.26f
};
g_apstMotors[0] = MotorMgr_Create(&stLeftMotorConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL);
// 配置右轮电机
Motor_Config_t stRightMotorConfig = {
.m_ucMotorID = 2,
.m_uiPulsePerRound = 10000,
.m_uiReductionRatio = 70,
.m_fWheelDiameter = 0.26f
};
g_apstMotors[1] = MotorMgr_Create(&stRightMotorConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL);
g_uiMotorModuleID = MsgCenter_Register(MODULE_NAME_MOTOR, Motor_ModuleHandler);
// 创建电机任务
const osThreadAttr_t motor_task_attributes = {
.name = "motor_task",
.stack_size = 1024,
.priority = (osPriority_t) osPriorityHigh5,
};
(void)osThreadNew(MotorTask, NULL, &motor_task_attributes);
}

1
motor/msp_motor_leisai.c

@ -18,7 +18,6 @@
******************************************************************************/
#include "msp_motor_leisai.h"
#include <math.h>
/*----------------------------------------------*
* *

2
optional/msp_MK32.c

@ -17,7 +17,7 @@
******************************************************************************/
#include "msp_MK32.h"
#include "BHBF_robot.h"
#include "BHBF.h"
#include <math.h>

2
optional/msp_TI5MOTOR.c

@ -17,7 +17,7 @@
******************************************************************************/
#include "msp_TI5MOTOR.h"
#include "BHBF_robot.h"
#include "BHBF.h"
/*----------------------------------------------*
* *

12
project/paint_robot_new/CMakeLists.txt

@ -9,14 +9,8 @@ else()
message(FATAL_ERROR "Cannot find common build logic at ${COMMON_CMAKE_PATH}")
endif()
if(TARGET RBcore)
target_link_libraries(${TARGET_NAME} PUBLIC RBcore)
if(TARGET bspMCU)
target_link_libraries(${TARGET_NAME} PUBLIC bspMCU)
else()
message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'RBcore' not found.")
endif()
if(TARGET peripheral)
target_link_libraries(${TARGET_NAME} PUBLIC peripheral)
else()
message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'peripheral' not found.")
message(FATAL_ERROR "[${TARGET_NAME}] Dependency 'bspMCU' not found.")
endif()

2
project/paint_robot_new/include/paint_robot_new.h

@ -16,8 +16,6 @@
:
******************************************************************************/
#include "motor_manager.h"
#include "msp_motor_leisai.h"
/*----------------------------------------------*
* *

74
project/paint_robot_new/paint_robot_new.c

@ -17,12 +17,10 @@
******************************************************************************/
#include "paint_robot_new.h"
#include "com.h"
#include "rd_time.h"
/*----------------------------------------------*
* *
*----------------------------------------------*/
extern TComCtrl *g_ptFDCAN1;
/*----------------------------------------------*
* *
@ -35,8 +33,6 @@ extern TComCtrl *g_ptFDCAN1;
/*----------------------------------------------*
* *
*----------------------------------------------*/
static Motor_Instance_t *g_pstLeftMotor; // 左轮电机实例
static Motor_Instance_t *g_pstRightMotor; // 右轮电机实例
/*----------------------------------------------*
* *
@ -50,73 +46,5 @@ static Motor_Instance_t *g_pstRightMotor; // 右轮电机实例
* *
*----------------------------------------------*/
void MotorInit(void)
{
// 初始化电机管理模块
MotorMgr_Init();
// 配置左轮电机
Motor_Config_t stLeftMotorConfig = {
.m_ucMotorID = 1,
.m_uiPulsePerRound = 10000,
.m_uiReductionRatio = 70,
.m_fWheelDiameter = 0.26f
};
g_pstLeftMotor = MotorMgr_Create(&stLeftMotorConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL);
// 配置右轮电机
Motor_Config_t stRightMotorConfig = {
.m_ucMotorID = 2,
.m_uiPulsePerRound = 10000,
.m_uiReductionRatio = 70,
.m_fWheelDiameter = 0.26f
};
g_pstRightMotor = MotorMgr_Create(&stRightMotorConfig, g_ptFDCAN1, LeiSai_GetProtocol(), NULL);
}
void MotorTask(void *argument)
{
// 等待系统稳定
Rd_Delay(1000);
// 复位并激活所有电机
if (g_pstLeftMotor != NULL)
{
MotorMgr_ResetAll(g_pstLeftMotor);
Rd_Delay(500);
MotorMgr_ActivateAll(g_pstLeftMotor);
Rd_Delay(500);
MotorMgr_SetHome(g_pstLeftMotor);
Rd_Delay(100);
MotorMgr_SpeedModeInit(g_pstLeftMotor);
}
while(1)
{
if (g_pstLeftMotor != NULL)
{
// 读取位置、速度、故障
MotorMgr_RequestPosition(g_pstLeftMotor);
MotorMgr_RequestFault(g_pstLeftMotor);
MotorMgr_RequestVelocity(g_pstLeftMotor);
// 设置目标速度
MotorMgr_SetTargetSpeed(g_pstLeftMotor, 0);
}
if (g_pstRightMotor != NULL)
{
// 读取位置、速度、故障
MotorMgr_RequestPosition(g_pstRightMotor);
MotorMgr_RequestFault(g_pstRightMotor);
MotorMgr_RequestVelocity(g_pstRightMotor);
// 设置目标速度
MotorMgr_SetTargetSpeed(g_pstRightMotor, 0);
}
Rd_Delay(2); // 2ms周期
}
}

Loading…
Cancel
Save