Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
682 lines (596 loc) · 18.7 KB
/
Copy pathmain.cpp
File metadata and controls
682 lines (596 loc) · 18.7 KB
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#include <Arduino.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include "Axes.h"
#include "Config.h"
#include "Sensors.h"
#include "ServoController.h"
#include "StepperController.h"
Sensors sensors;
StepperController stepperController(sensors);
Axes axes(stepperController, sensors);
ServoController servoController;
// One serial line being assembled (31 characters plus terminator).
// commandOverflowed marks a line that exceeded the buffer; it is dropped at newline.
char commandBuffer[32];
uint8_t commandLength = 0;
bool commandOverflowed = false;
void printCommands();
void readSerialCommands();
void processCommand(const char *command);
const char *skipCommandSeparators(const char *text);
bool onlyBlanksRemain(const char *text);
bool parseLongValue(const char *text, long &value);
bool parseFloatValue(const char *text, float &value);
void executeFMS();
bool executeFullBarrelDeployment();
bool deployPuck(uint8_t puckNumber);
bool waitForFireConfirmation();
void printFireConfirmationPrompt();
void executeYawCommand(char commandLetter, const char *valueText);
bool returnLeadToBottom();
// Starts serial, then sensors, steppers, and servos. Servo begin() moves them
// to rest. No homing is done: the firmware assumes lead at bottom, barrel at
// index 0, yaw at 0 degrees.
void setup()
{
Serial.begin(Config::SERIAL_BAUD);
sensors.begin();
stepperController.begin();
servoController.begin();
Serial.println(
F("HDM Motor and Servo Controller Ready.")
);
printCommands();
}
// Only polls the serial port; all work happens inside commands.
void loop()
{
readSerialCommands();
}
// Drains available serial bytes into commandBuffer. At newline the line is run
// (empty lines ignored) or, if it was too long, discarded with a message.
// '\r' is ignored. Any command runs to completion before reading continues.
void readSerialCommands()
{
while (Serial.available() > 0)
{
char incomingCharacter =
static_cast<char>(Serial.read());
// Ignore carriage return.
if (incomingCharacter == '\r')
{
continue;
}
// Process the command at newline.
if (incomingCharacter == '\n')
{
if (commandOverflowed)
{
Serial.println(F("Command too long; discarded."));
}
else if (commandLength > 0)
{
commandBuffer[commandLength] = '\0';
processCommand(commandBuffer);
}
commandLength = 0;
commandOverflowed = false;
continue;
}
if (!commandOverflowed && commandLength < sizeof(commandBuffer) - 1)
{
commandBuffer[commandLength++] =
incomingCharacter;
}
else
{
commandOverflowed = true;
}
}
}
// Runs one command line. The first character selects the command; the rest is
// its argument. Servo, T and U commands are matched first on the raw character
// (so 'w' and 'e' are lowercase-only); remaining commands are matched after
// uppercasing, so e.g. 's', 'b', 'n', 'p', 'i', 'y', 'r', 'z', 'c', 'x' work.
// Commands block until finished.
void processCommand(const char *command)
{
while (
*command == ' ' ||
*command == '\t'
)
{
command++;
}
if (*command == '\0')
{
return;
}
char cmd = command[0];
// ========================================================
// COMPLETE DEPLOYMENT
// ========================================================
if (cmd == 'Q' || cmd == 'q')
{
executeFMS();
return;
}
if (cmd == 'G' || cmd == 'g')
{
executeFullBarrelDeployment();
return;
}
// ========================================================
// HOME / HELP
// ========================================================
// H: see returnLeadToBottom() (identical to B when a bottom switch is configured).
if (cmd == 'H' || cmd == 'h')
{
returnLeadToBottom();
return;
}
if (cmd == '?')
{
printCommands();
return;
}
// ========================================================
// CASE-SENSITIVE SERVO COMMANDS
// ========================================================
// Lowercase w moves both servos to rest (uppercase W raises the elevator below).
if (cmd == 'w')
{
servoController.reset();
Serial.println(F("Both servos reset."));
return;
}
// Lowercase e moves both servos to the armed position (uppercase E is unknown).
if (cmd == 'e')
{
servoController.arm();
Serial.println(F("Both servos armed."));
return;
}
// M/m moves both servos to fire, holds, returns them to rest, and clears the
// puck latch. Does not move the elevator or check for a puck.
if (cmd == 'M' || cmd == 'm')
{
servoController.fire();
sensors.clearPuckLatch();
Serial.println(F("Both servos fired."));
return;
}
// Prints the lead TMC5160 SPI test result only; barrel/yaw have no test.
if (cmd == 'T' || cmd == 't')
{
stepperController.printConnectionTests(Serial);
return;
}
// Drives the burnwire pin high for 12 s (blocking, no interrupt possible).
// If the pin is unconfigured, nothing is driven and a message says so.
if (cmd == 'U' || cmd == 'u')
{
if (!sensors.hasBurnwireTrigger())
{
Serial.println(F("Burnwire trigger pin is not configured; no pulse sent."));
return;
}
Serial.println(F("Burnwire pulse started."));
sensors.fireBurnwire(12000);
Serial.println(F("Burnwire pulse complete."));
return;
}
char commandLetter =
static_cast<char>(
toupper(
static_cast<unsigned char>(cmd)
)
);
// Text after the command letter, parsed as the numeric argument by I, Y, R.
const char *valueText = command + 1;
switch (commandLetter) {
// Lead screw: one puck level up/down, 1 mm jogs, and B = full-travel
// descent that ignores the tracked position (stops early on bottom switch).
case 'W': axes.moveLeadUpOnePuck(); break;
case 'S': axes.moveLeadDownOnePuck(); break;
case '+': axes.moveLeadUpOneMillimeter(); break;
case '-': axes.moveLeadDownOneMillimeter(); break;
case 'B': axes.moveLeadToBottomFullDown(); break;
// Barrel: next/previous index, wrapping around.
case 'N': axes.moveBarrelToNextIndex(); break;
case 'P': axes.moveBarrelToPreviousIndex(); break;
// I<n>: barrel to index n; rejects unparsable or out-of-range n.
case 'I':
{
long requestedIndex = 0;
if (
!parseLongValue(
valueText,
requestedIndex
)
)
{
Serial.println(
F("Invalid index. Example: I5")
);
break;
}
if (
requestedIndex < 0 ||
requestedIndex >= Config::BARREL_POSITION_COUNT
)
{
Serial.print(F("Barrel index must be between 0 and "));
Serial.print(Config::BARREL_POSITION_COUNT - 1);
Serial.println(F("."));
break;
}
axes.moveBarrelToIndex(
static_cast<int>(requestedIndex)
);
break;
}
// ======================================================
// YAW
// ======================================================
case 'Y':
case 'R':
case 'Z':
executeYawCommand(commandLetter, valueText);
break;
// ======================================================
// STATUS
// ======================================================
case 'C':
axes.printStatus(Serial);
servoController.printStatus(Serial);
sensors.printStatus(Serial);
break;
// ======================================================
// STOP / DISABLE
// ======================================================
// X as a typed command only runs between moves: it sets STEP pins low and
// drives any configured enable pins to disable. Stopping a move already in
// progress is handled inside StepperController (see emergencyStopRequested).
case 'X':
if (stepperController.disableAllMotors())
{
Serial.println(F("All stepper drivers disabled."));
}
else
{
Serial.println(F("All step pulses stopped; holding torque may remain on because an enable pin is not configured."));
}
break;
default:
Serial.println(F("Unknown command."));
printCommands();
break;
}
}
// Skips spaces, tabs, ':' and '=' between a command letter and its number.
const char *skipCommandSeparators(const char *text)
{
while (
*text == ' ' ||
*text == '\t' ||
*text == ':' ||
*text == '='
)
{
text++;
}
return text;
}
// True if nothing but spaces/tabs remains in text.
bool onlyBlanksRemain(const char *text)
{
while (*text == ' ' || *text == '\t')
{
text++;
}
return *text == '\0';
}
// Parses a base-10 integer that must be the whole remaining text (trailing
// spaces/tabs allowed). Returns false, leaving value unchanged, if the text is
// empty, not a number, out of range, or has trailing characters.
bool parseLongValue(const char *text, long &value)
{
text = skipCommandSeparators(text);
char *endPointer = nullptr;
errno = 0;
const long parsedValue = strtol(text, &endPointer, 10);
if (endPointer == text || errno == ERANGE || !onlyBlanksRemain(endPointer))
{
return false;
}
value = parsedValue;
return true;
}
// Same rules as parseLongValue, for floats. Also rejects NaN/infinity and
// values that overflow a float.
bool parseFloatValue(const char *text, float &value)
{
text = skipCommandSeparators(text);
char *endPointer = nullptr;
errno = 0;
const double parsedValue = strtod(text, &endPointer);
if (
endPointer == text ||
errno == ERANGE ||
!isfinite(parsedValue) ||
!onlyBlanksRemain(endPointer)
)
{
return false;
}
const float parsedFloat = static_cast<float>(parsedValue);
if (!isfinite(parsedFloat))
{
return false;
}
value = parsedFloat;
return true;
}
// Deploy the aligned chamber first, then advance between chambers.
void executeFMS()
{
for (uint8_t barrel = 0; barrel < Config::BARREL_POSITION_COUNT; ++barrel)
{
if (!executeFullBarrelDeployment())
{
Serial.println(F("Barrel deployment did not complete; FMS stopped."));
return;
}
if (barrel + 1 < Config::BARREL_POSITION_COUNT && !axes.moveBarrelToNextIndex())
{
Serial.println(F("Barrel indexing failed; FMS stopped."));
return;
}
}
Serial.println(F("FMS deployment complete."));
}
// G command. From the bottom, raise to puck level 1, then per puck 1..PUCK_COUNT:
// raise +PUCK_HEIGHT_MM (plus the deployment offset on the first puck)
// -> arm servos -> check puck latch -> lower PRE_FIRE mm
// -> wait for operator confirmation -> fire -> raise POST_FIRE mm.
// The next puck's +PUCK_HEIGHT_MM raise starts from the realigned position.
// A failed lead move stops the whole run (servos reset in most cases).
// With a puck switch, a level with no latched puck is skipped, not fired.
// Without a puck switch, a puck is assumed present at every level.
bool executeFullBarrelDeployment() {
// Start from the bottom; abort unless the lead level is known to be 0.
if (!returnLeadToBottom()) {
servoController.reset();
Serial.println(F("Unable to return the elevator to its bottom reference; deployment cancelled."));
return false;
}
if (!sensors.hasPuckSwitch()) {
Serial.println(F("Puck switch is not configured; puck presence will not be verified."));
}
for (uint8_t puckNumber = 1; puckNumber <= Config::PUCK_COUNT; ++puckNumber) {
if (!deployPuck(puckNumber))
return false;
}
if (!returnLeadToBottom()) {
Serial.println(F("Full deployment finished, but the elevator did not reach the bottom reference."));
return false;
}
return true;
}
bool deployPuck(uint8_t puckNumber)
{
sensors.clearPuckLatch();
if (puckNumber == 1 && !axes.moveLeadToPuckLevel(1)) {
Serial.println(F("Unable to raise to the first puck level; full deployment stopped."));
servoController.reset();
return false;
}
const float raiseMillimeters = Config::PUCK_HEIGHT_MM +
(puckNumber == 1 ? Config::FULL_DEPLOYMENT_HEIGHT_OFFSET_MM : 0.0f);
if (!axes.moveLeadRelativeMillimeters(raiseMillimeters)) {
Serial.println(F("Unable to raise the puck to firing height; full deployment stopped."));
servoController.reset();
return false;
}
servoController.arm();
sensors.updatePuckLatch();
if (sensors.hasPuckSwitch() && !sensors.isPuckLatched()) {
Serial.print(F("No puck detected for level "));
Serial.print(puckNumber);
Serial.println(F("; skipping deployment."));
servoController.reset();
return true;
}
if (!axes.moveLeadRelativeMillimeters(-Config::FULL_DEPLOYMENT_PRE_FIRE_LOWER_MM)) {
Serial.println(F("Unable to position the puck for firing; full deployment stopped."));
servoController.reset();
return false;
}
if (!waitForFireConfirmation()) {
servoController.reset();
if (!returnLeadToBottom())
Serial.println(F("Unable to return the elevator to its bottom reference."));
Serial.println(F("Deployment cancelled."));
return false;
}
Serial.println(F("Firing servos..."));
servoController.fire();
sensors.clearPuckLatch();
if (!axes.moveLeadRelativeMillimeters(Config::FULL_DEPLOYMENT_POST_FIRE_RAISE_MM)) {
Serial.println(F("Unable to finish the puck cycle; full deployment stopped."));
return false;
}
return true;
}
// Wait for an entire line; yaw commands return to this prompt without firing.
bool waitForFireConfirmation()
{
char response[sizeof(commandBuffer)];
uint8_t length = 0;
bool overflowed = false;
bool invalid = false;
printFireConfirmationPrompt();
while (true)
{
if (Serial.available() == 0)
{
continue;
}
char responseCharacter =
static_cast<char>(Serial.read());
if (responseCharacter == '\r')
{
continue;
}
if (responseCharacter != '\n')
{
if (responseCharacter == '\0')
{
invalid = true;
continue;
}
if (!overflowed && length < sizeof(response) - 1)
response[length++] = responseCharacter;
else
overflowed = true;
continue;
}
if (invalid)
{
Serial.println(F("Invalid confirmation; cancelling deployment."));
return false;
}
else if (overflowed)
{
Serial.println(F("Command too long; discarded."));
}
else
{
response[length] = '\0';
const char *text = response;
while (*text == ' ' || *text == '\t')
++text;
if (*text == '\0')
return true;
const char commandCharacter = *text;
const char letter = static_cast<char>(toupper(static_cast<unsigned char>(*text)));
// Preserve the existing bare Y confirmation, but never treat a malformed
// yaw command as permission to fire.
if (letter == 'Y' && onlyBlanksRemain(text + 1))
return true;
if ((commandCharacter == '+' || commandCharacter == '-') &&
onlyBlanksRemain(text + 1))
{
axes.moveLeadRelativeMillimeters(commandCharacter == '+' ? 1.0f : -1.0f);
}
else if (commandCharacter == 'w' && onlyBlanksRemain(text + 1))
{
servoController.reset();
Serial.println(F("Servos reset."));
}
else if (commandCharacter == 'e' && onlyBlanksRemain(text + 1))
{
servoController.arm();
Serial.println(F("Servos armed."));
}
else if (letter == 'N' && onlyBlanksRemain(text + 1))
{
return false;
}
else if (letter != 'Y' && letter != 'R' && letter != 'Z')
{
return false;
}
else if (letter == 'Z' && !onlyBlanksRemain(text + 1))
{
Serial.println(F("Invalid yaw zero command. Use Z."));
}
else
{
executeYawCommand(letter, text + 1);
}
}
length = 0;
overflowed = false;
invalid = false;
printFireConfirmationPrompt();
}
}
// Prints the available actions while preserving the active firing prompt.
void printFireConfirmationPrompt()
{
Serial.println(F("Press Enter to fire, or type N to cancel."));
Serial.println(F("Type Y<angle>, R<angle>, or Z to adjust yaw."));
Serial.println(F("Type + or - to adjust lead by 1 mm; w resets servos; e arms them."));
}
// Handles yaw commands in both the normal command parser and the fire prompt.
void executeYawCommand(char commandLetter, const char *valueText)
{
if (commandLetter == 'Z')
{
axes.setYawPositionAsZero();
return;
}
float value = 0.0f;
if (!parseFloatValue(valueText, value))
{
if (commandLetter == 'Y')
Serial.println(F("Invalid yaw angle. Example: Y12.5"));
else
Serial.println(F("Invalid relative yaw. Example: R0.25"));
return;
}
if (commandLetter == 'Y')
axes.moveYawToDegrees(value);
else
axes.moveYawRelativeDegrees(value);
}
// Used by H and by full deployment.
// - Bottom switch configured: runs moveLeadToBottomFullDown(), i.e. the same
// full-travel descent as B, stopping early on the switch.
// - No bottom switch: moves to the tracked position 0 only (no extra travel).
bool returnLeadToBottom()
{
if (sensors.hasBottomSwitch())
{
return axes.moveLeadToBottomFullDown();
}
else
{
Serial.println(F("Bottom limit switch is not configured; using the software bottom reference."));
axes.moveLeadToBottom();
return axes.getLeadPuckLevel() == 0;
}
}
// Prints a fixed command list. It is static text, so it must be updated by hand
// when commands change. Case rules are not shown: only 'w' and 'e' are
// lowercase-only; most other letters also accept the opposite case.
void printCommands()
{
Serial.println();
Serial.println(F("========== COMMANDS =========="));
Serial.println(F(" Q / q Run entire FMS (every barrel)"));
Serial.println(F(" G / g Run full deployment (raises pucks, Enter to fire each)"));
Serial.println(F(" H / h Return lead screw to configured bottom"));
Serial.println(F(" w Reset servos"));
Serial.println(F(" e Arm servos"));
Serial.println(F(" m / M Fire both servos"));
Serial.println(F(" W / S Raise / Lower elevator 1 puck"));
Serial.println(F(" + / - Move lead screw up / down 1 mm"));
Serial.println(F(" N / P Move barrel to next / previous index"));
Serial.println(F(" B Drive lead screw down for the full travel"));
Serial.println(F(" I5 Move barrel directly to index 5"));
Serial.println(F(" Y12.5 Move absolute yaw angle"));
Serial.println(F(" R0.25 Move relative yaw angle"));
Serial.println(F(" Z Set current yaw position as zero"));
Serial.println(F(" C Print system status"));
Serial.println(F(" T / t Test SPI connection for TMC5160 drivers"));
Serial.println(F(" X Stop step pulses; disable configured drivers"));
Serial.println(F(" U Pulse burnwire for 12 seconds"));
Serial.println(F(" ? Show this command list"));
Serial.println(F("=============================="));
}