C++ 步进电机驱动程序:添加中文注释并翻译
/*
- 设置速度曲线 - 常数速度,线性速度(加速)
- accel 和 decel 以 [full steps/s^2] 为单位给出 */ void BasicStepperDriver::setSpeedProfile(Mode mode, short accel, short decel){ profile.mode = mode; profile.accel = accel; profile.decel = decel; } void BasicStepperDriver::setSpeedProfile(struct Profile profile){ this->profile = profile; }
/*
- 将电机移动给定数量的步骤。
- 正向移动为正数,反向移动为负数 / void BasicStepperDriver::move(long steps){ startMove(steps); while (nextAction()); } /
- 将电机旋转给定数量的度数(1-360) / void BasicStepperDriver::rotate(long deg){ move(calcStepsForRotation(deg)); } /
- 以亚度精度移动电机。
- 请注意,即使使用此函数一次,由于包含浮点支持,
- 它也会将 1K 添加到您的程序大小中。 */ void BasicStepperDriver::rotate(double deg){ move(calcStepsForRotation(deg)); }
/*
-
设置新的移动(计算并保存参数) / void BasicStepperDriver::startMove(long steps, long time){ float speed; // 设置新移动 dir_state = (steps >= 0) ? HIGH : LOW; last_action_end = 0; steps_remaining = labs(steps); step_count = 0; rest = 0; switch (profile.mode){ case LINEAR_SPEED: // 速度以 [steps/s] 为单位 speed = rpm * motor_steps / 60; if (time > 0){ // 计算新速度以在请求的时间内完成 float t = time / (1e+6); // 转换为秒 float d = steps_remaining / microsteps; // 转换为完整步骤 float a2 = 1.0 / profile.accel + 1.0 / profile.decel; float sqrt_candidate = tt - 2 * a2 * d; // 在 √b^2-4ac 中 if (sqrt_candidate >= 0){ speed = stepperMin(speed, (t - (float)sqrt(sqrt_candidate)) / a2); }; } // 从 0 到目标速度需要多少微步 steps_to_cruise = microsteps * (speed * speed / (2 * profile.accel)); // 从巡航速度到完全停止需要多少微步 steps_to_brake = steps_to_cruise * profile.accel / profile.decel; if (steps_remaining < steps_to_cruise + steps_to_brake){ // 无法达到最大速度,需要提前刹车 steps_to_cruise = steps_remaining * profile.decel / (profile.accel + profile.decel); steps_to_brake = steps_remaining - steps_to_cruise; } // 初始脉冲(c0),包括误差校正因子 0.676 [us] step_pulse = (1e+6)0.676sqrt(2.0f/profile.accel/microsteps); // 由于后面将不再有计算的目标速度,因此保存巡航时间 cruise_step_pulse = 1e+6 / speed / microsteps; break;
case CONSTANT_SPEED: default: steps_to_cruise = 0; steps_to_brake = 0; step_pulse = cruise_step_pulse = STEP_PULSE(motor_steps, microsteps, rpm); if (time > steps_remaining * step_pulse){ step_pulse = (float)time / steps_remaining; } } } /*
-
通过添加/删除步骤来更改正在运行的移动
-
FIXME:这是一个天真的实现,它只在 CRUISING 状态下工作良好 / void BasicStepperDriver::alterMove(long steps){ switch (getCurrentState()){ case ACCELERATING: // 这也起作用,但会保留原始速度目标 case CRUISING: if (steps >= 0){ steps_remaining += steps; } else { steps_remaining = stepperMax(steps_to_brake, steps_remaining+steps); }; break; case DECELERATING: // 需要重新开始加速 - 未实现 break; case STOPPED: startMove(steps); break; } } /
-
提前刹车。 */ void BasicStepperDriver::startBrake(void){ switch (getCurrentState()){ case CRUISING: // 这适用于 CONSTANT_SPEED 和 LINEAR_SPEED 模式 steps_remaining = steps_to_brake; break;
case ACCELERATING: steps_remaining = step_count * profile.accel / profile.decel; break;
default: break; // 如果已经停止或刹车,就没有要做的了 } }
AI 优化版
原文地址:https://www.cveoy.top/t/topic/oOF3 著作权归作者所有。请勿转载和采集!