|
|
@ -3,67 +3,196 @@ |
|
|
|
using namespace iflytop; |
|
|
|
#define TAG "LRCS"
|
|
|
|
|
|
|
|
const uint32_t LncubatorRotatingControlService::mc_onecircle_ustep_num = (256 * 200 * 50); |
|
|
|
const uint32_t LncubatorRotatingControlService::mc_default_velocity = 500000; // 500k
|
|
|
|
const uint32_t LncubatorRotatingControlService::mc_onecircle_ustep_num = (256 * 200 * 50); |
|
|
|
const uint32_t LncubatorRotatingControlService::mc_default_velocity = 500000; // 500kpps
|
|
|
|
const uint32_t LncubatorRotatingControlService::mc_one_sub_position_width = (256 * 200 * 50) / 20; |
|
|
|
|
|
|
|
LncubatorRotatingControlService::LncubatorRotatingControlService() {} |
|
|
|
LncubatorRotatingControlService::~LncubatorRotatingControlService() {} |
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief |
|
|
|
* |
|
|
|
* |
|
|
|
* 1. 定位 |
|
|
|
* 2. 标记零点位置 |
|
|
|
* 3. |
|
|
|
* |
|
|
|
*/ |
|
|
|
|
|
|
|
void LncubatorRotatingControlService::initialize(Hardware* hardware) { //
|
|
|
|
m_hardware = hardware; |
|
|
|
m_motor = &m_hardware->tmc4361motor1; |
|
|
|
m_os = m_hardware; |
|
|
|
m_workfinished = false; |
|
|
|
m_dowhat = kidle; |
|
|
|
m_maxVelocity = mc_default_velocity; |
|
|
|
m_hardware = hardware; |
|
|
|
m_motor = m_hardware->getMotor1(); |
|
|
|
m_os = m_hardware; |
|
|
|
m_workfinished = false; |
|
|
|
m_dowhat = kidle; |
|
|
|
m_maxVelocity = mc_default_velocity; |
|
|
|
m_inletPosition = 0; |
|
|
|
m_outletPosition = m_inletPosition + mc_one_sub_position_width / 2; |
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief 电机必须工作在位置模式 |
|
|
|
*/ |
|
|
|
m_motor->setMotorInPositionMode(); |
|
|
|
hardware->registerListener(this); |
|
|
|
m_motor->registerListener(this, TMC4361A::event_mask_t::ktarget_reached); |
|
|
|
} |
|
|
|
void LncubatorRotatingControlService::moveToHome() { |
|
|
|
m_dowhat = krotateToHome; |
|
|
|
m_motor->moveTo(mc_onecircle_ustep_num * 2, m_maxVelocity); |
|
|
|
|
|
|
|
void LncubatorRotatingControlService::setVelocity(int32_t velocityMax) { m_maxVelocity = velocityMax; } |
|
|
|
void LncubatorRotatingControlService::registerListener(LncubatorRotatingControlServiceListener* listener) { //
|
|
|
|
m_listener = listener; |
|
|
|
} |
|
|
|
void LncubatorRotatingControlService::Hardware_onHomeRefSwitch() { |
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* motorHelp * |
|
|
|
*******************************************************************************/ |
|
|
|
int32_t LncubatorRotatingControlService::tmc_getPosition() { |
|
|
|
int32_t position = m_motor->getENC_POS(); |
|
|
|
return normalizePosition(position); |
|
|
|
} |
|
|
|
void LncubatorRotatingControlService::tmc_roateToPosition(int32_t position) { |
|
|
|
int32_t _position = normalizePosition(position); |
|
|
|
m_motor->moveTo(position, m_maxVelocity); |
|
|
|
} |
|
|
|
int32_t LncubatorRotatingControlService::normalizePosition(int32_t position) { |
|
|
|
while (true) { |
|
|
|
if (position > mc_onecircle_ustep_num / 2) |
|
|
|
position = position - mc_onecircle_ustep_num; |
|
|
|
else if (position < mc_onecircle_ustep_num / 2) |
|
|
|
position = position + mc_onecircle_ustep_num; |
|
|
|
else |
|
|
|
break; |
|
|
|
} |
|
|
|
if (position == mc_onecircle_ustep_num / 2) position = position - 1; |
|
|
|
if (position == -mc_onecircle_ustep_num / 2) position = position + 1; |
|
|
|
return position; |
|
|
|
} |
|
|
|
void LncubatorRotatingControlService::tmc_moveBy(int32_t relativePosition) { m_motor->moveBy(relativePosition, m_maxVelocity); } |
|
|
|
/*******************************************************************************
|
|
|
|
* TriggerEvent * |
|
|
|
*******************************************************************************/ |
|
|
|
void LncubatorRotatingControlService::Hardware_onHomeRefSwitchISR() { |
|
|
|
/**
|
|
|
|
* @brief 电机零点光电触发 |
|
|
|
*/ |
|
|
|
m_homeKeyTriggered = true; |
|
|
|
} |
|
|
|
void LncubatorRotatingControlService::setVelocity(uint32_t velocityMax) { m_maxVelocity = velocityMax; } |
|
|
|
void LncubatorRotatingControlService::rotate(uint32_t count) { m_motor->moveBy(count, m_maxVelocity); } |
|
|
|
void LncubatorRotatingControlService::triggerEvent(interevent_t event) {} |
|
|
|
|
|
|
|
void LncubatorRotatingControlService::periodicJob() { |
|
|
|
if (krotateToHome == m_dowhat) { |
|
|
|
doRotateToHomeJob(); |
|
|
|
if (kmoveToHomeJob == m_dowhat) { |
|
|
|
doRotateToHomeJob(kPeriodicJobEvent); |
|
|
|
} else if (kmoveSubPositionJob == m_dowhat) { |
|
|
|
doMoveToSubPositionJob(kPeriodicJobEvent); |
|
|
|
} else if (kmoveByJob == m_dowhat) { |
|
|
|
doMoveByJob(kPeriodicJobEvent); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void LncubatorRotatingControlService::doRotateToHomeJob() { |
|
|
|
if (m_homeKeyTriggered) { |
|
|
|
m_homeKeyTriggered = false; |
|
|
|
if (kmoveToHomeJob == m_dowhat) { |
|
|
|
doRotateToHomeJob(kPeriodicJobEvent); |
|
|
|
} else if (kmoveSubPositionJob == m_dowhat) { |
|
|
|
doMoveToSubPositionJob(kPeriodicJobEvent); |
|
|
|
} else if (kmoveByJob == m_dowhat) { |
|
|
|
doMoveByJob(kPeriodicJobEvent); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
void LncubatorRotatingControlService::TMC4361A_onEvent(uint32_t event) { |
|
|
|
if (event & TMC4361A::event_mask_t::ktarget_reached) { |
|
|
|
if (kmoveToHomeJob == m_dowhat) { |
|
|
|
doRotateToHomeJob(kTMC4361AReachTargetEvent); |
|
|
|
} else if (kmoveSubPositionJob == m_dowhat) { |
|
|
|
doMoveToSubPositionJob(kTMC4361AReachTargetEvent); |
|
|
|
} else if (kmoveByJob == m_dowhat) { |
|
|
|
doMoveByJob(kTMC4361AReachTargetEvent); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
/*******************************************************************************
|
|
|
|
* RotateToHomeJob * |
|
|
|
*******************************************************************************/ |
|
|
|
|
|
|
|
void LncubatorRotatingControlService::moveToHome() { |
|
|
|
if (m_dowhat != kidle) return; |
|
|
|
m_dowhat = kmoveToHomeJob; |
|
|
|
moveBy(mc_onecircle_ustep_num * 2); |
|
|
|
} |
|
|
|
void LncubatorRotatingControlService::doRotateToHomeJob(interevent_t event) { |
|
|
|
if (event == kPeriodicJobEvent) { |
|
|
|
if (m_homeKeyTriggered) { |
|
|
|
m_homeKeyTriggered = false; |
|
|
|
/**
|
|
|
|
* @brief 零点光电触发,记录零点位置 |
|
|
|
*/ |
|
|
|
m_motor->stop(); |
|
|
|
m_motor->setZeroPoint(); |
|
|
|
m_dowhat = kidle; |
|
|
|
ZLOGI(TAG, "find Zero Point"); |
|
|
|
if (m_listener) m_listener->LncubatorRotatingControlService_onMoveToHomeJobFinished(); |
|
|
|
} |
|
|
|
} else if (event == kTMC4361AReachTargetEvent) { |
|
|
|
/**
|
|
|
|
* @brief 零点光电触发,记录零点位置 |
|
|
|
* @brief 电机转了两圈依然没有找到零点 |
|
|
|
*/ |
|
|
|
m_homePosition = m_motor->getENC_POS(); |
|
|
|
m_motor->stop(); |
|
|
|
if (m_listener) m_listener->LncubatorRotatingControlService_onException(kGoHomeFail); |
|
|
|
m_dowhat = kidle; |
|
|
|
} |
|
|
|
} |
|
|
|
void LncubatorRotatingControlService::calibrationPosition(refpoint_t refpoint) { |
|
|
|
/**
|
|
|
|
* @brief 此时是孵育船1对其了入口 |
|
|
|
*/ |
|
|
|
if (refpoint == REFPOINT_TYPE_INLET) { |
|
|
|
m_inletPosition = tmc_getPosition(); |
|
|
|
m_outletPosition = normalizePosition(m_inletPosition + mc_onecircle_ustep_num / 2); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* moveToSubPosition * |
|
|
|
*******************************************************************************/ |
|
|
|
void LncubatorRotatingControlService::moveToSubPosition(refpoint_t refpoint, int32_t positionIndex) { |
|
|
|
if (m_dowhat != kidle) return; |
|
|
|
m_dowhat = kmoveSubPositionJob; |
|
|
|
// 出口
|
|
|
|
if (refpoint == REFPOINT_TYPE_OUTLET) { |
|
|
|
int32_t targetPosition = m_outletPosition + positionIndex * mc_one_sub_position_width; |
|
|
|
tmc_roateToPosition(targetPosition); |
|
|
|
} else if (refpoint == REFPOINT_TYPE_INLET) { |
|
|
|
int32_t targetPosition = m_inletPosition + positionIndex * mc_one_sub_position_width; |
|
|
|
tmc_roateToPosition(targetPosition); |
|
|
|
} else { |
|
|
|
ZLOGE(TAG, "refpoint error"); |
|
|
|
} |
|
|
|
} |
|
|
|
void LncubatorRotatingControlService::doMoveToSubPositionJob(interevent_t event) { |
|
|
|
if (event == kPeriodicJobEvent) { |
|
|
|
} else if (event == kTMC4361AReachTargetEvent) { |
|
|
|
/**
|
|
|
|
* @brief moveToTarget |
|
|
|
*/ |
|
|
|
if (m_listener) m_listener->LncubatorRotatingControlService_onMoveSubPositionJobFinished(); |
|
|
|
m_dowhat = kidle; |
|
|
|
ZLOGI(TAG, "home position:%d", m_homePosition); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void LncubatorRotatingControlService::registerListener(LncubatorRotatingControlServiceListener* listener) { //
|
|
|
|
m_listener = listener; |
|
|
|
/*******************************************************************************
|
|
|
|
* moveByJob * |
|
|
|
*******************************************************************************/ |
|
|
|
void LncubatorRotatingControlService::moveBy(int32_t pluseCount) { |
|
|
|
if (m_dowhat == kidle || m_dowhat == kmoveByJob) { |
|
|
|
m_dowhat = kmoveByJob; |
|
|
|
tmc_moveBy(pluseCount); |
|
|
|
} |
|
|
|
} |
|
|
|
void LncubatorRotatingControlService::doMoveByJob(interevent_t event) { |
|
|
|
if (event == kPeriodicJobEvent) { |
|
|
|
} else if (event == kTMC4361AReachTargetEvent) { |
|
|
|
/**
|
|
|
|
* @brief moveToTarget |
|
|
|
*/ |
|
|
|
if (m_listener) m_listener->LncubatorRotatingControlService_onMoveByJobFinished(); |
|
|
|
m_dowhat = kidle; |
|
|
|
} |
|
|
|
} |
|
|
|
/*******************************************************************************
|
|
|
|
* STOP * |
|
|
|
*******************************************************************************/ |
|
|
|
void LncubatorRotatingControlService::stop() { |
|
|
|
if (m_dowhat == kidle) return; |
|
|
|
if (m_dowhat == kmoveToHomeJob) { |
|
|
|
m_motor->stop(); |
|
|
|
m_dowhat = kidle; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |