From ac23dadba11e51064707e4b7af26daa85196729b Mon Sep 17 00:00:00 2001 From: zhaohe Date: Fri, 29 Sep 2023 10:41:43 +0800 Subject: [PATCH] update --- .../xy_robot_ctrl_module/xy_robot_ctrl_module.hpp | 4 +- components/zcancmder/cmd/basic.hpp | 101 ---- components/zcancmder/cmd/cmd.hpp | 547 -------------------- components/zcancmder/zcanreceiver.cpp | 355 ------------- components/zcancmder/zcanreceiver.hpp | 126 ----- components/zcancmder_2/cmd/basic.hpp | 101 ++++ components/zcancmder_2/cmd/cmd.hpp | 549 +++++++++++++++++++++ components/zcancmder_2/zcanreceiver.cpp | 355 +++++++++++++ components/zcancmder_2/zcanreceiver.hpp | 126 +++++ 9 files changed, 1134 insertions(+), 1130 deletions(-) delete mode 100644 components/zcancmder/cmd/basic.hpp delete mode 100644 components/zcancmder/cmd/cmd.hpp delete mode 100644 components/zcancmder/zcanreceiver.cpp delete mode 100644 components/zcancmder/zcanreceiver.hpp create mode 100644 components/zcancmder_2/cmd/basic.hpp create mode 100644 components/zcancmder_2/cmd/cmd.hpp create mode 100644 components/zcancmder_2/zcanreceiver.cpp create mode 100644 components/zcancmder_2/zcanreceiver.hpp diff --git a/components/xy_robot_ctrl_module/xy_robot_ctrl_module.hpp b/components/xy_robot_ctrl_module/xy_robot_ctrl_module.hpp index ef92488..f671bc6 100644 --- a/components/xy_robot_ctrl_module/xy_robot_ctrl_module.hpp +++ b/components/xy_robot_ctrl_module/xy_robot_ctrl_module.hpp @@ -4,6 +4,8 @@ #include "sdk\components\tmc\basic\tmc_ic_interface.hpp" #include "sdk\components\zcancmder\zcanreceiver.hpp" +#include "sdk\components\zcancmder\cmd\cmd.hpp" + namespace iflytop { class XYRobotCtrlModule { public: @@ -55,7 +57,7 @@ class XYRobotCtrlModule { int32_t set_dec(int32_t dec); int32_t set_speed(int32_t speed); int32_t set_zero_robottype(RobotType_t robot_type); - int32_t set_shaft(bool x_shaft, bool y_shaft) ; + int32_t set_shaft(bool x_shaft, bool y_shaft); int32_t set_zero_shift(int32_t x, int32_t y); int32_t set_runtozero_max_distance(int32_t maxX, int32_t maxY); int32_t set_runtozero_speed(int32_t speed); diff --git a/components/zcancmder/cmd/basic.hpp b/components/zcancmder/cmd/basic.hpp deleted file mode 100644 index aa12b28..0000000 --- a/components/zcancmder/cmd/basic.hpp +++ /dev/null @@ -1,101 +0,0 @@ -#pragma once -#include - -#define ZPACKET_STRUCT(ordername, type, ...) \ - typedef struct { \ - __VA_ARGS__ \ - } ordername##_##type##_t - -#define ZPACKET_CMD_ACK(ordername, cmdpara, ackpara) \ - typedef struct { \ - cmdpara \ - } ordername##_##cmd##_t; \ - typedef struct { \ - ackpara \ - } ordername##_##ack##_t - -#define ZPACKET_CMD_ACK_AND_REPORT(ordername, cmdpara, ackpara, reportpara) \ - typedef struct { \ - cmdpara \ - } ordername##_##cmd##_t; \ - typedef struct { \ - ackpara \ - } ordername##_##ack##_t; \ - typedef struct { \ - reportpara \ - } ordername##_##report##_t - -#define PROCESS_PACKET(ordername, varid) \ - if (rxcmd->iscmd(ordername)) { \ - auto* cmd = rxcmd->get_data_as(); \ - auto* ack = (ordername##_##ack##_t*)m_txbuf; \ - auto cmdheader = rxcmd->get_cmdheader(); \ - uint32_t errorcode = 0; \ - if (cmd->id == varid) { \ - ack->id = cmd->id; - -#define END_PROCESS_PACKET() \ - if (errorcode == 0) { \ - m_cancmder->sendAck(rxcmd->get_cmdheader(), m_txbuf, sizeof(*ack)); \ - } else { \ - m_cancmder->sendErrorAck(rxcmd->get_cmdheader(), errorcode); \ - } \ - } \ - return; \ - } -#define CMD(x) x -#define ACK(x) x -#define REPORT(x) x -namespace iflytop { -namespace zcr { - -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; - -typedef int8_t s8; -typedef int16_t s16; -typedef int32_t s32; -typedef int64_t s64; - -typedef float f32; -typedef double f64; - -#pragma pack(push, 1) -typedef struct { - uint16_t packetindex; - uint16_t cmdid; - uint8_t subcmdid; - uint8_t packetType; - uint8_t data[]; -} Cmdheader_t; -#pragma pack(pop) - -typedef enum { - kpt_cmd = 0, - kpt_ack = 1, - kpt_error_ack = 2, - kpt_cmd_exec_status_report = 3, -} PacketType_t; - -typedef enum { - kset_cmd_type_read = 0, - kset_cmd_type_set = 1, -} SetCmdOperationType_t; - -typedef enum { - kMotorStopType_stop = 0, - kMotorStopType_break = 1, -} MotorStopType_t; - -#define CMDID(cmdid, subcmdid) ((cmdid << 8) + subcmdid) - -typedef enum { - kmodule_statu_idle, - kmodule_statu_work, - kmodule_statu_exception, -} module_statu_type_t; - -} // namespace zcr -} // namespace iflytop diff --git a/components/zcancmder/cmd/cmd.hpp b/components/zcancmder/cmd/cmd.hpp deleted file mode 100644 index f4ce84f..0000000 --- a/components/zcancmder/cmd/cmd.hpp +++ /dev/null @@ -1,547 +0,0 @@ -#pragma once -#include - -#include "basic.hpp" -namespace iflytop { -namespace zcr { - -typedef enum { - kcmd_ping = CMDID(0, 0), - kcmd_read_io = CMDID(1, 0), - kcmd_set_io = CMDID(2, 0), - kcmd_readadc_raw = CMDID(3, 0), - - kcmd_m211887_operation = CMDID(1000, 0), // 维萨拉臭氧传感器 - kcmd_read_presure_sensor = CMDID(1001, 0), // 压力传感器 - kcmd_triple_warning_light_ctl = CMDID(1002, 0), // 三色警示灯控制 - kcmd_high_power_electrical_ctl = CMDID(1003, 0), // 大功率电源控制 - kcmd_peristaltic_pump_ctl = CMDID(1004, 0), // 液泵控制 - kcmd_read_huacheng_pressure_sensor = CMDID(1005, 0), // 华诚压力传感器 - - /******************************************************************************* - * |Module_1006:XYRobot机器人控制模组 * - *******************************************************************************/ - kcmd_xy_robot_ctrl_enable = CMDID(1006, 0), // 机器人使能 - kcmd_xy_robot_ctrl_stop = CMDID(1006, 1), // 机器人停止 - kcmd_xy_robot_ctrl_move_to_zero = CMDID(1006, 2), // 机器人回零 - kcmd_xy_robot_ctrl_move_to_zero_with_calibrate = CMDID(1006, 3), // 机器人回零并标定 - kcmd_xy_robot_ctrl_move_to = CMDID(1006, 4), // 机器人移动到指定位置 - kcmd_xy_robot_ctrl_move_by = CMDID(1006, 5), // 机器人移动指定距离 - - kcmd_xy_robot_ctrl_read_version = CMDID(1006, 50), // 读取模块型号版本信息 - kcmd_xy_robot_ctrl_read_status = CMDID(1006, 51), // 读取模块精简状态信息 - kcmd_xy_robot_ctrl_read_debug_info = CMDID(1006, 52), // 读取模块详细状态信息 - - kcmd_xy_robot_ctrl_set_run_param = CMDID(1006, 100), // 设置运行参数 - kcmd_xy_robot_ctrl_set_warning_limit_param = CMDID(1006, 101), // 设置控制限制参数 - kcmd_xy_robot_ctrl_set_run_to_zero_param = CMDID(1006, 102), // 设置归零参数 - - /******************************************************************************* - * |Module_1007:步进电机控制模组 * - *******************************************************************************/ - kcmd_step_motor_ctrl_enable = CMDID(1007, 0), // 使能 - kcmd_step_motor_ctrl_stop = CMDID(1007, 1), // 停止 - kcmd_step_motor_ctrl_move_to_zero = CMDID(1007, 2), // 回零 - kcmd_step_motor_ctrl_move_to_zero_with_calibrate = CMDID(1007, 3), // 回零并校准 - kcmd_step_motor_ctrl_move_to = CMDID(1007, 4), // 移动到 - kcmd_step_motor_ctrl_move_by = CMDID(1007, 5), // 相对移动 - kcmd_step_motor_ctrl_rotate = CMDID(1007, 6), // 相对移动 - - kcmd_step_motor_ctrl_read_version = CMDID(1007, 50), // 读取模块型号版本信息 - kcmd_step_motor_ctrl_read_status = CMDID(1007, 51), // 读取模块精简状态信息 - kcmd_step_motor_ctrl_read_debug_info = CMDID(1007, 52), // 读取模块详细状态信息 - - kcmd_step_motor_ctrl_set_run_param = CMDID(1007, 100), // 设置运行参数 - kcmd_step_motor_ctrl_set_warning_limit_param = CMDID(1007, 101), // 设置警告限制参数 - kcmd_step_motor_ctrl_set_run_to_zero_param = CMDID(1007, 102), // 设置归零参数 - - /******************************************************************************* - * |Module_1008:舵机 * - *******************************************************************************/ - kcmd_mini_servo_ctrl_enable = CMDID(1008, 0), // 使能 - kcmd_mini_servo_ctrl_stop = CMDID(1008, 1), // 停止 - kcmd_mini_servo_ctrl_position_calibrate = CMDID(1008, 2), // 校准 - kcmd_mini_servo_ctrl_rotate = CMDID(1008, 3), // 速度模式 - kcmd_mini_servo_ctrl_move_to = CMDID(1008, 4), // 位置模式舵机 - kcmd_mini_servo_ctrl_move_by = CMDID(1008, 5), // 位置模式舵机 - kcmd_mini_servo_ctrl_run_with_torque = CMDID(1008, 6), // 开环扭矩模式 - kcmd_mini_servo_ctrl_move_by_nolimit = CMDID(1008, 7), // 相对移动,位置模式无限制,移动不受最大位置和最小位置限制,但无法读取到当前位置 - - kcmd_mini_servo_ctrl_read_version = CMDID(1008, 50), // 读取模块型号版本信息 - kcmd_mini_servo_ctrl_read_status = CMDID(1008, 51), // 读取模块精简状态信息 - kcmd_mini_servo_ctrl_read_debug_info = CMDID(1008, 52), // 读取模块详细状态信息 - - kcmd_mini_servo_ctrl_set_run_param = CMDID(1008, 100), // 位置限制 最大位置,最小位置 - kcmd_mini_servo_ctrl_set_warning_limit_param = CMDID(1008, 101), // 过压保护设置 最大电压 最小电压 - - /******************************************************************************* - * |Module_1009:EEPROM * - *******************************************************************************/ - kcmd_eeprom_read_block = CMDID(1009, 0), // 读取EEPROM - kcmd_eeprom_write_block = CMDID(1009, 1), // 写入EEPROM - - /******************************************************************************* - * |Module_1010:扫码器 * - *******************************************************************************/ - kcmd_barcode_reader_start_scan = CMDID(1010, 0), // 读取扫码器 - kcmd_barcode_reader_stop_scan = CMDID(1010, 1), // 停止读取扫码器 - - /******************************************************************************* - * |Module_1011:帕尔贴-水泵-风扇控温系统 * - *******************************************************************************/ - // 设置目标温度 - kcmd_heater_ctrl_module_start_ctrl_temperature = CMDID(1011, 0), // 设置目标温度 - kcmd_heater_ctrl_module_read_stop_temperature = CMDID(1011, 1), // 读取停止温度 - - kcmd_heater_ctrl_module_read_version = CMDID(1011, 50), // 读取模块型号版本信息 - kcmd_heater_ctrl_module_read_status = CMDID(1011, 51), // 读取模块精简状态信息 - kcmd_heater_ctrl_module_read_debug_info = CMDID(1011, 52), // 读取模块详细状态信息 - - kcmd_heater_ctrl_module_set_run_param = CMDID(1011, 100), // 设置运行参数 - kcmd_heater_ctrl_module_set_warning_limit_param = CMDID(1011, 101), // 设置限制参数 - - /******************************************************************************* - * |Module_1012:光学模组扫码器 * - *******************************************************************************/ - kcmd_optical_barcode_reader_start_scan = CMDID(1012, 0), // 开始扫码 - kcmd_optical_barcode_reader_stop_scan = CMDID(1012, 1), // 停止扫码 - kcmd_optical_barcode_reader_motor_move_to = CMDID(1012, 4), // 水平电机移动到 - kcmd_optical_barcode_reader_motor_move_to_zero = CMDID(1012, 6), // 电机校准 - kcmd_optical_barcode_reader_motor_move_to_zero_with_calibrate = CMDID(1012, 7), // 电机校准 - kcmd_optical_barcode_reader_motor_stop = CMDID(1012, 8), // 电机停止 - kcmd_optical_barcode_reader_motor_enable = CMDID(1012, 9), // 电机使能 - - kcmd_optical_barcode_reader_read_version = CMDID(1012, 50), // 读取模块型号版本信息 - kcmd_optical_barcode_reader_read_status = CMDID(1012, 51), // 读取模块精简状态信息 - kcmd_optical_barcode_reader_read_debug_info = CMDID(1012, 52), // 读取模块详细状态信息 - - kcmd_optical_barcode_reader_set_run_param = CMDID(1012, 100), // 设置运行参数 - kcmd_optical_barcode_reader_set_warning_limit_param = CMDID(1012, 101), // 设置限制参数 - kcmd_optical_barcode_reader_set_run_to_zero_param = CMDID(1012, 102), // 设置归零参数 - - /******************************************************************************* - * |Module_1013:板夹仓 * - *******************************************************************************/ - - kcmd_board_clamp_module_push_and_scan = CMDID(1013, 0), // 推出反应板并扫码 - kcmd_board_clamp_module_moter_move_to = CMDID(1013, 1), // 电机移动到 - kcmd_board_clamp_module_moter_move_by = CMDID(1013, 2), // 电机移动到 - kcmd_board_clamp_module_moter_move_to_zero = CMDID(1013, 3), // 电机归零 - kcmd_board_clamp_module_moter_move_to_zero_with_calibrate = CMDID(1013, 4), // 电机归零并校准 - kcmd_board_clamp_module_moter_stop = CMDID(1013, 5), // 电机停止 - kcmd_board_clamp_module_moter_enable = CMDID(1013, 6), // 电机使能 - - kcmd_board_clamp_module_read_version = CMDID(1013, 50), // 读取模块型号版本信息 - kcmd_board_clamp_module_read_status = CMDID(1013, 51), // 读取模块精简状态信息 - kcmd_board_clamp_module_read_debug_info = CMDID(1013, 52), // 读取模块详细状态信息 - - kcmd_board_clamp_module_set_run_param = CMDID(1013, 100), // 设置运行参数 - kcmd_board_clamp_module_set_warning_limit_param = CMDID(1013, 101), // 设置限制参数 - kcmd_board_clamp_module_set_run_to_zero_param = CMDID(1013, 102), // 设置归零参数 - - // 风扇 - /******************************************************************************* - * |Module_1014:风扇 * - *******************************************************************************/ - kcmd_fan_module_fan_ctrl = CMDID(1014, 0), // 风扇控制 - kcmd_fan_module_fan_read_status = CMDID(1014, 50), // 读取模块精简状态信息 - kcmd_fan_module_fan_read_debug_info = CMDID(1014, 51), // 读取模块详细状态信息 - - // 串口透传 - /******************************************************************************* - * |Module_1015:串口透传 * - *******************************************************************************/ - kcmd_serial_module_send_data = CMDID(1015, 0), // 串口透传发送数据 - kcmd_serial_module_start_read_data = CMDID(1015, 1), // 串口透传读取数据 - kcmd_serial_module_start_stop_data = CMDID(1015, 2), // 串口透传停止读取数据 - kcmd_serial_module_read_status = CMDID(1015, 50), // 读取模块状态信息 - kcmd_serial_module_set_cfg = CMDID(1015, 100), // 串口透传设置配置 - - // 移液枪控制 - /******************************************************************************* - * |Module_1016:移液枪控制 * - *******************************************************************************/ - kcmd_pipette_module_z_motor_enable = CMDID(1016, 0), // 使能 - kcmd_pipette_module_z_motor_move_to = CMDID(1016, 1), // 移动到 - kcmd_pipette_module_z_motor_move_to_zero = CMDID(1016, 2), // 归零 - kcmd_pipette_module_z_motor_move_to_zero_with_calibrate = CMDID(1016, 3), // 归零并校准 - kcmd_pipette_module_z_motor_stop = CMDID(1016, 4), // 停止 - - kcmd_pipette_module_take_liquid = CMDID(1016, 4), // 取液体 - kcmd_pipette_module_split_liquid = CMDID(1016, 5), // 吐液体 - kcmd_pipette_module_take_tip = CMDID(1016, 6), // 取tip - kcmd_pipette_module_remove_tip = CMDID(1016, 7), // 移除tip - - kcmd_pipette_module_read_version = CMDID(1016, 50), // 读取模块型号版本信息 - kcmd_pipette_module_read_status = CMDID(1016, 51), // 读取模块精简状态信息 - kcmd_pipette_module_read_debug_info = CMDID(1016, 52), // 读取模块详细状态信息 - - kcmd_pipette_module_set_run_param = CMDID(1016, 100), // 设置运行参数 - kcmd_pipette_module_set_warning_limit_param = CMDID(1016, 101), // 设置限制参数 - kcmd_pipette_module_set_run_to_zero_param = CMDID(1016, 102), // 设置归零参数 - -} CmdID_t; - -#pragma pack(push, 1) - -ZPACKET_CMD_ACK(kcmd_ping, // - CMD(u8 boardid;), // - ACK(u8 boardid;)); - -ZPACKET_CMD_ACK(kcmd_read_io, // - CMD(u8 ioid;), // - ACK(u8 ioid; u8 val;)); - -ZPACKET_CMD_ACK(kcmd_set_io, // - CMD(u8 ioid; u8 val;), // - ACK(u8 ioid; u8 val;)); - -ZPACKET_CMD_ACK(kcmd_readadc_raw, // - CMD(u8 sensorid;), // - ACK(u8 sensorid; s32 val;)); - -/******************************************************************************* - * |Module_1006:XYRobot机器人控制模组 * - *******************************************************************************/ -ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_enable, - CMD(u8 id; u8 enable;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_stop, - CMD(u8 id; u8 stop_type;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_xy_robot_ctrl_move_to_zero, - CMD(u8 id;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_xy_robot_ctrl_move_to_zero_with_calibrate, - CMD(u8 id; s32 nowx; s32 nowy;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 zero_shift_x; s32 zero_shift_y;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_xy_robot_ctrl_move_to, - CMD(u8 id; s32 x; s32 y; s32 speed;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 x; s32 y;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_xy_robot_ctrl_move_by, - CMD(u8 id; s32 dx; s32 dy; s32 speed;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 dx; s32 dy;)); -ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_read_version, // - CMD(u8 id;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_read_status, // - CMD(u8 id;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_read_debug_info, // - CMD(u8 id;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_set_run_param, // - CMD(u8 id; u8 opt_type; u8 shaft; s32 distance_scale; s32 shift_x; s32 shift_y; s32 acc; s32 dec; s32 maxspeed; s32 min_x; s32 max_x; s32 min_y; - s32 max_y;), // - ACK(u8 id; u8 opt_type; u8 shaft; s32 distance_scale; s32 shift_x; s32 shift_y; s32 acc; s32 dec; s32 maxspeed; s32 min_x; s32 max_x; s32 min_y; - s32 max_y;)); -ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_set_warning_limit_param, // - CMD(u8 id; u8 opt_type;), // - ACK(u8 id; u8 opt_type;)); -ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_set_run_to_zero_param, // - CMD(u8 id; u8 opt_type; u32 move_to_zero_max_d; u32 leave_from_zero_max_d; u32 speed; u32 dec;), // - ACK(u8 id; u8 opt_type; u32 move_to_zero_max_d; u32 leave_from_zero_max_d; u32 speed; u32 dec;)); - -/******************************************************************************* - * |Module_1007:步进电机控制模组 * - *******************************************************************************/ -ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_enable, - CMD(u8 id; u8 enable;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_stop, - CMD(u8 id; u8 stop_type;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_step_motor_ctrl_move_to_zero, - CMD(u8 id;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_step_motor_ctrl_move_to_zero_with_calibrate, - CMD(u8 id; s32 nowx; s32 nowy;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 zero_shift_x;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_step_motor_ctrl_move_to, - CMD(u8 id; s32 x; s32 speed;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; u32 x;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_step_motor_ctrl_move_by, - CMD(u8 id; s32 dx; s32 speed;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 dx;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_step_motor_ctrl_rotate, - CMD(u8 id; s32 speed; s32 run_time;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 has_run_time;)); -ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_read_version, // - CMD(u8 id;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_read_status, // - CMD(u8 id;), // - ACK(u8 id; u8 status; s32 x; s32 velocity;)); -ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_read_debug_info, // - CMD(u8 id;), // - ACK(u8 id; u8 status; s32 x; s32 velocity; s32 acc; s32 dec; s32 speed;)); -ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_set_run_param, // - CMD(u8 id; u8 opt_type; u8 shaft; s32 d_scale; s32 x_shift; s32 acc; s32 dec; s32 breakdec; s32 maxv; s32 minpos; s32 maxpos;), // - ACK(u8 id; u8 opt_type; u8 shaft; s32 d_scale; s32 x_shift; s32 acc; s32 dec; s32 breakdec; s32 maxv; s32 minpos; s32 maxpos;)); -ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_set_warning_limit_param, // - CMD(u8 id; u8 opt_type; s32 pad;), // - ACK(u8 id; u8 opt_type; s32 pad;)); -ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_set_run_to_zero_param, // - CMD(u8 id; u8 opt_type; u32 move_to_zero_max_d; u32 leave_from_zero_max_d; u32 speed; u32 dec;), // - ACK(u8 id; u8 opt_type; u32 move_to_zero_max_d; u32 leave_from_zero_max_d; u32 speed; u32 dec;)); - -/******************************************************************************* - * |Module_1008:舵机 * - *******************************************************************************/ - -ZPACKET_CMD_ACK(kcmd_mini_servo_ctrl_enable, - CMD(u8 id; u8 enable;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_mini_servo_ctrl_stop, - CMD(u8 id; u8 stop_type;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_mini_servo_ctrl_position_calibrate, - CMD(u8 id; s32 calibrate_pos;), // - ACK(u8 id;)); - -ZPACKET_CMD_ACK_AND_REPORT(kcmd_mini_servo_ctrl_rotate, - CMD(u8 id; s32 speed; s32 run_time;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 has_run_time;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_mini_servo_ctrl_move_to, - CMD(u8 id; s32 pos; s32 speed; s32 torque;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 pos;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_mini_servo_ctrl_move_by, - CMD(u8 id; s32 pos; s32 speed; s32 torque;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 pos;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_mini_servo_ctrl_run_with_torque, - CMD(u8 id; s32 torque; s32 run_time;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 has_run_time;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_mini_servo_ctrl_move_by_nolimit, - CMD(u8 id; s32 pos; s32 speed; s32 torque;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 pos;)); -/******************************************************************************* - * |Module_1009:EEPROM * - *******************************************************************************/ - -ZPACKET_CMD_ACK(kcmd_eeprom_read_block, - CMD(u8 id; u16 addr; u16 len;), // - ACK(u8 id; u16 addr; u16 len; u8 data[0];)); -ZPACKET_CMD_ACK(kcmd_eeprom_write_block, - CMD(u8 id; u16 addr; u16 len; u8 data[0];), // - ACK(u8 id; u16 addr; u16 len;)); -/******************************************************************************* - * |Module_1010:扫码器 * - *******************************************************************************/ - -ZPACKET_CMD_ACK_AND_REPORT(kcmd_barcode_reader_start_scan, - CMD(u8 id;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; u16 barcodelen; u8 barcode[0];)); -ZPACKET_CMD_ACK(kcmd_barcode_reader_stop_scan, - CMD(u8 id;), // - ACK(u8 id;)); -/******************************************************************************* - * |Module_1011:帕尔贴-水泵-风扇控温系统 * - *******************************************************************************/ -ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_start_ctrl_temperature, - CMD(u8 id; s32 target_temperature;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_read_stop_temperature, - CMD(u8 id;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_read_version, - CMD(u8 id;), // - ACK(u8 id; uint32_t version;)); -ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_read_status, - CMD(u8 id;), // - ACK(u8 id; u8 status; s32 temperature;)); -ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_read_debug_info, - CMD(u8 id;), // - ACK(u8 id; u8 status; s32 temperature;)); -ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_set_run_param, - CMD(u8 id; u8 opt_type; s32 kp; s32 ki; s32 kd; s32 max_pwm; s32 min_temperature; s32 max_temperature;), // - ACK(u8 id; u8 opt_type; s32 kp; s32 ki; s32 kd; s32 max_pwm; s32 min_temperature; s32 max_temperature;)); -ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_set_warning_limit_param, - CMD(u8 id; u8 opt_type;), // - ACK(u8 id; u8 opt_type;)); - -/******************************************************************************* - * |Module_1012:光学模组扫码器 * - *******************************************************************************/ -ZPACKET_CMD_ACK_AND_REPORT(kcmd_optical_barcode_reader_start_scan, - CMD(u8 id;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; u16 codelen; u16 code[0];)); -ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_stop_scan, - CMD(u8 id;), // - ACK(u8 id;)); - -ZPACKET_CMD_ACK_AND_REPORT(kcmd_optical_barcode_reader_motor_move_to, - CMD(u8 id; u8 motor_id; s32 pos; s32 speed;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 pos;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_optical_barcode_reader_motor_move_to_zero, - CMD(u8 id; u8 motor_id;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_optical_barcode_reader_motor_move_to_zero_with_calibrate, - CMD(u8 id; u8 motor_id; s32 now_pos;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 zero_shift;)); -ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_motor_stop, - CMD(u8 id; u8 motor_id;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_read_version, - CMD(u8 id;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_read_status, - CMD(u8 id;), // - ACK(u8 id; u8 status; u8 hasmove2zero[2]; s32 pos[2];)); -ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_read_debug_info, - CMD(u8 id;), // - ACK(u8 id; u8 status; u8 hasmove2zero[2]; s32 pos[2]; s32 acc[2]; s32 speed[2];)); -ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_set_run_param, // - CMD(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];), // - ACK(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];)); -ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_set_warning_limit_param, - CMD(u8 id; u8 opt_type; s32 pad;), // - ACK(u8 id; u8 opt_type; s32 pad;)); -ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_set_run_to_zero_param, - CMD(u8 id; u8 opt_type; s32 move_to_zero_max_d[2]; s32 leave_from_zero_max_d[2]; s32 speed[2]; s32 dec[2];), // - ACK(u8 id; u8 opt_type; s32 move_to_zero_max_d[2]; s32 leave_from_zero_max_d[2]; s32 speed[2]; s32 dec[2];)); - -/******************************************************************************* - * Module_1013:板夹仓 * - *******************************************************************************/ -ZPACKET_CMD_ACK_AND_REPORT(kcmd_board_clamp_module_push_and_scan, - CMD(u8 id;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; u16 barcodelen; u8 barcode[0];)); - -ZPACKET_CMD_ACK_AND_REPORT(kcmd_board_clamp_module_moter_move_to, - CMD(u8 id; u8 motor_id; s32 pos; s32 speed;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 pos;)); - -ZPACKET_CMD_ACK_AND_REPORT(kcmd_board_clamp_module_moter_move_by, - CMD(u8 id; u8 motor_id; s32 pos; s32 speed;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 pos;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_board_clamp_module_moter_move_to_zero, - CMD(u8 id; u8 motor_id;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_board_clamp_module_moter_move_to_zero_with_calibrate, - CMD(u8 id; u8 motor_id; s32 now_pos;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 zero_shift;)); -ZPACKET_CMD_ACK(kcmd_board_clamp_module_moter_stop, - CMD(u8 id; u8 motor_id;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_board_clamp_module_read_version, - CMD(u8 id;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_board_clamp_module_read_status, - CMD(u8 id;), // - ACK(u8 id; u8 status; u8 hasmove2zero[2]; s32 pos[2];)); -ZPACKET_CMD_ACK(kcmd_board_clamp_module_read_debug_info, - CMD(u8 id;), // - ACK(u8 id; u8 status; u8 hasmove2zero[2]; s32 pos[2]; s32 acc[2]; s32 speed[2];)); -ZPACKET_CMD_ACK(kcmd_board_clamp_module_set_run_param, // - CMD(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];), // - ACK(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];)); -ZPACKET_CMD_ACK(kcmd_board_clamp_module_set_warning_limit_param, - CMD(u8 id; u8 opt_type; s32 pad;), // - ACK(u8 id; u8 opt_type; s32 pad;)); -ZPACKET_CMD_ACK(kcmd_board_clamp_module_set_run_to_zero_param, - CMD(u8 id; u8 opt_type; s32 move_to_zero_max_d[2]; s32 leave_from_zero_max_d[2]; s32 speed[2]; s32 dec[2];), // - ACK(u8 id; u8 opt_type; s32 move_to_zero_max_d[2]; s32 leave_from_zero_max_d[2]; s32 speed[2]; s32 dec[2];)); - -/******************************************************************************* - * |Module_1014:风扇 * - *******************************************************************************/ -ZPACKET_CMD_ACK(kcmd_fan_module_fan_ctrl, - CMD(u8 id; u8 level;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_fan_module_fan_read_status, - CMD(u8 id;), // - ACK(u8 id; u8 status; u8 level;)); -ZPACKET_CMD_ACK(kcmd_fan_module_fan_read_debug_info, - CMD(u8 id;), // - ACK(u8 id; u8 status; u8 level; int16_t fbcount;)); - -/******************************************************************************* - * |Module_1016:移液枪控制 * - *******************************************************************************/ - - -ZPACKET_CMD_ACK(kcmd_pipette_module_motor_enable, - CMD(u8 id; u8 enable;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_pipette_module_motor_move_to, - CMD(u8 id; u8 motor_id; s32 pos; s32 speed;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 pos;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_pipette_module_motor_move_to_zero, - CMD(u8 id; u8 motor_id;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status;)); -ZPACKET_CMD_ACK_AND_REPORT(kcmd_pipette_module_motor_move_to_zero_with_calibrate, - CMD(u8 id; u8 motor_id; s32 now_pos;), // - ACK(u8 id;), // - REPORT(u8 id; u8 status; s32 zero_shift;)); -ZPACKET_CMD_ACK(kcmd_pipette_module_motor_stop, - CMD(u8 id; u8 motor_id;), // - ACK(u8 id;)); - -ZPACKET_CMD_ACK(kcmd_pipette_module_take_liquid, - CMD(u8 id; u8 motor_id; s32 speed; s32 pos;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_pipette_module_split_liquid, - CMD(u8 id; u8 motor_id; s32 speed; s32 pos;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_pipette_module_take_tip, - CMD(u8 id; u8 motor_id; s32 speed; s32 pos;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_pipette_module_remove_tip, - CMD(u8 id; u8 motor_id; s32 speed; s32 pos;), // - ACK(u8 id;)); - -#if 0 - kcmd_pipette_module_read_status = CMDID(1016, 51), // 读取模块精简状态信息 - kcmd_pipette_module_read_debug_info = CMDID(1016, 52), // 读取模块详细状态信息 - - kcmd_pipette_module_set_run_param = CMDID(1016, 100), // 设置运行参数 - kcmd_pipette_module_set_warning_limit_param = CMDID(1016, 101), // 设置限制参数 - kcmd_pipette_module_set_run_to_zero_param = CMDID(1016, 102), // 设置归零参数 -#endif - -ZPACKET_CMD_ACK(kcmd_pipette_module_read_version, - CMD(u8 id;), // - ACK(u8 id;)); -ZPACKET_CMD_ACK(kcmd_pipette_module_read_status, - CMD(u8 id;), // - ACK(u8 id; u8 status; s32 pos[2];)); -ZPACKET_CMD_ACK(kcmd_pipette_module_read_debug_info, - CMD(u8 id;), // - ACK(u8 id; u8 status; s32 pos[2]; s32 acc[2]; s32 speed[2];)); -ZPACKET_CMD_ACK(kcmd_pipette_module_set_run_param, - CMD(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];), // - ACK(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];)); -ZPACKET_CMD_ACK(kcmd_pipette_module_set_warning_limit_param, - CMD(u8 id; u8 opt_type; s32 pad;), // - ACK(u8 id; u8 opt_type; s32 pad;)); - -#pragma pack(pop) - -} // namespace zcr -} // namespace iflytop diff --git a/components/zcancmder/zcanreceiver.cpp b/components/zcancmder/zcanreceiver.cpp deleted file mode 100644 index bf1ed71..0000000 --- a/components/zcancmder/zcanreceiver.cpp +++ /dev/null @@ -1,355 +0,0 @@ -#include "zcanreceiver.hpp" - -#ifdef HAL_CAN_MODULE_ENABLED -#include -#include -#include -using namespace iflytop; -using namespace zcr; - -#define TAG "ZCanCmder" - -#define OVER_TIME_MS 5 - -uint16_t CanPacketRxBuffer::get_packetindex() { - Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; - return cmdheader->packetindex; -} -uint16_t CanPacketRxBuffer::get_cmdid() { - Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; - return cmdheader->cmdid; -} -uint8_t CanPacketRxBuffer::get_subcmdid() { - Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; - return cmdheader->subcmdid; -} -uint8_t CanPacketRxBuffer::get_packetType() { - Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; - return cmdheader->packetType; -} -uint8_t *CanPacketRxBuffer::get_data() { - Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; - return cmdheader->data; -} -uint16_t CanPacketRxBuffer::get_datalen() { - Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; - return rxdataSize - sizeof(Cmdheader_t); -} -bool CanPacketRxBuffer::iscmd(CmdID_t id) { - Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; - uint16_t maincmdid = ((uint32_t)id >> 8) & 0xFFFF; - uint8_t subcmdId = ((uint32_t)id) & 0xFF; - return cmdheader->cmdid == maincmdid && cmdheader->subcmdid == subcmdId; -} -Cmdheader_t *CanPacketRxBuffer::get_cmdheader() { - Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; - return cmdheader; -} - -ZCanCmder::CFG *ZCanCmder::createCFG(uint8_t deviceId) { - CFG *cfg = new CFG(); - ZASSERT(cfg != NULL); - cfg->deviceId = deviceId; -#ifdef STM32F103xB - cfg->canHandle = &hcan; -#else - cfg->canHandle = &hcan1; -#endif - cfg->canFilterIndex0 = 0; - cfg->maxFilterNum = 7; - cfg->rxfifoNum = CAN_RX_FIFO0; - return cfg; -} -void ZCanCmder::init(CFG *cfg) { - HAL_StatusTypeDef hal_status; - m_config = cfg; - - /** - * @brief 初始化CAN - */ - - /** - * @brief 初始化消息接收buf - */ - m_canPacketRxBuffer[0].dataIsReady = false; - m_canPacketRxBuffer[0].id = 1; // 只接收来自主机的消息 - m_canPacketRxBuffer[0].m_canPacketNum = 0; - - /** - * @brief 初始化过滤器 - */ - hal_status = initializeFilter(); - if (hal_status != HAL_OK) { - ZLOGE(TAG, "start can initializeFilter fail\r\n"); - return; - } - /** - * @brief 启动CAN - */ - hal_status = HAL_CAN_Start(m_config->canHandle); // 开启CAN - if (hal_status != HAL_OK) { - ZLOGE(TAG, "start can fail\r\n"); - return; - } - /** - * @brief 监听回调 - */ - ZCanIRQDispatcher::instance().regListener(this); - HAL_StatusTypeDef status = activateRxIT(); - if (status != HAL_OK) { - ZLOGE(TAG, "activateRxIT fail\r\n"); - return; - } - // ZHALCORE::getInstance()->regPeriodJob([this](ZHALCORE::Context &context) { loop(); }, 0); -} -HAL_StatusTypeDef ZCanCmder::initializeFilter() { - /** - * @brief ID区帧格式 - * [ 27:0 ] - * [ STDID ] [ EXTID ] - * [11 :9] [8:6] [5:0] [17:16] [15:8] [7:0] - * 优先级 属性 帧类型 目标ID 源ID - */ - HAL_StatusTypeDef HAL_Status; - CAN_FilterTypeDef sFilterConfig; - - uint32_t filterId; - uint32_t mask; - - memset(&sFilterConfig, 0, sizeof(sFilterConfig)); - sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; // 设为MASK模式 - sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; // CAN_FILTERSCALE_16BIT - sFilterConfig.FilterFIFOAssignment = m_config->rxfifoNum; // 关联过滤器到rxfifoNum - sFilterConfig.FilterActivation = ENABLE; // 激活过滤器 - sFilterConfig.SlaveStartFilterBank = m_config->maxFilterNum; // slave filter start index - - /******************************************************************************* - * 接收所有消息 * - *******************************************************************************/ - filterId = (0); // - mask = (0); // - sFilterConfig.FilterBank = m_config->canFilterIndex0; // - sFilterConfig.FilterMaskIdLow = mask & 0xffff; // - sFilterConfig.FilterMaskIdHigh = (mask & 0xffff0000) >> 16; // - sFilterConfig.FilterIdLow = filterId & 0xffff; // - sFilterConfig.FilterIdHigh = (filterId & 0xffff0000) >> 16; // - HAL_Status = HAL_CAN_ConfigFilter(m_config->canHandle, &sFilterConfig); - if (HAL_Status != HAL_OK) { - ZLOGE(TAG, "HAL_CAN_ConfigFilter filter0 fail"); - return HAL_Status; - } - ZLOGI(TAG, "HAL_CAN_ConfigFilter filterID1 %08x", filterId >> 3); - return HAL_Status; -} - -void ZCanCmder::registerListener(ZCanCmderListener *listener) { m_listenerList.push_back(listener); } -void ZCanCmder::regListener(zcan_cmder_listener_t listener) { m_listenerList2.push_back(listener); } -void ZCanCmder::sendPacket(uint8_t *packet, size_t len) { - /** - * @brief - */ - int npacket = len / 8 + (len % 8 == 0 ? 0 : 1); - if (npacket > 255) { - ZLOGE(TAG, "sendPacket fail, len:%d", len); - return; - } - int finalpacketlen = len % 8 == 0 ? 8 : len % 8; - - for (uint8_t i = 0; i < npacket; i++) { - bool suc = false; - if (i == npacket - 1) { - suc = sendPacketSub(npacket, i, packet + i * 8, finalpacketlen, OVER_TIME_MS); - } else { - suc = sendPacketSub(npacket, i, packet + i * 8, 8, OVER_TIME_MS); - } - if (!suc) { - ZLOGE(TAG, "sendPacket fail, packet(%d:%d)", npacket, i); - return; - } - } -} - -void ZCanCmder::sendAck(Cmdheader_t *cmdheader, uint8_t *data, size_t len) { - Cmdheader_t *txheader = (Cmdheader_t *)txbuff; - memcpy(txheader, cmdheader, sizeof(Cmdheader_t)); - txheader->packetType = kpt_ack; - memcpy(txheader->data, data, len); - sendPacket(txbuff, sizeof(Cmdheader_t) + len); -} -void ZCanCmder::sendExecStatusReport(Cmdheader_t *rxcmdheader, uint8_t *data, size_t len) { - Cmdheader_t *txheader = (Cmdheader_t *)txbuff; - memcpy(txheader, rxcmdheader, sizeof(Cmdheader_t)); - txheader->packetType = kpt_cmd_exec_status_report; - memcpy(txheader->data, data, len); - sendPacket(txbuff, sizeof(Cmdheader_t) + len); -} - -void ZCanCmder::sendErrorAck(Cmdheader_t *cmdheader, uint32_t errcode) { - Cmdheader_t *txheader = (Cmdheader_t *)txbuff; - memcpy(txheader, cmdheader, sizeof(Cmdheader_t)); - txheader->packetType = kpt_error_ack; - memcpy(txheader->data, &errcode, sizeof(errcode)); - sendPacket(txbuff, sizeof(Cmdheader_t) + sizeof(errcode)); -} - -bool ZCanCmder::sendPacketSub(int npacket, int packetIndex, uint8_t *packet, size_t len, int overtimems) { - // ZLOGI(TAG, "sendPacketSub(%d:%d)", npacket, packetIndex); - CAN_TxHeaderTypeDef pHeader; - uint8_t aData[8] /*8byte table*/; - uint32_t txMailBox = 0; - - uint32_t enterticket = zos_get_tick(); - - memset(&pHeader, 0, sizeof(pHeader)); - memset(aData, 0, sizeof(aData)); - pHeader.StdId = 0x00; - pHeader.ExtId = (m_config->deviceId << 16) | (npacket << 8) | packetIndex; - pHeader.IDE = CAN_ID_EXT; - pHeader.RTR = CAN_RTR_DATA; - pHeader.DLC = len; - pHeader.TransmitGlobalTime = DISABLE; - - memcpy(aData, packet, len); - - m_lastTransmitStatus = HAL_CAN_AddTxMessage(m_config->canHandle, &pHeader, aData, &txMailBox); - if (m_lastTransmitStatus != HAL_OK) { - ZLOGE(TAG, "HAL_CAN_AddTxMessage fail"); - return false; - } - - while (HAL_CAN_IsTxMessagePending(m_config->canHandle, txMailBox)) { - if (zos_haspassedms(enterticket) > (uint32_t)overtimems) { - m_lastTransmitStatus = HAL_TIMEOUT; - HAL_CAN_AbortTxRequest(m_config->canHandle, txMailBox); - return false; - } - // m_os->sleepMS(1); - } - if (txPacketInterval_ms > 0) { - osDelay(txPacketInterval_ms); - } - return true; -} - -bool ZCanCmder::getRxMessage(CAN_RxHeaderTypeDef *pHeader, uint8_t aData[] /*8byte table*/) { - /** - * @brief 读取当前FIFO中缓存了多少帧的数据 - */ - uint32_t level = HAL_CAN_GetRxFifoFillLevel(m_config->canHandle, m_config->rxfifoNum); - if (level == 0) { - return false; - } - HAL_StatusTypeDef HAL_RetVal; - HAL_RetVal = HAL_CAN_GetRxMessage(m_config->canHandle, m_config->rxfifoNum, pHeader, aData); - if (HAL_OK == HAL_RetVal) { - // 处理接收到的can总线数据 - return true; - } - return false; -} -void ZCanCmder::STM32_HAL_onCAN_RxFifo0MsgPending(CAN_HandleTypeDef *canHandle) { - /** - * @brief 中断上下文 - */ - // ZLOG_INFO("%s\n", __FUNCTION__); - // printf("------------------%s\n", __FUNCTION__); - if (canHandle != m_config->canHandle) { - return; - } - /** - * @brief 处理can接收到消息 - */ - CAN_RxHeaderTypeDef pHeader; - uint8_t aData[8] /*8byte table*/; - while (getRxMessage(&pHeader, aData)) { - /** - * @brief 消息格式 - * - * [2] [3bit] [8bit] [8bit] [8bit] - * , from frameNum frameId - */ - uint8_t from = (pHeader.ExtId >> 16 & 0xFF); - uint8_t nframe = (pHeader.ExtId & 0xFF00) >> 8; - uint8_t frameId = (pHeader.ExtId & 0x00FF); - CanPacketRxBuffer *rxbuf = &m_canPacketRxBuffer[0]; - if (from != rxbuf->id) { - // 目前只接收来自主机的消息 - continue; - } - - if (rxbuf->dataIsReady) { - // 上次接收到的消息还没有来的急处理 - continue; - } - /** - * @TODO:判断是否丢包 - */ - if (frameId == 0) { - rxbuf->m_canPacketNum = 0; - } - - if (rxbuf->m_canPacketNum < 255) { - rxbuf->m_canPacket[rxbuf->m_canPacketNum].pHeader = pHeader; - memcpy(rxbuf->m_canPacket[rxbuf->m_canPacketNum].aData, aData, 8); - rxbuf->m_canPacketNum++; - } - if (nframe == frameId + 1) { - rxbuf->dataIsReady = true; - } - } - - // deactivateRxIT(); -} -void ZCanCmder::STM32_HAL_onCAN_Error(CAN_HandleTypeDef *canHandle) { - if (canHandle != m_config->canHandle) { - return; - } - ZLOGE(TAG, "onCAN_Error\r\n"); -} -void ZCanCmder::loop() { - CanPacketRxBuffer *rxbuf = &m_canPacketRxBuffer[0]; - if (rxbuf->dataIsReady) { - int dataoff = 0; - for (size_t i = 0; i < rxbuf->m_canPacketNum; i++) { - memcpy(rxbuf->rxdata + dataoff, rxbuf->m_canPacket[i].aData, rxbuf->m_canPacket[i].pHeader.DLC); - dataoff += rxbuf->m_canPacket[i].pHeader.DLC; - } - rxbuf->rxdataSize = dataoff; - - for (auto &var : m_listenerList) { - var->onRceivePacket(rxbuf); - } - - for (auto &var : m_listenerList2) { - var(rxbuf); - } - - rxbuf->dataIsReady = false; - } -} -HAL_StatusTypeDef ZCanCmder::activateRxIT() { - HAL_StatusTypeDef hal_status = HAL_ERROR; - if (m_config->rxfifoNum == CAN_RX_FIFO0) { - hal_status = HAL_CAN_ActivateNotification(m_config->canHandle, CAN_IT_RX_FIFO0_MSG_PENDING); - } else if (m_config->rxfifoNum == CAN_RX_FIFO1) { - hal_status = HAL_CAN_ActivateNotification(m_config->canHandle, CAN_IT_RX_FIFO1_MSG_PENDING); - } else { - ZLOGE(TAG, "start can HAL_CAN_ActivateNotification CAN_IT_RX_FIFO0_MSG_PENDING fail\r\n"); - return hal_status; - } - return hal_status; -} -HAL_StatusTypeDef ZCanCmder::deactivateRxIT() { - HAL_StatusTypeDef hal_status = HAL_ERROR; - if (m_config->rxfifoNum == CAN_RX_FIFO0) { - hal_status = HAL_CAN_DeactivateNotification(m_config->canHandle, CAN_IT_RX_FIFO0_MSG_PENDING); - } else if (m_config->rxfifoNum == CAN_RX_FIFO1) { - hal_status = HAL_CAN_DeactivateNotification(m_config->canHandle, CAN_IT_RX_FIFO1_MSG_PENDING); - } else { - ZLOGE(TAG, "start can HAL_CAN_ActivateNotification CAN_IT_RX_FIFO0_MSG_PENDING fail\r\n"); - return hal_status; - } - return hal_status; -} - -#endif diff --git a/components/zcancmder/zcanreceiver.hpp b/components/zcancmder/zcanreceiver.hpp deleted file mode 100644 index 332cd3f..0000000 --- a/components/zcancmder/zcanreceiver.hpp +++ /dev/null @@ -1,126 +0,0 @@ -// -// Created by zwsd -// - -#pragma once -#include "cmd/cmd.hpp" -#include "sdk/os/zos.hpp" - -namespace iflytop { -namespace zcr { -typedef enum { - kpacketHeader = 0, - kpacketData = 1, - kpacketTail = 2, -} packet_type_t; - -class CANPacket { - public: - CAN_RxHeaderTypeDef pHeader; - uint8_t aData[8]; /*8byte table*/ -}; - -}; // namespace zcr -using namespace zcr; - -class CanPacketRxBuffer { - public: - uint16_t id = 0; - CANPacket m_canPacket[256] = {0}; // 用于接收can消息 - uint8_t m_canPacketNum = 0; - bool dataIsReady = false; - uint8_t rxdata[2000] = {0}; - - int rxdataSize = 0; - - public: - uint16_t get_packetindex(); - uint16_t get_cmdid(); - uint8_t get_subcmdid(); - uint8_t get_packetType(); - uint8_t *get_data(); - uint16_t get_datalen(); - - Cmdheader_t *get_cmdheader(); - - bool iscmd(CmdID_t id); - - template - T *get_data_as() { - return (T *)get_data(); - } -}; - -class ZCanCmderListener { - public: - virtual void onRceivePacket(CanPacketRxBuffer *rxcmd) = 0; -}; - -typedef function zcan_cmder_listener_t; - -class ZCanCmder : public ZCanIRQListener { - public: - class CFG { - public: - uint8_t deviceId; // - /******************************************************************************* - * CANConfig * - *******************************************************************************/ - CAN_HandleTypeDef *canHandle; // 默认使用CAN1 - int canFilterIndex0; // 过滤器0 接收,发给自身的消息 - int maxFilterNum; // 使用的过滤器数量,最大值14,默认为7 - int rxfifoNum; // 使用的FIFO,默认使用FIFO0 - int packetRxOvertime_ms; // - }; - - uint8_t txbuff[1000]; - - public: - class LoopJobContext { - public: - bool hasDoneSomething; - }; - - private: - CFG *m_config = NULL; // 配置 - bool m_canOnRxDataFlag = false; // 是否有数据接收,用于从中断上下文转移到MainLoop上下文 - uint32_t m_lastPacketTicket = 0; // 上一次接收到消息的时间,用于判断与主机是否断开连接 - HAL_StatusTypeDef m_lastTransmitStatus; // 上次调用can发送方法的返回值 - - list m_listenerList; - list m_listenerList2; - CanPacketRxBuffer m_canPacketRxBuffer[1]; - - int txPacketInterval_ms = 0; - - public: - ZCanCmder() {} - CFG *createCFG(uint8_t deviceId); - void init(CFG *cfg); - - void registerListener(ZCanCmderListener *listener); - - void regListener(zcan_cmder_listener_t listener); - - void sendPacket(uint8_t *packet, size_t len); - void sendExecStatusReport(Cmdheader_t *rxcmdheader, uint8_t *data, size_t len); - void sendAck(Cmdheader_t *rxcmdheader, uint8_t *data, size_t len); - void sendErrorAck(Cmdheader_t *cmdheader, uint32_t errcode); - bool sendPacketSub(int npacket, int packetIndex, uint8_t *packet, size_t len, int overtimems); - uint8_t getDeviceId() { return m_config->deviceId; } - void setTxPacketInterval(int interval_ms) { txPacketInterval_ms = interval_ms; } - - void loop(); - - public: - virtual void STM32_HAL_onCAN_RxFifo0MsgPending(CAN_HandleTypeDef *can); - virtual void STM32_HAL_onCAN_Error(CAN_HandleTypeDef *can); - - private: - HAL_StatusTypeDef initializeFilter(); - HAL_StatusTypeDef activateRxIT(); - HAL_StatusTypeDef deactivateRxIT(); - bool getRxMessage(CAN_RxHeaderTypeDef *pHeader, uint8_t aData[] /*8byte table*/); -}; - -} // namespace iflytop \ No newline at end of file diff --git a/components/zcancmder_2/cmd/basic.hpp b/components/zcancmder_2/cmd/basic.hpp new file mode 100644 index 0000000..aa12b28 --- /dev/null +++ b/components/zcancmder_2/cmd/basic.hpp @@ -0,0 +1,101 @@ +#pragma once +#include + +#define ZPACKET_STRUCT(ordername, type, ...) \ + typedef struct { \ + __VA_ARGS__ \ + } ordername##_##type##_t + +#define ZPACKET_CMD_ACK(ordername, cmdpara, ackpara) \ + typedef struct { \ + cmdpara \ + } ordername##_##cmd##_t; \ + typedef struct { \ + ackpara \ + } ordername##_##ack##_t + +#define ZPACKET_CMD_ACK_AND_REPORT(ordername, cmdpara, ackpara, reportpara) \ + typedef struct { \ + cmdpara \ + } ordername##_##cmd##_t; \ + typedef struct { \ + ackpara \ + } ordername##_##ack##_t; \ + typedef struct { \ + reportpara \ + } ordername##_##report##_t + +#define PROCESS_PACKET(ordername, varid) \ + if (rxcmd->iscmd(ordername)) { \ + auto* cmd = rxcmd->get_data_as(); \ + auto* ack = (ordername##_##ack##_t*)m_txbuf; \ + auto cmdheader = rxcmd->get_cmdheader(); \ + uint32_t errorcode = 0; \ + if (cmd->id == varid) { \ + ack->id = cmd->id; + +#define END_PROCESS_PACKET() \ + if (errorcode == 0) { \ + m_cancmder->sendAck(rxcmd->get_cmdheader(), m_txbuf, sizeof(*ack)); \ + } else { \ + m_cancmder->sendErrorAck(rxcmd->get_cmdheader(), errorcode); \ + } \ + } \ + return; \ + } +#define CMD(x) x +#define ACK(x) x +#define REPORT(x) x +namespace iflytop { +namespace zcr { + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +typedef float f32; +typedef double f64; + +#pragma pack(push, 1) +typedef struct { + uint16_t packetindex; + uint16_t cmdid; + uint8_t subcmdid; + uint8_t packetType; + uint8_t data[]; +} Cmdheader_t; +#pragma pack(pop) + +typedef enum { + kpt_cmd = 0, + kpt_ack = 1, + kpt_error_ack = 2, + kpt_cmd_exec_status_report = 3, +} PacketType_t; + +typedef enum { + kset_cmd_type_read = 0, + kset_cmd_type_set = 1, +} SetCmdOperationType_t; + +typedef enum { + kMotorStopType_stop = 0, + kMotorStopType_break = 1, +} MotorStopType_t; + +#define CMDID(cmdid, subcmdid) ((cmdid << 8) + subcmdid) + +typedef enum { + kmodule_statu_idle, + kmodule_statu_work, + kmodule_statu_exception, +} module_statu_type_t; + +} // namespace zcr +} // namespace iflytop diff --git a/components/zcancmder_2/cmd/cmd.hpp b/components/zcancmder_2/cmd/cmd.hpp new file mode 100644 index 0000000..d5848dd --- /dev/null +++ b/components/zcancmder_2/cmd/cmd.hpp @@ -0,0 +1,549 @@ +#pragma once +#include + +#include "basic.hpp" +namespace iflytop { +namespace zcr { + +typedef enum { + kcmd_ping = CMDID(0, 0), + kcmd_read_io = CMDID(1, 0), + kcmd_set_io = CMDID(2, 0), + kcmd_readadc_raw = CMDID(3, 0), + + kcmd_m211887_operation = CMDID(1000, 0), // 维萨拉臭氧传感器 + kcmd_read_presure_sensor = CMDID(1001, 0), // 压力传感器 + kcmd_triple_warning_light_ctl = CMDID(1002, 0), // 三色警示灯控制 + kcmd_high_power_electrical_ctl = CMDID(1003, 0), // 大功率电源控制 + kcmd_peristaltic_pump_ctl = CMDID(1004, 0), // 液泵控制 + kcmd_read_huacheng_pressure_sensor = CMDID(1005, 0), // 华诚压力传感器 + + /******************************************************************************* + * |Module_1006:XYRobot机器人控制模组 * + *******************************************************************************/ + kcmd_xy_robot_ctrl_enable = CMDID(1006, 0), // 机器人使能 + kcmd_xy_robot_ctrl_stop = CMDID(1006, 1), // 机器人停止 + kcmd_xy_robot_ctrl_move_to_zero = CMDID(1006, 2), // 机器人回零 + kcmd_xy_robot_ctrl_move_to_zero_with_calibrate = CMDID(1006, 3), // 机器人回零并标定 + kcmd_xy_robot_ctrl_move_to = CMDID(1006, 4), // 机器人移动到指定位置 + kcmd_xy_robot_ctrl_move_by = CMDID(1006, 5), // 机器人移动指定距离 + + kcmd_xy_robot_ctrl_read_version = CMDID(1006, 50), // 读取模块型号版本信息 + kcmd_xy_robot_ctrl_read_status = CMDID(1006, 51), // 读取模块精简状态信息 + kcmd_xy_robot_ctrl_read_debug_info = CMDID(1006, 52), // 读取模块详细状态信息 + + kcmd_xy_robot_ctrl_set_run_param = CMDID(1006, 100), // 设置运行参数 + kcmd_xy_robot_ctrl_set_warning_limit_param = CMDID(1006, 101), // 设置控制限制参数 + kcmd_xy_robot_ctrl_set_run_to_zero_param = CMDID(1006, 102), // 设置归零参数 + + /******************************************************************************* + * |Module_1007:步进电机控制模组 * + *******************************************************************************/ + kcmd_step_motor_ctrl_enable = CMDID(1007, 0), // 使能 + kcmd_step_motor_ctrl_stop = CMDID(1007, 1), // 停止 + kcmd_step_motor_ctrl_move_to_zero = CMDID(1007, 2), // 回零 + kcmd_step_motor_ctrl_move_to_zero_with_calibrate = CMDID(1007, 3), // 回零并校准 + kcmd_step_motor_ctrl_move_to = CMDID(1007, 4), // 移动到 + kcmd_step_motor_ctrl_move_by = CMDID(1007, 5), // 相对移动 + kcmd_step_motor_ctrl_rotate = CMDID(1007, 6), // 相对移动 + + kcmd_step_motor_ctrl_read_version = CMDID(1007, 50), // 读取模块型号版本信息 + kcmd_step_motor_ctrl_read_status = CMDID(1007, 51), // 读取模块精简状态信息 + kcmd_step_motor_ctrl_read_debug_info = CMDID(1007, 52), // 读取模块详细状态信息 + + kcmd_step_motor_ctrl_set_run_param = CMDID(1007, 100), // 设置运行参数 + kcmd_step_motor_ctrl_set_warning_limit_param = CMDID(1007, 101), // 设置警告限制参数 + kcmd_step_motor_ctrl_set_run_to_zero_param = CMDID(1007, 102), // 设置归零参数 + + /******************************************************************************* + * |Module_1008:舵机 * + *******************************************************************************/ + kcmd_mini_servo_ctrl_enable = CMDID(1008, 0), // 使能 + kcmd_mini_servo_ctrl_stop = CMDID(1008, 1), // 停止 + kcmd_mini_servo_ctrl_position_calibrate = CMDID(1008, 2), // 校准 + kcmd_mini_servo_ctrl_rotate = CMDID(1008, 3), // 速度模式 + kcmd_mini_servo_ctrl_move_to = CMDID(1008, 4), // 位置模式舵机 + kcmd_mini_servo_ctrl_move_by = CMDID(1008, 5), // 位置模式舵机 + kcmd_mini_servo_ctrl_run_with_torque = CMDID(1008, 6), // 开环扭矩模式 + kcmd_mini_servo_ctrl_move_by_nolimit = CMDID(1008, 7), // 相对移动,位置模式无限制,移动不受最大位置和最小位置限制,但无法读取到当前位置 + + kcmd_mini_servo_ctrl_read_version = CMDID(1008, 50), // 读取模块型号版本信息 + kcmd_mini_servo_ctrl_read_status = CMDID(1008, 51), // 读取模块精简状态信息 + kcmd_mini_servo_ctrl_read_debug_info = CMDID(1008, 52), // 读取模块详细状态信息 + + kcmd_mini_servo_ctrl_set_run_param = CMDID(1008, 100), // 位置限制 最大位置,最小位置 + kcmd_mini_servo_ctrl_set_warning_limit_param = CMDID(1008, 101), // 过压保护设置 最大电压 最小电压 + + /******************************************************************************* + * |Module_1009:EEPROM * + *******************************************************************************/ + kcmd_eeprom_read_block = CMDID(1009, 0), // 读取EEPROM + kcmd_eeprom_write_block = CMDID(1009, 1), // 写入EEPROM + + /******************************************************************************* + * |Module_1010:扫码器 * + *******************************************************************************/ + kcmd_barcode_reader_start_scan = CMDID(1010, 0), // 读取扫码器 + kcmd_barcode_reader_stop_scan = CMDID(1010, 1), // 停止读取扫码器 + + /******************************************************************************* + * |Module_1011:帕尔贴-水泵-风扇控温系统 * + *******************************************************************************/ + // 设置目标温度 + kcmd_heater_ctrl_module_start_ctrl_temperature = CMDID(1011, 0), // 设置目标温度 + kcmd_heater_ctrl_module_read_stop_temperature = CMDID(1011, 1), // 读取停止温度 + + kcmd_heater_ctrl_module_read_version = CMDID(1011, 50), // 读取模块型号版本信息 + kcmd_heater_ctrl_module_read_status = CMDID(1011, 51), // 读取模块精简状态信息 + kcmd_heater_ctrl_module_read_debug_info = CMDID(1011, 52), // 读取模块详细状态信息 + + kcmd_heater_ctrl_module_set_run_param = CMDID(1011, 100), // 设置运行参数 + kcmd_heater_ctrl_module_set_warning_limit_param = CMDID(1011, 101), // 设置限制参数 + + /******************************************************************************* + * |Module_1012:光学模组扫码器 * + *******************************************************************************/ + kcmd_optical_barcode_reader_start_scan = CMDID(1012, 0), // 开始扫码 + kcmd_optical_barcode_reader_stop_scan = CMDID(1012, 1), // 停止扫码 + kcmd_optical_barcode_reader_motor_move_to = CMDID(1012, 4), // 水平电机移动到 + kcmd_optical_barcode_reader_motor_move_to_zero = CMDID(1012, 6), // 电机校准 + kcmd_optical_barcode_reader_motor_move_to_zero_with_calibrate = CMDID(1012, 7), // 电机校准 + kcmd_optical_barcode_reader_motor_stop = CMDID(1012, 8), // 电机停止 + kcmd_optical_barcode_reader_motor_enable = CMDID(1012, 9), // 电机使能 + + kcmd_optical_barcode_reader_read_version = CMDID(1012, 50), // 读取模块型号版本信息 + kcmd_optical_barcode_reader_read_status = CMDID(1012, 51), // 读取模块精简状态信息 + kcmd_optical_barcode_reader_read_debug_info = CMDID(1012, 52), // 读取模块详细状态信息 + + kcmd_optical_barcode_reader_set_run_param = CMDID(1012, 100), // 设置运行参数 + kcmd_optical_barcode_reader_set_warning_limit_param = CMDID(1012, 101), // 设置限制参数 + kcmd_optical_barcode_reader_set_run_to_zero_param = CMDID(1012, 102), // 设置归零参数 + + /******************************************************************************* + * |Module_1013:板夹仓 * + *******************************************************************************/ + + kcmd_board_clamp_module_push_and_scan = CMDID(1013, 0), // 推出反应板并扫码 + kcmd_board_clamp_module_moter_move_to = CMDID(1013, 1), // 电机移动到 + kcmd_board_clamp_module_moter_move_by = CMDID(1013, 2), // 电机移动到 + kcmd_board_clamp_module_moter_move_to_zero = CMDID(1013, 3), // 电机归零 + kcmd_board_clamp_module_moter_move_to_zero_with_calibrate = CMDID(1013, 4), // 电机归零并校准 + kcmd_board_clamp_module_moter_stop = CMDID(1013, 5), // 电机停止 + kcmd_board_clamp_module_moter_enable = CMDID(1013, 6), // 电机使能 + + kcmd_board_clamp_module_read_version = CMDID(1013, 50), // 读取模块型号版本信息 + kcmd_board_clamp_module_read_status = CMDID(1013, 51), // 读取模块精简状态信息 + kcmd_board_clamp_module_read_debug_info = CMDID(1013, 52), // 读取模块详细状态信息 + + kcmd_board_clamp_module_set_run_param = CMDID(1013, 100), // 设置运行参数 + kcmd_board_clamp_module_set_warning_limit_param = CMDID(1013, 101), // 设置限制参数 + kcmd_board_clamp_module_set_run_to_zero_param = CMDID(1013, 102), // 设置归零参数 + + // 风扇 + /******************************************************************************* + * |Module_1014:风扇 * + *******************************************************************************/ + kcmd_fan_module_fan_ctrl = CMDID(1014, 0), // 风扇控制 + kcmd_fan_module_fan_read_status = CMDID(1014, 50), // 读取模块精简状态信息 + kcmd_fan_module_fan_read_debug_info = CMDID(1014, 51), // 读取模块详细状态信息 + + // 串口透传 + /******************************************************************************* + * |Module_1015:串口透传 * + *******************************************************************************/ + kcmd_serial_module_send_data = CMDID(1015, 0), // 串口透传发送数据 + kcmd_serial_module_start_read_data = CMDID(1015, 1), // 串口透传读取数据 + kcmd_serial_module_start_stop_data = CMDID(1015, 2), // 串口透传停止读取数据 + kcmd_serial_module_read_status = CMDID(1015, 50), // 读取模块状态信息 + kcmd_serial_module_set_cfg = CMDID(1015, 100), // 串口透传设置配置 + + // 移液枪控制 + /******************************************************************************* + * |Module_1016:移液枪控制 * + *******************************************************************************/ + kcmd_pipette_module_z_motor_enable = CMDID(1016, 0), // 使能 + kcmd_pipette_module_z_motor_move_to = CMDID(1016, 1), // 移动到 + kcmd_pipette_module_z_motor_move_to_zero = CMDID(1016, 2), // 归零 + kcmd_pipette_module_z_motor_move_to_zero_with_calibrate = CMDID(1016, 3), // 归零并校准 + kcmd_pipette_module_z_motor_stop = CMDID(1016, 4), // 停止 + + kcmd_pipette_module_take_liquid = CMDID(1016, 4), // 取液体 + kcmd_pipette_module_split_liquid = CMDID(1016, 5), // 吐液体 + kcmd_pipette_module_take_tip = CMDID(1016, 6), // 取tip + kcmd_pipette_module_remove_tip = CMDID(1016, 7), // 移除tip + + kcmd_pipette_module_read_version = CMDID(1016, 50), // 读取模块型号版本信息 + kcmd_pipette_module_read_status = CMDID(1016, 51), // 读取模块精简状态信息 + kcmd_pipette_module_read_debug_info = CMDID(1016, 52), // 读取模块详细状态信息 + + kcmd_pipette_module_set_run_param = CMDID(1016, 100), // 设置运行参数 + kcmd_pipette_module_set_warning_limit_param = CMDID(1016, 101), // 设置限制参数 + kcmd_pipette_module_set_run_to_zero_param = CMDID(1016, 102), // 设置归零参数 + +} CmdID_t; + +#pragma pack(push, 1) + +ZPACKET_CMD_ACK(kcmd_ping, // + CMD(u8 boardid;), // + ACK(u8 boardid;)); + +ZPACKET_CMD_ACK(kcmd_read_io, // + CMD(u8 ioid;), // + ACK(u8 ioid; u8 val;)); + +ZPACKET_CMD_ACK(kcmd_set_io, // + CMD(u8 ioid; u8 val;), // + ACK(u8 ioid; u8 val;)); + +ZPACKET_CMD_ACK(kcmd_readadc_raw, // + CMD(u8 sensorid;), // + ACK(u8 sensorid; s32 val;)); + +/******************************************************************************* + * |Module_1006:XYRobot机器人控制模组 * + *******************************************************************************/ +// ACTION +ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_enable, + CMD(u8 id; u8 enable;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_stop, + CMD(u8 id; u8 stop_type;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_xy_robot_ctrl_move_to_zero, + CMD(u8 id;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_xy_robot_ctrl_move_to_zero_with_calibrate, + CMD(u8 id; s32 nowx; s32 nowy;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 zero_shift_x; s32 zero_shift_y;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_xy_robot_ctrl_move_to, + CMD(u8 id; s32 x; s32 y; s32 speed;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 x; s32 y;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_xy_robot_ctrl_move_by, + CMD(u8 id; s32 dx; s32 dy; s32 speed;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 dx; s32 dy;)); +// READ +ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_read_version, // + CMD(u8 id;), // + ACK(u8 id; u32 version;)); +ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_read_status, // + CMD(u8 id;), // + ACK(u8 id; u8 status; s32 x; s32 y;)); +ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_read_debug_info, // + CMD(u8 id;), // + ACK(u8 id; u8 status; s32 x; s32 y;)); +// SET +ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_set_run_param, // + CMD(u8 id; u8 opt_type; u8 shaft; s32 distance_scale; s32 shift_x; s32 shift_y; s32 acc; s32 dec; s32 maxspeed; s32 min_x; s32 max_x; s32 min_y; + s32 max_y;), // + ACK(u8 id; u8 opt_type; u8 shaft; s32 distance_scale; s32 shift_x; s32 shift_y; s32 acc; s32 dec; s32 maxspeed; s32 min_x; s32 max_x; s32 min_y; + s32 max_y;)); +ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_set_warning_limit_param, // + CMD(u8 id; u8 opt_type;), // + ACK(u8 id; u8 opt_type;)); +ZPACKET_CMD_ACK(kcmd_xy_robot_ctrl_set_run_to_zero_param, // + CMD(u8 id; u8 opt_type; u32 move_to_zero_max_d; u32 leave_from_zero_max_d; u32 speed; u32 dec;), // + ACK(u8 id; u8 opt_type; u32 move_to_zero_max_d; u32 leave_from_zero_max_d; u32 speed; u32 dec;)); + +/******************************************************************************* + * |Module_1007:步进电机控制模组 * + *******************************************************************************/ +ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_enable, + CMD(u8 id; u8 enable;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_stop, + CMD(u8 id; u8 stop_type;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_step_motor_ctrl_move_to_zero, + CMD(u8 id;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_step_motor_ctrl_move_to_zero_with_calibrate, + CMD(u8 id; s32 nowx; s32 nowy;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 zero_shift_x;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_step_motor_ctrl_move_to, + CMD(u8 id; s32 x; s32 speed;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; u32 x;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_step_motor_ctrl_move_by, + CMD(u8 id; s32 dx; s32 speed;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 dx;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_step_motor_ctrl_rotate, + CMD(u8 id; s32 speed; s32 run_time;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 has_run_time;)); +ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_read_version, // + CMD(u8 id;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_read_status, // + CMD(u8 id;), // + ACK(u8 id; u8 status; s32 x; s32 velocity;)); +ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_read_debug_info, // + CMD(u8 id;), // + ACK(u8 id; u8 status; s32 x; s32 velocity; s32 acc; s32 dec; s32 speed;)); +ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_set_run_param, // + CMD(u8 id; u8 opt_type; u8 shaft; s32 d_scale; s32 x_shift; s32 acc; s32 dec; s32 breakdec; s32 maxv; s32 minpos; s32 maxpos;), // + ACK(u8 id; u8 opt_type; u8 shaft; s32 d_scale; s32 x_shift; s32 acc; s32 dec; s32 breakdec; s32 maxv; s32 minpos; s32 maxpos;)); +ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_set_warning_limit_param, // + CMD(u8 id; u8 opt_type; s32 pad;), // + ACK(u8 id; u8 opt_type; s32 pad;)); +ZPACKET_CMD_ACK(kcmd_step_motor_ctrl_set_run_to_zero_param, // + CMD(u8 id; u8 opt_type; u32 move_to_zero_max_d; u32 leave_from_zero_max_d; u32 speed; u32 dec;), // + ACK(u8 id; u8 opt_type; u32 move_to_zero_max_d; u32 leave_from_zero_max_d; u32 speed; u32 dec;)); + +/******************************************************************************* + * |Module_1008:舵机 * + *******************************************************************************/ + +ZPACKET_CMD_ACK(kcmd_mini_servo_ctrl_enable, + CMD(u8 id; u8 enable;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_mini_servo_ctrl_stop, + CMD(u8 id; u8 stop_type;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_mini_servo_ctrl_position_calibrate, + CMD(u8 id; s32 calibrate_pos;), // + ACK(u8 id;)); + +ZPACKET_CMD_ACK_AND_REPORT(kcmd_mini_servo_ctrl_rotate, + CMD(u8 id; s32 speed; s32 run_time;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 has_run_time;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_mini_servo_ctrl_move_to, + CMD(u8 id; s32 pos; s32 speed; s32 torque;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 pos;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_mini_servo_ctrl_move_by, + CMD(u8 id; s32 pos; s32 speed; s32 torque;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 pos;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_mini_servo_ctrl_run_with_torque, + CMD(u8 id; s32 torque; s32 run_time;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 has_run_time;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_mini_servo_ctrl_move_by_nolimit, + CMD(u8 id; s32 pos; s32 speed; s32 torque;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 pos;)); +/******************************************************************************* + * |Module_1009:EEPROM * + *******************************************************************************/ + +ZPACKET_CMD_ACK(kcmd_eeprom_read_block, + CMD(u8 id; u16 addr; u16 len;), // + ACK(u8 id; u16 addr; u16 len; u8 data[0];)); +ZPACKET_CMD_ACK(kcmd_eeprom_write_block, + CMD(u8 id; u16 addr; u16 len; u8 data[0];), // + ACK(u8 id; u16 addr; u16 len;)); +/******************************************************************************* + * |Module_1010:扫码器 * + *******************************************************************************/ + +ZPACKET_CMD_ACK_AND_REPORT(kcmd_barcode_reader_start_scan, + CMD(u8 id;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; u16 barcodelen; u8 barcode[0];)); +ZPACKET_CMD_ACK(kcmd_barcode_reader_stop_scan, + CMD(u8 id;), // + ACK(u8 id;)); +/******************************************************************************* + * |Module_1011:帕尔贴-水泵-风扇控温系统 * + *******************************************************************************/ +ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_start_ctrl_temperature, + CMD(u8 id; s32 target_temperature;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_read_stop_temperature, + CMD(u8 id;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_read_version, + CMD(u8 id;), // + ACK(u8 id; uint32_t version;)); +ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_read_status, + CMD(u8 id;), // + ACK(u8 id; u8 status; s32 temperature;)); +ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_read_debug_info, + CMD(u8 id;), // + ACK(u8 id; u8 status; s32 temperature;)); +ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_set_run_param, + CMD(u8 id; u8 opt_type; s32 kp; s32 ki; s32 kd; s32 max_pwm; s32 min_temperature; s32 max_temperature;), // + ACK(u8 id; u8 opt_type; s32 kp; s32 ki; s32 kd; s32 max_pwm; s32 min_temperature; s32 max_temperature;)); +ZPACKET_CMD_ACK(kcmd_heater_ctrl_module_set_warning_limit_param, + CMD(u8 id; u8 opt_type;), // + ACK(u8 id; u8 opt_type;)); + +/******************************************************************************* + * |Module_1012:光学模组扫码器 * + *******************************************************************************/ +ZPACKET_CMD_ACK_AND_REPORT(kcmd_optical_barcode_reader_start_scan, + CMD(u8 id;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; u16 codelen; u16 code[0];)); +ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_stop_scan, + CMD(u8 id;), // + ACK(u8 id;)); + +ZPACKET_CMD_ACK_AND_REPORT(kcmd_optical_barcode_reader_motor_move_to, + CMD(u8 id; u8 motor_id; s32 pos; s32 speed;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 pos;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_optical_barcode_reader_motor_move_to_zero, + CMD(u8 id; u8 motor_id;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_optical_barcode_reader_motor_move_to_zero_with_calibrate, + CMD(u8 id; u8 motor_id; s32 now_pos;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 zero_shift;)); +ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_motor_stop, + CMD(u8 id; u8 motor_id;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_read_version, + CMD(u8 id;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_read_status, + CMD(u8 id;), // + ACK(u8 id; u8 status; u8 hasmove2zero[2]; s32 pos[2];)); +ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_read_debug_info, + CMD(u8 id;), // + ACK(u8 id; u8 status; u8 hasmove2zero[2]; s32 pos[2]; s32 acc[2]; s32 speed[2];)); +ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_set_run_param, // + CMD(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];), // + ACK(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];)); +ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_set_warning_limit_param, + CMD(u8 id; u8 opt_type; s32 pad;), // + ACK(u8 id; u8 opt_type; s32 pad;)); +ZPACKET_CMD_ACK(kcmd_optical_barcode_reader_set_run_to_zero_param, + CMD(u8 id; u8 opt_type; s32 move_to_zero_max_d[2]; s32 leave_from_zero_max_d[2]; s32 speed[2]; s32 dec[2];), // + ACK(u8 id; u8 opt_type; s32 move_to_zero_max_d[2]; s32 leave_from_zero_max_d[2]; s32 speed[2]; s32 dec[2];)); + +/******************************************************************************* + * Module_1013:板夹仓 * + *******************************************************************************/ +ZPACKET_CMD_ACK_AND_REPORT(kcmd_board_clamp_module_push_and_scan, + CMD(u8 id;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; u16 barcodelen; u8 barcode[0];)); + +ZPACKET_CMD_ACK_AND_REPORT(kcmd_board_clamp_module_moter_move_to, + CMD(u8 id; u8 motor_id; s32 pos; s32 speed;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 pos;)); + +ZPACKET_CMD_ACK_AND_REPORT(kcmd_board_clamp_module_moter_move_by, + CMD(u8 id; u8 motor_id; s32 pos; s32 speed;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 pos;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_board_clamp_module_moter_move_to_zero, + CMD(u8 id; u8 motor_id;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_board_clamp_module_moter_move_to_zero_with_calibrate, + CMD(u8 id; u8 motor_id; s32 now_pos;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 zero_shift;)); +ZPACKET_CMD_ACK(kcmd_board_clamp_module_moter_stop, + CMD(u8 id; u8 motor_id;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_board_clamp_module_read_version, + CMD(u8 id;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_board_clamp_module_read_status, + CMD(u8 id;), // + ACK(u8 id; u8 status; u8 hasmove2zero[2]; s32 pos[2];)); +ZPACKET_CMD_ACK(kcmd_board_clamp_module_read_debug_info, + CMD(u8 id;), // + ACK(u8 id; u8 status; u8 hasmove2zero[2]; s32 pos[2]; s32 acc[2]; s32 speed[2];)); +ZPACKET_CMD_ACK(kcmd_board_clamp_module_set_run_param, // + CMD(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];), // + ACK(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];)); +ZPACKET_CMD_ACK(kcmd_board_clamp_module_set_warning_limit_param, + CMD(u8 id; u8 opt_type; s32 pad;), // + ACK(u8 id; u8 opt_type; s32 pad;)); +ZPACKET_CMD_ACK(kcmd_board_clamp_module_set_run_to_zero_param, + CMD(u8 id; u8 opt_type; s32 move_to_zero_max_d[2]; s32 leave_from_zero_max_d[2]; s32 speed[2]; s32 dec[2];), // + ACK(u8 id; u8 opt_type; s32 move_to_zero_max_d[2]; s32 leave_from_zero_max_d[2]; s32 speed[2]; s32 dec[2];)); + +/******************************************************************************* + * |Module_1014:风扇 * + *******************************************************************************/ +ZPACKET_CMD_ACK(kcmd_fan_module_fan_ctrl, + CMD(u8 id; u8 level;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_fan_module_fan_read_status, + CMD(u8 id;), // + ACK(u8 id; u8 status; u8 level;)); +ZPACKET_CMD_ACK(kcmd_fan_module_fan_read_debug_info, + CMD(u8 id;), // + ACK(u8 id; u8 status; u8 level; int16_t fbcount;)); + +/******************************************************************************* + * |Module_1016:移液枪控制 * + *******************************************************************************/ + +ZPACKET_CMD_ACK(kcmd_pipette_module_motor_enable, + CMD(u8 id; u8 enable;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_pipette_module_motor_move_to, + CMD(u8 id; u8 motor_id; s32 pos; s32 speed;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 pos;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_pipette_module_motor_move_to_zero, + CMD(u8 id; u8 motor_id;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status;)); +ZPACKET_CMD_ACK_AND_REPORT(kcmd_pipette_module_motor_move_to_zero_with_calibrate, + CMD(u8 id; u8 motor_id; s32 now_pos;), // + ACK(u8 id;), // + REPORT(u8 id; u8 status; s32 zero_shift;)); +ZPACKET_CMD_ACK(kcmd_pipette_module_motor_stop, + CMD(u8 id; u8 motor_id;), // + ACK(u8 id;)); + +ZPACKET_CMD_ACK(kcmd_pipette_module_take_liquid, + CMD(u8 id; u8 motor_id; s32 speed; s32 pos;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_pipette_module_split_liquid, + CMD(u8 id; u8 motor_id; s32 speed; s32 pos;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_pipette_module_take_tip, + CMD(u8 id; u8 motor_id; s32 speed; s32 pos;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_pipette_module_remove_tip, + CMD(u8 id; u8 motor_id; s32 speed; s32 pos;), // + ACK(u8 id;)); + +#if 0 + kcmd_pipette_module_read_status = CMDID(1016, 51), // 读取模块精简状态信息 + kcmd_pipette_module_read_debug_info = CMDID(1016, 52), // 读取模块详细状态信息 + + kcmd_pipette_module_set_run_param = CMDID(1016, 100), // 设置运行参数 + kcmd_pipette_module_set_warning_limit_param = CMDID(1016, 101), // 设置限制参数 + kcmd_pipette_module_set_run_to_zero_param = CMDID(1016, 102), // 设置归零参数 +#endif + +ZPACKET_CMD_ACK(kcmd_pipette_module_read_version, + CMD(u8 id;), // + ACK(u8 id;)); +ZPACKET_CMD_ACK(kcmd_pipette_module_read_status, + CMD(u8 id;), // + ACK(u8 id; u8 status; s32 pos[2];)); +ZPACKET_CMD_ACK(kcmd_pipette_module_read_debug_info, + CMD(u8 id;), // + ACK(u8 id; u8 status; s32 pos[2]; s32 acc[2]; s32 speed[2];)); +ZPACKET_CMD_ACK(kcmd_pipette_module_set_run_param, + CMD(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];), // + ACK(u8 id; u8 opt_type; s32 acc[2]; s32 dec[2]; s32 max_speed[2]; s32 min_pos[2]; s32 max_pos[2];)); +ZPACKET_CMD_ACK(kcmd_pipette_module_set_warning_limit_param, + CMD(u8 id; u8 opt_type; s32 pad;), // + ACK(u8 id; u8 opt_type; s32 pad;)); + +#pragma pack(pop) + +} // namespace zcr +} // namespace iflytop diff --git a/components/zcancmder_2/zcanreceiver.cpp b/components/zcancmder_2/zcanreceiver.cpp new file mode 100644 index 0000000..bf1ed71 --- /dev/null +++ b/components/zcancmder_2/zcanreceiver.cpp @@ -0,0 +1,355 @@ +#include "zcanreceiver.hpp" + +#ifdef HAL_CAN_MODULE_ENABLED +#include +#include +#include +using namespace iflytop; +using namespace zcr; + +#define TAG "ZCanCmder" + +#define OVER_TIME_MS 5 + +uint16_t CanPacketRxBuffer::get_packetindex() { + Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; + return cmdheader->packetindex; +} +uint16_t CanPacketRxBuffer::get_cmdid() { + Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; + return cmdheader->cmdid; +} +uint8_t CanPacketRxBuffer::get_subcmdid() { + Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; + return cmdheader->subcmdid; +} +uint8_t CanPacketRxBuffer::get_packetType() { + Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; + return cmdheader->packetType; +} +uint8_t *CanPacketRxBuffer::get_data() { + Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; + return cmdheader->data; +} +uint16_t CanPacketRxBuffer::get_datalen() { + Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; + return rxdataSize - sizeof(Cmdheader_t); +} +bool CanPacketRxBuffer::iscmd(CmdID_t id) { + Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; + uint16_t maincmdid = ((uint32_t)id >> 8) & 0xFFFF; + uint8_t subcmdId = ((uint32_t)id) & 0xFF; + return cmdheader->cmdid == maincmdid && cmdheader->subcmdid == subcmdId; +} +Cmdheader_t *CanPacketRxBuffer::get_cmdheader() { + Cmdheader_t *cmdheader = (Cmdheader_t *)rxdata; + return cmdheader; +} + +ZCanCmder::CFG *ZCanCmder::createCFG(uint8_t deviceId) { + CFG *cfg = new CFG(); + ZASSERT(cfg != NULL); + cfg->deviceId = deviceId; +#ifdef STM32F103xB + cfg->canHandle = &hcan; +#else + cfg->canHandle = &hcan1; +#endif + cfg->canFilterIndex0 = 0; + cfg->maxFilterNum = 7; + cfg->rxfifoNum = CAN_RX_FIFO0; + return cfg; +} +void ZCanCmder::init(CFG *cfg) { + HAL_StatusTypeDef hal_status; + m_config = cfg; + + /** + * @brief 初始化CAN + */ + + /** + * @brief 初始化消息接收buf + */ + m_canPacketRxBuffer[0].dataIsReady = false; + m_canPacketRxBuffer[0].id = 1; // 只接收来自主机的消息 + m_canPacketRxBuffer[0].m_canPacketNum = 0; + + /** + * @brief 初始化过滤器 + */ + hal_status = initializeFilter(); + if (hal_status != HAL_OK) { + ZLOGE(TAG, "start can initializeFilter fail\r\n"); + return; + } + /** + * @brief 启动CAN + */ + hal_status = HAL_CAN_Start(m_config->canHandle); // 开启CAN + if (hal_status != HAL_OK) { + ZLOGE(TAG, "start can fail\r\n"); + return; + } + /** + * @brief 监听回调 + */ + ZCanIRQDispatcher::instance().regListener(this); + HAL_StatusTypeDef status = activateRxIT(); + if (status != HAL_OK) { + ZLOGE(TAG, "activateRxIT fail\r\n"); + return; + } + // ZHALCORE::getInstance()->regPeriodJob([this](ZHALCORE::Context &context) { loop(); }, 0); +} +HAL_StatusTypeDef ZCanCmder::initializeFilter() { + /** + * @brief ID区帧格式 + * [ 27:0 ] + * [ STDID ] [ EXTID ] + * [11 :9] [8:6] [5:0] [17:16] [15:8] [7:0] + * 优先级 属性 帧类型 目标ID 源ID + */ + HAL_StatusTypeDef HAL_Status; + CAN_FilterTypeDef sFilterConfig; + + uint32_t filterId; + uint32_t mask; + + memset(&sFilterConfig, 0, sizeof(sFilterConfig)); + sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; // 设为MASK模式 + sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; // CAN_FILTERSCALE_16BIT + sFilterConfig.FilterFIFOAssignment = m_config->rxfifoNum; // 关联过滤器到rxfifoNum + sFilterConfig.FilterActivation = ENABLE; // 激活过滤器 + sFilterConfig.SlaveStartFilterBank = m_config->maxFilterNum; // slave filter start index + + /******************************************************************************* + * 接收所有消息 * + *******************************************************************************/ + filterId = (0); // + mask = (0); // + sFilterConfig.FilterBank = m_config->canFilterIndex0; // + sFilterConfig.FilterMaskIdLow = mask & 0xffff; // + sFilterConfig.FilterMaskIdHigh = (mask & 0xffff0000) >> 16; // + sFilterConfig.FilterIdLow = filterId & 0xffff; // + sFilterConfig.FilterIdHigh = (filterId & 0xffff0000) >> 16; // + HAL_Status = HAL_CAN_ConfigFilter(m_config->canHandle, &sFilterConfig); + if (HAL_Status != HAL_OK) { + ZLOGE(TAG, "HAL_CAN_ConfigFilter filter0 fail"); + return HAL_Status; + } + ZLOGI(TAG, "HAL_CAN_ConfigFilter filterID1 %08x", filterId >> 3); + return HAL_Status; +} + +void ZCanCmder::registerListener(ZCanCmderListener *listener) { m_listenerList.push_back(listener); } +void ZCanCmder::regListener(zcan_cmder_listener_t listener) { m_listenerList2.push_back(listener); } +void ZCanCmder::sendPacket(uint8_t *packet, size_t len) { + /** + * @brief + */ + int npacket = len / 8 + (len % 8 == 0 ? 0 : 1); + if (npacket > 255) { + ZLOGE(TAG, "sendPacket fail, len:%d", len); + return; + } + int finalpacketlen = len % 8 == 0 ? 8 : len % 8; + + for (uint8_t i = 0; i < npacket; i++) { + bool suc = false; + if (i == npacket - 1) { + suc = sendPacketSub(npacket, i, packet + i * 8, finalpacketlen, OVER_TIME_MS); + } else { + suc = sendPacketSub(npacket, i, packet + i * 8, 8, OVER_TIME_MS); + } + if (!suc) { + ZLOGE(TAG, "sendPacket fail, packet(%d:%d)", npacket, i); + return; + } + } +} + +void ZCanCmder::sendAck(Cmdheader_t *cmdheader, uint8_t *data, size_t len) { + Cmdheader_t *txheader = (Cmdheader_t *)txbuff; + memcpy(txheader, cmdheader, sizeof(Cmdheader_t)); + txheader->packetType = kpt_ack; + memcpy(txheader->data, data, len); + sendPacket(txbuff, sizeof(Cmdheader_t) + len); +} +void ZCanCmder::sendExecStatusReport(Cmdheader_t *rxcmdheader, uint8_t *data, size_t len) { + Cmdheader_t *txheader = (Cmdheader_t *)txbuff; + memcpy(txheader, rxcmdheader, sizeof(Cmdheader_t)); + txheader->packetType = kpt_cmd_exec_status_report; + memcpy(txheader->data, data, len); + sendPacket(txbuff, sizeof(Cmdheader_t) + len); +} + +void ZCanCmder::sendErrorAck(Cmdheader_t *cmdheader, uint32_t errcode) { + Cmdheader_t *txheader = (Cmdheader_t *)txbuff; + memcpy(txheader, cmdheader, sizeof(Cmdheader_t)); + txheader->packetType = kpt_error_ack; + memcpy(txheader->data, &errcode, sizeof(errcode)); + sendPacket(txbuff, sizeof(Cmdheader_t) + sizeof(errcode)); +} + +bool ZCanCmder::sendPacketSub(int npacket, int packetIndex, uint8_t *packet, size_t len, int overtimems) { + // ZLOGI(TAG, "sendPacketSub(%d:%d)", npacket, packetIndex); + CAN_TxHeaderTypeDef pHeader; + uint8_t aData[8] /*8byte table*/; + uint32_t txMailBox = 0; + + uint32_t enterticket = zos_get_tick(); + + memset(&pHeader, 0, sizeof(pHeader)); + memset(aData, 0, sizeof(aData)); + pHeader.StdId = 0x00; + pHeader.ExtId = (m_config->deviceId << 16) | (npacket << 8) | packetIndex; + pHeader.IDE = CAN_ID_EXT; + pHeader.RTR = CAN_RTR_DATA; + pHeader.DLC = len; + pHeader.TransmitGlobalTime = DISABLE; + + memcpy(aData, packet, len); + + m_lastTransmitStatus = HAL_CAN_AddTxMessage(m_config->canHandle, &pHeader, aData, &txMailBox); + if (m_lastTransmitStatus != HAL_OK) { + ZLOGE(TAG, "HAL_CAN_AddTxMessage fail"); + return false; + } + + while (HAL_CAN_IsTxMessagePending(m_config->canHandle, txMailBox)) { + if (zos_haspassedms(enterticket) > (uint32_t)overtimems) { + m_lastTransmitStatus = HAL_TIMEOUT; + HAL_CAN_AbortTxRequest(m_config->canHandle, txMailBox); + return false; + } + // m_os->sleepMS(1); + } + if (txPacketInterval_ms > 0) { + osDelay(txPacketInterval_ms); + } + return true; +} + +bool ZCanCmder::getRxMessage(CAN_RxHeaderTypeDef *pHeader, uint8_t aData[] /*8byte table*/) { + /** + * @brief 读取当前FIFO中缓存了多少帧的数据 + */ + uint32_t level = HAL_CAN_GetRxFifoFillLevel(m_config->canHandle, m_config->rxfifoNum); + if (level == 0) { + return false; + } + HAL_StatusTypeDef HAL_RetVal; + HAL_RetVal = HAL_CAN_GetRxMessage(m_config->canHandle, m_config->rxfifoNum, pHeader, aData); + if (HAL_OK == HAL_RetVal) { + // 处理接收到的can总线数据 + return true; + } + return false; +} +void ZCanCmder::STM32_HAL_onCAN_RxFifo0MsgPending(CAN_HandleTypeDef *canHandle) { + /** + * @brief 中断上下文 + */ + // ZLOG_INFO("%s\n", __FUNCTION__); + // printf("------------------%s\n", __FUNCTION__); + if (canHandle != m_config->canHandle) { + return; + } + /** + * @brief 处理can接收到消息 + */ + CAN_RxHeaderTypeDef pHeader; + uint8_t aData[8] /*8byte table*/; + while (getRxMessage(&pHeader, aData)) { + /** + * @brief 消息格式 + * + * [2] [3bit] [8bit] [8bit] [8bit] + * , from frameNum frameId + */ + uint8_t from = (pHeader.ExtId >> 16 & 0xFF); + uint8_t nframe = (pHeader.ExtId & 0xFF00) >> 8; + uint8_t frameId = (pHeader.ExtId & 0x00FF); + CanPacketRxBuffer *rxbuf = &m_canPacketRxBuffer[0]; + if (from != rxbuf->id) { + // 目前只接收来自主机的消息 + continue; + } + + if (rxbuf->dataIsReady) { + // 上次接收到的消息还没有来的急处理 + continue; + } + /** + * @TODO:判断是否丢包 + */ + if (frameId == 0) { + rxbuf->m_canPacketNum = 0; + } + + if (rxbuf->m_canPacketNum < 255) { + rxbuf->m_canPacket[rxbuf->m_canPacketNum].pHeader = pHeader; + memcpy(rxbuf->m_canPacket[rxbuf->m_canPacketNum].aData, aData, 8); + rxbuf->m_canPacketNum++; + } + if (nframe == frameId + 1) { + rxbuf->dataIsReady = true; + } + } + + // deactivateRxIT(); +} +void ZCanCmder::STM32_HAL_onCAN_Error(CAN_HandleTypeDef *canHandle) { + if (canHandle != m_config->canHandle) { + return; + } + ZLOGE(TAG, "onCAN_Error\r\n"); +} +void ZCanCmder::loop() { + CanPacketRxBuffer *rxbuf = &m_canPacketRxBuffer[0]; + if (rxbuf->dataIsReady) { + int dataoff = 0; + for (size_t i = 0; i < rxbuf->m_canPacketNum; i++) { + memcpy(rxbuf->rxdata + dataoff, rxbuf->m_canPacket[i].aData, rxbuf->m_canPacket[i].pHeader.DLC); + dataoff += rxbuf->m_canPacket[i].pHeader.DLC; + } + rxbuf->rxdataSize = dataoff; + + for (auto &var : m_listenerList) { + var->onRceivePacket(rxbuf); + } + + for (auto &var : m_listenerList2) { + var(rxbuf); + } + + rxbuf->dataIsReady = false; + } +} +HAL_StatusTypeDef ZCanCmder::activateRxIT() { + HAL_StatusTypeDef hal_status = HAL_ERROR; + if (m_config->rxfifoNum == CAN_RX_FIFO0) { + hal_status = HAL_CAN_ActivateNotification(m_config->canHandle, CAN_IT_RX_FIFO0_MSG_PENDING); + } else if (m_config->rxfifoNum == CAN_RX_FIFO1) { + hal_status = HAL_CAN_ActivateNotification(m_config->canHandle, CAN_IT_RX_FIFO1_MSG_PENDING); + } else { + ZLOGE(TAG, "start can HAL_CAN_ActivateNotification CAN_IT_RX_FIFO0_MSG_PENDING fail\r\n"); + return hal_status; + } + return hal_status; +} +HAL_StatusTypeDef ZCanCmder::deactivateRxIT() { + HAL_StatusTypeDef hal_status = HAL_ERROR; + if (m_config->rxfifoNum == CAN_RX_FIFO0) { + hal_status = HAL_CAN_DeactivateNotification(m_config->canHandle, CAN_IT_RX_FIFO0_MSG_PENDING); + } else if (m_config->rxfifoNum == CAN_RX_FIFO1) { + hal_status = HAL_CAN_DeactivateNotification(m_config->canHandle, CAN_IT_RX_FIFO1_MSG_PENDING); + } else { + ZLOGE(TAG, "start can HAL_CAN_ActivateNotification CAN_IT_RX_FIFO0_MSG_PENDING fail\r\n"); + return hal_status; + } + return hal_status; +} + +#endif diff --git a/components/zcancmder_2/zcanreceiver.hpp b/components/zcancmder_2/zcanreceiver.hpp new file mode 100644 index 0000000..332cd3f --- /dev/null +++ b/components/zcancmder_2/zcanreceiver.hpp @@ -0,0 +1,126 @@ +// +// Created by zwsd +// + +#pragma once +#include "cmd/cmd.hpp" +#include "sdk/os/zos.hpp" + +namespace iflytop { +namespace zcr { +typedef enum { + kpacketHeader = 0, + kpacketData = 1, + kpacketTail = 2, +} packet_type_t; + +class CANPacket { + public: + CAN_RxHeaderTypeDef pHeader; + uint8_t aData[8]; /*8byte table*/ +}; + +}; // namespace zcr +using namespace zcr; + +class CanPacketRxBuffer { + public: + uint16_t id = 0; + CANPacket m_canPacket[256] = {0}; // 用于接收can消息 + uint8_t m_canPacketNum = 0; + bool dataIsReady = false; + uint8_t rxdata[2000] = {0}; + + int rxdataSize = 0; + + public: + uint16_t get_packetindex(); + uint16_t get_cmdid(); + uint8_t get_subcmdid(); + uint8_t get_packetType(); + uint8_t *get_data(); + uint16_t get_datalen(); + + Cmdheader_t *get_cmdheader(); + + bool iscmd(CmdID_t id); + + template + T *get_data_as() { + return (T *)get_data(); + } +}; + +class ZCanCmderListener { + public: + virtual void onRceivePacket(CanPacketRxBuffer *rxcmd) = 0; +}; + +typedef function zcan_cmder_listener_t; + +class ZCanCmder : public ZCanIRQListener { + public: + class CFG { + public: + uint8_t deviceId; // + /******************************************************************************* + * CANConfig * + *******************************************************************************/ + CAN_HandleTypeDef *canHandle; // 默认使用CAN1 + int canFilterIndex0; // 过滤器0 接收,发给自身的消息 + int maxFilterNum; // 使用的过滤器数量,最大值14,默认为7 + int rxfifoNum; // 使用的FIFO,默认使用FIFO0 + int packetRxOvertime_ms; // + }; + + uint8_t txbuff[1000]; + + public: + class LoopJobContext { + public: + bool hasDoneSomething; + }; + + private: + CFG *m_config = NULL; // 配置 + bool m_canOnRxDataFlag = false; // 是否有数据接收,用于从中断上下文转移到MainLoop上下文 + uint32_t m_lastPacketTicket = 0; // 上一次接收到消息的时间,用于判断与主机是否断开连接 + HAL_StatusTypeDef m_lastTransmitStatus; // 上次调用can发送方法的返回值 + + list m_listenerList; + list m_listenerList2; + CanPacketRxBuffer m_canPacketRxBuffer[1]; + + int txPacketInterval_ms = 0; + + public: + ZCanCmder() {} + CFG *createCFG(uint8_t deviceId); + void init(CFG *cfg); + + void registerListener(ZCanCmderListener *listener); + + void regListener(zcan_cmder_listener_t listener); + + void sendPacket(uint8_t *packet, size_t len); + void sendExecStatusReport(Cmdheader_t *rxcmdheader, uint8_t *data, size_t len); + void sendAck(Cmdheader_t *rxcmdheader, uint8_t *data, size_t len); + void sendErrorAck(Cmdheader_t *cmdheader, uint32_t errcode); + bool sendPacketSub(int npacket, int packetIndex, uint8_t *packet, size_t len, int overtimems); + uint8_t getDeviceId() { return m_config->deviceId; } + void setTxPacketInterval(int interval_ms) { txPacketInterval_ms = interval_ms; } + + void loop(); + + public: + virtual void STM32_HAL_onCAN_RxFifo0MsgPending(CAN_HandleTypeDef *can); + virtual void STM32_HAL_onCAN_Error(CAN_HandleTypeDef *can); + + private: + HAL_StatusTypeDef initializeFilter(); + HAL_StatusTypeDef activateRxIT(); + HAL_StatusTypeDef deactivateRxIT(); + bool getRxMessage(CAN_RxHeaderTypeDef *pHeader, uint8_t aData[] /*8byte table*/); +}; + +} // namespace iflytop \ No newline at end of file