-
Notifications
You must be signed in to change notification settings - Fork 6
/
StepperDriver.cpp
executable file
·153 lines (118 loc) · 3.01 KB
/
StepperDriver.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
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "Arduino.h"
#include "StepperDriver.h"
StepperDriver::StepperDriver(int number_of_steps, int step_division, int en_pin, int cw_pin, int clk_pin)
{
this->number_of_steps = number_of_steps;
this->step_division = step_division;
this->step_interval = 10000;
this->last_step_time = 0;
this->target_step_time1 = 0;
this->target_step_time2 = 0;
// Arduino pins for the motor control connection:
this->en_pin = en_pin;
this->cw_pin = cw_pin;
this->clk_pin = clk_pin;
// setup the pins on the microcontroller:
pinMode(en_pin, OUTPUT);
pinMode(cw_pin, OUTPUT);
pinMode(clk_pin, OUTPUT);
}
/*
Sets the speed in revs per minute
*/
void StepperDriver::setSpeed(float rpm)
{
step_interval = 60000000L / (number_of_steps * rpm * step_division);
}
void StepperDriver::powerEnable(bool ena)
{
if (ena) {
digitalWrite(en_pin, LOW);
} else {
digitalWrite(en_pin, HIGH);
}
}
void StepperDriver::positioning()
{
unsigned long step_interval_0 = step_interval;
step_interval *= 8;
step(4);
delay(200);
step(-4);
delay(200);
step_interval = step_interval_0;
}
/*
Moves the motor steps_to_move steps. If the number is negative,
the motor moves in the reverse direction.
*/
void StepperDriver::step(long steps_to_move)
{
steps_to_move *= step_division;
setDirection(steps_to_move);
last_step_time = micros();
for (long i = abs(steps_to_move); i > 0; i--) {
move();
}
}
void StepperDriver::step(long steps_to_move, long steps_acc, long steps_dec)
{
steps_to_move *= step_division;
steps_acc *= step_division;
steps_dec *= step_division;
setDirection(steps_to_move);
last_step_time = micros();
if (steps_acc > 0) {
for (long i = 1; i <= steps_acc; i++) {
dynamicMove( i , steps_acc );
}
}
for (long i = (abs(steps_to_move) - abs(steps_acc) - abs(steps_dec)); i > 0; i--) {
move();
}
if (steps_dec > 0) {
for (long i = (steps_dec - 1); i >= 0; i--) {
dynamicMove( i , steps_dec );
}
}
}
void StepperDriver::setDirection(long steps_to_move)
{
if (steps_to_move < 0) {
digitalWrite(cw_pin, HIGH);
}
else {
digitalWrite(cw_pin, LOW);
}
}
void StepperDriver::move()
{
digitalWrite(clk_pin, HIGH);
moveInterval(step_interval);
}
void StepperDriver::dynamicMove(int s1, int s2)
{
digitalWrite(clk_pin, HIGH);
double r1 = (double)s1 / (double)s2;
double r2 = 0.1 + 0.2*r1 + 2.2*r1*r1 - 1.5*r1*r1*r1;
moveInterval( (unsigned long)(step_interval / r2) );
}
void StepperDriver::moveInterval(unsigned long target_delay)
{
target_step_time1 = last_step_time + (target_delay / 2);
target_step_time2 = last_step_time + target_delay;
if (target_step_time1 >= last_step_time) {
while (micros() < target_step_time1) {}
}
else {
while ((long)(micros()) < (long)target_step_time1) {}
}
digitalWrite(clk_pin, LOW);
if (target_step_time2 >= last_step_time) {
while (micros() < target_step_time2) {}
}
else {
while ((long)(micros()) < (long)target_step_time2) {}
}
last_step_time = micros();
}