KmdFoc通信库
大约 5 分钟KmdFoc使用指南
KmdFoc通信库
所有操作函数已经封装为如下形式,用户可以快速的阅览、并操作各种接口,以实现自己的功能
typedef struct
{
bool (*const hal_kmd_disable_motor_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_set_pisotion_hook)(hal_frame_struct *pFrame, uint16_t node_id, float tData);
bool (*const hal_kmd_set_velocity_hook)(hal_frame_struct *pFrame, uint16_t node_id, float tData);
bool (*const hal_kmd_set_current_hook)(hal_frame_struct *pFrame, uint16_t node_id, float tData);
bool (*const hal_kmd_get_position_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_get_velocity_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_get_current_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_get_bus_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_get_version_hook)(hal_frame_struct *pFrame, uint16_t node_id);
struct
{
bool (*const hal_kmd_fsm_motor_disable_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_fsm_motor_enable_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_fsm_motor_reset_error_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_fsm_motor_get_state_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_fsm_motor_cali_start_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_fsm_motor_cali_stop_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_fsm_motor_configs_updata_hook)(hal_frame_struct *pFrame, uint16_t node_id);
bool (*const hal_kmd_fsm_motor_configs_reset_hook)(hal_frame_struct *pFrame, uint16_t node_id)
} kmd_fsm;
struct
{
bool (*const hal_kmd_user_config_set_hook)(hal_frame_struct *pFrame, uint16_t node_id, uint16_t tOrderCmd, uint8_t *pData);
bool (*const hal_kmd_user_config_get_hook)(hal_frame_struct *pFrame, uint16_t node_id, uint16_t tOrderCmd);
} kmd_config;
} hal_kmd_user_struct;
hal_kmd_user_struct tHalKmdUser =
{
.hal_kmd_disable_motor_hook = &hal_kmd_disable_motor,//失能电机
.hal_kmd_set_pisotion_hook = &hal_kmd_set_pisotion,//设置目标位置
.hal_kmd_set_velocity_hook = &hal_kmd_set_velocity,//设置目标转速
.hal_kmd_set_current_hook = &hal_kmd_set_current,//设置目标电流
.hal_kmd_get_position_hook = &hal_kmd_get_position,//目标位置,实际位置消息请求
.hal_kmd_get_velocity_hook = &hal_kmd_get_velocity,//目标转速,实际转速消息请求
.hal_kmd_get_current_hook = &hal_kmd_get_current,//目标电流,实际电流消息请求
.hal_kmd_get_bus_hook = &hal_kmd_get_bus,//总线电压,实际电压消息请求
.hal_kmd_get_version_hook = &hal_kmd_get_version,//获取电机版本号消息请求
.kmd_fsm.hal_kmd_fsm_motor_disable_hook = &hal_kmd_fsm_motor_disable,//失能电机
.kmd_fsm.hal_kmd_fsm_motor_enable_hook = &hal_kmd_fsm_motor_enable,//使能电机
.kmd_fsm.hal_kmd_fsm_motor_reset_error_hook = &hal_kmd_fsm_motor_reset_error,//复位错误
.kmd_fsm.hal_kmd_fsm_motor_get_state_hook = &hal_kmd_fsm_motor_get_state,//获取电机状态消息请求
.kmd_fsm.hal_kmd_fsm_motor_cali_start_hook = &hal_kmd_fsm_motor_cali_start,//开始校准
.kmd_fsm.hal_kmd_fsm_motor_cali_stop_hook = &hal_kmd_fsm_motor_cali_stop,//停止校准
.kmd_fsm.hal_kmd_fsm_motor_configs_updata_hook = &hal_kmd_fsm_motor_configs_updata,//保存参数
.kmd_fsm.hal_kmd_fsm_motor_configs_reset_hook = &hal_kmd_fsm_motor_configs_reset,//复位所有参数
.kmd_config.hal_kmd_user_config_set_hook = &hal_kmd_user_config_set,//设置参数,使用前务必安装正确的数据格式进行参数传递
.kmd_config.hal_kmd_user_config_get_hook = &hal_kmd_user_config_get};//获取参数消息请求
对于设置(hal_kmd_user_config_set)和获取参数(hal_kmd_user_config_get),可以直接使用命令,但是对于同时设置或获取多个参数,通过查表方式更为方便
const uint8_t KMD_CONFIG_MAP_MAX_LENGTH = 33;
const uint8_t kmd_config_map[][2] =
{
// Name,单位,控件类型,CMD,提示
{KMD_TYPE_INT, HAL_USER_CONFIG_MOTOR_POLE_PAIRS}, //"motor_pole_pairs", "Auto","电磁极对数,在校准过程中自动测量,目前支持2~30极对数,无需设置"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_MOTOR_PHASE_RESISTANCE}, //"motor_phase_resistance", "Auto", "电机定子绕组线圈电阻值,在校准过程中自动测量,无需设置。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_MOTOR_PHASE_INDUCTANCE}, //"motor_phase_inductance", "Auto","电机定子线圈相电感值,在校准过程中自动测量,无需设置。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_INERTIA}, //"inertia", "A/(turn/s^2)","inertia", "A/(turn/s^2)","设置转动惯量,电机轴维持以1转每秒的加速度运行时所需要提供的电流值(能量),需要根据电机轴重量和所带负载来进行调试,HAL_CONTROL_MODE_VELOCITY_RAMP和HAL_CONTROL_MODE_POSITION_TRAP控制模式下有效。"},
// Encoder
{KMD_TYPE_INT, HAL_USER_CONFIG_ENCODER_TYPE}, //"encoder_type", "","设置编码器类型,更改编码器之后必须复位和重启,有效更改复位和重启前还应该先保存设置。"},
{KMD_TYPE_INT, HAL_USER_CONFIG_ENCODER_PLL_BW}, //"encoder_pll_bandwidth", "Auto","设置编码器 PLL 带宽,一般对于高分辨率编码器 (> 4000个计数/转) 此值应该越高,这样有助于减少电机振动。对于HALL这种分辨率很低的编码器,不宜设置过高的参数。"},
{KMD_TYPE_INT, HAL_USER_CONFIG_ENCODER_CPR}, //"encoder_cpr", "Auto","设置编码器分辨率,绝对值编码器无需设置,对于HALL编码器必须在校准前设置为(6*极对数)。},
// Calib
{KMD_TYPE_INT, HAL_USER_CONFIG_CALIB_VALID}, //"calib_valid", "Auto", "当前校准数据是否有效,校准完成后设置为True。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_CALIB_CURRENT}, //"calib_current", "A","设置校准时的电流值,设置太小校准时无法顺畅转动,设置过大则可能导致电机发热严重。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_CALIB_MAX_VOLTAGE}, //"calib_max_voltage", "V","设置校准时的最大电压。"},
// Control
{KMD_TYPE_INT, HAL_USER_CONFIG_CONTROL_MODE}, //"control_mode", "","设置电机控制模式,一共有六种模式。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_CURRENT_RAMP_RATE}, //"current_ramp_rate", "A/sec", "设置电流爬升控制模式下电流爬升速度,电机控制模式为CURRENT_RAMP时有效。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_VEL_RAMP_RATE}, //"ramp_rate", "(turn/s)/s", "设置转速爬升模式下转速爬升速度,电机控制模式为VELOCITY_RAMP时有效。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_TRAJ_VEL}, //"traj_vel", "turn/s", "设置梯形轨迹控制模式下最大转速,电机控制模式为POSITION_TRAP时有效。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_TRAJ_ACCEL}, //"traj_accel", "(turn/s)/s", "设置梯形轨迹控制模式下加速度,电机控制模式为POSITION_TRAP时有效。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_TRAJ_DECEL}, //"traj_decel", "(turn/s)/s", "设置梯形轨迹控制模式下减速度,电机控制模式为POSITION_TRAP时有效。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_POS_GAIN}, //"pos_gain", "(turn/s)/turn","设置位置环增益。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_VEL_GAIN}, //"vel_gain", "A/(turn/s)", "设置转速环增益。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_VEL_INTEGRATOR_GAIN}, //"vel_integrator_gain", "A/((turn/s)*s)", "设置转速环积分增益。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_VEL_LIMIT}, //"vel_limit", "turn/s","设置转速环限制。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_CURRENT_LIMIT}, //"current_limit", "A","设置电流环限制。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_CURRENT_CTRL_P_GAIN}, //"current_ctrl_p_gain", "Auto","设置电流环增益,在校准过程中自动计算得到,也可自行设置。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_CURRENT_CTRL_I_GAIN}, //"current_ctrl_i_gain", "Auto", "设置电流环积分增益,在校准过程中自动计算得到,也可自行设置。"},
{KMD_TYPE_INT, HAL_USER_CONFIG_CURRENT_CTRL_BW}, //"current_ctrl_bandwidth", "rad/s","设置电流环滤波带宽,更新带宽时也会同时更新电流环增益和电流环积分增益。[100~2000]"},
// Protect
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_PROTECT_UNDER_VOLTAGE}, //"protect_under_voltage", "V", "设置低压保护阈值。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_PROTECT_OVER_VOLTAGE}, //"protect_over_voltage", "V", "设置过压保护阈值。""},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_PROTECT_OVER_CURRENT}, //"protect_over_current", "A", "设置过流保护阈值。"},
{KMD_TYPE_FLOAT, HAL_USER_CONFIG_PROTECT_OVER_SPEED}, //"protect_over_speed", "turn/s","设置电机超速保护阈值。"},
// CAN
{KMD_TYPE_INT, HAL_USER_CONFIG_CAN_ID}, //"can_id", "","设置设备ID,范围1~16。"},
{KMD_TYPE_INT, HAL_USER_CONFIG_CAN_TIMEOUT_MS}, //"can_timeout_ms", "","设置通信超时时间,超过此时间未进行通信,电机将停止运行,设置为0则不启用。如果启用,设置值需满足大于500ms,默认0。"},
{KMD_TYPE_INT, HAL_USER_CONFIG_CAN_HEARTBEAT_MS}, //"can_report_heart_ms", "","设置设备心跳,设备将周期性上报状态和位置,设置为0则不启用。如果启用,设置值需满足大于2ms,特别的串口方式需要大于10ms,默认0。"},
{KMD_TYPE_INT, HAL_USER_CONFIG_CAN_HEARTBEAT_CH}, //"cen_report_heart_choose", "","选择can心跳周期性更新的数据,默认[(1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)]。"},
// Debug
{KMD_TYPE_INT, HAL_USER_CONFIG_UART_DEBUG_MS}, //"uart_debug_ms", "", "设置串口开启调试,按设定周期打印数据波形。设置为0则不启用。如果启用,设置值需满足大于15ms,默认0。"}
};