|
|
@ -80,7 +80,7 @@ int32_t StepMotorCtrlModule::_move_to(int32_t tox, action_cb_status_t status_cb) |
|
|
|
|
|
|
|
m_thread.start( |
|
|
|
[this, tox, status_cb]() { |
|
|
|
_motor_move_to(tox, m_default_speed, m_param.motor_default_acc, m_param.motor_default_dec); |
|
|
|
_move_to_nolimit(tox, m_default_speed, m_param.motor_default_acc, m_param.motor_default_dec); |
|
|
|
while (!_motor_is_reach_target()) { |
|
|
|
if (m_thread.getExitFlag()) break; |
|
|
|
vTaskDelay(10); |
|
|
@ -139,7 +139,7 @@ int32_t StepMotorCtrlModule::calculate_curpos(action_cb_status_t status_cb) { |
|
|
|
[this, status_cb]() { |
|
|
|
//
|
|
|
|
int32_t dx; |
|
|
|
int32_t erro = exec_move_to_zero_task(dx); |
|
|
|
int32_t erro = _exec_move_to_zero_task(dx); |
|
|
|
int xstart = dx + m_param.motor_shift; |
|
|
|
m_calculate_curpos_result = xstart; |
|
|
|
call_exec_status_cb(erro, status_cb); |
|
|
@ -160,7 +160,7 @@ int32_t StepMotorCtrlModule::move_to_zero(action_cb_status_t status_cb) { |
|
|
|
m_thread.start( |
|
|
|
[this, status_cb]() { |
|
|
|
int32_t dx; |
|
|
|
int32_t erro = exec_move_to_zero_task(dx); |
|
|
|
int32_t erro = _exec_move_to_zero_task(dx); |
|
|
|
if (erro == 0) { |
|
|
|
m_stepM1->setXACTUAL(0); |
|
|
|
} |
|
|
@ -178,9 +178,9 @@ int32_t StepMotorCtrlModule::move_to_zero_with_calibrate(int32_t nowx, action_cb |
|
|
|
m_thread.start([this, nowx, status_cb]() { |
|
|
|
int32_t dx; |
|
|
|
|
|
|
|
int32_t erro = exec_move_to_zero_task(dx); |
|
|
|
int32_t erro = _exec_move_to_zero_task(dx); |
|
|
|
if (erro != 0) { |
|
|
|
ZLOGI(TAG, "exec_move_to_zero_task failed:%d", erro); |
|
|
|
ZLOGI(TAG, "_exec_move_to_zero_task failed:%d", erro); |
|
|
|
_motor_stop(); |
|
|
|
call_exec_status_cb(erro, status_cb); |
|
|
|
|
|
|
@ -258,50 +258,6 @@ int32_t StepMotorCtrlModule::rotate(int32_t speed, int32_t lastforms, action_cb_ |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::set_base_param(const base_param_t& param) { |
|
|
|
m_param = param; |
|
|
|
active_cfg(); |
|
|
|
ZLOGI(TAG, "=========== base param ============"); |
|
|
|
ZLOGI(TAG, "= x_shaft :%d", m_param.motor_shaft); |
|
|
|
ZLOGI(TAG, "= distance_scale :%f", m_param.motor_one_circle_pulse); |
|
|
|
ZLOGI(TAG, "= ihold :%d", m_param.stepmotor_ihold); |
|
|
|
ZLOGI(TAG, "= irun :%d", m_param.stepmotor_irun); |
|
|
|
ZLOGI(TAG, "= iholddelay :%d", m_param.stepmotor_iholddelay); |
|
|
|
ZLOGI(TAG, "= acc :%d", m_param.motor_default_acc); |
|
|
|
ZLOGI(TAG, "= dec :%d", m_param.motor_default_dec); |
|
|
|
ZLOGI(TAG, "= maxspeed :%d", m_param.motor_default_velocity); |
|
|
|
ZLOGI(TAG, "= min_x :%d", m_param.min_d); |
|
|
|
ZLOGI(TAG, "= max_x :%d", m_param.max_d); |
|
|
|
ZLOGI(TAG, "= motor_shift :%d", m_param.motor_shift); |
|
|
|
|
|
|
|
#if 0
|
|
|
|
int32_t run_to_zero_max_d; |
|
|
|
int32_t run_to_zero_speed; |
|
|
|
int32_t run_to_zero_dec; |
|
|
|
|
|
|
|
int32_t look_zero_edge_max_d; |
|
|
|
int32_t look_zero_edge_speed; |
|
|
|
int32_t look_zero_edge_dec; |
|
|
|
#endif
|
|
|
|
ZLOGI(TAG, "= run_to_zero_max_d :%d", m_param.motor_run_to_zero_max_d); |
|
|
|
ZLOGI(TAG, "= run_to_zero_speed :%d", m_param.motor_run_to_zero_speed); |
|
|
|
ZLOGI(TAG, "= run_to_zero_dec :%d", m_param.motor_run_to_zero_dec); |
|
|
|
ZLOGI(TAG, "= look_zero_edge_max_d :%d", m_param.motor_look_zero_edge_max_d); |
|
|
|
ZLOGI(TAG, "= look_zero_edge_speed :%d", m_param.motor_look_zero_edge_speed); |
|
|
|
ZLOGI(TAG, "= look_zero_edge_dec :%d", m_param.motor_look_zero_edge_dec); |
|
|
|
|
|
|
|
// 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, "=================================="); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::get_base_param(base_param_t& param) { |
|
|
|
param = m_param; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::read_pos(int32_t& pos) { |
|
|
|
getnowpos(pos); |
|
|
|
return 0; |
|
|
@ -317,93 +273,6 @@ bool StepMotorCtrlModule::read_zero_io_state() { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::exec_move_to_zero_task(int32_t& dx) { |
|
|
|
int32_t xnow; |
|
|
|
getnowpos(xnow); |
|
|
|
|
|
|
|
int32_t ret = exec_move_to_zero_task(); |
|
|
|
|
|
|
|
int32_t xnow2; |
|
|
|
getnowpos(xnow2); |
|
|
|
|
|
|
|
dx = xnow2 - xnow; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::exec_move_to_zero_task() { |
|
|
|
/*******************************************************************************
|
|
|
|
* 第一次移动到零点 * |
|
|
|
*******************************************************************************/ |
|
|
|
if (!m_Xgpio->getState()) { |
|
|
|
{ |
|
|
|
ZLOGI(TAG, "---------STEP1-------- move to zero first"); |
|
|
|
_rotate(-m_param.motor_run_to_zero_speed, m_param.motor_default_acc, m_param.motor_run_to_zero_dec); |
|
|
|
bool xreach_zero = false; |
|
|
|
while (!m_thread.getExitFlag()) { |
|
|
|
// 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); |
|
|
|
|
|
|
|
while (!m_stepM1->isStoped() && !m_thread.getExitFlag()) { // 等待电机停止
|
|
|
|
_motor_stop(-1); |
|
|
|
vTaskDelay(10); |
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
if (!xreach_zero) { |
|
|
|
// 触发异常回调
|
|
|
|
ZLOGE(TAG, "x reach zero failed"); |
|
|
|
return err::kxymotor_not_found_x_zero_point; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* 远离X零点 * |
|
|
|
*******************************************************************************/ |
|
|
|
{ |
|
|
|
ZLOGI(TAG, "---------STEP2-------- leave away zero"); |
|
|
|
/**
|
|
|
|
* @brief 如果设备已经在零点,则反向移动一定距离远离零点 |
|
|
|
*/ |
|
|
|
if (m_Xgpio->getState()) { |
|
|
|
_rotate(m_param.motor_look_zero_edge_speed, m_param.motor_default_acc, m_param.motor_look_zero_edge_dec); |
|
|
|
while (true) { |
|
|
|
if (m_thread.getExitFlag()) break; |
|
|
|
if (!m_Xgpio->getState()) { |
|
|
|
_motor_stop(-1); |
|
|
|
while (!m_stepM1->isStoped() && !m_thread.getExitFlag()) { // 等待电机停止
|
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (m_thread.getExitFlag()) { |
|
|
|
ZLOGI(TAG, "break move to zero thread exit"); |
|
|
|
return err::kmodule_opeation_break_by_user; |
|
|
|
} |
|
|
|
if (m_Xgpio->getState()) { |
|
|
|
ZLOGE(TAG, "leave away zero failed"); |
|
|
|
return err::kxymotor_x_find_zero_edge_fail; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (m_thread.getExitFlag()) { |
|
|
|
ZLOGI(TAG, "break move to zero thread exit"); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
ZLOGI(TAG, "x reach zero"); |
|
|
|
ZLOGI(TAG, "move_to_zero success"); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
void StepMotorCtrlModule::_rotate(int32_t vel, int32_t acc, int32_t dec) { |
|
|
|
ZLOGI(TAG, "m%d _rotate %d %d %d", m_id, vel, acc, dec); |
|
|
|
m_stepM1->setAcceleration(acc); |
|
|
@ -415,15 +284,15 @@ void StepMotorCtrlModule::getnowpos(int32_t& pos) { |
|
|
|
int32_t motor_pos = m_stepM1->getXACTUAL(); |
|
|
|
inverse_kinematics(motor_pos, pos); |
|
|
|
} |
|
|
|
void StepMotorCtrlModule::_motor_move_to(int32_t pos, int32_t maxv, int32_t acc, int32_t dec) { |
|
|
|
ZLOGI(TAG, "m%d _motor_move_to %d maxv:%d acc:%d dec:%d", m_id, pos, maxv, acc, dec); |
|
|
|
void StepMotorCtrlModule::_move_to_nolimit(int32_t pos, int32_t maxv, int32_t acc, int32_t dec) { |
|
|
|
ZLOGI(TAG, "m%d _move_to_nolimit %d maxv:%d acc:%d dec:%d", m_id, pos, maxv, acc, dec); |
|
|
|
m_stepM1->setAcceleration(acc); |
|
|
|
m_stepM1->setDeceleration(dec); |
|
|
|
int32_t motor_pos = 0; |
|
|
|
forward_kinematics(pos, motor_pos); |
|
|
|
m_stepM1->moveTo(motor_pos, maxv); |
|
|
|
} |
|
|
|
void StepMotorCtrlModule::_motor_move_by(int32_t dpos, int32_t maxv, int32_t acc, int32_t dec) { |
|
|
|
void StepMotorCtrlModule::_move_by_nolimit(int32_t dpos, int32_t maxv, int32_t acc, int32_t dec) { |
|
|
|
ZLOGI(TAG, "m%d _motor_move_by %d maxv:%d acc:%d dec:%d", m_id, dpos, maxv, acc, dec); |
|
|
|
m_stepM1->setAcceleration(acc); |
|
|
|
m_stepM1->setDeceleration(dec); |
|
|
@ -500,7 +369,6 @@ int32_t StepMotorCtrlModule::do_motor_action_block_2(function<int32_t()> action, |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::do_motor_action_block(function<int32_t()> action) { return do_motor_action_block_2(action, nullptr); } |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::move_to_block(int32_t tox, int overtime) { |
|
|
|
return do_motor_action_block([this, tox]() { return move_to(tox, nullptr); }); |
|
|
|
} |
|
|
@ -618,9 +486,7 @@ int32_t StepMotorCtrlModule::module_xxx_reg(int32_t param_id, bool read, int32_t |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::do_action(int32_t actioncode) { return 0; } |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::module_clear_error() { return 0; } |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::module_readio(int32_t* io) { |
|
|
|
*io = 0; |
|
|
|
for (int32_t i = 0; i < m_nio; i++) { |
|
|
@ -640,14 +506,12 @@ int32_t StepMotorCtrlModule::module_read_adc(int32_t adcindex, int32_t* adc) { |
|
|
|
*adc = 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::module_factory_reset() { return factory_reset(); } |
|
|
|
int32_t StepMotorCtrlModule::module_flush_cfg() { return flush(); } |
|
|
|
int32_t StepMotorCtrlModule::module_active_cfg() { |
|
|
|
active_cfg(); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::motor_enable(int32_t varenable) { |
|
|
|
zlock_guard l(m_lock); |
|
|
|
ZLOGI(TAG, "m%d motor_enable %ld", m_id, varenable); |
|
|
@ -691,7 +555,7 @@ int32_t StepMotorCtrlModule::motor_move_to(int32_t tox, int32_t motor_velocity, |
|
|
|
|
|
|
|
m_thread.start( |
|
|
|
[this, tox, motor_velocity, acc]() { |
|
|
|
_motor_move_to(tox, motor_velocity, acc, acc); |
|
|
|
_move_to_nolimit(tox, motor_velocity, acc, acc); |
|
|
|
while (!_motor_is_reach_target()) { |
|
|
|
if (m_thread.getExitFlag()) break; |
|
|
|
vTaskDelay(10); |
|
|
@ -702,13 +566,6 @@ int32_t StepMotorCtrlModule::motor_move_to(int32_t tox, int32_t motor_velocity, |
|
|
|
// ZLOGI(TAG, "m%d motor_move_to %d end", m_id, tox);
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::motor_rotate_with_torque(int32_t direction, int32_t torque) { return err::koperation_not_support; } |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::motor_rotate_acctime(int32_t direction, int32_t motor_velocity, int32_t acctime) { return err::koperation_not_support; } |
|
|
|
int32_t StepMotorCtrlModule::motor_move_by_acctime(int32_t distance, int32_t motor_velocity, int32_t acctime) { return err::koperation_not_support; } |
|
|
|
int32_t StepMotorCtrlModule::motor_move_to_acctime(int32_t position, int32_t motor_velocity, int32_t acctime) { return err::koperation_not_support; } |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::motor_move_to_zero_forward(int32_t findzerospeed, int32_t findzeroedge_speed, int32_t acc, int32_t overtime) { return err::koperation_not_support; } |
|
|
|
int32_t StepMotorCtrlModule::motor_move_to_zero_backward(int32_t findzerospeed, int32_t findzeroedge_speed, int32_t acc, int32_t overtime) { |
|
|
|
ZLOGI(TAG, "motor_move_to_zero_backward"); |
|
|
|
if (findzerospeed != 0) { |
|
|
@ -724,9 +581,7 @@ int32_t StepMotorCtrlModule::motor_move_to_zero_backward(int32_t findzerospeed, |
|
|
|
return move_to_zero(nullptr); |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::motor_move_to_zero_forward_and_calculated_shift(int32_t findzerospeed, int32_t findzeroedge_speed, int32_t acc, int32_t overtime) { return err::koperation_not_support; } |
|
|
|
int32_t StepMotorCtrlModule::motor_move_to_zero_backward_and_calculated_shift(int32_t findzerospeed, int32_t findzeroedge_speed, int32_t acc, int32_t overtime) { return move_to_zero_with_calibrate(0, nullptr); } |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::motor_read_pos(int32_t* pos) { |
|
|
|
int32_t xnow = 0; |
|
|
|
getnowpos(xnow); |
|
|
@ -750,7 +605,7 @@ int32_t StepMotorCtrlModule::motor_calculated_pos_by_move_to_zero() { |
|
|
|
[this]() { |
|
|
|
//
|
|
|
|
int32_t dx; |
|
|
|
int32_t erro = exec_move_to_zero_task(dx); |
|
|
|
int32_t erro = _exec_move_to_zero_task(dx); |
|
|
|
if (erro != 0) { |
|
|
|
set_last_exec_status(erro); |
|
|
|
} else { |
|
|
@ -769,26 +624,194 @@ int32_t StepMotorCtrlModule::motor_easy_rotate(int32_t direction) { |
|
|
|
m_thread.stop(); |
|
|
|
_rotate(direction * m_param.motor_default_velocity, m_param.motor_default_acc, m_param.motor_default_dec); |
|
|
|
return 0; |
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::motor_easy_move_by(int32_t distance) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "m%d motor_easy_move_by %d", m_id, distance); |
|
|
|
m_thread.stop(); |
|
|
|
return motor_move_by(distance, m_param.motor_default_velocity, m_param.motor_default_acc); |
|
|
|
}; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::motor_easy_move_to(int32_t position) { |
|
|
|
zlock_guard lock(m_lock); |
|
|
|
ZLOGI(TAG, "m%d motor_easy_move_to %d", m_id, position); |
|
|
|
m_thread.stop(); |
|
|
|
return motor_move_to(position, m_param.motor_default_velocity, m_param.motor_default_acc); |
|
|
|
}; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::motor_easy_move_to_zero(int32_t direction) { |
|
|
|
if (direction <= 0) { |
|
|
|
return move_to_zero(nullptr); |
|
|
|
} else { |
|
|
|
return err::koperation_not_support; |
|
|
|
} |
|
|
|
}; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::motor_easy_move_to_io(int32_t ioindex, int32_t direction) { return _exec_move_to_io_task(ioindex, direction); } |
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* EXEC_TASK * |
|
|
|
*******************************************************************************/ |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::_exec_move_to_io_task(int32_t ioindex, int32_t direction) { |
|
|
|
m_thread.stop(); |
|
|
|
m_thread.start([this, ioindex, direction]() { m_com_reg.module_last_cmd_exec_status = _exec_move_to_io_task_fn(ioindex, direction); }); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::_exec_move_to_io_task_fn(int32_t ioindex, int32_t direction) { |
|
|
|
int32_t runToPointSpeed = m_param.motor_run_to_zero_speed; |
|
|
|
int32_t runToPointDec = m_param.motor_run_to_zero_dec; |
|
|
|
// int32_t runToPointMaxD = m_param.motor_run_to_zero_max_d;
|
|
|
|
|
|
|
|
int32_t lookPointVelocity = m_param.motor_look_zero_edge_speed; |
|
|
|
int32_t lookPointDec = m_param.motor_look_zero_edge_dec; |
|
|
|
// int32_t lookPointMaxD = m_param.motor_look_zero_edge_max_d;
|
|
|
|
|
|
|
|
direction = direction >= 0 ? 1 : -1; |
|
|
|
|
|
|
|
ZGPIO* gpio = &m_iotable[ioindex]; |
|
|
|
|
|
|
|
if (!gpio->getState()) { |
|
|
|
ZLOGI(TAG, "---------STEP1-------- move to point"); |
|
|
|
_rotate(runToPointSpeed * direction, runToPointDec, runToPointDec); |
|
|
|
bool xreach_io = false; |
|
|
|
|
|
|
|
while (!m_thread.getExitFlag()) { |
|
|
|
if (gpio->getState()) { |
|
|
|
xreach_io = true; |
|
|
|
_motor_stop(-1); |
|
|
|
break; |
|
|
|
} |
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
|
|
|
|
// 等待电机停止
|
|
|
|
while (!m_stepM1->isStoped() && !m_thread.getExitFlag()) { |
|
|
|
_motor_stop(-1); |
|
|
|
vTaskDelay(100); |
|
|
|
} |
|
|
|
|
|
|
|
// 失败返回
|
|
|
|
if (!xreach_io) { |
|
|
|
ZLOGE(TAG, "x reach io failed"); |
|
|
|
return err::kxymotor_not_found_x_zero_point; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 如果设备已经在零点,则反向移动一定距离远离零点
|
|
|
|
if (gpio->getState()) { |
|
|
|
ZLOGI(TAG, "---------STEP2-------- find edge"); |
|
|
|
_rotate(-direction * lookPointVelocity, lookPointDec, lookPointDec); |
|
|
|
bool reach_edge = false; |
|
|
|
|
|
|
|
while (!m_thread.getExitFlag()) { |
|
|
|
if (!gpio->getState()) { |
|
|
|
reach_edge = true; |
|
|
|
_motor_stop(-1); |
|
|
|
break; |
|
|
|
} |
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
|
|
|
|
while (!m_stepM1->isStoped() && !m_thread.getExitFlag()) { // 等待电机停止
|
|
|
|
_motor_stop(-1); |
|
|
|
vTaskDelay(100); |
|
|
|
} |
|
|
|
|
|
|
|
if (!reach_edge) { |
|
|
|
ZLOGE(TAG, "leave away zero failed"); |
|
|
|
return err::kxymotor_x_find_zero_edge_fail; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (m_thread.getExitFlag()) { |
|
|
|
ZLOGI(TAG, "break move to zero thread exit"); |
|
|
|
return err::kmodule_opeation_break_by_user; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::motor_easy_move_to_io(int32_t direction, int32_t io_index) { return 0; } |
|
|
|
ZLOGI(TAG, "_exec_move_to_io_task_fn success"); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int32_t StepMotorCtrlModule::_exec_move_to_zero_task(int32_t& dx) { |
|
|
|
int32_t pos1 = 0; |
|
|
|
int32_t pos2 = 0; |
|
|
|
int32_t ret = 0; |
|
|
|
getnowpos(pos1); |
|
|
|
ret = _exec_move_to_zero_task(); |
|
|
|
getnowpos(pos2); |
|
|
|
dx = pos2 - pos1; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
int32_t StepMotorCtrlModule::_exec_move_to_zero_task() { |
|
|
|
/*******************************************************************************
|
|
|
|
* 第一次移动到零点 * |
|
|
|
*******************************************************************************/ |
|
|
|
if (!m_Xgpio->getState()) { |
|
|
|
{ |
|
|
|
ZLOGI(TAG, "---------STEP1-------- move to zero first"); |
|
|
|
_rotate(-m_param.motor_run_to_zero_speed, m_param.motor_default_acc, m_param.motor_run_to_zero_dec); |
|
|
|
bool xreach_zero = false; |
|
|
|
while (!m_thread.getExitFlag()) { |
|
|
|
// 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); |
|
|
|
|
|
|
|
while (!m_stepM1->isStoped() && !m_thread.getExitFlag()) { // 等待电机停止
|
|
|
|
_motor_stop(-1); |
|
|
|
vTaskDelay(10); |
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
if (!xreach_zero) { |
|
|
|
// 触发异常回调
|
|
|
|
ZLOGE(TAG, "x reach zero failed"); |
|
|
|
return err::kxymotor_not_found_x_zero_point; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* 远离X零点 * |
|
|
|
*******************************************************************************/ |
|
|
|
{ |
|
|
|
ZLOGI(TAG, "---------STEP2-------- leave away zero"); |
|
|
|
/**
|
|
|
|
* @brief 如果设备已经在零点,则反向移动一定距离远离零点 |
|
|
|
*/ |
|
|
|
if (m_Xgpio->getState()) { |
|
|
|
_rotate(m_param.motor_look_zero_edge_speed, m_param.motor_default_acc, m_param.motor_look_zero_edge_dec); |
|
|
|
while (true) { |
|
|
|
if (m_thread.getExitFlag()) break; |
|
|
|
if (!m_Xgpio->getState()) { |
|
|
|
_motor_stop(-1); |
|
|
|
while (!m_stepM1->isStoped() && !m_thread.getExitFlag()) { // 等待电机停止
|
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
vTaskDelay(1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (m_thread.getExitFlag()) { |
|
|
|
ZLOGI(TAG, "break move to zero thread exit"); |
|
|
|
return err::kmodule_opeation_break_by_user; |
|
|
|
} |
|
|
|
if (m_Xgpio->getState()) { |
|
|
|
ZLOGE(TAG, "leave away zero failed"); |
|
|
|
return err::kxymotor_x_find_zero_edge_fail; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (m_thread.getExitFlag()) { |
|
|
|
ZLOGI(TAG, "break move to zero thread exit"); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
ZLOGI(TAG, "x reach zero"); |
|
|
|
ZLOGI(TAG, "move_to_zero success"); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |