-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuygens.ino
513 lines (443 loc) · 14.2 KB
/
huygens.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
#include <PID_v1.h>
#include "Arduino.h"
#include "content.h"
#define MOTOR_SPEED_DOWN 0.50
#define MOTOR_SPEED_UP 1
#define NUM_LEDS 25
#define MAX_ENABLED_LEDS 7 // limit number of simultaneous LEDs on to avoid powersupply overload
//states
#define DEFAULT_STATE 0
#define UP_STATE 1
#define NOTIFY_STATE 2
#define DOWN_STATE 3
//#define DEBUG
//#define DEBUG_FLOW
//#define DEBUG_SERIAL
//#define DEBUG_ALWAYS_TO_TOP
//#define DEBUG_CONTENT
//#define DEBUG_POSITION_OBJECT_MAPPING
//#define DEBUG_ROUTE_SELECTION
//#define DEBUG_AUDIO_PLAYER
//#define DEBUG_AUDIO_PLAYER_SERIAL
//#define DEBUG_LEDS
//#define DEBUG_PID
//#define DEBUG_PID_VERBOSE
//#define DEBUG_PEDAL
//#define DEBUG_PEDAL_VERBOSE
//#define DEBUG_START_OBJECT_SELECTOR
//#define DISABLE_LEDS
//#define OVERRULE_ROUTE 5 // overrule selected route
//#define OVERRULE_TARGET_POS 0.90 // overrule the position of the TijdWijzer when the pedal is pressed
//#define OVERRULE_AUDIO_DURATION 5000 // limit audio duration (time per route step) for quick testing
//#include "TimerThree.h"
#include "Pedal.h"
#include "AudioPlayer.h"
#include "StartObjectSelector.h"
#include "Leds.h"
#include "TijdWijzer.h"
unsigned int count;
float pointerTopPos = 423; //475; //0.43; //0.45; //0.47 // (unmapped)
float pointerBottomPos = 67; //137; //0.15; //0.17; //0.17; //0.13 // (unmapped)
int state = DEFAULT_STATE;
int currentRoute = 0;
int currentStep = -1; // starts at -1 for not at first step yet
int currentObject = -1;
int pedalPressedForce = -1;
float pointerTargetPos = 0;
// position debug for DEBUG_FLOW
long unsigned int prevPosPrintTime = 0;
int posPrintInterval = 1000;
float prevPrintedPos = 0;
////////////// PINS //////////////
// motor pins
int motorPosInputPin = A0; // motor position potmeter
int motorSpeedPin = 12; // PWM / D2
//int motorSFPin = 11; // status flag
int motorFBPin = A1; // analog power consumption feedback
int motorEnablePin = 10;
int motorSlewPin = 9;
int motorInversePin = 8;
// startObjects pins
int startObjectInputPins[] = {2,7,5,3}; // input of start object selection slider
//int startObjectInputPins[] = {1,6,4,2}; // input of start object selection slider
// pedal pins
int pedalInputTopPin = 21; //2;
int pedalInputBottomPin = 20; //3;
int inputTopInterrupt = 2; //0;
int inputBottomInterrupt = 3; //1;
////////////// PARTS //////////////
Pedal pedal(pedalInputTopPin, pedalInputBottomPin, onPedalPressed);
StartObjectSelector startObjectSelector(startObjectInputPins, onStartObjectSelected);
Tijdwijzer tijdWijzer(motorPosInputPin, motorSpeedPin, motorInversePin, motorEnablePin, motorSlewPin, motorFBPin, pointerBottomPos, pointerTopPos);
Leds leds(objectLedsPins);
AudioPlayer audioPlayer(objectTracks, onAudioFinished);
void setup() {
count = 0;
// motor
tijdWijzer.gotoPos(0.0,MOTOR_SPEED_DOWN);
// pinMode(3, OUTPUT);
// Timer3.initialize(10000); // initialize timer, and set a 10000 millisecond period
// Timer3.pwm(3, 512); // setup pwm on pin 3 (nterrupt 1), 50% duty cycle
// Timer3.attachInterrupt(booboo); // attaches callback() as a timer overflow interrupt
#ifdef DEBUG
Serial.begin(9600);
Serial.println("");
Serial.println("========================");
Serial.println("");
#endif
//pid.currentPos = 0.5;
//state = UP_STATE;
//audioPlayer.playObject(a);
#ifdef DEBUG_CONTENT
for(int i = 0;i<NUM_OBJECTS;i++) {
Serial.print(i);
Serial.print('\t');
Serial.print(objectNames[i]);
Serial.print('\t');
Serial.print(objectPositions[i]);
Serial.print('\t');
Serial.print(objectLedsPins[i]);
Serial.print('\t');
Serial.println(objectTracks[i]);
}
Serial.println("");
for(int i = 0;i<NUM_START_OBJECTS;i++) {
Serial.print(routeStartObjects[i]);
Serial.print('\t');
Serial.print(objectNames(routeStartObjects[i]));
Serial.print('\t');
Serial.println(objectLedsPins[routeStartObjects[i]]);
}
Serial.println("");
#endif
setStartObject(currentStartObject);
//pedal.enable();
/*leds.enableObject(a);
leds.enableObject(c);
leds.enableObject(e);
leds.disableObject(c);
leds.disableObject(b);
leds.disableAllObjects();
leds.enableObject(c);*/
}
//void booboo() {
//pid.update();
// tijdWijzer.update();
//}
void loop() {
float pointerPos;
/* if (millis()%1000 == 0) {
Serial.print(count/1000);
Serial.println("cycles/ms");
count = 0;
}
else count++;
*/
tijdWijzer.update();
#ifdef DEBUG_SERIAL
while (Serial.available() > 0) {
float value = Serial.parseFloat();
Serial.println(value);
//pid.gotoPos(value,MOTOR_SPEED_UP);
tijdWijzer.gotoPos(value,MOTOR_SPEED_UP);
//pid.currentPos = value;
//state = UP_STATE;
}
#endif
//Serial.println(state);
switch(state) {
case DEFAULT_STATE:
startObjectSelector.update();
pedal.update();
break;
case UP_STATE:
//pid.currentPos = 0.5; // hardcode to half way
//pointerPos = float(round(pid.currentPos*10))/10; // rounding to make sure it reaches 1
pointerPos = float(round(tijdWijzer.getPosition()*10))/10; // rounding to make sure it reaches 1
//pointerPos = pid.currentPos;
//pointerPos = 0.40;
#ifdef DEBUG_POSITION_OBJECT_MAPPING
Serial.print("pointerPos: ");
Serial.println(pointerPos);
Serial.print(" route: ");
Serial.println(currentRoute);
#endif
currentStep = -1;
for(int i=0;i<MAX_ROUTE_STEPS;i++) {
int object = routes[MAX_ROUTE_STEPS*currentRoute+i];
#ifdef DEBUG_POSITION_OBJECT_MAPPING
Serial.print(" object: ");
Serial.print(object);
#endif
if(object == -1) {
#ifdef DEBUG_POSITION_OBJECT_MAPPING
Serial.println("");
#endif
continue;
}
float objectPosition = objectPositions[object];
#ifdef DEBUG_POSITION_OBJECT_MAPPING
Serial.print(" step: ");
Serial.print(i);
Serial.print(" stepPosition: ");
Serial.print(' ');
Serial.println(stepPosition);
#endif
//if(pointerPos < stepPosition) {
//leds.disableObject(object);
//break;
//} else {
if(pointerPos >= objectPosition) {
leds.enableObject(object);
currentStep = i;
currentObject = object;
} else {
leds.disableObject(object);
}
}
#ifdef DEBUG_POSITION_OBJECT_MAPPING
Serial.print(" @ step: ");
Serial.print(currentStep);
Serial.print(" @ object: ");
Serial.println(currentObject);
#endif
//leds.enableObject(currentObject);
for(int i=0;i<MAX_ROUTE_STEPS;i++) {
int object = routes[MAX_ROUTE_STEPS*currentRoute+i];
#ifdef DEBUG_POSITION_OBJECT_MAPPING
Serial.print(" object: ");
Serial.print(object);
#endif
if(object == -1) {
#ifdef DEBUG_POSITION_OBJECT_MAPPING
Serial.println("");
#endif
continue;
}
float objectPosition = objectPositions[object];
}
/*#ifdef DEBUG_FLOW
//if(round(millis()/20)*20%1000 == 0) {
if(millis()-prevPosPrintTime > posPrintInterval && prevPrintedPos != tijdWijzer.getPosition()) {
Serial.print(tijdWijzer.getPosition());
Serial.print('/');
Serial.println(tijdWijzer.getTargetPosition());
prevPosPrintTime = millis();
prevPrintedPos = tijdWijzer.getPosition();
}
#endif*/
if(fabs(tijdWijzer.getTargetPosition()-tijdWijzer.getPosition()) < 0.01) { // pointer at target pos?
gotoState(NOTIFY_STATE);
}
break;
case NOTIFY_STATE:
break;
case DOWN_STATE:
/*#ifdef DEBUG_FLOW
//if(millis())
if(millis()-prevPosPrintTime > posPrintInterval && prevPrintedPos != tijdWijzer.getPosition()) {
Serial.print(tijdWijzer.getPosition());
Serial.print('/');
Serial.println(tijdWijzer.getTargetPosition());
prevPosPrintTime = millis();
prevPrintedPos = tijdWijzer.getPosition();
}
#endif*/
/*if(fabs(0-tijdWijzer.getPosition()) < 0.01) { // pointer at bottom?
gotoState(DEFAULT_STATE);
}*/
break;
}
// pid.update();
audioPlayer.update();
//delay(500);
}
void gotoState(int newState) {
#ifdef DEBUG_FLOW
Serial.print("state > ");
Serial.println(newState);
#endif
state = newState;
switch(newState) {
case DEFAULT_STATE:
//pedal.reset(); // reset state of pedal (could have changed by interrupt
//pedal.enable(); // reset state of pedal (could have changed by interrupt
leds.disableAllObjects();
setStartObject(currentStartObject);
break;
case UP_STATE:
//pedal.disable();
pickRoute();
#ifdef DEBUG_FLOW
Serial.print("selected route: ");
Serial.println(currentRoute);
#endif
//routeStartObjects
// float pointerPos = mapfloat(pedalPressedForce,0.0,1.0,pointerBottomPos,pointerTopPos);
//pointerTargetPos = min(0.25 + 0.83*pedalPressedForce, 1.0);
// pointerTargetPos = pedalPressedForce;
//pid.gotoPos(pointerTargetPos,MOTOR_SPEED_UP);
tijdWijzer.gotoPos(pointerTargetPos,MOTOR_SPEED_UP);
#ifdef DEBUG_FLOW
Serial.print(" > ");
Serial.println(pointerTargetPos);
#endif
break;
case NOTIFY_STATE:
//delay(1000);
leds.disableAllObjects();
//Serial.print("currentObject: ");
//Serial.println(currentObject);
leds.enableObject(currentObject);
//delay(1000);
gotoState(DOWN_STATE);
break;
case DOWN_STATE:
gotoStep(currentStep);
break;
}
#ifdef DEBUG_FLOW
Serial.print("@ state: ");
Serial.println(state);
#endif
}
void onPedalPressed(float force) {
if(state != DEFAULT_STATE) return;
#ifdef DEBUG_FLOW
Serial.print("pedal pressed: ");
Serial.print(force);
#endif
pedalPressedForce = force;
pointerTargetPos = min(0.35 + 0.75*force, 1.0);
#ifdef OVERRULE_TARGET_POS
pointerTargetPos = OVERRULE_TARGET_POS;
#endif
#ifdef DEBUG_ALWAYS_TO_TOP
pointerTargetPos = 1;
#endif
#ifdef DEBUG_FLOW
Serial.print(" > ");
Serial.println(pointerTargetPos);
#endif
#ifndef DEBUG_PEDAL
gotoState(UP_STATE);
#endif
}
void onStartObjectSelected(int startObjectPos) {
if(state != DEFAULT_STATE) return;
int startObject = routeStartObjects[startObjectPos];
setStartObject(startObject);
}
void onAudioFinished() {
#ifdef DEBUG_FLOW
Serial.println("audio finished");
#endif
//delay(1000); //TEMP
gotoStep(currentStep-1);
}
void setStartObject(int newStartObject) {
#ifdef DEBUG_FLOW
Serial.println("");
Serial.print("new startobject: ");
Serial.print(newStartObject);
Serial.print(' ');
Serial.println(objectNames[newStartObject]);
#endif
currentStartObject = newStartObject;
//currentRoute = route;
//leds.lightStartObjectSelection(newStartObject);
for(int i=0;i<NUM_START_OBJECTS;i++) {
int startObject = routeStartObjects[i];
if(startObject == newStartObject) {
leds.enableObject(startObject);
} else {
leds.disableObject(startObject);
}
}
#ifdef DEBUG_ROUTE_SELECTION
pickRoute();
#endif
}
void pickRoute() {
#ifdef DEBUG_ROUTE_SELECTION
Serial.println("pickRoute");
Serial.print("currentStartObject: ");
Serial.println(currentStartObject);
#endif
int numCompatibleRoutes = 0; // count routes that start with selected startObject
for(int i=0;i<NUM_ROUTES;i++) {
//int routeStartObject = routes[MAX_ROUTE_STEPS*i];
int routeStartObject = startObjectsPerRoute[i];
#ifdef DEBUG_ROUTE_SELECTION
Serial.print(' ');
Serial.print(i);
Serial.print(' ');
Serial.print(routeStartObject);
#endif
if(routeStartObject == currentStartObject) {
numCompatibleRoutes++;
#ifdef DEBUG_ROUTE_SELECTION
Serial.print(" compatible route");
#endif
}
#ifdef DEBUG_ROUTE_SELECTION
Serial.println(' ');
#endif
}
int randomRouteIndex = random(0,numCompatibleRoutes);
#ifdef DEBUG_ROUTE_SELECTION
Serial.print("numCompatibleRoutes: ");
Serial.println(numCompatibleRoutes);
Serial.print("randomRouteIndex: ");
Serial.println(randomRouteIndex);
#endif
int compatibleRouteIndex = 0;
for(int i=0;i<NUM_ROUTES;i++) {
//int routeStartObject = routes[MAX_ROUTE_STEPS*i];
int routeStartObject = startObjectsPerRoute[i];
if(routeStartObject == currentStartObject) {
if(compatibleRouteIndex == randomRouteIndex) {
currentRoute = i;
}
compatibleRouteIndex++;
}
}
#ifdef OVERRULE_ROUTE
currentRoute = OVERRULE_ROUTE;
#endif
#ifdef DEBUG_ROUTE_SELECTION
Serial.print("picked route: ");
Serial.println(currentRoute);
#endif
}
void gotoStep(int newStep) {
#ifdef DEBUG_FLOW
Serial.print("goto step: ");
Serial.println(newStep);
#endif
if(newStep >= 0) { // is stepping to an object
currentObject = routes[MAX_ROUTE_STEPS*currentRoute+newStep];
#ifdef DEBUG_FLOW
Serial.print(" currentObject: ");
Serial.print(currentObject);
Serial.print(' ');
Serial.println(objectNames[currentObject]);
#endif
// highlight next object
leds.enableObject(currentObject);
// play next audio file
audioPlayer.playObject(currentObject);
// move pointer to object
//pid.gotoPos(objectPositions[currentObject],MOTOR_SPEED_DOWN);
tijdWijzer.gotoPos(objectPositions[currentObject],MOTOR_SPEED_DOWN);
} else { // if done route
//pid.gotoPos(0,MOTOR_SPEED_DOWN);
tijdWijzer.gotoPos(0,MOTOR_SPEED_DOWN);
gotoState(DEFAULT_STATE);
}
currentStep = newStep;
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}