|
|
@ -19,6 +19,9 @@ void SingleAxisMotorControler::initialize(const char* name, IflytopCanProtocolSt |
|
|
|
ZASSERT(motor); |
|
|
|
if (m_homegpio) { |
|
|
|
ZASSERT(m_homegpio->isItRisingAndItFallingEXITGPIO()); |
|
|
|
m_homegpio->regListener([this](ZGPIO* GPIO_Pin, ZGPIO::IrqTypeEvent_t irqevent) { |
|
|
|
if (m_gpioirqprocesser) m_gpioirqprocesser(GPIO_Pin, irqevent); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
m_slave = m_processer->createICPSSlaveModule(name, this, m_regaddoff); |
|
|
@ -47,6 +50,9 @@ void SingleAxisMotorControler::initialize(const char* name, IflytopCanProtocolSt |
|
|
|
cfg_min_pos = m_processer->activeReg(m_slave, SAMC_REG_CFG_MIN_POS, icps::kwr, 0); // 最小位置
|
|
|
|
cfg_max_pos = m_processer->activeReg(m_slave, SAMC_REG_CFG_MAX_POS, icps::kwr, 0); // 最大位置
|
|
|
|
cfg_runtohome_keep_move_distance = m_processer->activeReg(m_slave, SAMC_REG_CFG_RUNTOHOME_KEEP_MOVE_DISTANCE, icps::kwr, 0); // 归零阶段1移动距离
|
|
|
|
|
|
|
|
m_step_scheduler.initialize(10, 1); |
|
|
|
ZHALCORE::getInstance()->regPeriodJob([this](ZHALCORE::Context& context) { this->periodJob(&context); }, 1); |
|
|
|
} |
|
|
|
|
|
|
|
icps::error_t SingleAxisMotorControler::onHostRegisterWriteEvent(icps::WriteEvent* event) { |
|
|
@ -171,6 +177,7 @@ icps::error_t SingleAxisMotorControler::runToHome() { |
|
|
|
* |
|
|
|
*/ |
|
|
|
m_step_scheduler.pushActionAction([this](StepActionContext* context) { //
|
|
|
|
ZLOGI(m_name, "runToHome step 2 move away from zero point"); |
|
|
|
regGpioIrqProcesser(nullptr); |
|
|
|
motor_moveBy(cfg_runtohome_leave_zero_point_distance->getValue(), //
|
|
|
|
cfg_runhome_velocity->getValue(), //
|
|
|
@ -183,6 +190,7 @@ icps::error_t SingleAxisMotorControler::runToHome() { |
|
|
|
* @brief STEP 3 返回原点 |
|
|
|
*/ |
|
|
|
m_step_scheduler.pushActionAction([this](StepActionContext* context) { //
|
|
|
|
ZLOGI(m_name, "runToHome step 3 move to zero point"); |
|
|
|
regGpioIrqProcesser([this](ZGPIO* GPIO_Pin, ZGPIO::IrqTypeEvent_t irqevent) { |
|
|
|
if (GPIO_Pin != m_homegpio) return; |
|
|
|
m_runToHomeContext.stopByIrq = true; |
|
|
@ -217,6 +225,7 @@ icps::error_t SingleAxisMotorControler::runToHome() { |
|
|
|
* @brief STEP 4 移动到零点 |
|
|
|
*/ |
|
|
|
m_step_scheduler.pushActionAction([this](StepActionContext* context) { //
|
|
|
|
ZLOGI(m_name, "runToHome step 4 move to zero point"); |
|
|
|
regGpioIrqProcesser(nullptr); |
|
|
|
motor_moveTo(0, //
|
|
|
|
cfg_runhome_velocity->getValue(), //
|
|
|
@ -228,8 +237,12 @@ icps::error_t SingleAxisMotorControler::runToHome() { |
|
|
|
/**
|
|
|
|
* @brief 异常处理 |
|
|
|
*/ |
|
|
|
m_step_scheduler.regDoneCallback([this]() { changeToState(kstate_idle); }); |
|
|
|
m_step_scheduler.regDoneCallback([this]() { |
|
|
|
regGpioIrqProcesser(nullptr); |
|
|
|
changeToState(kstate_idle); |
|
|
|
}); |
|
|
|
m_step_scheduler.regBreakCallback([this]() { |
|
|
|
regGpioIrqProcesser(nullptr); |
|
|
|
m_exceptionReg->setValue(kexcep_loseZeroPoint); |
|
|
|
changeToState(kstate_exception); |
|
|
|
}); |
|
|
@ -251,11 +264,19 @@ icps::error_t SingleAxisMotorControler::clearException() { |
|
|
|
} |
|
|
|
|
|
|
|
void SingleAxisMotorControler::changeToState(state_t state) { |
|
|
|
ZLOGI(m_name, "changeState %d->%d", m_state, state); |
|
|
|
m_state = state; |
|
|
|
m_statusReg->setValue(m_state); |
|
|
|
} |
|
|
|
|
|
|
|
void SingleAxisMotorControler::periodJob(ZHALCORE::Context* context) {} |
|
|
|
void SingleAxisMotorControler::periodJob(ZHALCORE::Context* context) { |
|
|
|
if (context->getScheduleTimes() % 33 == 0 /*33 ms*/) { |
|
|
|
m_currentVelocityReg->setValue(m_motor->getVACTUAL()); |
|
|
|
m_currentPosReg->setValue(m_motor->getXACTUAL()); |
|
|
|
// ZLOGI(m_name, "%d", m_statusReg->getValue());
|
|
|
|
} |
|
|
|
// m_statusReg->setValue(context->getScheduleTimes() % 4);
|
|
|
|
} |
|
|
|
void SingleAxisMotorControler::motor_moveTo(int32_t pos, int32_t velocity, int32_t acc, int32_t dec) { |
|
|
|
ZLOGI(m_name, "motor_moveTo %d %d %d %d", pos, velocity, acc, dec); |
|
|
|
int32_t position = m_motor->getXACTUAL(); |
|
|
|