-
Notifications
You must be signed in to change notification settings - Fork 2
/
EQFocuserStepper-MotorShield-NEMA17.ino
317 lines (283 loc) · 8.12 KB
/
EQFocuserStepper-MotorShield-NEMA17.ino
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// EQ Simple Focuser
// Arduino code in controlling an absolute focuser
// using a stepper motor
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <dht.h>
// Define the stepper and the pins it will use
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *motor1 = AFMS.getStepper(200, 1);
// for the temperature and hubmidity sensor
#define DHT11_PIN 5
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {
motor1->onestep(FORWARD, MICROSTEP);
}
void backwardstep() {
motor1->onestep(BACKWARD, MICROSTEP);
}
// Declaration needed for the AccelStepper Library
AccelStepper stepper1(forwardstep, backwardstep);
// 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;
// temperature and humidity sensor
dht DHT;
int chkSensor;
void setup() {
AFMS.begin();
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) {
Serial.print("MOVING:");
if (ccwVal == LOW) {
toPosition = stepper1.currentPosition() - variableResistorValue / 10;
Serial.print(toPosition);
applyBacklashStep(toPosition, lastDirection, "INWARD");
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 = "";
}
}
}
/**
* for DHT routine
*/
void humidityTemperatureReport() {
chkSensor = DHT.read11(DHT11_PIN);
switch (chkSensor) {
case DHTLIB_OK:
Serial.print("TEMPERATURE:");
Serial.print(DHT.temperature, 1);
Serial.println("#");
delay(50);
Serial.print("HUMIDITY:");
Serial.print(DHT.humidity, 1);
Serial.println("#");
delay(50);
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("TEMPERATURE:");
Serial.print("CHECKSUMERROR");
Serial.println("#");
Serial.print("HUMIDITY:");
Serial.print("CHECKSUMERROR");
Serial.println("#");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("TEMPERATURE:");
Serial.print("TIMEOUTERROR");
Serial.println("#");
Serial.print("HUMIDITY:");
Serial.print("TIMEOUTERROR");
Serial.println("#");
break;
default:
Serial.print("TEMPERATURE:");
Serial.print("UNKNOWNERROR");
Serial.println("#");
Serial.print("HUMIDITY:");
Serial.print("UNKNOWNERROR");
Serial.println("#");
break;
}
}