给函数和声明添加中文注释并将注释翻译成中文 Generic Stepper Motor Driver Driver Indexer mode only Copyright C2015-2018 Laurentiu Badea This file may be redistributed under the terms of the MIT license A copy of this
/*
- 通用步进电机驱动程序,仅支持索引器模式。
- 版权所有(C)2015-2018 Laurentiu Badea
- 本文件可以在 MIT 许可证的条款下重新分发。
- 本分发包含在文件 LICENSE 中的许可证的副本。 */ #ifndef STEPPER_DRIVER_BASE_H #define STEPPER_DRIVER_BASE_H #include <Arduino.h>
// 由库内部用于标记未连接的引脚 #define PIN_UNCONNECTED -1 #define IS_CONNECTED(pin) (pin != PIN_UNCONNECTED)
/*
- 根据给定的转速值计算微秒级步进脉冲。
- 60 [s/min] * 1000000 [us/s] / microsteps / steps / rpm / #define STEP_PULSE(steps, microsteps, rpm) (60.01000000L/steps/microsteps/rpm)
// 如果等待时间短于此,则不要调用 yield() #define MIN_YIELD_MICROS 50
/*
- 基本步进驱动程序类。
- 微步进级别应由外部控制或硬连线。
*/
class BasicStepperDriver {
public:
enum Mode {CONSTANT_SPEED, LINEAR_SPEED};
enum State {STOPPED, ACCELERATING, CRUISING, DECELERATING};
struct Profile {
Mode mode = CONSTANT_SPEED;
short accel = 1000; // 加速度 [步/s^2]
short decel = 1000; // 减速度 [步/s^2]
}; static inline void delayMicros(unsigned long delay_us, unsigned long start_us = 0){ if (delay_us){ if (!start_us){ start_us = micros(); } if (delay_us > MIN_YIELD_MICROS){ yield(); } // 请参阅 https://www.gammon.com.au/millis while (micros() - start_us < delay_us); } }
private: // Atmel DOC8017 中用于增加精度的计算余数 long rest; unsigned long last_action_end = 0; unsigned long next_action_interval = 0;
protected: /* * 电机配置 */ short motor_steps; // 电机每转步数(通常为200)
/*
* 驱动器配置
*/
short dir_pin;
short step_pin;
short enable_pin = PIN_UNCONNECTED;
short enable_active_state = HIGH;
// 获取设备支持的最大微步进数
virtual short getMaxMicrostep();
// 当前微步进级别(1,2,4,8,...),必须 < getMaxMicrostep()
short microsteps = 1;
// tWH(STEP) 脉冲持续时间,STEP 高电平,最小值(微秒)
static const int step_high_min = 1;
// tWL(STEP) 脉冲持续时间,STEP 低电平,最小值(微秒)
static const int step_low_min = 1;
// tWAKE 唤醒时间,nSLEEP 无效到 STEP (微秒)
static const int wakeup_time = 0;
float rpm = 0;
/*
* 运动状态
*/
struct Profile profile;
long step_count; // 当前位置
long steps_remaining; // 完成当前移动所需的步数(绝对值)
long steps_to_cruise; // 达到巡航(最大)转速所需的步数
long steps_to_brake; // 到达完全停止所需的步数
long step_pulse; // 步进脉冲持续时间(微秒)
long cruise_step_pulse; // 常数速度部分的步进脉冲持续时间(最大转速)
// DIR 引脚状态
short dir_state;
void calcStepPulse(void);
// 这是内部的,因为可以在 CRUISING 时调用 start() 方法来到这里
void alterMove(long steps);
private: // 微步进范围(1,16,32等) static const short MAX_MICROSTEP = 128;
public: /* * 基本连接: DIR、STEP 连接。 / BasicStepperDriver(short steps, short dir_pin, short step_pin); BasicStepperDriver(short steps, short dir_pin, short step_pin, short enable_pin); / * 初始化引脚,计算时间等 / void begin(float rpm=60, short microsteps=1); / * 设置当前微步进级别,1=全速,32=微微调速 * 如果值超出范围,则返回新级别或先前级别 / virtual short setMicrostep(short microsteps); short getMicrostep(void){ return microsteps; } short getSteps(void){ return motor_steps; } / * 设置目标电机转速(1-200 是一个合理的范围) / void setRPM(float rpm); float getRPM(void){ return rpm; }; float getCurrentRPM(void){ return (60.01000000L / step_pulse / microsteps / motor_steps); } /* * 设置速度曲线——CONSTANT_SPEED、LINEAR_SPEED(加速) * 加速度和减速度以[全步/s^2]为单位给出 / void setSpeedProfile(Mode mode, short accel=1000, short decel=1000); void setSpeedProfile(struct Profile profile); struct Profile getSpeedProfile(void){ return profile; } short getAcceleration(void){ return profile.accel; } short getDeceleration(void){ return profile.decel; } / * 移动电机给定数量的步数。 * 正数向前移动,负数向后移动 / void move(long steps); / * 旋转电机给定数量的角度(1-360) / void rotate(long deg); inline void rotate(int deg){ rotate((long)deg); }; / * 使用浮点数或双精度浮点数进行旋转,以增加运动精度。 / void rotate(double deg); / * 配置 ENABLE 引脚的逻辑状态表示活动状态 * 当使用 SLEEP(默认) 时,这是高电平活动 / void setEnableActiveState(short state); / * 关闭/打开电机,以允许手动移动电机/保持位置不变 / virtual void enable(void); virtual void disable(void); / * 非阻塞模式的方法。 * 它们使用更多的代码,但允许在脉冲之间执行其他操作。 * 流程有两个部分——开始/初始化,然后是 nextAction 循环。 * 请参阅 NonBlocking 示例。 / / * 计算并保存已知距离上的移动(计算并保存参数) * 根据移动类型和距离类型选择一个。 * 如果给定了时间(微秒),则驱动器将尝试在这个时间内精确执行移动 * 通过仅对此移动更改 rpm (高达预设 rpm)。 / void startMove(long steps, long time=0); inline void startRotate(int deg){ startRotate((long)deg); }; void startRotate(long deg); void startRotate(double deg); / * 在正确的时间切换步进并返回需要下一次更改的时间(微秒) / long nextAction(void); / * 可选地,调用此方法以提前开始制动(然后停止) * 对于恒定速度,这与 stop() 相同 / void startBrake(void); / * 立即停止 * 返回剩余步数。 / long stop(void); / * 状态查询 / enum State getCurrentState(void); / * 获取到目前为止完成的步数。 * 这始终是一个正数 / long getStepsCompleted(void){ return step_count; } / * 获取完成移动所需的剩余步数 * 这始终是一个正数 / long getStepsRemaining(void){ return steps_remaining; } / * 获取运动方向: 前进 +1,后退 -1 / int getDirection(void){ return (dir_state == HIGH) ? 1 : -1; } / * 返回计算完成所需的时间 / long getTimeForMove(long steps); / * 计算旋转所需的步数,单位为度 */ long calcStepsForRotation(long deg){ return deg * motor_steps * (long)microsteps / 360; } long calcStepsForRotation(double deg){ return deg * motor_steps * microsteps / 360; } }; #endif // STEPPER_DRIVER_BASE_
原文地址: https://www.cveoy.top/t/topic/hlSy 著作权归作者所有。请勿转载和采集!