|
|
|
@ -18,6 +18,7 @@ |
|
|
|
******************************************************************************/ |
|
|
|
|
|
|
|
#include "motor_manager.h" |
|
|
|
#include "rd_thread.h" |
|
|
|
|
|
|
|
/*----------------------------------------------*
|
|
|
|
* 外部变量说明 * |
|
|
|
@ -39,6 +40,7 @@ |
|
|
|
* 模块级变量 * |
|
|
|
*----------------------------------------------*/ |
|
|
|
static rd_slist_t g_stMotorList; // 电机链表头
|
|
|
|
static rd_mutex_t g_stMotorLock = RD_MUTEX_INITIALIZER; // 电机链表互斥锁(保护g_stMotorList的增删查)
|
|
|
|
|
|
|
|
/*----------------------------------------------*
|
|
|
|
* 常量定义 * |
|
|
|
@ -70,6 +72,7 @@ static rd_slist_t g_stMotorList; // 电机链表头 |
|
|
|
*****************************************************************************/ |
|
|
|
void MotorMgr_Init(void) |
|
|
|
{ |
|
|
|
(void)Rd_MutexInit(&g_stMotorLock); |
|
|
|
rd_slist_init(&g_stMotorList); |
|
|
|
} |
|
|
|
|
|
|
|
@ -91,7 +94,7 @@ void MotorMgr_Init(void) |
|
|
|
修改内容 : 新生成函数 |
|
|
|
|
|
|
|
*****************************************************************************/ |
|
|
|
Motor_Instance_t* MotorMgr_Create(const Motor_Config_t *pstConfig, |
|
|
|
Motor_Instance_t* MotorMgr_Create(const Motor_Config_t *pstConfig, |
|
|
|
TComCtrl *ptCAN, |
|
|
|
const Motor_Protocol_t *pstProtocol, |
|
|
|
void *pstPrivData) |
|
|
|
@ -101,6 +104,10 @@ Motor_Instance_t* MotorMgr_Create(const Motor_Config_t *pstConfig, |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
Motor_Instance_t *pstMotor = NULL; |
|
|
|
|
|
|
|
Rd_MutexLock(&g_stMotorLock, __func__, NULL); |
|
|
|
|
|
|
|
// 检查是否已存在相同ID的电机
|
|
|
|
rd_slist_t *pos; |
|
|
|
rd_slist_t *n; |
|
|
|
@ -109,15 +116,15 @@ Motor_Instance_t* MotorMgr_Create(const Motor_Config_t *pstConfig, |
|
|
|
Motor_Instance_t *pstCheck = rd_slist_entry(pos, Motor_Instance_t, node); |
|
|
|
if (pstCheck->m_stConfig.m_ucMotorID == pstConfig->m_ucMotorID) |
|
|
|
{ |
|
|
|
return NULL; // ID重复
|
|
|
|
goto END; // ID重复
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 分配内存
|
|
|
|
Motor_Instance_t *pstMotor = (Motor_Instance_t *)RD_CALLOC(1, sizeof(Motor_Instance_t)); |
|
|
|
pstMotor = (Motor_Instance_t *)RD_CALLOC(1, sizeof(Motor_Instance_t)); |
|
|
|
if (NULL == pstMotor) |
|
|
|
{ |
|
|
|
return NULL; |
|
|
|
goto END; |
|
|
|
} |
|
|
|
|
|
|
|
// 初始化链表节点
|
|
|
|
@ -142,6 +149,8 @@ Motor_Instance_t* MotorMgr_Create(const Motor_Config_t *pstConfig, |
|
|
|
// 添加到链表尾部
|
|
|
|
rd_slist_append(&g_stMotorList, &pstMotor->node); |
|
|
|
|
|
|
|
END: |
|
|
|
Rd_MutexUnlock(&g_stMotorLock, __func__, NULL); |
|
|
|
return pstMotor; |
|
|
|
} |
|
|
|
|
|
|
|
@ -167,8 +176,11 @@ int MotorMgr_Destroy(Motor_Instance_t *pstMotor) |
|
|
|
return RD_NULL; |
|
|
|
} |
|
|
|
|
|
|
|
// 从链表中移除
|
|
|
|
// 从链表中移除(节点摘除后即可释放,FREE放在锁外减少持锁时间)
|
|
|
|
Rd_MutexLock(&g_stMotorLock, __func__, NULL); |
|
|
|
rd_slist_remove(&g_stMotorList, &pstMotor->node); |
|
|
|
Rd_MutexUnlock(&g_stMotorLock, __func__, NULL); |
|
|
|
|
|
|
|
RD_FREE(pstMotor); |
|
|
|
|
|
|
|
return RD_SUCCESS; |
|
|
|
@ -196,6 +208,9 @@ Motor_Instance_t* MotorMgr_FindByName(const char *pszName) |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
Motor_Instance_t *pstResult = NULL; |
|
|
|
|
|
|
|
Rd_MutexLock(&g_stMotorLock, __func__, NULL); |
|
|
|
rd_slist_t *pos; |
|
|
|
rd_slist_t *n; |
|
|
|
rd_slist_for_each(pos, n, &g_stMotorList) |
|
|
|
@ -203,10 +218,13 @@ Motor_Instance_t* MotorMgr_FindByName(const char *pszName) |
|
|
|
Motor_Instance_t *pstMotor = rd_slist_entry(pos, Motor_Instance_t, node); |
|
|
|
if (RD_STRCMP(pstMotor->m_szName, pszName) == 0) |
|
|
|
{ |
|
|
|
return pstMotor; |
|
|
|
pstResult = pstMotor; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
return NULL; |
|
|
|
Rd_MutexUnlock(&g_stMotorLock, __func__, NULL); |
|
|
|
|
|
|
|
return pstResult; |
|
|
|
} |
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
@ -226,6 +244,9 @@ Motor_Instance_t* MotorMgr_FindByName(const char *pszName) |
|
|
|
*****************************************************************************/ |
|
|
|
Motor_Instance_t* MotorMgr_FindByID(uint8_t ucMotorID) |
|
|
|
{ |
|
|
|
Motor_Instance_t *pstResult = NULL; |
|
|
|
|
|
|
|
Rd_MutexLock(&g_stMotorLock, __func__, NULL); |
|
|
|
rd_slist_t *pos; |
|
|
|
rd_slist_t *n; |
|
|
|
rd_slist_for_each(pos, n, &g_stMotorList) |
|
|
|
@ -233,10 +254,13 @@ Motor_Instance_t* MotorMgr_FindByID(uint8_t ucMotorID) |
|
|
|
Motor_Instance_t *pstMotor = rd_slist_entry(pos, Motor_Instance_t, node); |
|
|
|
if (pstMotor->m_stConfig.m_ucMotorID == ucMotorID) |
|
|
|
{ |
|
|
|
return pstMotor; |
|
|
|
pstResult = pstMotor; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
return NULL; |
|
|
|
Rd_MutexUnlock(&g_stMotorLock, __func__, NULL); |
|
|
|
|
|
|
|
return pstResult; |
|
|
|
} |
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
|