|
|
@ -35,29 +35,8 @@ void XYRobotCtrlModule::dumpcfg(const char* tag) { |
|
|
|
ZLOGI(TAG, "= m_cfg_runtozero_dec :%d", m_cfg_runtozero_dec); |
|
|
|
ZLOGI(TAG, "="); |
|
|
|
} |
|
|
|
int32_t XYRobotCtrlModule::set_zero_robottype(RobotType_t robot_type) { |
|
|
|
m_robot_type = robot_type; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t XYRobotCtrlModule::set_x_shaft(bool x_shaft) { |
|
|
|
m_x_shaft = x_shaft; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t XYRobotCtrlModule::set_y_shaft(bool y_shaft) { |
|
|
|
m_y_shaft = y_shaft; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t XYRobotCtrlModule::enable(bool venable) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
|
|
|
|
ZLOGI(TAG, "enable:%d", venable); |
|
|
|
m_stepM1->enable(venable); |
|
|
|
m_stepM2->enable(venable); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t XYRobotCtrlModule::move_to(int32_t x, int32_t y) { |
|
|
|
int32_t XYRobotCtrlModule::move_to(int32_t x, int32_t y, function<void(int32_t exec_status, int32_t tox, int32_t toy)> status_cb) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "move_to x:%d y:%d", x, y); |
|
|
|
int32_t m1pos, m2pos; |
|
|
@ -66,59 +45,194 @@ int32_t XYRobotCtrlModule::move_to(int32_t x, int32_t y) { |
|
|
|
* @TODO:添加限位检查 |
|
|
|
*/ |
|
|
|
updateStatus(1); |
|
|
|
m_thread.start([this, x, y]() { |
|
|
|
m_thread.start([this, x, y, status_cb]() { |
|
|
|
_motor_move_to(x, y, m_cfg_velocity, m_cfg_acc, m_cfg_dec); |
|
|
|
while (!_motor_is_reach_target()) { |
|
|
|
if (m_thread.getExitFlag()) break; |
|
|
|
vTaskDelay(10); |
|
|
|
} |
|
|
|
_motor_stop(); |
|
|
|
int32_t xnow, ynow; |
|
|
|
getnowpos(xnow, ynow); |
|
|
|
if (status_cb) status_cb(0, xnow, ynow); |
|
|
|
updateStatus(0); |
|
|
|
}); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t XYRobotCtrlModule::move_by(int32_t dx, int32_t dy) { |
|
|
|
int32_t XYRobotCtrlModule::move_by(int32_t dx, int32_t dy, function<void(int32_t exec_status, int32_t tox, int32_t toy)> status_cb) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "move_by dx:%d dy:%d", dx, dy); |
|
|
|
int32_t now_x, now_y; |
|
|
|
getnowpos(now_x, now_y); |
|
|
|
return move_to(now_x + dx, now_y + dy); |
|
|
|
return move_to(now_x + dx, now_y + dy, status_cb); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t XYRobotCtrlModule::move_to_zero() { |
|
|
|
int32_t XYRobotCtrlModule::move_to_zero(function<void(int32_t exec_status, int32_t dx, int32_t dy)> status_cb) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "move_to_zero"); |
|
|
|
m_thread.stop(); |
|
|
|
|
|
|
|
updateStatus(1); |
|
|
|
m_thread.stop(); |
|
|
|
m_thread.start([this]() { |
|
|
|
int32_t erro = exec_move_to_zero_task(); |
|
|
|
m_thread.start([this, status_cb]() { |
|
|
|
int32_t dx, dy; |
|
|
|
|
|
|
|
int32_t erro = exec_move_to_zero_task(dx, dy); |
|
|
|
if (erro != 0) { |
|
|
|
ZLOGI(TAG, "exec_move_to_zero_task failed:%d", erro); |
|
|
|
_motor_stop(); |
|
|
|
if (status_cb) status_cb(erro, dx, dy); |
|
|
|
updateStatus(0); |
|
|
|
return; |
|
|
|
} |
|
|
|
m_stepM1->setXACTUAL(0); |
|
|
|
m_stepM2->setXACTUAL(0); |
|
|
|
if (status_cb) status_cb(erro, dx, dy); |
|
|
|
updateStatus(0); |
|
|
|
return; |
|
|
|
}); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t XYRobotCtrlModule::move_to_with_calibrate(int32_t x, int32_t y) { |
|
|
|
|
|
|
|
int32_t XYRobotCtrlModule::move_to_zero_with_calibrate(int32_t x, int32_t y, |
|
|
|
function<void(int32_t exec_status, int32_t zero_shift_x, int32_t zero_shift_y)> status_cb) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "move_to_with_calibrate x:%d y:%d", x, y); |
|
|
|
ZLOGI(TAG, "move_to_zero_with_calibrate x:%d y:%d", x, y); |
|
|
|
m_thread.stop(); |
|
|
|
updateStatus(1); |
|
|
|
|
|
|
|
m_thread.start([this, x, y, status_cb]() { |
|
|
|
int32_t dx, dy; |
|
|
|
int32_t erro = exec_move_to_zero_task(dx, dy); |
|
|
|
if (erro != 0) { |
|
|
|
ZLOGI(TAG, "exec_move_to_zero_task failed:%d", erro); |
|
|
|
_motor_stop(); |
|
|
|
if (status_cb) status_cb(erro, 0, 0); |
|
|
|
updateStatus(0); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t calibrate_x, calibrate_y; |
|
|
|
calibrate_x = dx + x; |
|
|
|
calibrate_y = dy + y; |
|
|
|
|
|
|
|
m_zero_shift_x = calibrate_x; |
|
|
|
m_zero_shift_y = calibrate_y; |
|
|
|
|
|
|
|
m_stepM1->setXACTUAL(0); |
|
|
|
m_stepM2->setXACTUAL(0); |
|
|
|
if (status_cb) status_cb(erro, m_zero_shift_x, m_zero_shift_y); |
|
|
|
updateStatus(0); |
|
|
|
return; |
|
|
|
}); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t XYRobotCtrlModule::exec_move_to_zero_task(int32_t& dx, int32_t& dy) { |
|
|
|
int32_t xnow, ynow; |
|
|
|
getnowpos(xnow, ynow); |
|
|
|
|
|
|
|
int32_t ret = exec_move_to_zero_task(); |
|
|
|
|
|
|
|
int32_t xnow2, ynow2; |
|
|
|
getnowpos(xnow2, ynow2); |
|
|
|
|
|
|
|
dx = xnow2 - xnow; |
|
|
|
dy = ynow2 - ynow; |
|
|
|
|
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t XYRobotCtrlModule::exec_move_to_zero_task() { |
|
|
|
/*******************************************************************************
|
|
|
|
* 移动X轴到零点 * |
|
|
|
*******************************************************************************/ |
|
|
|
_motor_move_by(m_cfg_runtozero_maxX, 0, m_cfg_runtozero_speed, m_cfg_acc, m_cfg_runtozero_dec); |
|
|
|
bool xreach_zero = false; |
|
|
|
while (!m_thread.getExitFlag() && !_motor_is_reach_target()) { |
|
|
|
// ZLOGI(TAG, "xgpio %d %d %d", m_Xgpio->getState(), m_stepM1->isReachTarget(), m_stepM2->isReachTarget());
|
|
|
|
if (m_Xgpio->getState()) { |
|
|
|
xreach_zero = true; |
|
|
|
_motor_stop(-1); |
|
|
|
break; |
|
|
|
} |
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
|
|
|
|
if (!xreach_zero) { |
|
|
|
// 触发异常回调
|
|
|
|
ZLOGE(TAG, "x reach zero failed"); |
|
|
|
return err::kcommon_error_not_found_x_zero_point; |
|
|
|
} |
|
|
|
|
|
|
|
if (m_thread.getExitFlag()) { |
|
|
|
ZLOGI(TAG, "break move to zero thread exit"); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
ZLOGI(TAG, "x reach zero"); |
|
|
|
/*******************************************************************************
|
|
|
|
* 移动Y轴到零点 * |
|
|
|
*******************************************************************************/ |
|
|
|
_motor_move_by(0, m_cfg_runtozero_maxY, m_cfg_runtozero_speed, m_cfg_acc, m_cfg_runtozero_dec); |
|
|
|
bool yreach_zero = false; |
|
|
|
while (!m_thread.getExitFlag() && !_motor_is_reach_target()) { |
|
|
|
// ZLOGI(TAG, "ygpio %d", m_Ygpio->getState());
|
|
|
|
if (m_Ygpio->getState()) { |
|
|
|
yreach_zero = true; |
|
|
|
_motor_stop(-1); |
|
|
|
break; |
|
|
|
} |
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
if (!yreach_zero) { |
|
|
|
// 触发异常回调
|
|
|
|
ZLOGE(TAG, "y reach zero failed"); |
|
|
|
return err::kcommon_error_not_found_y_zero_point; |
|
|
|
} |
|
|
|
if (m_thread.getExitFlag()) { |
|
|
|
ZLOGI(TAG, "break move to zero thread exit"); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
ZLOGI(TAG, "y reach zero"); |
|
|
|
ZLOGI(TAG, "move_to_zero success"); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
void XYRobotCtrlModule::updateStatus(uint8_t status) { |
|
|
|
zlock_guard lock(m_statu_lock); |
|
|
|
m_status = status; |
|
|
|
} |
|
|
|
void XYRobotCtrlModule::getnowpos(int32_t& x, int32_t& y) { |
|
|
|
int32_t m1pos, m2pos; |
|
|
|
m1pos = m_stepM1->getXACTUAL(); |
|
|
|
m2pos = m_stepM2->getXACTUAL(); |
|
|
|
inverse_kinematics(m1pos, m2pos, x, y); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t XYRobotCtrlModule::stop() { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "stop"); |
|
|
|
m_thread.stop(); |
|
|
|
_motor_stop(); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t XYRobotCtrlModule::read_status(uint8_t* module_statu, int32_t* x, int32_t* y) { |
|
|
|
zlock_guard lock(m_statu_lock); |
|
|
|
*module_statu = m_status; |
|
|
|
|
|
|
|
if (x != nullptr && y != nullptr) { |
|
|
|
getnowpos(*x, *y); |
|
|
|
} |
|
|
|
ZLOGI(TAG, "read_status:%d %d %d", *module_statu, *x, *y); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
uint8_t XYRobotCtrlModule::read_status(uint8_t* module_statu, int32_t* x, int32_t* y) { return 0; } |
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* SET * |
|
|
|
*******************************************************************************/ |
|
|
|
int32_t XYRobotCtrlModule::set_acc(int32_t acc) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "set_acc acc:%d", acc); |
|
|
@ -173,67 +287,57 @@ int32_t XYRobotCtrlModule::set_distance_scale(float distance_scale) { |
|
|
|
ZLOGI(TAG, "set_distance_scale:%f", distance_scale); |
|
|
|
m_stepM1->setDistanceK(distance_scale); |
|
|
|
m_stepM2->setDistanceK(distance_scale); |
|
|
|
m_cfg_distance_scale = distance_scale; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
void XYRobotCtrlModule::loop() {} |
|
|
|
|
|
|
|
int32_t XYRobotCtrlModule::exec_move_to_zero_task() { |
|
|
|
/*******************************************************************************
|
|
|
|
* 移动X轴到零点 * |
|
|
|
*******************************************************************************/ |
|
|
|
_motor_move_by(m_cfg_runtozero_maxX, 0, m_cfg_runtozero_speed, m_cfg_acc, m_cfg_runtozero_dec); |
|
|
|
bool xreach_zero = false; |
|
|
|
while (true) { |
|
|
|
// ZLOGI(TAG, "xgpio %d %d %d", m_Xgpio->getState(), m_stepM1->isReachTarget(), m_stepM2->isReachTarget());
|
|
|
|
if (m_Xgpio->getState()) { |
|
|
|
xreach_zero = true; |
|
|
|
_motor_stop(-1); |
|
|
|
break; |
|
|
|
} |
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
int32_t XYRobotCtrlModule::set_zero_robottype(RobotType_t robot_type) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "set_zero_robottype:%d", robot_type); |
|
|
|
m_robot_type = robot_type; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t XYRobotCtrlModule::set_x_shaft(bool x_shaft) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "set_x_shaft:%d", x_shaft); |
|
|
|
m_x_shaft = x_shaft; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t XYRobotCtrlModule::set_y_shaft(bool y_shaft) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "set_y_shaft:%d", y_shaft); |
|
|
|
m_y_shaft = y_shaft; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
if (!xreach_zero) { |
|
|
|
// 触发异常回调
|
|
|
|
ZLOGE(TAG, "x reach zero failed"); |
|
|
|
return err::kcommon_error_not_found_x_zero_point; |
|
|
|
} |
|
|
|
ZLOGI(TAG, "x reach zero"); |
|
|
|
/*******************************************************************************
|
|
|
|
* 移动Y轴到零点 * |
|
|
|
*******************************************************************************/ |
|
|
|
_motor_move_by(0, m_cfg_runtozero_maxY, m_cfg_runtozero_speed, m_cfg_acc, m_cfg_runtozero_dec); |
|
|
|
bool yreach_zero = false; |
|
|
|
while (!_motor_is_reach_target()) { |
|
|
|
// ZLOGI(TAG, "ygpio %d", m_Ygpio->getState());
|
|
|
|
if (m_Ygpio->getState()) { |
|
|
|
yreach_zero = true; |
|
|
|
_motor_stop(-1); |
|
|
|
break; |
|
|
|
} |
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
if (!yreach_zero) { |
|
|
|
// 触发异常回调
|
|
|
|
ZLOGE(TAG, "y reach zero failed"); |
|
|
|
return err::kcommon_error_not_found_y_zero_point; |
|
|
|
} |
|
|
|
ZLOGI(TAG, "y reach zero"); |
|
|
|
ZLOGI(TAG, "move_to_zero success"); |
|
|
|
int32_t XYRobotCtrlModule::enable(bool venable) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
|
|
|
|
ZLOGI(TAG, "enable:%d", venable); |
|
|
|
m_stepM1->enable(venable); |
|
|
|
m_stepM2->enable(venable); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
void XYRobotCtrlModule::updateStatus(uint8_t status) { |
|
|
|
zlock_guard lock(m_statu_lock); |
|
|
|
m_status = status; |
|
|
|
int32_t XYRobotCtrlModule::set_zero_shift(int32_t x, int32_t y) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "set_zero_shift x:%d y:%d", x, y); |
|
|
|
m_zero_shift_x = x; |
|
|
|
m_zero_shift_y = y; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
void XYRobotCtrlModule::getnowpos(int32_t& x, int32_t& y) { |
|
|
|
int32_t m1pos, m2pos; |
|
|
|
m1pos = m_stepM1->getXACTUAL(); |
|
|
|
m2pos = m_stepM1->getXACTUAL(); |
|
|
|
inverse_kinematics(m1pos, m2pos, x, y); |
|
|
|
int32_t XYRobotCtrlModule::get_zero_shift(int32_t* x, int32_t* y) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
*x = m_zero_shift_x; |
|
|
|
*y = m_zero_shift_y; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* BASIC_ACTION * |
|
|
|
*******************************************************************************/ |
|
|
|
void XYRobotCtrlModule::_motor_move_to(int32_t x, int32_t y, int32_t maxv, int32_t acc, int32_t dec) { |
|
|
|
ZLOGI(TAG, "_motor_move_to x:%d y:%d maxv:%d,acc:%d,dec:%d", x, y, maxv, acc, dec); |
|
|
|
int32_t target_m1pos, target_m2pos; |
|
|
@ -244,6 +348,7 @@ void XYRobotCtrlModule::_motor_move_to(int32_t x, int32_t y, int32_t maxv, int32 |
|
|
|
m_stepM1->setDeceleration(dec); |
|
|
|
m_stepM2->setDeceleration(dec); |
|
|
|
|
|
|
|
ZLOGI(TAG, "_motor_move_to target_m1pos:%d target_m2pos:%d", target_m1pos, target_m2pos); |
|
|
|
m_stepM1->moveTo(target_m1pos, maxv); |
|
|
|
m_stepM2->moveTo(target_m2pos, maxv); |
|
|
|
} |
|
|
@ -286,11 +391,17 @@ void XYRobotCtrlModule::inverse_kinematics(int32_t m1pos, int32_t m2pos, int32_t |
|
|
|
} |
|
|
|
if (m_x_shaft) x = -x; |
|
|
|
if (m_y_shaft) y = -y; |
|
|
|
|
|
|
|
x += m_zero_shift_x; |
|
|
|
y += m_zero_shift_y; |
|
|
|
} |
|
|
|
void XYRobotCtrlModule::forward_kinematics(int32_t x, int32_t y, int32_t& m1pos, int32_t& m2pos) { |
|
|
|
if (m_x_shaft) x = -x; |
|
|
|
if (m_y_shaft) y = -y; |
|
|
|
|
|
|
|
x -= m_zero_shift_x; |
|
|
|
y -= m_zero_shift_y; |
|
|
|
|
|
|
|
if (m_robot_type == corexy) { |
|
|
|
m1pos = ((x + y) / 2); |
|
|
|
m2pos = ((y - x) / 2); |
|
|
|