增加了CANcomm的守护线程

This commit is contained in:
NeoZng 2023-04-21 13:48:25 +08:00
commit eba6c4fe4f
3 changed files with 55 additions and 11 deletions

View File

@ -284,14 +284,35 @@ void BalanceInit()
Motor_Init_Config_s joint_conf = {
// 写一个,剩下的修改方向和id即可
.can_init_config = {
.can_handle = &hcan1,
},
.controller_param_init_config = {
.current_PID = {
.Kp = 1,
},
},
.controller_setting_init_config = {
.close_loop_type = CURRENT_LOOP,
.outer_loop_type = CURRENT_LOOP,
.motor_reverse_flag = FEEDBACK_DIRECTION_NORMAL,
.angle_feedback_source = MOTOR_FEED,
.speed_feedback_source = MOTOR_FEED,
},
.motor_type = HT04,
};
joint_conf.can_init_config.tx_id = 1;
joint_conf.can_init_config.rx_id = 11;
lf = HTMotorInit(&joint_conf);
rf = HTMotorInit(&joint_conf);
joint_conf.can_init_config.tx_id = 2;
joint_conf.can_init_config.rx_id = 12;
lb = HTMotorInit(&joint_conf);
joint_conf.can_init_config.tx_id = 3;
joint_conf.can_init_config.rx_id = 13;
rf = HTMotorInit(&joint_conf);
joint_conf.can_init_config.tx_id = 4;
joint_conf.can_init_config.rx_id = 14;
rb = HTMotorInit(&joint_conf);
// ↓↓↓---------------驱动电机初始化----------------↓↓↓
@ -300,10 +321,7 @@ void BalanceInit()
.can_init_config.can_handle = &hcan1,
.controller_param_init_config = {
.current_PID = {
.Kp = 1,
.Ki = 0,
.Kd = 0,
.MaxOut = 500,
.Kp = 274.348,
},
},
.controller_setting_init_config = {
@ -311,12 +329,12 @@ void BalanceInit()
.speed_feedback_source = MOTOR_FEED,
.outer_loop_type = CURRENT_LOOP,
.close_loop_type = CURRENT_LOOP,
.motor_reverse_flag = MOTOR_DIRECTION_NORMAL,
},
.motor_type = LK9025,
};
driven_conf.can_init_config.tx_id = 1;
l_driven = LKMotorInit(&driven_conf);
driven_conf.can_init_config.tx_id = 2;
r_driven = LKMotorInit(&driven_conf);

View File

@ -60,6 +60,7 @@ static void CANCommRxCallback(CANInstance *_instance)
{ // 数据量大的话考虑使用DMA
memcpy(comm->unpacked_recv_data, comm->raw_recvbuf + 2, comm->recv_data_len);
comm->update_flag = 1; // 数据更新flag置为1
DaemonReload(comm->comm_daemon); // 重载daemon,避免数据更新后一直不被读取而导致数据更新不及时
}
}
CANCommResetRx(comm);
@ -84,6 +85,13 @@ CANCommInstance *CANCommInit(CANComm_Init_Config_s *comm_config)
comm_config->can_config.id = ins; // CANComm的实例指针作为CANInstance的id,回调函数中会用到
comm_config->can_config.can_module_callback = CANCommRxCallback;
ins->can_ins = CANRegister(&comm_config->can_config);
Daemon_Init_Config_s daemon_config = {
.callback = NULL,
.owner_id = (void *)ins,
.reload_count = comm_config->daemon_count,
};
ins->comm_daemon = DaemonRegister(&daemon_config);
return ins;
}
@ -110,4 +118,9 @@ void *CANCommGet(CANCommInstance *instance)
{
instance->update_flag = 0; // 读取后将更新flag置为0
return instance->unpacked_recv_data;
}
}
uint8_t CANCommIsOnline(CANCommInstance *instance)
{
return DaemonIsOnline(instance->comm_daemon);
}

View File

@ -12,6 +12,7 @@
#define CAN_COMM_H
#include "bsp_can.h"
#include "daemon.h"
#define MX_CAN_COMM_COUNT 4 // 注意均衡负载,一条总线上不要挂载过多的外设
@ -38,6 +39,8 @@ typedef struct
uint8_t recv_state; // 接收状态,
uint8_t cur_recv_len; // 当前已经接收到的数据长度(包括帧头帧尾datalen和校验和)
uint8_t update_flag; // 数据更新标志位,当接收到新数据时,会将此标志位置1,调用CANCommGet()后会将此标志位置0
DaemonInstance* comm_daemon;
} CANCommInstance;
#pragma pack()
@ -47,6 +50,8 @@ typedef struct
CAN_Init_Config_s can_config; // CAN初始化结构体
uint8_t send_data_len; // 发送数据长度
uint8_t recv_data_len; // 接收数据长度
uint16_t daemon_count; // 守护进程计数,用于初始化守护进程
} CANComm_Init_Config_s;
/**
@ -76,4 +81,12 @@ void CANCommSend(CANCommInstance *instance, uint8_t *data);
*/
void *CANCommGet(CANCommInstance *instance);
/**
* @brief CANComm是否在线
*
* @param instance
* @return uint8_t
*/
uint8_t CANCommIsOnline(CANCommInstance *instance);
#endif // !CAN_COMM_H