|
|
@ -29,7 +29,7 @@ void StepMotorCtrlModule::initialize(int id, IStepperMotor* stepM, ZGPIO* zero_g |
|
|
|
|
|
|
|
bool StepMotorCtrlModule::isbusy() { return m_thread.isworking(); } |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::move_to(int32_t tox, function<void(move_to_cb_status_t& status)> status_cb) { //
|
|
|
|
int32_t StepMotorCtrlModule::move_to(int32_t tox, action_cb_status_t status_cb) { //
|
|
|
|
|
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "m%d move_to %d", m_id, tox); |
|
|
@ -42,26 +42,24 @@ int32_t StepMotorCtrlModule::move_to(int32_t tox, function<void(move_to_cb_statu |
|
|
|
tox = m_cfg_max_x; |
|
|
|
} |
|
|
|
|
|
|
|
updateStatus(1); |
|
|
|
m_thread.start([this, tox, status_cb]() { |
|
|
|
_motor_move_to(tox, 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 nowx = 0; |
|
|
|
getnowpos(nowx); |
|
|
|
move_to_cb_status_t status; |
|
|
|
status.exec_status = 0; |
|
|
|
status.tox = nowx; |
|
|
|
if (status_cb) status_cb(status); |
|
|
|
updateStatus(0); |
|
|
|
}); |
|
|
|
m_thread.start( |
|
|
|
[this, tox, status_cb]() { |
|
|
|
_motor_move_to(tox, m_cfg_velocity, m_cfg_acc, m_cfg_dec); |
|
|
|
while (!_motor_is_reach_target()) { |
|
|
|
if (m_thread.getExitFlag()) break; |
|
|
|
vTaskDelay(10); |
|
|
|
} |
|
|
|
m_lastexecstatus = 0; |
|
|
|
}, |
|
|
|
[this, tox, status_cb]() { |
|
|
|
_motor_stop(); |
|
|
|
int32_t nowx = 0; |
|
|
|
getnowpos(nowx); |
|
|
|
if (status_cb) status_cb(m_lastexecstatus); |
|
|
|
}); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::move_by(int32_t dx, function<void(move_by_cb_status_t& status)> status_cb) { |
|
|
|
int32_t StepMotorCtrlModule::move_by(int32_t dx, action_cb_status_t status_cb) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "m%d move_by %d", m_id, dx); |
|
|
|
m_thread.stop(); |
|
|
@ -70,58 +68,46 @@ int32_t StepMotorCtrlModule::move_by(int32_t dx, function<void(move_by_cb_status |
|
|
|
getnowpos(xnow); |
|
|
|
|
|
|
|
int32_t toxnow = xnow + dx; |
|
|
|
return move_to(toxnow, [this, status_cb, xnow](move_to_cb_status_t& status) { |
|
|
|
move_by_cb_status_t movebycb_status; |
|
|
|
movebycb_status.exec_status = status.exec_status; |
|
|
|
movebycb_status.dx = status.tox - xnow; |
|
|
|
if (status_cb) status_cb(movebycb_status); |
|
|
|
return move_to(toxnow, [this, status_cb, xnow](int32_t status) { |
|
|
|
if (status_cb) status_cb(status); |
|
|
|
}); |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::move_to_zero(function<void(move_to_zero_cb_status_t& status)> status_cb) { |
|
|
|
int32_t StepMotorCtrlModule::move_to_zero(action_cb_status_t status_cb) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "move_to_zero"); |
|
|
|
m_thread.stop(); |
|
|
|
|
|
|
|
updateStatus(1); |
|
|
|
m_thread.start([this, status_cb]() { |
|
|
|
int32_t dx; |
|
|
|
move_to_zero_cb_status_t ret_status = {0}; |
|
|
|
int32_t erro = exec_move_to_zero_task(dx); |
|
|
|
if (erro != 0) { |
|
|
|
ZLOGI(TAG, "exec_move_to_zero_task failed:%d", erro); |
|
|
|
_motor_stop(); |
|
|
|
ret_status.exec_status = erro; |
|
|
|
if (status_cb) status_cb(ret_status); |
|
|
|
updateStatus(0); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
m_stepM1->setXACTUAL(0); |
|
|
|
ret_status.exec_status = 0; |
|
|
|
if (status_cb) status_cb(ret_status); |
|
|
|
updateStatus(0); |
|
|
|
return; |
|
|
|
}); |
|
|
|
m_thread.start( |
|
|
|
[this, status_cb]() { |
|
|
|
int32_t dx; |
|
|
|
int32_t erro = exec_move_to_zero_task(dx); |
|
|
|
m_lastexecstatus = erro; |
|
|
|
}, |
|
|
|
[this, status_cb]() { |
|
|
|
_motor_stop(); |
|
|
|
if (m_lastexecstatus == 0) { |
|
|
|
m_stepM1->setXACTUAL(0); |
|
|
|
} |
|
|
|
if (status_cb) status_cb(m_lastexecstatus); |
|
|
|
return; |
|
|
|
}); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::move_to_zero_with_calibrate(int32_t nowx, function<void(move_to_zero_with_calibrate_cb_status_t& status)> status_cb) { |
|
|
|
int32_t StepMotorCtrlModule::move_to_zero_with_calibrate(int32_t nowx, action_cb_status_t status_cb) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "move_to_zero_with_calibrate x:%d", nowx); |
|
|
|
m_thread.stop(); |
|
|
|
|
|
|
|
updateStatus(1); |
|
|
|
m_thread.start([this, nowx, status_cb]() { |
|
|
|
int32_t dx; |
|
|
|
move_to_zero_with_calibrate_cb_status_t ret_status = {0}; |
|
|
|
int32_t dx; |
|
|
|
|
|
|
|
int32_t erro = exec_move_to_zero_task(dx); |
|
|
|
if (erro != 0) { |
|
|
|
ZLOGI(TAG, "exec_move_to_zero_task failed:%d", erro); |
|
|
|
_motor_stop(); |
|
|
|
ret_status.exec_status = erro; |
|
|
|
if (status_cb) status_cb(ret_status); |
|
|
|
updateStatus(0); |
|
|
|
m_lastexecstatus = erro; |
|
|
|
if (status_cb) status_cb(erro); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
@ -130,10 +116,8 @@ int32_t StepMotorCtrlModule::move_to_zero_with_calibrate(int32_t nowx, function< |
|
|
|
m_zero_shift_x = calibrate_x; |
|
|
|
|
|
|
|
m_stepM1->setXACTUAL(0); |
|
|
|
ret_status.exec_status = 0; |
|
|
|
ret_status.zero_shift_x = m_zero_shift_x; |
|
|
|
if (status_cb) status_cb(ret_status); |
|
|
|
updateStatus(0); |
|
|
|
m_lastexecstatus = 0; |
|
|
|
if (status_cb) status_cb(m_lastexecstatus); |
|
|
|
return; |
|
|
|
}); |
|
|
|
|
|
|
@ -160,7 +144,7 @@ int32_t StepMotorCtrlModule::force_change_current_pos(int32_t xpos) { |
|
|
|
m_stepM1->setXACTUAL(motor_pos); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::rotate(int32_t speed, int32_t lastforms, function<void(rotate_cb_status_t& status)> status_cb) { //
|
|
|
|
int32_t StepMotorCtrlModule::rotate(int32_t speed, int32_t lastforms, action_cb_status_t status_cb) { //
|
|
|
|
zlock_guard l(m_lock); |
|
|
|
ZLOGI(TAG, "rotate speed:%d lastforms:%d acc:%d dec:%d", speed, lastforms, m_cfg_acc, m_cfg_dec); |
|
|
|
|
|
|
@ -174,9 +158,7 @@ int32_t StepMotorCtrlModule::rotate(int32_t speed, int32_t lastforms, function<v |
|
|
|
speed = m_cfg_max_speed; |
|
|
|
} |
|
|
|
m_thread.stop(); |
|
|
|
updateStatus(1); |
|
|
|
m_thread.start([this, lastforms, speed, status_cb]() { |
|
|
|
rotate_cb_status_t status_report; |
|
|
|
m_stepM1->setAcceleration(m_cfg_acc); |
|
|
|
m_stepM1->setDeceleration(m_cfg_dec); |
|
|
|
m_stepM1->rotate(speed); |
|
|
@ -192,11 +174,9 @@ int32_t StepMotorCtrlModule::rotate(int32_t speed, int32_t lastforms, function<v |
|
|
|
} |
|
|
|
osDelay(1); |
|
|
|
} |
|
|
|
status_report.exec_status = 0; |
|
|
|
status_report.lastforms = zos_haspassedms(startticket); |
|
|
|
if (status_cb) status_cb(status_report); |
|
|
|
m_lastexecstatus = 0; |
|
|
|
if (status_cb) status_cb(m_lastexecstatus); |
|
|
|
m_stepM1->stop(); |
|
|
|
updateStatus(0); |
|
|
|
return; |
|
|
|
}); |
|
|
|
return 0; |
|
|
@ -205,14 +185,14 @@ int32_t StepMotorCtrlModule::rotate(int32_t speed, int32_t lastforms, function<v |
|
|
|
int32_t StepMotorCtrlModule::read_version(version_t& version) { return 0; } |
|
|
|
int32_t StepMotorCtrlModule::read_status(status_t& status) { |
|
|
|
zlock_guard l(m_statu_lock); |
|
|
|
status.status = m_status; |
|
|
|
status.status = m_thread.isworking() ? 1 : 0; |
|
|
|
if (m_Xgpio) status.io_state |= m_Xgpio->getState() ? 0x01 : 0x00; |
|
|
|
if (m_end_gpio) status.io_state |= m_end_gpio->getState() ? 0x02 : 0x00; |
|
|
|
getnowpos(status.x); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::read_detailed_status(detailed_status_t& debug_info) { |
|
|
|
debug_info.status = m_status; |
|
|
|
debug_info.status = m_thread.isworking() ? 1 : 0; |
|
|
|
if (m_Xgpio) debug_info.io_state |= m_Xgpio->getState() ? 0x01 : 0x00; |
|
|
|
if (m_end_gpio) debug_info.io_state |= m_end_gpio->getState() ? 0x02 : 0x00; |
|
|
|
getnowpos(debug_info.x); |
|
|
@ -407,7 +387,9 @@ void StepMotorCtrlModule::forward_kinematics(int32_t x, int32_t& motor_pos) { |
|
|
|
x -= m_zero_shift_x; |
|
|
|
motor_pos = x; |
|
|
|
} |
|
|
|
#if 0
|
|
|
|
void StepMotorCtrlModule::updateStatus(uint8_t status) { |
|
|
|
zlock_guard lock(m_statu_lock); |
|
|
|
m_status = status; |
|
|
|
} |
|
|
|
} |
|
|
|
#endif
|