-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMoonlite_arduino_ceesnz_30sSleepDelay.ino
269 lines (224 loc) · 6.67 KB
/
Moonlite_arduino_ceesnz_30sSleepDelay.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
// Moonlite-compatible stepper controller
//
// Uses AccelStepper (http://www.airspayce.com/mikem/arduino/AccelStepper/)
//
// Inspired by (http://orlygoingthirty.blogspot.co.nz/2014/04/arduino-based-motor-focuser-controller.html)
// orly.andico@gmail.com, 13 April 2014
//
// Modified for indilib, easydriver by Cees Lensink
// Added sleep function by Daniel Franzén
#include <AccelStepper.h>
int stepperPin = 3;
int dirPin = 2;
int powerPin = 4;
boolean useSleep = true; // true= use sleep pin, false = use enable pin
int ledPin = 13;
// maximum speed is 160pps which should be OK for most
// tin can steppers
#define MAXSPEED 160
#define SPEEDMULT 3
AccelStepper stepper(1, stepperPin, dirPin);
#define MAXCOMMAND 8
char inChar;
char cmd[MAXCOMMAND];
char param[MAXCOMMAND];
char line[MAXCOMMAND];
long pos;
int eoc = 0;
int idx = 0;
boolean isRunning = false;
boolean powerIsOn = false;
long timerStartTime = 0;
//Define the period to wait before turning power off (in milliseconds)
const int activeTimePeriod = 30000;
char tempString[10];
void setup()
{
Serial.begin(9600);
pinMode(powerPin,OUTPUT);
// we ignore the Moonlite speed setting because Accelstepper implements
// ramping, making variable speeds un-necessary
stepper.setSpeed(MAXSPEED);
stepper.setMaxSpeed(MAXSPEED);
stepper.setAcceleration(50);
turnOff();
memset(line, 0, MAXCOMMAND);
}
//
//
void loop(){
if (isRunning) { // only have to do this is stepper is on
stepper.run();
if (stepper.distanceToGo() == 0) {
//start timer to decide when to power off the board.
timerStartTime = millis();
isRunning = false;
}
}
else if(powerIsOn)
{
//Turn power off if active time period has passed.
if(millis() - timerStartTime > activeTimePeriod)
{
turnOff();
}
}
// read the command until the terminating # character
while (Serial.available() && !eoc) {
inChar = Serial.read();
if (inChar != '#' && inChar != ':') {
line[idx++] = inChar;
if (idx >= MAXCOMMAND) {
idx = MAXCOMMAND - 1;
}
}
else {
if (inChar == '#') {
eoc = 1;
}
}
} // end while Serial.available()
// we may not have a complete command yet but there is no character coming in for now and might as well loop in case stepper needs updating
// eoc will flag if a full command is there to act upon
// process the command we got
if (eoc) {
memset(cmd, 0, MAXCOMMAND);
memset(param, 0, MAXCOMMAND);
int len = strlen(line);
if (len >= 2) {
strncpy(cmd, line, 2);
}
if (len > 2) {
strncpy(param, line + 2, len - 2);
}
memset(line, 0, MAXCOMMAND);
eoc = 0;
idx = 0;
//now execute the command
//Immediately stop any focus motor movement. returns nothing
//code from Quickstop example. This is blocking
if (!strcasecmp(cmd, "FQ")) {
if(!isRunning)
{
turnOn();
}
stepper.stop(); // Stop as fast as possible: sets new target
stepper.runToPosition();
// Now stopped after quickstop
}
//Go to the new position as set by the ":SNYYYY#" command. returns nothing // initiate a move
//turn stepper on and flag it is running
// is this the only command that should actually make the stepper run ?
if (!strcasecmp(cmd, "FG")) {
if(!isRunning)
{
turnOn();
}
}
//Returns the temperature coefficient where XX is a two-digit signed (2’s complement) hex number.
//hardcoded
if (!strcasecmp(cmd, "GC")) {
Serial.print("02#");
}
//Returns the current stepping delay where XX is a two-digit unsigned hex number. See the :SD# command for a list of possible return values.
//hardcoded for now
// might turn this into AccelStepper acceleration at some point
if (!strcasecmp(cmd, "GD")) {
Serial.print("02#");
}
//Returns "FF#" if the focus motor is half-stepped otherwise return "00#"
//hardcoded
if (!strcasecmp(cmd, "GH")) {
Serial.print("00#");
}
//Returns "00#" if the focus motor is not moving, otherwise return "01#",
//AccelStepper returns Positive as clockwise
if (!strcasecmp(cmd, "GI")) {
if (stepper.distanceToGo() == 0) {
Serial.print("00#");
}
else {
Serial.print("01#");
}
}
//Returns the new position previously set by a ":SNYYYY" command where YYYY is a four-digit unsigned hex number.
if (!strcasecmp(cmd, "GN")) {
pos = stepper.targetPosition();
sprintf(tempString, "%04X", pos);
Serial.print(tempString);
Serial.print("#");
}
//Returns the current position where YYYY is a four-digit unsigned hex number.
if (!strcasecmp(cmd, "GP")) {
pos = stepper.currentPosition();
sprintf(tempString, "%04X", pos);
Serial.print(tempString);
Serial.print("#");
}
//Returns the current temperature where YYYY is a four-digit signed (2’s complement) hex number.
if (!strcasecmp(cmd, "GT")) {
Serial.print("0020#");
}
//Get the version of the firmware as a two-digit decimal number where the first digit is the major version number, and the second digit is the minor version number.
//hardcoded
if (!strcasecmp(cmd, "GV")) {
Serial.print("10#");
}
//Set the new temperature coefficient where XX is a two-digit, signed (2’s complement) hex number.
if (!strcasecmp(cmd, "SC")) {
//do nothing yet
}
//Set the new stepping delay where XX is a two-digit,unsigned hex number.
if (!strcasecmp(cmd, "SD")) {
//do nothing yet
}
//Set full-step mode.
if (!strcasecmp(cmd, "SF")) {
//do nothing yet
}
//Set half-step mode.
if (!strcasecmp(cmd, "SH")) {
//do nothing yet
}
//Set the new position where YYYY is a four-digit
if (!strcasecmp(cmd, "SN")) {
pos = hexstr2long(param);
// stepper.enableOutputs(); // turn the motor on here ??
if(!isRunning)
{
turnOn();
}
stepper.moveTo(pos);
}
//Set the current position where YYYY is a four-digit unsigned hex number.
if (!strcasecmp(cmd, "SP")) {
pos = hexstr2long(param);
stepper.setCurrentPosition(pos);
}
}// end if(eoc)
} // end loop
long hexstr2long(char *line) {
long ret = 0;
ret = strtol(line, NULL, 16);
return (ret);
}
void turnOn() {
if (useSleep) {
digitalWrite(powerPin, HIGH);
} else {
digitalWrite(powerPin, LOW);
}
digitalWrite(ledPin, HIGH);
isRunning = true;
powerIsOn = true;
}
void turnOff() {
if (useSleep) {
digitalWrite(powerPin, LOW);
} else {
digitalWrite(powerPin, HIGH);
}
digitalWrite(ledPin, LOW);
isRunning = false;
powerIsOn = false;
}