-
Notifications
You must be signed in to change notification settings - Fork 4
/
EQStepper-ULN2003
265 lines (237 loc) · 7.3 KB
/
EQStepper-ULN2003
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// EQFocuserStepper without the Temperature/Humidity Sensor
// This driver is for ULN2003 Motor Driver
// Can work with ST35 (12v) Stepper Motor or
// 28BYJ-48 (5v) Stepper Motor
#include <AccelStepper.h>
// motor pins
#define motorPin5 8 // blue
#define motorPin6 9 // pink
#define motorPin7 10 // yellow
#define motorPin8 11 // orange
// Declaration needed for the AccelStepper Library
AccelStepper stepper1(AccelStepper::FULL4WIRE, motorPin5, motorPin7, motorPin6, motorPin8);
// for command purposes
String inputString = "";
int step = 0;
int backlashStep = 0;
String lastDirection = "NONE"; //"OUTWARD"
String currentDirection = "NONE";
// for pin values
int ccwPin = 7;
int cwPin = 6;
int ccwVal = 0;
int cwVal = 0;
// for the variable resistor
int potpin = 0;
int variableResistorValue = 10;
int oldVariableResistorValue = 10;
// for manual control
long toPosition;
bool positionReported = false;
void setup() {
Serial.begin(115200);
Serial.println("EQFOCUSER_STEPPER#");
stepper1.setMaxSpeed(100.0);
stepper1.setAcceleration(100.0);
stepper1.setSpeed(100);
inputString.reserve(200);
pinMode(ccwPin, INPUT_PULLUP);
pinMode(cwPin, INPUT_PULLUP);
}
void loop() {
variableResistorValue = analogRead(potpin);
cwVal = digitalRead(cwPin);
ccwVal = digitalRead(ccwPin);
if (ccwVal == LOW || cwVal == LOW) {
// if (oldVariableResistorValue < variableResistorValue - 5 || oldVariableResistorValue > variableResistorValue + 5 ){
// // set the max acceleration
// stepper1.setAcceleration(variableResistorValue / 102.4);
// oldVariableResistorValue = variableResistorValue;
// Serial.print("Changing Value of acceleration");
// Serial.println(oldVariableResistorValue);
// }
// the PULLUP Pins are pressed
Serial.print("MOVING:");
if (ccwVal == LOW) {
toPosition = stepper1.currentPosition() - variableResistorValue / 10;
Serial.print(toPosition);
applyBacklashStep(toPosition, lastDirection, "INWARD");
// stepper1.moveTo(toPosition);
lastDirection = "INWARD";
}
if (cwVal == LOW) {
toPosition = stepper1.currentPosition() + variableResistorValue / 10;
Serial.print(toPosition);
applyBacklashStep(toPosition, lastDirection, "OUTWARD");
// stepper1.moveTo(toPosition);
lastDirection = "OUTWARD";
}
Serial.println("#");
stepper1.run();
if (toPosition == stepper1.currentPosition()) {
// the stepper is not really moving here so just report the posiiton
reportPosition();
}
}
else {
if (stepper1.distanceToGo() != 0) {
// let the stepper finish the movement
stepper1.run();
positionReported = false;
}
if (stepper1.distanceToGo() == 0 && !positionReported) {
reportPosition();
delay(500);
positionReported = true;
}
}
}
void reportPosition() {
Serial.print("POSITION:");
Serial.print(stepper1.currentPosition());
Serial.println("#");
}
// test if direction is the same, otherwise apply backlash step
// this method is only applicable for manual focusing changes
void applyBacklashStep(int toPosition, String lastDirection, String currentDirection){
if (lastDirection == currentDirection){
// no backlash
stepper1.moveTo(toPosition);
}
else {
// apply backlash
stepper1.moveTo(toPosition + backlashStep);
stepper1.setCurrentPosition(toPosition - backlashStep);
}
}
/**
* process the command we recieved from the client
* command format is <Letter><Space><Integer>
* i.e. A 500 ---- Fast Rewind with 500 steps
*/
void serialCommand(String commandString) {
char _command = commandString.charAt(0);
int _step = commandString.substring(2).toInt();
String _answer = "";
int _currentPosition = stepper1.currentPosition();
int _newPosition = _currentPosition;
int _backlashStep;
switch (_command) {
case 'A': // FAST REVERSE "<<"
case 'a': _newPosition = _currentPosition - ( _step * 2 );
currentDirection = "INWARD";
break;
case 'B': // REVERSE "<"
case 'b': _newPosition = _currentPosition - _step;
currentDirection = "INWARD";
break;
case 'C': // FORWARD ">"
case 'c': _newPosition = _currentPosition + _step;
currentDirection = "OUTWARD";
break;
case 'D': // FAST FORWARD ">>"
case 'd': _newPosition = _currentPosition + ( _step * 2 );
currentDirection = "OUTWARD";
break;
case 'E': // MOVE TO POSITION
case 'e': _newPosition = _step;
break;
case 'F': // GET CURRENT POSITION
case 'f': _answer += _currentPosition;
break;
case 'G': // SET POSITION TO 0
case 'g': _newPosition = 0;
_currentPosition = 0;
stepper1.setCurrentPosition(0);
break;
case 'H': // SET ACCELERATION
case 'h': _newPosition = _currentPosition; // non move command
stepper1.setAcceleration(_step);
_answer += "SET-ACCELERATION:";
_answer += _step;
break;
case 'I': // SET SPEED
_newPosition = _currentPosition; // non move command
stepper1.setSpeed(_step);
_answer += "SET-SPEED:";
_answer += _step;
break;
case 'i': // GET SPEED
_newPosition = _currentPosition; // non move command
_answer += "GET-SPEED:";
_answer += stepper1.speed();
break;
case 'J': // SET MAX SPEED
case 'j': _newPosition = _currentPosition; // non move command
stepper1.setMaxSpeed(_step);
_answer += "SET-MAXSPEED:";
_answer += _step;
break;
case 'k': // GET TEMPERATURE / HUMIDITY
_newPosition = _currentPosition; // non move command
// humidityTemperatureReport();
break;
case 'L' :
case 'l' :
backlashStep = _step;
_answer += "SET-BACKLASHSTEP:";
_answer += _step;
break;
case 'X': // GET STATUS - may not be needed
case 'x':
stepper1.stop();
break;
case 'Z': // IDENTIFY
case 'z': _answer += "EQFOCUSER_STEPPER";
break;
default:
_answer += "EQFOCUSER_STEPPER";
break;
}
if (_newPosition != _currentPosition) {
if (lastDirection != "NONE"){
if (stepper1.currentPosition() < _newPosition){
// moving forward
currentDirection == "OUTWARD";
}
if (stepper1.currentPosition() > _newPosition){
// moving backward
currentDirection == "INWARD";
}
Serial.print(lastDirection);Serial.print("===");Serial.println(currentDirection);
if (lastDirection != currentDirection){
if (currentDirection == "OUTWARD") _newPosition = _newPosition + backlashStep;
if (currentDirection == "INWARD") _newPosition = _newPosition - backlashStep;
}
else {
_backlashStep = 0;
}
}
// a move command was issued
Serial.print("MOVING:");
Serial.print(_newPosition);
Serial.println("#");
// stepper1.runToNewPosition(_newPosition); // this will block the execution
stepper1.moveTo(_newPosition);
stepper1.runSpeedToPosition();
lastDirection = currentDirection;
_answer += "POSITION:";
_answer += stepper1.currentPosition();
}
Serial.print(_answer);
Serial.println("#");
}
/**
* handler for the serial communicationes
* calls the SerialCommand whenever a new command is received
*/
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
serialCommand(inputString);
inputString = "";
}
}
}