-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrolloff.ino.boutons
494 lines (448 loc) · 15 KB
/
rolloff.ino.boutons
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*Program to open the roof of my Observatory.
With 2 buttons Open,Close and an Emergency Stop.
With realy supply power
There are also 2 limit switches and a control as the telescope and of course the park position.*/
/*********************************/
#define BAUD_RATE 38400
#define MAX_INPUT 45
#define MAX_RESPONSE 127
#define MAX_MESSAGE 63
#define MAX_INACTIVE_TIME 5 // 20 minutes of inactivity from roof driver before auto shutdown (maximum is 16 hours)
/*********************************/
const int ledRepos = 13; // LedWaiting output
const int RelaisOuverture1 = 4; // Open relay1 output2
const int RelaisOuverture2 = 5; // Open relay2 output3
const int RelaisFermeture1 = 6; // Close relay1 output4
const int RelaisFermeture2 = 7; // Close relay2 output5
const int RelaisAlimTelescope = 8; // Power I-Optron output6
const int RelaisAlimentation = 9; // Power supply relay output7
const int BoutonOpen = A0; // Open button input
const int BoutonClose = A1; // Close button input
const int BoutonStop = A2; // Stop button input
const int FinOuverture = A3; // limit switche Open input
const int FinFermeture = A4; // limit switche Close input
const int Telescope_Parc = A5; // park position switche input
const int BoutonRelais = 10; // Power supply control input
const int FinOuvertureSec= 11; // limit switche Open security input
const int FinFermetureSec= 12; // limit switche Close security input
int BoutonOpenState = 0;
int BoutonCloseState = 0;
int FinOuvertureState = LOW;
int FinOuvertureSecState = LOW;
int FinFermetureState = LOW;
int FinFermetureSecState = LOW;
int Telescope_ParcState = LOW;
int BoutonStopState = 0;
bool BoutonRelaisState = HIGH; // Changed
//les états possible
enum {Repos, Ouverture, Ouvert, Fermeture, Fermer}; // At Rest, Opening. Open, Closing, Closed
int etat = Repos;
bool estAlimente = false;
/*********************************/
const int cLen = 15;
const int tLen = 15;
const int vLen = MAX_RESPONSE;
char command[cLen + 1];
char target[tLen + 1];
char value[vLen + 1];
bool remotePowerRequest = false;
bool timerActive = false;
unsigned long t_seconds = 0; // seconds accumulated since last activity
unsigned long t_millisec = 0; // milli-seconds accumulated since last checked
unsigned long t_prev = 0; // milli-second count when last checked
// Maximum length of messages = 63
const char* ERROR1 = "The controller response message was too long";
const char* ERROR2 = "The controller failure message was too long";
const char* ERROR3 = "Command input request is too long";
const char* ERROR4 = "Invalid command syntax, both start and end tokens missing";
const char* ERROR5 = "Invalid command syntax, no start token found";
const char* ERROR6 = "Invalid command syntax, no end token found";
const char* ERROR7 = "Roof controller unable to parse command";
const char* ERROR8 = "Command must map to either set a relay or get a switch";
const char* ERROR9 = "Request not implemented in controller";
const char* ERROR10 = "Abort command ignored, roof already stationary";
const char* ERROR11 = "Observatory power is off, command ignored";
const char* INFO_1 = "V1.1-1";
void sendAck(char* val)
{
char response [MAX_RESPONSE];
if (strlen(val) > MAX_MESSAGE)
sendNak(ERROR1);
else
{
strcpy(response, "(ACK:");
strcat(response, target);
strcat(response, ":");
strcat(response, val);
strcat(response, ")");
if (Serial.availableForWrite() > 0)
{
Serial.println(response);
Serial.flush();
}
}
}
void sendNak(const char* errorMsg)
{
char buffer[MAX_RESPONSE];
if (strlen(errorMsg) > MAX_MESSAGE)
sendNak(ERROR2);
else
{
strcpy(buffer, "(NAK:ERROR:");
strcat(buffer, value);
strcat(buffer, ":");
strcat(buffer, errorMsg);
strcat(buffer, ")");
if (Serial.availableForWrite() > 0)
{
Serial.println(buffer);
Serial.flush();
}
}
}
bool parseCommand() // (command:target:value)
{
bool start = false;
bool eof = false;
int recv_count = 0;
int wait = 0;
int offset = 0;
char startToken = '(';
char endToken = ')';
const int bLen = MAX_INPUT;
char inpBuf[bLen + 1];
memset(inpBuf, 0, sizeof(inpBuf));
memset(command, 0, sizeof(command));
memset(target, 0, sizeof(target));
memset(value, 0, sizeof(value));
while (!eof && (wait < 20))
{
if (Serial.available() > 0)
{
Serial.setTimeout(1000);
recv_count = Serial.readBytes((inpBuf + offset), 1);
if (recv_count == 1)
{
offset++;
if (offset >= MAX_INPUT)
{
sendNak(ERROR3);
return false;
}
if (inpBuf[offset - 1] == startToken)
{
start = true;
}
if (inpBuf[offset - 1] == endToken)
{
eof = true;
inpBuf[offset] = '\0';
}
continue;
}
}
wait++;
delay(100);
}
if (!start || !eof)
{
if (!start && !eof)
sendNak(ERROR4);
else if (!start)
sendNak(ERROR5);
else if (!eof)
sendNak(ERROR6);
return false;
}
else
{
strcpy(command, strtok(inpBuf, "(:"));
strcpy(target, strtok(NULL, ":"));
strcpy(value, strtok(NULL, ")"));
if ((strlen(command) >= 3) && (strlen(target) >= 1) && (strlen(value) >= 1))
{
return true;
}
else
{
sendNak(ERROR7);
return false;
}
}
}
boolean inactivityCheck()
{
unsigned long t_curr = millis();
if (t_curr >= t_prev) // Accumulate millseconds, ignore overflow
t_millisec += (t_curr - t_prev);
t_prev = t_curr;
t_seconds += t_millisec/1000; // Add to seconds accumulated
t_millisec = (t_millisec%1000); // Retain remainder
if (t_seconds >= 60UL * MAX_INACTIVE_TIME)
{
t_seconds = 0;
t_millisec = 0;
return true;
}
return false;
}
void readUSB()
{
// See if there is input available from host, read and parse it.
if (Serial && (Serial.available() > 0))
{
if (parseCommand())
{
bool connecting = false;
bool powerOff = false;
bool found = true;
t_seconds = 0;
t_millisec = 0;
t_prev = millis();
if (strcmp(command, "CON") == 0)
{
if (!estAlimente)
{
remotePowerRequest = true;
}
timerActive = true; // Whether power turned on manually, from a prior session or auto, When connected timer will run
connecting = true;
strcpy(value, INFO_1); // To indicate the version of the Arduino code
}
// Map the general input command term to the local action to be taken
// SET: OPEN, CLOSE, ABORT, AUXSET
else if (strcmp(command, "SET") == 0)
{
if (strcmp(target, "AUXSET") == 0) // Relais Alimentation
{
if (strcmp(value, "ON") == 0)
{
timerActive = true;
if (!estAlimente)
remotePowerRequest = true;
}
else
{
timerActive = false;
if (estAlimente)
remotePowerRequest = true;
}
}
else
{
if (!estAlimente)
{
powerOff = true;
}
// Power is on
else
{
if (strcmp(target, "OPEN") == 0) // BoutonOpenState
{
BoutonOpenState = LOW;
}
else if (strcmp(target, "CLOSE") == 0) // BoutonCloseState
{
BoutonCloseState = LOW;
}
else if (strcmp(target, "ABORT") == 0) // BoutonStopState
{
BoutonStopState = LOW;
}
else
{
found = false;
}
} // End power is on
} // End roof movement commands
} // End set commands
else
{
// Handle requests to obtain the status of switches
// GET: OPENED, CLOSED, LOCKED, AUXSTATE
strcpy(value, "OFF");
if (strcmp(command, "GET") == 0)
{
if (strcmp(target, "OPENED") == 0) // FinOuvertureState
{
if (FinOuvertureState == LOW)
strcpy(value, "ON");
}
else if (strcmp(target, "CLOSED") == 0) // FinFermetureState
{
if (FinFermetureState == LOW)
strcpy(value, "ON");
}
else if (strcmp(target, "LOCKED") == 0) // Telescope_ParcState
{
if ((Telescope_ParcState == HIGH) || !estAlimente) // Unsafe telescope or power off, either could indicate a lock preventing open/close commands
strcpy(value, "ON");
}
else if (strcmp(target, "AUXSTATE") == 0) // Relais Alimentation
{
if (estAlimente)
strcpy(value, "ON");
}
else
{
found = false;
}
} // End GET command
else
{
found = false; // Not a known command
}
} // End looking for commands
/*
See if the command was recognized
*/
if (!connecting && !found)
{
sendNak(ERROR8);
}
else if (powerOff)
{
sendNak(ERROR11);
}
else
{
sendAck(value); // Found a CON, SET or GET
}
} // end of parseCommand
} // end look for USB input
}
/*********************************/
// le N° des connection et fonction
void setup() {
pinMode(ledRepos, OUTPUT);
pinMode(RelaisOuverture1, OUTPUT);
pinMode(RelaisOuverture2, OUTPUT);
pinMode(RelaisFermeture1, OUTPUT);
pinMode(RelaisFermeture2, OUTPUT);
pinMode(RelaisAlimentation, OUTPUT);
pinMode(RelaisAlimTelescope, OUTPUT);
pinMode(BoutonOpen, INPUT_PULLUP);
pinMode(BoutonClose, INPUT_PULLUP);
pinMode(BoutonStop, INPUT_PULLUP);
pinMode(FinOuverture, INPUT_PULLUP);
pinMode(FinOuvertureSec,INPUT_PULLUP);
pinMode(FinFermeture, INPUT_PULLUP);
pinMode(FinFermetureSec,INPUT_PULLUP);
pinMode(Telescope_Parc, INPUT_PULLUP);
pinMode(BoutonRelais, INPUT_PULLUP);
Serial.begin(BAUD_RATE); // Establish serial port. Baud rate to match that in the driver
}
// fin setup
void position_toit () { // début des statuts pour le loop
switch (etat) { // début du switch avec les possibilté
case Repos: // At Rest
digitalWrite(ledRepos, HIGH); // LedWaiting
digitalWrite(RelaisOuverture1, LOW);
digitalWrite(RelaisOuverture2, LOW);
digitalWrite(RelaisFermeture1, LOW);
digitalWrite(RelaisFermeture2, LOW);
if (BoutonOpenState == LOW && Telescope_ParcState == LOW ) {
etat = Ouverture;
delay(30);
}
else if (BoutonCloseState == LOW && FinFermetureState == HIGH ) {
etat = Fermeture;
delay(30);
}
break;
case Ouverture: // Opening
digitalWrite(ledRepos, LOW); // LedWaiting
digitalWrite(RelaisOuverture1, HIGH);
digitalWrite(RelaisOuverture2, HIGH);
digitalWrite(RelaisFermeture1, LOW);
digitalWrite(RelaisFermeture2, LOW);
digitalWrite(RelaisAlimTelescope, LOW);
if (FinOuvertureState == LOW || FinOuvertureSecState == LOW ) {
etat = Ouvert;
delay(30);
}
else if (Telescope_ParcState == HIGH || BoutonStopState == LOW) {
etat = Repos;
delay(30);
}
break;
case Ouvert: // Open
digitalWrite(ledRepos, HIGH); // LedWaiting
digitalWrite(RelaisOuverture1, LOW);
digitalWrite(RelaisOuverture2, LOW);
digitalWrite(RelaisFermeture1, LOW);
digitalWrite(RelaisFermeture2, LOW);
digitalWrite(RelaisAlimTelescope, HIGH);
digitalWrite(RelaisAlimentation, HIGH); // sécuriter pour pas perdre l'alimentation au cas ou
if (BoutonCloseState == LOW && Telescope_ParcState == LOW ) {
etat = Fermeture;
delay(30);
}
break;
case Fermeture: // Closing
digitalWrite(ledRepos, LOW); // LedWaiting
digitalWrite(RelaisOuverture1, LOW);
digitalWrite(RelaisOuverture2, LOW);
digitalWrite(RelaisFermeture1, HIGH);
digitalWrite(RelaisFermeture2, HIGH);
digitalWrite(RelaisAlimTelescope, LOW);
if (FinFermetureState == LOW || FinFermetureSecState == LOW) {
etat = Fermer;
delay(30);
}
else if (Telescope_ParcState == HIGH || BoutonStopState == LOW) {
etat = Repos;
delay(30);
}
break;
case Fermer: // Closed
digitalWrite(ledRepos, LOW); // LedWaiting
digitalWrite(RelaisOuverture1, LOW);
digitalWrite(RelaisOuverture2, LOW);
digitalWrite(RelaisFermeture1, LOW);
digitalWrite(RelaisFermeture2, LOW);
digitalWrite(RelaisAlimTelescope, LOW);
etat = Repos;
break;
} //fin switch
}// fin void
void loop()
{
// Handle any change request to power state
BoutonRelaisState = digitalRead (BoutonRelais);
if (BoutonRelaisState == LOW)
timerActive = false; // If local power button is used disable remote auto power off timer
if ((BoutonRelaisState == HIGH) && !estAlimente) // if power not pressed, and power is off, check for remote power on.
if (Serial && Serial.available() > 0)
readUSB(); // When power is on the readUSB below will catch any remote power request
if ((BoutonRelaisState == LOW) || remotePowerRequest) { // If either local or remote power request, change the power relay state
remotePowerRequest = false;
if (estAlimente == true) {
estAlimente = false;
digitalWrite (RelaisAlimentation, LOW); // Power off
}
else {
estAlimente = true;
digitalWrite (RelaisAlimentation, HIGH); // Power on
}
delay (250);
}
// début du programme des fonctions
if (estAlimente) {
BoutonOpenState = digitalRead (BoutonOpen) ;
BoutonCloseState = digitalRead (BoutonClose) ;
FinOuvertureState = digitalRead (FinOuverture) ;
FinOuvertureSecState = digitalRead (FinOuvertureSec) ;
FinFermetureState = digitalRead (FinFermeture) ;
FinFermetureSecState = digitalRead (FinFermetureSec) ;
Telescope_ParcState = digitalRead (Telescope_Parc);
BoutonStopState = digitalRead (BoutonStop);
if (timerActive)
remotePowerRequest = inactivityCheck();
if (Serial && Serial.available() > 0) // Check for remote power off
readUSB();
position_toit() ;
}
delay(250); // 0.25 second loop
}// fin loop ou programme