#include "intelligent_winding_robot_ctrl.hpp" #include #include #include #include using namespace std; using namespace iflytop; #define TAG "APPDM" #define DO(exptr) \ { \ int32_t ret = exptr; \ if (ret != 0) { \ return ret; \ } \ } #define PARAM int32_t paramN, const char **paraV, ICmdParserACK *ack void IntelligentWindingRobotCtrl::processError(int32_t err) { ZLOGE(TAG, "processError: %d", err); } class myexception : public exception { virtual const char* what() const throw() { return "My exception happened"; } }; int32_t IntelligentWindingRobotCtrl::initialize(APPDM* dm, ICmdParser* cmdparse) { m_dm = dm; m_cmdparse = cmdparse; initialize_device(); regcb(); return 0; } int32_t IntelligentWindingRobotCtrl::initialize_device() { cfg.xy_platform_takeline_clip00_pos_x = 1736; cfg.xy_platform_takeline_clip00_pos_y = 16853; cfg.xy_platform_takeline_clipXX_pos_x = 52307; cfg.xy_platform_takeline_clipXX_pos_y = 31993; cfg.clip_line = 12; cfg.clip_each_line_num = 5; cfg.m11_arm_jiaxian_reset_pos = 2619; cfg.m11_arm_jiaxian_clamp_torque = 330; cfg.m11_arm_jiaxian_clamp_direction = -1; cfg.m12_jiaxian_reset_pos = 2600; cfg.m12_jiaxian_clamp_direction = -1; cfg.m12_jiaxian_clamp_torque = 330; cfg.m13_yaxian_forward_reset_pos = 1015; cfg.m13_yaxian_backward_reset_pos = 2885; cfg.m13_jiaxian_clamp_direction = -1; cfg.m13_jiaxian_clamp_torque = 200; cfg.m14_raoxiantance_reset_pos = 2047; cfg.m14_raoxiantance_tance_zero_pos = 2517; cfg.m15_paifei_reset_pos = 2046; cfg.m15_paifei_press_direction = 1; cfg.m15_paifei_press_torque = 330; cfg.m16_xianlajin_reset_pos = 2047; cfg.m16_xianlajin_tight_line_pos = 1966; cfg.m16_xianlajin_winding_low_pos = 1900; cfg.m16_xianlajin_winding_up_pos = 1865; cfg.m16_xianlajin_line_entry_pos = 1833; cfg.m21_arm_hook_claws_full_pos = 2558; cfg.m21_arm_hook_claws_half_pos = 2294; cfg.z_axis_take_clip_pos = 6924; return 0; } void IntelligentWindingRobotCtrl::regcb() { // device_reset m_cmdparse->regCMD("device_reset", "()", 0, [this](PARAM) { device_reset(); }); m_cmdparse->regCMD("enable_all_module", "()", 0, [this](PARAM) { enable_all_module(); }); m_cmdparse->regCMD("disable_all_module", "()", 0, [this](PARAM) { disable_all_module(); }); m_cmdparse->regCMD("xy_run_to_clip_pos_test", "()", 1, [this](PARAM) { return xy_run_to_clip_pos_test(atoi(paraV[0])); }); // m_cmdparse->regCMD("disable_all_motor", "()", 0, [this](PARAM) { return disable_all_motor(); }); m_cmdparse->regCMD("app_m11_arm_jiaxian_move_to_reset_pos", "()", 0, [this](PARAM) { return m11_arm_jiaxian_move_to_reset_pos(); }); m_cmdparse->regCMD("app_m11_arm_jiaxian_move_to_clamp_pos", "()", 0, [this](PARAM) { return m11_arm_jiaxian_move_to_clamp_pos(); }); m_cmdparse->regCMD("app_m12_jiaxian_move_to_reset_pos", "()", 0, [this](PARAM) { return m12_jiaxian_move_to_reset_pos(); }); m_cmdparse->regCMD("app_m12_jiaxian_move_to_clamp_pos", "()", 0, [this](PARAM) { return m12_jiaxian_move_to_clamp_pos(); }); m_cmdparse->regCMD("app_m13_yaxian_move_to_reset_forward", "()", 0, [this](PARAM) { return m13_yaxian_move_to_reset_forward(); }); m_cmdparse->regCMD("app_m13_yaxian_move_to_reset_backward", "()", 0, [this](PARAM) { return m13_yaxian_move_to_reset_backward(); }); m_cmdparse->regCMD("app_m13_yaxian_press_clip", "()", 0, [this](PARAM) { return m13_yaxian_press_clip(); }); m_cmdparse->regCMD("app_m14_raoxiantance_move_to_reset", "()", 0, [this](PARAM) { return m14_raoxiantance_move_to_reset(); }); m_cmdparse->regCMD("app_m15_paifei_moveto_reset", "()", 0, [this](PARAM) { return m15_paifei_moveto_reset(); }); m_cmdparse->regCMD("app_m15_paifei_moveto_press", "()", 0, [this](PARAM) { return m15_paifei_moveto_press(); }); m_cmdparse->regCMD("app_m16_xianlajin_move_to_reset", "()", 0, [this](PARAM) { return m16_xianlajin_move_to_reset(); }); m_cmdparse->regCMD("app_m16_xianlajin_move_to_tight_line_pos", "()", 0, [this](PARAM) { return m16_xianlajin_move_to_tight_line_pos(); }); m_cmdparse->regCMD("app_m16_xianlajin_move_to_winding_low_pos", "()", 0, [this](PARAM) { return m16_xianlajin_move_to_winding_low_pos(); }); m_cmdparse->regCMD("app_m16_xianlajin_move_to_winding_up_pos", "()", 0, [this](PARAM) { return m16_xianlajin_move_to_winding_up_pos(); }); m_cmdparse->regCMD("app_m16_xianlajin_move_to_line_entry_pos", "()", 0, [this](PARAM) { return m16_xianlajin_move_to_line_entry_pos(); }); m_cmdparse->regCMD("app_m21_arm_hook_claws_reset", "()", 0, [this](PARAM) { return m21_arm_hook_claws_reset(); }); m_cmdparse->regCMD("app_m21_arm_hook_claws_move_to_half_pos", "()", 0, [this](PARAM) { return m21_arm_hook_claws_move_to_half_pos(); }); m_cmdparse->regCMD("app_m21_arm_hook_claws_move_to_full_pos", "()", 0, [this](PARAM) { return m21_arm_hook_claws_move_to_full_pos(); }); m_cmdparse->regCMD("app_m22_scissors_move_reset_pos", "()", 0, [this](PARAM) { return m22_scissors_move_reset_pos(); }); m_cmdparse->regCMD("app_m22_scissors_cut", "()", 0, [this](PARAM) { return m22_scissors_cut(); }); m_cmdparse->regCMD("app_m23_laxian_motor_move_to_reset_pos", "()", 0, [this](PARAM) { return m23_laxian_motor_move_to_reset_pos(); }); m_cmdparse->regCMD("app_m23_laxian_motor_move_to_tight_line_pos", "()", 0, [this](PARAM) { return m23_laxian_motor_move_to_tight_line_pos(); }); } void IntelligentWindingRobotCtrl::wait_module_idle(int32_t moduleid) { zos_delay(100); int i = 0; while (true) { int32_t status = 0; int32_t ecode = m_dm->module_get_status(moduleid, &status); if (ecode != 0) { processError(ecode); break; }; if (status == 0) { ZLOGI(TAG, "wait_module_idle %d %d....", moduleid, status); break; } if (status == 2) { processError(err::kfail); break; }; if (i % 30 == 0) { ZLOGI(TAG, "wait_module_idle %d %d....", moduleid, status); break; } i++; zos_delay(100); } } void IntelligentWindingRobotCtrl::wait_modules_idle(void* mark, ...) { int32_t moduleid = 0; va_list args; va_start(args, mark); moduleid = va_arg(args, int32_t); while (moduleid > 0) { wait_module_idle(moduleid); moduleid = va_arg(args, int32_t); } va_end(args); } int32_t IntelligentWindingRobotCtrl::disable_all_module() { m_dm->motor_enable(2, 0); m_dm->xymotor_enable(3, 0); m_dm->motor_enable(4, 0); m_dm->motor_enable(11, 0); m_dm->motor_enable(12, 0); m_dm->motor_enable(13, 0); m_dm->motor_enable(14, 0); m_dm->motor_enable(15, 0); m_dm->motor_enable(16, 0); m_dm->motor_enable(21, 0); m_dm->motor_enable(22, 0); m_dm->motor_enable(23, 0); return 0; } int32_t IntelligentWindingRobotCtrl::enable_all_module() { m_dm->motor_enable(2, 1); m_dm->xymotor_enable(3, 1); m_dm->motor_enable(4, 1); m_dm->motor_enable(11, 1); m_dm->motor_enable(12, 1); m_dm->motor_enable(13, 1); m_dm->motor_enable(14, 1); m_dm->motor_enable(15, 1); m_dm->motor_enable(16, 1); m_dm->motor_enable(21, 1); m_dm->motor_enable(22, 1); m_dm->motor_enable(23, 1); return 0; } #define WAIT_MODULES_IDLE(...) \ { wait_modules_idle(NULL, __VA_ARGS__, NULL); } int32_t IntelligentWindingRobotCtrl::device_reset() { // Z轴复位 /** * @ * * 1. Z轴及其组件复位 * 2. 除了M13外,其他电机复位 * 3. M13复位,等待一定时间,查看M13是否复位成功 * 4. 机械臂复位 */ enable_all_module(); m4_zreset(); m11_arm_jiaxian_move_to_reset_pos(); m21_arm_hook_claws_reset(); WAIT_MODULES_IDLE(4, 11, 21); m12_jiaxian_move_to_reset_pos(); m14_raoxiantance_move_to_reset(); m15_paifei_moveto_reset(); m16_xianlajin_move_to_reset(); m22_scissors_move_reset_pos(); m23_laxian_motor_move_to_reset_pos(); WAIT_MODULES_IDLE(12, 14, 15, 16, 22, 23); /** * @brief 优化复位逻辑,超时复位失败,报警 */ m13_yaxian_move_to_reset_backward(); wait_module_idle(13); m_dm->xymotor_move_to_zero(XYRobot_ID); wait_module_idle(XYRobot_ID); ZLOGI(TAG, "device_reset finished...."); } int32_t IntelligentWindingRobotCtrl::m11_arm_jiaxian_move_to_reset_pos() { return m_dm->motor_move_to(11, cfg.m11_arm_jiaxian_reset_pos, 2000, 0); } int32_t IntelligentWindingRobotCtrl::m11_arm_jiaxian_move_to_clamp_pos() { return m_dm->motor_rotate_with_torque(11, cfg.m11_arm_jiaxian_clamp_direction, cfg.m11_arm_jiaxian_clamp_torque); } int32_t IntelligentWindingRobotCtrl::m12_jiaxian_move_to_reset_pos() { return m_dm->motor_move_to(12, cfg.m12_jiaxian_reset_pos, 2000, 0); } int32_t IntelligentWindingRobotCtrl::m12_jiaxian_move_to_clamp_pos() { return m_dm->motor_rotate_with_torque(12, cfg.m12_jiaxian_clamp_direction, cfg.m12_jiaxian_clamp_torque); } int32_t IntelligentWindingRobotCtrl::m13_yaxian_move_to_reset_forward() { return m_dm->motor_move_to(13, cfg.m13_yaxian_forward_reset_pos, 2000, 0); } int32_t IntelligentWindingRobotCtrl::m13_yaxian_move_to_reset_backward() { return m_dm->motor_move_to(13, cfg.m13_yaxian_backward_reset_pos, 2000, 0); } int32_t IntelligentWindingRobotCtrl::m13_yaxian_press_clip() { return m_dm->motor_rotate_with_torque(13, cfg.m13_jiaxian_clamp_direction, cfg.m13_jiaxian_clamp_torque); } int32_t IntelligentWindingRobotCtrl::m14_raoxiantance_move_to_reset() { return m_dm->motor_move_to(14, cfg.m14_raoxiantance_reset_pos, 2000, 0); } int32_t IntelligentWindingRobotCtrl::m15_paifei_moveto_reset() { return m_dm->motor_move_to(15, cfg.m15_paifei_reset_pos, 2000, 0); } int32_t IntelligentWindingRobotCtrl::m15_paifei_moveto_press() { return m_dm->motor_rotate_with_torque(15, cfg.m15_paifei_press_direction, cfg.m15_paifei_press_torque); } int32_t IntelligentWindingRobotCtrl::m16_xianlajin_move_to_reset() { return m_dm->motor_move_to(16, cfg.m16_xianlajin_reset_pos, 300, 0); } int32_t IntelligentWindingRobotCtrl::m16_xianlajin_move_to_tight_line_pos() { return m_dm->motor_move_to(16, cfg.m16_xianlajin_tight_line_pos, 100, 0); } int32_t IntelligentWindingRobotCtrl::m16_xianlajin_move_to_winding_low_pos() { return m_dm->motor_move_to(16, cfg.m16_xianlajin_winding_low_pos, 100, 0); } int32_t IntelligentWindingRobotCtrl::m16_xianlajin_move_to_winding_up_pos() { return m_dm->motor_move_to(16, cfg.m16_xianlajin_winding_up_pos, 100, 0); } int32_t IntelligentWindingRobotCtrl::m16_xianlajin_move_to_line_entry_pos() { return m_dm->motor_move_to(16, cfg.m16_xianlajin_line_entry_pos, 100, 0); } int32_t IntelligentWindingRobotCtrl::m21_arm_hook_claws_reset() { return m_dm->motor_move_to_zero_backward(21, 0, 0, 0, 0); } int32_t IntelligentWindingRobotCtrl::m21_arm_hook_claws_move_to_half_pos() { return m_dm->motor_move_to(21, cfg.m21_arm_hook_claws_half_pos, 0, 0); } int32_t IntelligentWindingRobotCtrl::m21_arm_hook_claws_move_to_full_pos() { return m_dm->motor_move_to(21, cfg.m21_arm_hook_claws_full_pos, 0, 0); } int32_t IntelligentWindingRobotCtrl::m22_scissors_move_reset_pos() { return 0; } int32_t IntelligentWindingRobotCtrl::m22_scissors_cut() { return m_dm->motor_rotate_with_torque(22, 1, 4095); } int32_t IntelligentWindingRobotCtrl::m23_laxian_motor_move_to_reset_pos() { return m_dm->motor_move_to_zero_backward(23, 0, 0, 0, 0); } int32_t IntelligentWindingRobotCtrl::m23_laxian_motor_move_to_tight_line_pos() { return m_dm->motor_move_to(23, 500, 0, 0); } int32_t IntelligentWindingRobotCtrl::m4_zreset() { m_dm->motor_move_to_zero_backward(4, 450, 300, 2000, 0); } int32_t IntelligentWindingRobotCtrl::m4_zmove_to(int32_t pos) { ZLOGI(TAG, "zmove_to %d", pos); int32_t nowpos = 0; m_dm->motor_read_pos(ZMOTOR_ID, &nowpos); if (pos >= nowpos) { m_dm->motor_move_to(ZMOTOR_ID, pos, 450, 600); } else { m_dm->motor_move_to(ZMOTOR_ID, pos, 1000, 600); } if (pos == 0) { m4_zreset(); } } int32_t IntelligentWindingRobotCtrl::main_shaft_run() { ZLOGI(TAG, "main_shaft_run"); DO(m_dm->motor_rotate_acctime(2, 1, 1000, 10000)); return 0; } int32_t IntelligentWindingRobotCtrl::main_shaft_stop() { ZLOGI(TAG, "main_shaft_stop"); DO(m_dm->module_stop(2)); return 0; } /** * @brief XY平台 */ int32_t IntelligentWindingRobotCtrl::xy_platform_reset() { return 0; } /** * @brief Z轴 */ int32_t IntelligentWindingRobotCtrl::z_axis_reset() { return 0; } int32_t IntelligentWindingRobotCtrl::z_axis_move_to(int32_t pos) { return 0; } int32_t IntelligentWindingRobotCtrl::xy_move_to_zero() { return 0; } // 移动到零位 int32_t IntelligentWindingRobotCtrl::xy_take_clip(int32_t index) { return 0; } // 取弹夹 int32_t IntelligentWindingRobotCtrl::xy_take_line() { return 0; } // 取线 int32_t IntelligentWindingRobotCtrl::xy_take_back_clip() { return 0; } // 放弹夹 int32_t IntelligentWindingRobotCtrl::xy_remove_line() { return 0; } // 移除线 int32_t IntelligentWindingRobotCtrl::do_reset_device() {} int32_t IntelligentWindingRobotCtrl::do_winding(int32_t index) {} /** * @brief */ int32_t IntelligentWindingRobotCtrl::start_winding() { return 0; } int32_t IntelligentWindingRobotCtrl::stop_winding() { return 0; } int32_t IntelligentWindingRobotCtrl::reset_and_check_device() { return 0; } int32_t IntelligentWindingRobotCtrl::xy_get_point(int32_t clip_index, int32_t& x, int32_t& y) { int clip_x_off = clip_index / cfg.clip_each_line_num; int clip_y_off = clip_index % cfg.clip_each_line_num; float eachx = (cfg.xy_platform_takeline_clipXX_pos_x - cfg.xy_platform_takeline_clip00_pos_x) / (cfg.clip_line - 1); float eachy = (cfg.xy_platform_takeline_clipXX_pos_y - cfg.xy_platform_takeline_clip00_pos_y) / (cfg.clip_each_line_num - 1); x = cfg.xy_platform_takeline_clip00_pos_x + eachx * clip_x_off; y = cfg.xy_platform_takeline_clip00_pos_y + eachy * clip_y_off; return 0; } #if 0 int32_t IntelligentWindingRobotCtrl::zmove_to(int32_t pos) { ZLOGI(TAG, "zmove_to %d", pos); int32_t nowpos = 0; m_dm->motor_read_pos(ZMOTOR_ID, &nowpos); if (pos >= nowpos) { m_dm->motor_move_to(ZMOTOR_ID, pos, 450, 600); } else { m_dm->motor_move_to(ZMOTOR_ID, pos, 1000, 600); } if (pos == 0) { zreset(); } wait_module_idle(ZMOTOR_ID); return 0; } int32_t IntelligentWindingRobotCtrl::zreset() { ZLOGI(TAG, "zreset"); m_dm->motor_enable(ZMOTOR_ID, 1); wait_module_idle(ZMOTOR_ID); return 0; } #endif int32_t IntelligentWindingRobotCtrl::xymove_to(int32_t x, int32_t y) { ZLOGI(TAG, "xymove_to %d %d", x, y); m_dm->xymotor_move_to(XYRobot_ID, x, y, 0); wait_module_idle(XYRobot_ID); return 0; } int32_t IntelligentWindingRobotCtrl::xy_reset() { ZLOGI(TAG, "xy_reset"); m_dm->xymotor_move_to_zero(XYRobot_ID); wait_module_idle(XYRobot_ID); return 0; } int32_t IntelligentWindingRobotCtrl::xy_run_to_clip_pos_test(int32_t clip_index) { #if 0 ZLOGI(TAG, "xy_run_to_clip_pos_test %d", clip_index); if (clip_index >= cfg.clip_line * cfg.clip_each_line_num) { ZLOGE(TAG, "clip_index %d out of range", clip_index); return err::kparam_out_of_range; } zreset(); m11_arm_jiaxian_move_to_reset_pos(); m21_arm_hook_claws_reset(); int32_t x = 0; int32_t y = 0; xy_get_point(clip_index, x, y); xymove_to(x, y); zmove_to(cfg.z_axis_take_clip_pos); m21_arm_hook_claws_move_to_full_pos(); zmove_to(0); zmove_to(cfg.z_axis_take_clip_pos); m21_arm_hook_claws_reset(); zmove_to(0); #endif } int32_t IntelligentWindingRobotCtrl::setcfg(const char* cfgname, int32_t cfgvalue) { #if 0 if (strcmp(cfgname, "paifei_reset_pos") == 0) cfg.paifei_reset_pos = cfgvalue; else if (strcmp(cfgname, "paifei_press_pos") == 0) cfg.paifei_press_pos = cfgvalue; else if (strcmp(cfgname, "paifei_press_torque") == 0) cfg.paifei_press_torque = cfgvalue; else if (strcmp(cfgname, "raoxiantance_reset_pos") == 0) cfg.raoxiantance_reset_pos = cfgvalue; else if (strcmp(cfgname, "raoxiantance_tance_zero_pos") == 0) cfg.raoxiantance_tance_zero_pos = cfgvalue; else if (strcmp(cfgname, "yaxian_reset_pos") == 0) cfg.yaxian_reset_pos = cfgvalue; else if (strcmp(cfgname, "yaxian_press_pos") == 0) cfg.yaxian_press_pos = cfgvalue; else if (strcmp(cfgname, "yaxian_press_torque") == 0) cfg.yaxian_press_torque = cfgvalue; else if (strcmp(cfgname, "yaxian_wait_for_press_pos") == 0) cfg.yaxian_wait_for_press_pos = cfgvalue; else if (strcmp(cfgname, "xianlajin_reset_pos") == 0) cfg.xianlajin_reset_pos = cfgvalue; else if (strcmp(cfgname, "xianlajin_line_entry_pos") == 0) cfg.xianlajin_line_entry_pos = cfgvalue; else if (strcmp(cfgname, "xianlajin_tight_line_pos") == 0) cfg.xianlajin_tight_line_pos = cfgvalue; else if (strcmp(cfgname, "xianlajin_loose_line_pos") == 0) cfg.xianlajin_loose_line_pos = cfgvalue; else if (strcmp(cfgname, "jiaxian_reset_pos") == 0) cfg.jiaxian_reset_pos = cfgvalue; else if (strcmp(cfgname, "jiaxian_clamp_pos") == 0) cfg.jiaxian_clamp_pos = cfgvalue; else if (strcmp(cfgname, "jiaxian_clamp_torque") == 0) cfg.jiaxian_clamp_torque = cfgvalue; else if (strcmp(cfgname, "arm_jiaxian_reset_pos") == 0) cfg.arm_jiaxian_reset_pos = cfgvalue; else if (strcmp(cfgname, "arm_jiaxian_clamp_pos") == 0) cfg.arm_jiaxian_clamp_pos = cfgvalue; else if (strcmp(cfgname, "arm_jiaxian_clamp_torque") == 0) cfg.arm_jiaxian_clamp_torque = cfgvalue; else if (strcmp(cfgname, "scissors_reset_pos") == 0) cfg.scissors_reset_pos = cfgvalue; else if (strcmp(cfgname, "m22_scissors_cut_pos") == 0) cfg.m22_scissors_cut_pos = cfgvalue; else if (strcmp(cfgname, "m21_arm_hook_claws_half_pos") == 0) cfg.m21_arm_hook_claws_half_pos = cfgvalue; else if (strcmp(cfgname, "m21_arm_hook_claws_full_pos") == 0) cfg.m21_arm_hook_claws_full_pos = cfgvalue; return 0; #endif return 0; } int32_t IntelligentWindingRobotCtrl::dumpcfg() { return 0; } #if 1 #endif