|
|
@ -14,6 +14,7 @@ void StepMotorCtrlModule::initialize(int id, IStepperMotor* stepM, ZGPIO* zero_g |
|
|
|
m_statu_lock.init(); |
|
|
|
m_thread.init("XYRobotCtrlModule", 1024, osPriorityNormal); |
|
|
|
|
|
|
|
#if 0
|
|
|
|
run_param_t run_param; |
|
|
|
run_to_zero_param_t run_to_zero_param; |
|
|
|
warning_limit_param_t warning_limit_param; |
|
|
@ -25,6 +26,7 @@ void StepMotorCtrlModule::initialize(int id, IStepperMotor* stepM, ZGPIO* zero_g |
|
|
|
set_run_param(run_param); |
|
|
|
set_run_to_zero_param(run_to_zero_param); |
|
|
|
set_warning_limit_param(warning_limit_param); |
|
|
|
#endif
|
|
|
|
} |
|
|
|
|
|
|
|
bool StepMotorCtrlModule::isbusy() { return m_thread.isworking(); } |
|
|
@ -35,16 +37,16 @@ int32_t StepMotorCtrlModule::move_to(int32_t tox, action_cb_status_t status_cb) |
|
|
|
ZLOGI(TAG, "m%d move_to %d", m_id, tox); |
|
|
|
m_thread.stop(); |
|
|
|
|
|
|
|
if (m_cfg_min_x != 0 && tox < m_cfg_min_x) { |
|
|
|
tox = m_cfg_min_x; |
|
|
|
if (m_param.min_x != 0 && tox < m_param.min_x) { |
|
|
|
tox = m_param.min_x; |
|
|
|
} |
|
|
|
if (m_cfg_max_x != 0 && tox > m_cfg_max_x) { |
|
|
|
tox = m_cfg_max_x; |
|
|
|
if (m_param.max_x != 0 && tox > m_param.max_x) { |
|
|
|
tox = m_param.max_x; |
|
|
|
} |
|
|
|
|
|
|
|
m_thread.start( |
|
|
|
[this, tox, status_cb]() { |
|
|
|
_motor_move_to(tox, m_cfg_velocity, m_cfg_acc, m_cfg_dec); |
|
|
|
_motor_move_to(tox, m_param.maxspeed, m_param.acc, m_param.dec); |
|
|
|
while (!_motor_is_reach_target()) { |
|
|
|
if (m_thread.getExitFlag()) break; |
|
|
|
vTaskDelay(10); |
|
|
@ -112,8 +114,8 @@ int32_t StepMotorCtrlModule::move_to_zero_with_calibrate(int32_t nowx, action_cb |
|
|
|
} |
|
|
|
|
|
|
|
int32_t calibrate_x, calibrate_y; |
|
|
|
calibrate_x = dx + nowx; |
|
|
|
m_zero_shift_x = calibrate_x; |
|
|
|
calibrate_x = dx + nowx; |
|
|
|
m_param.shift_x = calibrate_x; |
|
|
|
|
|
|
|
m_stepM1->setXACTUAL(0); |
|
|
|
m_lastexecstatus = 0; |
|
|
@ -146,21 +148,21 @@ int32_t StepMotorCtrlModule::force_change_current_pos(int32_t xpos) { |
|
|
|
} |
|
|
|
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); |
|
|
|
ZLOGI(TAG, "rotate speed:%d lastforms:%d acc:%d dec:%d", speed, lastforms, m_param.acc, m_param.dec); |
|
|
|
|
|
|
|
if (lastforms < 0) { |
|
|
|
ZLOGW(TAG, "lastforms:%d < 0", lastforms); |
|
|
|
return err::kcommon_error_param_out_of_range; |
|
|
|
} |
|
|
|
|
|
|
|
if (abs(speed) > m_cfg_max_speed) { |
|
|
|
ZLOGW(TAG, "speed:%d > m_cfg_max_speed:%d", speed, m_cfg_max_speed); |
|
|
|
speed = m_cfg_max_speed; |
|
|
|
if (abs(speed) > m_param.maxspeed) { |
|
|
|
ZLOGW(TAG, "speed:%d > m_cfg_max_speed:%d", speed, m_param.maxspeed); |
|
|
|
speed = m_param.maxspeed; |
|
|
|
} |
|
|
|
m_thread.stop(); |
|
|
|
m_thread.start([this, lastforms, speed, status_cb]() { |
|
|
|
m_stepM1->setAcceleration(m_cfg_acc); |
|
|
|
m_stepM1->setDeceleration(m_cfg_dec); |
|
|
|
m_stepM1->setAcceleration(m_param.acc); |
|
|
|
m_stepM1->setDeceleration(m_param.dec); |
|
|
|
m_stepM1->rotate(speed); |
|
|
|
|
|
|
|
int32_t startticket = zos_get_tick(); |
|
|
@ -199,83 +201,34 @@ int32_t StepMotorCtrlModule::read_detailed_status(detailed_status_t& debug_info) |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::set_run_param(uint8_t operation, const run_param_t& param, run_param_t& ack) { |
|
|
|
if (operation == kset_cmd_type_set) { |
|
|
|
m_cfg_distance_scale = param.distance_scale / 1000.0; |
|
|
|
m_cfg_acc = param.acc; |
|
|
|
m_cfg_dec = param.dec; |
|
|
|
m_cfg_max_speed = param.maxspeed; |
|
|
|
m_cfg_min_x = param.min_x; |
|
|
|
m_cfg_max_x = param.max_x; |
|
|
|
m_cfg_ihold = param.ihold; |
|
|
|
m_cfg_irun = param.irun; |
|
|
|
m_cfg_iholddelay = param.iholddelay; |
|
|
|
m_cfg_x_shaft = param.x_shaft; |
|
|
|
m_cfg_shift_x = param.shift_x; |
|
|
|
|
|
|
|
ZLOGI(TAG, "=========== run param ============"); |
|
|
|
ZLOGI(TAG, "= distance_scale :%f", m_cfg_distance_scale); |
|
|
|
ZLOGI(TAG, "= acc :%d", m_cfg_acc); |
|
|
|
ZLOGI(TAG, "= dec :%d", m_cfg_dec); |
|
|
|
ZLOGI(TAG, "= max_speed :%d", m_cfg_max_speed); |
|
|
|
ZLOGI(TAG, "= min_x :%d", m_cfg_min_x); |
|
|
|
ZLOGI(TAG, "= max_x :%d", m_cfg_max_x); |
|
|
|
ZLOGI(TAG, "= ihold :%d", m_cfg_ihold); |
|
|
|
ZLOGI(TAG, "= irun :%d", m_cfg_irun); |
|
|
|
ZLOGI(TAG, "= iholddelay :%d", m_cfg_iholddelay); |
|
|
|
ZLOGI(TAG, "= x_shaft :%d", m_cfg_x_shaft); |
|
|
|
ZLOGI(TAG, "= shift_x :%d", m_cfg_shift_x); |
|
|
|
ZLOGI(TAG, "=================================="); |
|
|
|
|
|
|
|
m_stepM1->setIHOLD_IRUN(m_cfg_ihold, m_cfg_irun, m_cfg_iholddelay); |
|
|
|
m_stepM1->setScale(m_cfg_distance_scale); |
|
|
|
} |
|
|
|
|
|
|
|
ack.distance_scale = m_cfg_distance_scale * 1000; |
|
|
|
ack.acc = m_cfg_acc; |
|
|
|
ack.dec = m_cfg_dec; |
|
|
|
ack.maxspeed = m_cfg_max_speed; |
|
|
|
ack.min_x = m_cfg_min_x; |
|
|
|
ack.max_x = m_cfg_max_x; |
|
|
|
ack.ihold = m_cfg_ihold; |
|
|
|
ack.irun = m_cfg_irun; |
|
|
|
ack.iholddelay = m_cfg_iholddelay; |
|
|
|
ack.x_shaft = m_cfg_x_shaft; |
|
|
|
ack.shift_x = m_cfg_shift_x; |
|
|
|
|
|
|
|
return 0; |
|
|
|
int32_t StepMotorCtrlModule::set_base_param(const base_param_t& param) { |
|
|
|
m_param = param; |
|
|
|
m_stepM1->setIHOLD_IRUN(m_param.ihold, m_param.irun, m_param.iholddelay); |
|
|
|
m_stepM1->setScale(m_param.distance_scale / 1000.0); |
|
|
|
|
|
|
|
ZLOGI(TAG, "=========== base param ============"); |
|
|
|
ZLOGI(TAG, "= x_shaft :%d", m_param.x_shaft); |
|
|
|
ZLOGI(TAG, "= distance_scale :%f", m_param.distance_scale); |
|
|
|
ZLOGI(TAG, "= ihold :%d", m_param.ihold); |
|
|
|
ZLOGI(TAG, "= irun :%d", m_param.irun); |
|
|
|
ZLOGI(TAG, "= iholddelay :%d", m_param.iholddelay); |
|
|
|
ZLOGI(TAG, "= acc :%d", m_param.acc); |
|
|
|
ZLOGI(TAG, "= dec :%d", m_param.dec); |
|
|
|
ZLOGI(TAG, "= maxspeed :%d", m_param.maxspeed); |
|
|
|
ZLOGI(TAG, "= min_x :%d", m_param.min_x); |
|
|
|
ZLOGI(TAG, "= max_x :%d", m_param.max_x); |
|
|
|
ZLOGI(TAG, "= shift_x :%d", m_param.shift_x); |
|
|
|
ZLOGI(TAG, "= run_to_zero_max_d :%d", m_param.run_to_zero_move_to_zero_max_d); |
|
|
|
ZLOGI(TAG, "= leave_from_zero_max_d :%d", m_param.run_to_zero_leave_from_zero_max_d); |
|
|
|
ZLOGI(TAG, "= run_to_zero_speed :%d", m_param.run_to_zero_speed); |
|
|
|
ZLOGI(TAG, "= run_to_zero_dec :%d", m_param.run_to_zero_dec); |
|
|
|
ZLOGI(TAG, "=================================="); |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::set_run_to_zero_param(uint8_t operation, const run_to_zero_param_t& param, run_to_zero_param_t& ack) { |
|
|
|
if (operation == kset_cmd_type_set) { |
|
|
|
m_cfg_runtozero_maxX = param.move_to_zero_max_d; |
|
|
|
m_cfg_runtozero_leave_away_zero_max_distance = param.leave_from_zero_max_d; |
|
|
|
m_cfg_runtozero_speed = param.speed; |
|
|
|
m_cfg_runtozero_dec = param.dec; |
|
|
|
|
|
|
|
ZLOGI(TAG, "=========== run_to_zero_param ============"); |
|
|
|
ZLOGI(TAG, "= move_to_zero_max_d :%d", m_cfg_runtozero_maxX); |
|
|
|
ZLOGI(TAG, "= leave_from_zero_max_d :%d", m_cfg_runtozero_leave_away_zero_max_distance); |
|
|
|
ZLOGI(TAG, "= speed :%d", m_cfg_runtozero_speed); |
|
|
|
ZLOGI(TAG, "= dec :%d", m_cfg_runtozero_dec); |
|
|
|
ZLOGI(TAG, "==========================================="); |
|
|
|
|
|
|
|
m_stepM1->setAcceleration(m_cfg_runtozero_dec); |
|
|
|
m_stepM1->setDeceleration(m_cfg_runtozero_dec); |
|
|
|
} |
|
|
|
|
|
|
|
ack.move_to_zero_max_d = m_cfg_runtozero_maxX; |
|
|
|
ack.leave_from_zero_max_d = m_cfg_runtozero_leave_away_zero_max_distance; |
|
|
|
ack.speed = m_cfg_runtozero_speed; |
|
|
|
ack.dec = m_cfg_runtozero_dec; |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::set_warning_limit_param(uint8_t operation, const warning_limit_param_t& param, warning_limit_param_t& ack) { |
|
|
|
if (operation == kset_cmd_type_set) { |
|
|
|
ZLOGI(TAG, "set warning limit param"); |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::get_base_param(base_param_t& param) { |
|
|
|
param = m_param; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::exec_move_to_zero_task(int32_t& dx) { |
|
|
|
int32_t xnow; |
|
|
|
getnowpos(xnow); |
|
|
@ -296,7 +249,7 @@ int32_t StepMotorCtrlModule::exec_move_to_zero_task() { |
|
|
|
/**
|
|
|
|
* @brief 如果设备已经在零点,则反向移动一定距离远离零点 |
|
|
|
*/ |
|
|
|
_motor_move_by(m_cfg_runtozero_leave_away_zero_max_distance, m_cfg_runtozero_speed, m_cfg_acc, m_cfg_runtozero_dec); |
|
|
|
_motor_move_by(m_param.run_to_zero_leave_from_zero_max_d, m_param.run_to_zero_speed, m_param.acc, m_param.run_to_zero_dec); |
|
|
|
while (!_motor_is_reach_target()) { |
|
|
|
if (m_thread.getExitFlag()) break; |
|
|
|
vTaskDelay(1); |
|
|
@ -314,7 +267,7 @@ int32_t StepMotorCtrlModule::exec_move_to_zero_task() { |
|
|
|
/*******************************************************************************
|
|
|
|
* 移动X轴到零点 * |
|
|
|
*******************************************************************************/ |
|
|
|
_motor_move_by(-m_cfg_runtozero_maxX, m_cfg_runtozero_speed, m_cfg_acc, m_cfg_runtozero_dec); |
|
|
|
_motor_move_by(-m_param.run_to_zero_move_to_zero_max_d, m_param.run_to_zero_speed, m_param.acc, m_param.run_to_zero_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());
|
|
|
@ -379,12 +332,12 @@ bool StepMotorCtrlModule::_motor_is_reach_target() { return m_stepM1->isReachTar |
|
|
|
void StepMotorCtrlModule::inverse_kinematics(int32_t motor_pos, int32_t& x) { |
|
|
|
// m_zero_shift_x
|
|
|
|
x = motor_pos; |
|
|
|
if (m_cfg_x_shaft != 0) x = -x; |
|
|
|
x += m_zero_shift_x; |
|
|
|
if (m_param.x_shaft != 0) x = -x; |
|
|
|
x += m_param.shift_x; |
|
|
|
} |
|
|
|
void StepMotorCtrlModule::forward_kinematics(int32_t x, int32_t& motor_pos) { |
|
|
|
if (m_cfg_x_shaft != 0) x = -x; |
|
|
|
x -= m_zero_shift_x; |
|
|
|
if (m_param.x_shaft != 0) x = -x; |
|
|
|
x -= m_param.shift_x; |
|
|
|
motor_pos = x; |
|
|
|
} |
|
|
|
#if 0
|
|
|
@ -392,4 +345,40 @@ void StepMotorCtrlModule::updateStatus(uint8_t status) { |
|
|
|
zlock_guard lock(m_statu_lock); |
|
|
|
m_status = status; |
|
|
|
} |
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::do_motor_action_block(function<int32_t()> action) { |
|
|
|
if (action == nullptr) return -1; |
|
|
|
int32_t ret = action(); |
|
|
|
if (ret != 0) { |
|
|
|
stop(0); |
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
ThisThread thisThread; |
|
|
|
|
|
|
|
while (!thisThread.getExitFlag()) { |
|
|
|
if (!isbusy()) break; |
|
|
|
thisThread.sleep(1000); |
|
|
|
} |
|
|
|
|
|
|
|
if (isbusy()) { |
|
|
|
stop(0); |
|
|
|
return err::kcommon_error_break_by_user; |
|
|
|
} |
|
|
|
|
|
|
|
return get_last_exec_status(); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::move_to_block(int32_t tox) { |
|
|
|
return do_motor_action_block([this, tox]() { return move_to(tox, nullptr); }); |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::move_by_block(int32_t dx) { |
|
|
|
return do_motor_action_block([this, dx]() { return move_by(dx, nullptr); }); |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::move_to_zero_block() { |
|
|
|
return do_motor_action_block([this]() { return move_to_zero(nullptr); }); |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::move_to_zero_with_calibrate_block(int32_t x) { |
|
|
|
return do_motor_action_block([this, x]() { return move_to_zero_with_calibrate(x, nullptr); }); |
|
|
|
} |