-
Notifications
You must be signed in to change notification settings - Fork 3
/
BasicStepperDriver.h
104 lines (94 loc) · 3.01 KB
/
BasicStepperDriver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Generic Stepper Motor Driver Driver
* Indexer mode only.
*
* Copyright (C)2015 Laurentiu Badea
*
* This file may be redistributed under the terms of the MIT license.
* A copy of this license has been included with this distribution in the file LICENSE.
*/
#ifndef STEPPER_DRIVER_BASE_H
#define STEPPER_DRIVER_BASE_H
#include <Arduino.h>
#include "BasicStepperDriver.h"
// used internally by the library to mark unconnected pins
#define PIN_UNCONNECTED -1
#define IS_CONNECTED(pin) (pin != PIN_UNCONNECTED)
/*
* calculate the step pulse in microseconds for a given rpm value.
* 60[s/min] * 1000000[us/s] / microsteps / steps / rpm
*/
#define STEP_PULSE(steps, microsteps, rpm) (60*1000000L/steps/microsteps/rpm)
inline void microWaitUntil(unsigned long target_micros){
yield();
while (micros() < target_micros);
}
#define DELAY_MICROS(us) microWaitUntil(micros() + us)
/*
* Basic Stepper Driver class.
* Microstepping level should be externally controlled or hardwired.
*/
class BasicStepperDriver {
protected:
int motor_steps;
int rpm = 60;
int dir_pin;
int step_pin;
int enable_pin = PIN_UNCONNECTED;
// current microstep level, must be < getMaxMicrostep()
// for 1:16 microsteps is 16
unsigned microsteps = 1;
// step pulse duration (microseconds), depends on rpm and microstep level
void setDirection(int direction);
void init(void);
void calcStepPulse(void);
// tWH(STEP) pulse duration, STEP high, min value (us)
static const int step_high_min = 1;
// tWL(STEP) pulse duration, STEP low, min value (us)
static const int step_low_min = 1;
// tWAKE wakeup time, nSLEEP inactive to STEP (us)
static const int wakeup_time = 0;
// Get max microsteps supported by the device
virtual unsigned getMaxMicrostep();
private:
// microstep range (1, 16, 32 etc)
static const unsigned MAX_MICROSTEP = 128;
public:
unsigned long step_pulse;
/*
* Basic connection: DIR, STEP are connected.
*/
BasicStepperDriver(int steps, int dir_pin, int step_pin);
BasicStepperDriver(int steps, int dir_pin, int step_pin, int enable_pin);
/*
* Set current microstep level, 1=full speed, 32=fine microstepping
* Returns new level or previous level if value out of range
*/
unsigned setMicrostep(unsigned microsteps);
/*
* Move the motor a given number of steps.
* positive to move forward, negative to reverse
*/
void move(long steps);
/*
* Rotate the motor a given number of degrees (1-360)
*/
void rotate(long deg);
inline void rotate(int deg){
rotate((long)deg);
};
/*
* Rotate using a float or double for increased movement precision.
*/
void rotate(double deg);
/*
* Set target motor RPM (1-200 is a reasonable range)
*/
void setRPM(unsigned rpm);
/*
* Turn off/on motor to allow the motor to be moved by hand/hold the position in place
*/
void enable(void);
void disable(void);
};
#endif // STEPPER_DRIVER_BASE_H