给函数和声明添加中文注释并将注释翻译成中文 Stop movement immediately and return remaining steps long BasicStepperDriverstopvoid long retval = steps_remaining; steps_remaining = 0; return retval; Return calculat
/*
- 立即停止运动并返回剩余步数。 / long BasicStepperDriver::stop(void){ long retval = steps_remaining; steps_remaining = 0; return retval; } /
- 返回完成给定移动的预计时间。 */ long BasicStepperDriver::getTimeForMove(long steps){ float t; long cruise_steps; float speed; if (steps == 0){ return 0; } switch (profile.mode){ case LINEAR_SPEED: startMove(steps); cruise_steps = steps_remaining - steps_to_cruise - steps_to_brake; speed = rpm * motor_steps / 60; // full steps/s t = (cruise_steps / (microsteps * speed)) + sqrt(2.0 * steps_to_cruise / profile.accel / microsteps) + sqrt(2.0 * steps_to_brake / profile.decel / microsteps); t = (1e+6); // seconds -> micros break; case CONSTANT_SPEED: default: t = steps * STEP_PULSE(motor_steps, microsteps, rpm); } return round(t); } /
- 移动电机一个整数度数(360 = 完整旋转)
- 对于小量,这具有较差的精度,因为步距通常为1.8度 / void BasicStepperDriver::startRotate(long deg){ startMove(calcStepsForRotation(deg)); } /
- 用亚度精度移动电机。
- 请注意,由于包含浮点支持,调用此函数将大大增加程序大小。 */ void BasicStepperDriver::startRotate(double deg){ startMove(calcStepsForRotation(deg)); }
/*
-
计算下一个脉冲的间隔 */ void BasicStepperDriver::calcStepPulse(void){ if (steps_remaining <= 0){ // 这不应该发生,但可以避免奇怪的计算 return; } steps_remaining--; step_count++;
if (profile.mode == LINEAR_SPEED){ switch (getCurrentState()){ case ACCELERATING: if (step_count < steps_to_cruise){ step_pulse = step_pulse - (2step_pulse+rest)/(4step_count+1); rest = (step_count < steps_to_cruise) ? (2step_pulse+rest) % (4step_count+1) : 0; } else { // The series approximates target, set the final value to what it should be instead step_pulse = cruise_step_pulse; } break;
case DECELERATING: step_pulse = step_pulse - (2*step_pulse+rest)/(-4*steps_remaining+1); rest = (2*step_pulse+rest) % (-4*steps_remaining+1); break; default: break; // 无速度更改 }} } /*
-
屈服于步进控制
-
切换步骤并返回下一次更改所需的时间(微秒) / long BasicStepperDriver::nextAction(void){ if (steps_remaining > 0){ delayMicros(next_action_interval, last_action_end); / * DIR引脚在上升STEP边沿上采样,因此首先设置它 */ digitalWrite(dir_pin, dir_state); digitalWrite(step_pin, HIGH); unsigned m = micros(); unsigned long pulse = step_pulse; // 保存值,因为calcStepPulse()将覆盖它 calcStepPulse(); // 我们应该至少拉高1-2us(step_high_min) delayMicros(step_high_min); digitalWrite(step_pin, LOW); // 考虑到calcStepPulse()的执行时间; 设置较慢MCU的最大rpm的上限 last_action_end = micros(); m = last_action_end - m; next_action_interval = (pulse > m) ? pulse - m : 1; } else { // 移动结束 last_action_end = 0; next_action_interval = 0; } return next_action_interval; }
enum BasicStepperDriver::State BasicStepperDriver::getCurrentState(void){ enum State state; if (steps_remaining <= 0){ state = STOPPED; } else { if (steps_remaining <= steps_to_brake){ state = DECELERATING; } else if (step_count <= steps_to_cruise){ state = ACCELERATING; } else { state = CRUISING; } } return state; } /*
- 配置使用ENABLE引脚时表示活动的逻辑状态
- 当使用SLEEP(默认)时,这是高电平活动 / void BasicStepperDriver::setEnableActiveState(short state){ enable_active_state = state; } /
- 通过设置数字标志来启用/禁用电机 */ void BasicStepperDriver::enable(void){ if IS_CONNECTED(enable_pin){ digitalWrite(enable_pin, enable_active_state); }; delayMicros(2); }
void BasicStepperDriver::disable(void){ if IS_CONNECTED(enable_pin){ digitalWrite(enable_pin, (enable_active_state == HIGH) ? LOW : HIGH); } }
short BasicStepperDriver::getMaxMicrostep(){ return BasicStepperDriver::MAX_MICROSTEP;
原文地址: https://www.cveoy.top/t/topic/hlSj 著作权归作者所有。请勿转载和采集!