-
Notifications
You must be signed in to change notification settings - Fork 3
/
BasicStepperDriver.cpp
140 lines (122 loc) · 3.43 KB
/
BasicStepperDriver.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* 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.
*/
#include "BasicStepperDriver.h"
/*
* Basic connection: only DIR, STEP are connected.
* Microstepping controls should be hardwired.
*/
BasicStepperDriver::BasicStepperDriver(int steps, int dir_pin, int step_pin)
:motor_steps(steps), dir_pin(dir_pin), step_pin(step_pin)
{
init();
}
BasicStepperDriver::BasicStepperDriver(int steps, int dir_pin, int step_pin, int enable_pin)
:motor_steps(steps), dir_pin(dir_pin), step_pin(step_pin), enable_pin(enable_pin)
{
init();
}
void BasicStepperDriver::init(void){
pinMode(dir_pin, OUTPUT);
digitalWrite(dir_pin, HIGH);
pinMode(step_pin, OUTPUT);
digitalWrite(step_pin, LOW);
if IS_CONNECTED(enable_pin){
pinMode(enable_pin, OUTPUT);
digitalWrite(enable_pin, HIGH); // disable
}
setMicrostep(1);
setRPM(60); // 60 rpm is a reasonable default
enable();
}
void BasicStepperDriver::calcStepPulse(void){
step_pulse = STEP_PULSE(rpm, motor_steps, microsteps);
}
/*
* Set target motor RPM (1-200 is a reasonable range)
*/
void BasicStepperDriver::setRPM(unsigned rpm){
this->rpm = rpm;
calcStepPulse();
}
/*
* Set stepping mode (1:microsteps)
* Allowed ranges for BasicStepperDriver are 1:1 to 1:128
*/
unsigned BasicStepperDriver::setMicrostep(unsigned microsteps){
for (unsigned ms=1; ms <= this->getMaxMicrostep(); ms<<=1){
if (microsteps == ms){
this->microsteps = microsteps;
break;
}
}
calcStepPulse();
return this->microsteps;
}
/*
* DIR: forward HIGH, reverse LOW
*/
void BasicStepperDriver::setDirection(int direction){
digitalWrite(dir_pin, (direction<0) ? LOW : HIGH);
}
/*
* Move the motor a given number of steps.
* positive to move forward, negative to reverse
*/
void BasicStepperDriver::move(long steps){
if (steps >= 0){
setDirection(1);
} else {
setDirection(-1);
steps = -steps;
}
/*
* We currently try to do a 50% duty cycle so it's easy to see.
* Other option is step_high_min, pulse_duration-step_high_min.
*/
unsigned long pulse_duration = step_pulse/2;
while (steps--){
digitalWrite(step_pin, HIGH);
unsigned long next_edge = micros() + pulse_duration;
microWaitUntil(next_edge);
digitalWrite(step_pin, LOW);
microWaitUntil(next_edge + pulse_duration);
}
}
/*
* Move the motor a given number of degrees (1-360)
*/
void BasicStepperDriver::rotate(long deg){
long steps = deg * motor_steps * (long)microsteps / 360;
move(steps);
}
/*
* Move the motor with sub-degree precision.
* Note that using this function even once will add 1K to your program size
* due to inclusion of float support.
*/
void BasicStepperDriver::rotate(double deg){
long steps = deg * motor_steps * microsteps / 360;
move(steps);
}
/*
* Enable/Disable the motor by setting a digital flag
*/
void BasicStepperDriver::enable(void){
if IS_CONNECTED(enable_pin){
digitalWrite(enable_pin, LOW);
}
}
void BasicStepperDriver::disable(void){
if IS_CONNECTED(enable_pin){
digitalWrite(enable_pin, HIGH);
}
}
unsigned BasicStepperDriver::getMaxMicrostep(){
return BasicStepperDriver::MAX_MICROSTEP;
}