forked from scottbez1/splitflap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Splitflap.ino
518 lines (455 loc) · 13.8 KB
/
Splitflap.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
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
/*
Copyright 2020 Scott Bezek and the splitflap contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//***** READ ME FIRST *****//
//
// Set up the configuration in config.h
//
// We highly recommend using PlatformIO (via Visual Studio Code) rather than
// the Arduino IDE for this project.
//
// Although PlatformIO may be slightly more complicated to learn at first, it
// pays for itself quickly by making library management and board management
// automatic, and is generally a far more powerful development environment.
//
// While we strive to maintain Arduino IDE support for this project, all active
// development is being done with PlatformIO, so Arduino IDE compatibility may
// be inadvertantly broken from time to time. Please open an issue on Github
// if you run into problems with the Arduino IDE.
//
//*************************//
#include <Arduino.h>
#include <Wire.h>
#include "config.h"
#include "src/splitflap_module.h"
#if SPI_IO
#include "src/spi_io_config.h"
#else
#include "src/basic_io_config.h"
#endif
#if NEOPIXEL_DEBUGGING_ENABLED
#include <Adafruit_NeoPixel.h>
#endif
#if SSD1306_DISPLAY
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSans12pt7b.h>
#endif
#if INA219_POWER_SENSE
#include "src/Adafruit_INA219.h"
#endif
#ifdef __AVR__
#define FAVR(x) F(x)
#else
#define FAVR(x) x
#endif
int recv_buffer[NUM_MODULES];
#if NEOPIXEL_DEBUGGING_ENABLED
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_MODULES, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
uint32_t color_green = strip.Color(0, 30, 0);
uint32_t color_red = strip.Color(100, 0, 0);
uint32_t color_purple = strip.Color(15, 0, 15);
uint32_t color_orange = strip.Color(30, 7, 0);
#endif
#if SSD1306_DISPLAY
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
char labelString[NUM_MODULES + 1];
char statusString[NUM_MODULES + 1];
char displayBuffer[200];
char debugMessage[100];
#endif
#if INA219_POWER_SENSE
Adafruit_INA219 powerSense;
// Latest current reading
float currentmA;
uint32_t lastCurrentReadMillis = 0;
// Char buffers for building voltage/current strings
char voltageBuf[10];
char currentBuf[10];
#endif
void dump_status(void);
void setup() {
Serial.begin(MONITOR_SPEED);
initialize_modules();
motor_sensor_io();
#ifdef OUTPUT_ENABLE_PIN
// Wait until after 1 motor_sensor_io to turn on shift register outputs
pinMode(OUTPUT_ENABLE_PIN, OUTPUT);
digitalWrite(OUTPUT_ENABLE_PIN, LOW);
#endif
#if NEOPIXEL_DEBUGGING_ENABLED
strip.begin();
strip.show();
// Pulse neopixels for fun
uint8_t vals[] = {0, 1, 3, 10, 16, 20, 24, 28, 28, 24, 20, 16, 10, 3, 1, 0};
for (int16_t i = 0; i < 400; i++) {
for (int j = 0; j < NUM_MODULES; j++) {
int8_t index = i / 8 - j;
if (index < 0) {
index = 0;
} else if (index > 15) {
index = index % 16;
}
strip.setPixelColor(j, vals[index] * 2, vals[index] * 2, 0);
}
strip.show();
delay(3);
}
#endif
#if SSD1306_DISPLAY
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.setTextWrap(false);
display.setTextColor(SSD1306_WHITE);
display_large_text("Splitflap");
delay(500);
display_large_text("calibrating");
debugMessage[0] = '\0';
for (uint8_t i = 0; i < NUM_MODULES; i++) {
labelString[i] = 'A' + i;
}
labelString[NUM_MODULES] = '\0';
#endif
#if !NEOPIXEL_DEBUGGING_ENABLED && !SSD1306_DISPLAY
pinMode(LED_BUILTIN, OUTPUT);
// Pulse the builtin LED - not as fun but indicates that we're running
for (int i = 0; i < 11; i++) {
digitalWrite(LED_BUILTIN, i % 2 ? HIGH : LOW);
delay(100);
}
#endif
Serial.print("\n\n\n");
Serial.print(FAVR("{\"type\":\"init\", \"num_modules\":"));
Serial.print(NUM_MODULES);
Serial.print(FAVR(", \"character_list\":\""));
for(uint8_t i = 0; i < NUM_FLAPS; i++) {
Serial.print((char)flaps[i]);
}
Serial.print(FAVR("\"}\n"));
for (uint8_t i = 0; i < NUM_MODULES; i++) {
recv_buffer[i] = 0;
modules[i]->Init();
#if !SENSOR_TEST
modules[i]->GoHome();
#endif
}
#if INA219_POWER_SENSE
powerSense.begin();
Wire.setClock(400000);
#endif
}
inline int8_t FindFlapIndex(uint8_t character, uint8_t current_flap_index) {
for (uint8_t i = current_flap_index; i < NUM_FLAPS; i++) {
if (character == flaps[i]) {
return i;
}
}
for (uint8_t i = 0; i < current_flap_index; i++) {
if (character == flaps[i]) {
return i;
}
}
return -1;
}
bool pending_move_response = true;
bool pending_no_op = false;
uint8_t recv_count = 0;
bool disabled = false;
void disableAll(char* message) {
for (uint8_t i = 0; i < NUM_MODULES; i++) {
modules[i]->Disable();
}
motor_sensor_io();
if (disabled) {
return;
}
disabled = true;
Serial.println("#### DISABLED! ####");
Serial.println(message);
#if SSD1306_DISPLAY
snprintf(debugMessage, sizeof(debugMessage), "### DISABLED! ###\n%s", message);
#endif
}
boolean was_stopped = false;
uint32_t stopped_at_millis = 0;
inline void run_iteration() {
uint32_t iterationStartMillis = millis();
boolean all_idle = true;
boolean all_stopped = true;
for (uint8_t i = 0; i < NUM_MODULES; i++) {
modules[i]->Update();
bool is_idle = modules[i]->state == PANIC
|| modules[i]->state == STATE_DISABLED
|| modules[i]->state == LOOK_FOR_HOME
|| modules[i]->state == SENSOR_ERROR
|| (modules[i]->state == NORMAL && modules[i]->current_accel_step == 0);
bool is_stopped = modules[i]->state == PANIC
|| modules[i]->state == STATE_DISABLED
|| modules[i]->current_accel_step == 0;
all_idle &= is_idle;
all_stopped &= is_stopped;
// if (i & 0b11) motor_sensor_io();
}
if (all_stopped && !was_stopped) {
stopped_at_millis = iterationStartMillis;
}
was_stopped = all_stopped;
motor_sensor_io();
#if INA219_POWER_SENSE
if (iterationStartMillis - lastCurrentReadMillis > 100) {
currentmA = powerSense.getCurrent_mA();
if (currentmA > NUM_MODULES * 250) {
disableAll("Over current");
} else if (all_stopped && iterationStartMillis - stopped_at_millis > 100 && currentmA >= 3) {
disableAll("Unexpected current");
}
lastCurrentReadMillis = iterationStartMillis;
}
#endif
if (all_idle) {
#if NEOPIXEL_DEBUGGING_ENABLED
for (int i = 0; i < NUM_MODULES; i++) {
uint32_t color = 0;
switch (modules[i]->state) {
case NORMAL:
color = color_green;
break;
case LOOK_FOR_HOME:
color = color_purple;
break;
case SENSOR_ERROR:
color = color_orange;
break;
case PANIC: // Intentional fall-through
case STATE_DISABLED:
color = color_red;
break;
}
strip.setPixelColor(i, color);
}
strip.show();
#endif
#if SSD1306_DISPLAY
if (all_stopped) {
for (int i = 0; i < NUM_MODULES; i++) {
uint32_t color;
switch (modules[i]->state) {
case NORMAL:
statusString[i] = '_';
break;
case LOOK_FOR_HOME:
statusString[i] = 'H';
break;
case SENSOR_ERROR:
statusString[i] = 'E';
break;
case STATE_DISABLED:
statusString[i] = 'D';
break;
case PANIC:
statusString[i] = '!';
break;
default:
statusString[i] = '?';
break;
}
}
statusString[NUM_MODULES] = '\0';
display.clearDisplay();
if (!disabled) {
display.setCursor(0, 0);
display.println(labelString);
display.println(statusString);
}
#if INA219_POWER_SENSE
float voltage = powerSense.getBusVoltage_V();
if (voltage > 14) {
disableAll("Over voltage");
} else if (voltage < 10) {
disableAll("Under voltage");
}
dtostrf(voltage, 6, 3, voltageBuf);
snprintf(displayBuffer, sizeof(displayBuffer), "%sV", voltageBuf);
display.setCursor(84, 0);
display.print(displayBuffer);
dtostrf(currentmA / 1000, 6, 3, currentBuf);
snprintf(displayBuffer, sizeof(displayBuffer), "%sA", currentBuf);
display.setCursor(84, 8);
display.print(displayBuffer);
#endif
display.setCursor(0, 16);
display.print(debugMessage);
if ((millis() >> 7) & 1) {
display.drawPixel(127, 31, SSD1306_WHITE);
}
display.display();
}
#endif
if (pending_no_op && all_stopped) {
Serial.print(FAVR("{\"type\":\"no_op\"}\n"));
Serial.flush();
pending_no_op = false;
}
if (pending_move_response && all_stopped) {
pending_move_response = false;
dump_status();
}
while (Serial.available() > 0) {
int b = Serial.read();
switch (b) {
case '@':
#if SSD1306_DISPLAY
if (all_stopped) {
display_large_text("calibrating");
}
#endif
for (uint8_t i = 0; i < NUM_MODULES; i++) {
modules[i]->ResetErrorCounters();
modules[i]->GoHome();
}
break;
case '#':
pending_no_op = true;
break;
case '=':
recv_count = 0;
break;
case '\n':
pending_move_response = true;
Serial.print(FAVR("{\"type\":\"move_echo\", \"dest\":\""));
Serial.flush();
#if SSD1306_DISPLAY
if (all_stopped) {
display_large_text("moving...");
}
#endif
for (uint8_t i = 0; i < recv_count; i++) {
int8_t index = FindFlapIndex(recv_buffer[i], modules[i]->GetCurrentFlapIndex());
if (index != -1) {
if (FORCE_FULL_ROTATION || index != modules[i]->GetTargetFlapIndex()) {
modules[i]->GoToFlapIndex(index);
}
}
Serial.write(recv_buffer[i]);
if (i % 8 == 0) {
Serial.flush();
}
}
Serial.print(FAVR("\"}\n"));
Serial.flush();
break;
default:
if (recv_count > NUM_MODULES - 1) {
break;
}
recv_buffer[recv_count] = b;
recv_count++;
break;
}
}
}
}
void sensor_test_iteration() {
motor_sensor_io();
Serial.println();
#if NEOPIXEL_DEBUGGING_ENABLED
for (uint8_t i = 0; i < NUM_MODULES; i++) {
uint32_t color;
if (!modules[i]->GetHomeState()) {
color = color_green;
} else {
color = color_purple;
}
Serial.print(modules[i]->GetHomeState() ? '0' : '1');
// Make LEDs flash in sequence to indicate sensor test mode
if ((millis() / 32) % NUM_MODULES == i) {
color += 8 + (8 << 8) + ((uint32_t)8 << 16);
}
strip.setPixelColor(i, color);
}
strip.show();
#else
#if NUM_MODULES > 1 && SENSOR_TEST
#error NEOPIXEL_DEBUGGING_ENABLED is false, but NUM_MODULES is > 1. To run a sensor test without neopixels, the Arduino will use the builtin LED so NUM_MODULES must be set to 1.
#endif
// We only have one LED - just show the first module's home state status
digitalWrite(LED_BUILTIN, !modules[0]->GetHomeState() ? HIGH : LOW);
#endif
delay(100);
}
void loop() {
while (1) {
#if SENSOR_TEST
sensor_test_iteration();
#else
run_iteration();
#endif
#if defined(ESP8266) || defined(ESP32)
// Yield to avoid triggering Soft WDT
yield();
#endif
}
}
void dump_status() {
Serial.print(FAVR("{\"type\":\"status\", \"modules\":["));
for (uint8_t i = 0; i < NUM_MODULES; i++) {
Serial.print(FAVR("{\"state\":\""));
switch (modules[i]->state) {
case NORMAL:
Serial.print(FAVR("normal"));
break;
case LOOK_FOR_HOME:
Serial.print(FAVR("look_for_home"));
break;
case SENSOR_ERROR:
Serial.print(FAVR("sensor_error"));
break;
case PANIC:
Serial.print(FAVR("panic"));
break;
case STATE_DISABLED:
Serial.print(FAVR("disabled"));
break;
}
Serial.print(FAVR("\", \"flap\":\""));
Serial.write(flaps[modules[i]->GetCurrentFlapIndex()]);
Serial.print(FAVR("\", \"count_missed_home\":"));
Serial.print(modules[i]->count_missed_home);
Serial.print(FAVR(", \"count_unexpected_home\":"));
Serial.print(modules[i]->count_unexpected_home);
Serial.print(FAVR("}"));
if (i < NUM_MODULES - 1) {
Serial.print(FAVR(", "));
}
}
Serial.print(FAVR("]}\n"));
Serial.flush();
}
#if SSD1306_DISPLAY
void display_large_text(char* message) {
display.clearDisplay();
display.setFont(&FreeSans12pt7b);
int16_t outX, outY;
uint16_t outW, outH;
display.getTextBounds(message, 0, 0, &outX, &outY, &outW, &outH);
display.setCursor(0, outH);
display.print(message);
display.display();
// Reset text size
display.setFont();
}
#endif