-
Notifications
You must be signed in to change notification settings - Fork 0
/
E30_power_boot.ino
1049 lines (819 loc) · 23 KB
/
E30_power_boot.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
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Actual board
//ARDUINO Leonardo
#include <Servo.h>
#include <Streaming.h>
#include "Switch.h"
Servo myservo; // create servo object to control a servo
#include <NewPing.h>
//Variables and constants for ultrasound switch
unsigned long t1, t2, t3, t4, old_millis, now_millis, time_sig;
unsigned short cycle;
int dist, old_dist;
bool show_us_distance;
#define DIST_LOW 29
#define DIST_HIGH 30
#define PIN_US_TRIG A3
#define PIN_US_ECHO 4
#define MAX_DISTANCE 50
NewPing sonar(PIN_US_TRIG, PIN_US_ECHO, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// Pin defintion on Pro Micro board
#define PIN_LOCK_SW1 3
#define PIN_LOCK_SW2 5
#define PIN_PWM_SERVO 6
#define PIN_SERVO_FEEDBACK A2
#define PIN_CURRENT_SENSE A10 //pin 10 on board
#define PIN_INPUT_1 9
#define PIN_INPUT_2 8
#define PIN_INPUT_3 7
#define PIN_INPUT_4 A1
#define POWER_DRIVES A0
#define LOCK_BUTTON PIN_INPUT_1
#define LOCK_BUTTON_REMOTE PIN_INPUT_2
#define DIGITAL_IN_LOCK_END_STOP PIN_INPUT_4
#define MOTOR_RUNNING PIN_INPUT_3
// For Switch to work as intended, object must be defined as LOW polarity
// and event released() must be used to detect button pressed. Released goes to 1
// as soon as the button is pressed
Switch InputButton = Switch (LOCK_BUTTON, INPUT, LOW, 200, 300, 250, 10);
Switch InputRemoteButton = Switch (LOCK_BUTTON_REMOTE, INPUT, LOW, 200, 300, 250, 10);
#define PIN_LED_ERR 4
#define PIN_LED_BTNS A3
#define PIN_DIR_MOTOR_CAM 15
#define PIN_DIR_MOTOR_UNLOCKER -1
#define PIN_PWM_MOTOR_CAM 14
#define PIN_PWM_MOTOR_UNLOCKER 16
////////////////STATES AND MODES ///////////////
#define MODE_OPENING 0
#define MODE_CLOSING 1
#define MODE_IDLE 2
#define MODE_SAFETY_CLOSING 3
#define MODE_MANUAL_STOP 4
#define STATE_BOOT_LOCKED 0
#define STATE_ENGAGED 1
#define STATE_SWINGING 2
#define STATE_AT_TOP_END 3
//////////////////////////////////////
#define SERVO_POSITION_ENGAGEMENT 156
#define SERVO_POSITION_ENGAGEMENT_INCREASE_CURRENT 142
#define SERVO_POSITION_TOP_END 78
#define SERVO_POSITION_UNLOCK 142
#define POSITION_TOLERANCE 6
#define CAM_COMMAND_GO_TO_LOCK -1
#define CAM_COMMAND_UNLOCK 1
#define CURRENT_LIMIT 6 //2.8
#define CURRENT_EXTRA_ALLOWANCE_LOCK 3.0 // <===============================
#define OVERCURRENT_CONSECUTIVE_STEPS 40
#define MV_PER_AMP 100
#define POS_FEEDBACK_LOW_BOUND 1000
#define POS_FEEDBACK_HIGH_BOUND 2000
#define MOTOR_CAM 1
#define MOTOR_UNLOCKER 2
unsigned short mode, old_mode, mode_when_stopped_was;
unsigned short state, old_state;
int pos = 0; // variable to store the servo position
int current_pos;
unsigned short overcurrent_cnt = 0;
boolean show_current_measure;
boolean show_position;
boolean show_switches;
boolean show_mode;
boolean off_servo;
boolean power;
boolean drivers_on;
boolean old_sw1, old_sw2;
boolean old_lock_cmd_button, old_lock_cmd_remote;
boolean inhibit_first;
boolean servo_stopped;
#define ANALOG_AVERAGING_STEPS 10
float raw_current[ANALOG_AVERAGING_STEPS];
float raw_position[ANALOG_AVERAGING_STEPS];
unsigned long motion_started;
short unsigned int current_av_steps, position_av_steps;
void setup() {
pinMode(PIN_LOCK_SW1, INPUT);
pinMode(PIN_LOCK_SW2, INPUT);
pinMode(PIN_SERVO_FEEDBACK, INPUT);
pinMode(PIN_CURRENT_SENSE, INPUT);
pinMode(PIN_INPUT_1, INPUT);
pinMode(PIN_INPUT_2, INPUT);
pinMode(PIN_INPUT_3, INPUT);
pinMode(PIN_INPUT_4, INPUT);
pinMode(PIN_LED_ERR, OUTPUT);
pinMode(PIN_LED_BTNS, OUTPUT);
pinMode(PIN_DIR_MOTOR_CAM, OUTPUT);
pinMode(PIN_PWM_MOTOR_CAM, OUTPUT);
pinMode(PIN_PWM_MOTOR_UNLOCKER, OUTPUT);
pinMode(POWER_DRIVES, OUTPUT);
pinMode (PIN_US_TRIG, OUTPUT);
pinMode (PIN_US_ECHO, INPUT);
StopServo();
old_sw1 = 0;
old_sw2 = 0;
old_state = -1;
old_mode = -1;
mode = MODE_IDLE;
show_switches = 0;
show_position = 0;
int j;
for (j = 0; j < ANALOG_AVERAGING_STEPS; j++)
{
raw_current [j] = 0;
raw_position [j] = 0;
}
EvaluateState();
OutMotor (MOTOR_UNLOCKER, 0);
OutMotor (MOTOR_CAM, 0);
InputButton.poll();
InputRemoteButton.poll();
inhibit_first = 1;
servo_stopped = true;
mode_when_stopped_was = MODE_IDLE;
drivers_on = true;
PowerDrivesOff();
cycle = 0;
t1 = millis();
t2 = t1;
t3 = t2;
t4 = t3;
time_sig = t1;
dist = 20;
show_us_distance = false;
old_millis = t1;
}
void StopServo()
{
if (servo_stopped)
return;
int i;
servo_stopped = true;
pos = ScalePosition();
for (i = 0; i < 3; i++)
{
myservo.write(pos);
delay (100);
}
mode = MODE_IDLE;
}
void loop() {
//Serial << digitalRead (PIN_INPUT_1) << " " << digitalRead (PIN_INPUT_2)<< digitalRead (PIN_INPUT_3) << " " << digitalRead (PIN_INPUT_4) <<"\n";
// delay(200);
// return;
// PowerDrivesOn();
// UnlockCam();
// delay(2000);
// LockCam();
//delay(2000);
//return;
InputButton.poll();
InputRemoteButton.poll();
//TestServo();
if (show_mode)
DisplayModeAndState();
ReadKeyboardCmds();
EvaluateState();
ReadUserCommands();
switch (mode)
{
case MODE_OPENING : PowerDrivesOn(); ProcessOpening(); break;
case MODE_CLOSING : PowerDrivesOn(); ProcessClosing(); break;
case MODE_MANUAL_STOP : StopServo(); break;
case MODE_SAFETY_CLOSING : mode = MODE_IDLE; break;
case MODE_IDLE : {
if ( FootSwitchSwing() && !isMotorRunning())
{
if (state == STATE_BOOT_LOCKED)
mode = MODE_OPENING;
else
mode = MODE_CLOSING;
break;
}
if (0)//current_pos < SERVO_POSITION_TOP_END - POSITION_TOLERANCE)
SetServo(SERVO_POSITION_TOP_END);
/* else
if (current_pos > SERVO_POSITION_ENGAGEMENT +POSITION_TOLERANCE)
SetServo(SERVO_POSITION_ENGAGEMENT);
*/
else
{
StopServo();
OutMotor(MOTOR_CAM, 0);
OutMotor(MOTOR_UNLOCKER, 0);
myservo.detach();
PowerDrivesOff();
}
break;
}
default:
StopServo();
}
CurrentProtection();
}
void PowerDrivesOn()
{
if (drivers_on)
return;
digitalWrite(POWER_DRIVES, HIGH);
delay(200);
drivers_on = true;
}
void PowerDrivesOff()
{
if (!drivers_on)
return;
digitalWrite(POWER_DRIVES, LOW);
drivers_on = false;
}
void ProcessOpening()
{
switch (state)
{
case STATE_BOOT_LOCKED : UnlockCam(); /*Serial << "process op boot locked \n";*/ break;
case STATE_ENGAGED :
case STATE_SWINGING : OutMotor(MOTOR_CAM, 0); SetServo(SERVO_POSITION_TOP_END);/* Serial << "process op swinging\n";*/ break;
case STATE_AT_TOP_END : StopServo(); Serial << "top end\n"; mode = MODE_IDLE; break;
}
}
void ProcessClosing ()
{
switch (state)
{
case STATE_AT_TOP_END :
case STATE_SWINGING : SetServo(SERVO_POSITION_ENGAGEMENT); /*Serial << "process cl top end , swinging\n";*/ break;
case STATE_ENGAGED : StopServo(); LockCam(); //SetServo(SERVO_POSITION_ENGAGEMENT);LockCam(); StopServo(); /*erial << "process closing state engaged \n"; */ break;
case STATE_BOOT_LOCKED : OutMotor(MOTOR_CAM, 0); /*Serial << "process cl boot mocked\n";*/mode = MODE_IDLE; break;
}
}
void UnlockCam()
{
myservo.write(SERVO_POSITION_UNLOCK);
OutMotor(MOTOR_UNLOCKER, 1);
delay (500);
OutMotor(MOTOR_UNLOCKER, 0);
unsigned long start_time = millis();
OutMotor(MOTOR_CAM, CAM_COMMAND_UNLOCK);
do
{
if ( digitalRead (DIGITAL_IN_LOCK_END_STOP))
{
Serial << "got to unlock cam position ... STOP!!!! " << millis() - start_time << " \n";
OutMotor(MOTOR_CAM, 0);
return;
}
delay (2);
} while ( (millis() - start_time) <= 1300);
Serial << " Cam unlock out on timeout ... STOP!!!! \n";
OutMotor(MOTOR_CAM, 0);
}
void LockCam()
{
unsigned long start_time = millis();
OutMotor(MOTOR_CAM, CAM_COMMAND_GO_TO_LOCK);
do
{
if ( digitalRead (PIN_LOCK_SW2))
{
Serial << "got to locked position ... STOP!!!! " << millis() - start_time << " \n";
OutMotor(MOTOR_CAM, 0);
return;
}
delay (2);
} while ( (millis() - start_time) <= 1400);
Serial << "out on timeout ... STOP!!!! \n";
OutMotor(MOTOR_CAM, 0);
}
void DisplayModeAndState()
{
static short u = 0;
if (u < 40)
{
u++;
return;
}
u = 0;
Serial << "Current mode: " ;
switch (mode)
{
case MODE_IDLE : Serial << " IDLE "; break;
case MODE_OPENING : Serial << " MODE_OPENING "; break;
case MODE_CLOSING : Serial << " MODE_CLOSING "; break;
case MODE_SAFETY_CLOSING : Serial << " MODE_SAFETY_CLOSING "; break;
case MODE_MANUAL_STOP: Serial << " MODE_MANUAL_STOP"; break;
}
Serial << "Current state: " ;
switch (state)
{
case STATE_BOOT_LOCKED : Serial << "BOOT LOCKED \n"; break;
case STATE_ENGAGED : Serial << "BOOT ENGAGED \n"; break;
case STATE_SWINGING : Serial << "SWINGING \n"; break;
case STATE_AT_TOP_END : Serial << "AT TOP END \n"; break;
}
}
void send_unlock_motor()
{
OutMotor(MOTOR_UNLOCKER, 1);
delay(2000);
OutMotor(MOTOR_UNLOCKER, 0);
}
void CamLock()
{
}
void OutMotor (int _motor_ID, float _command)
{
// CAM Motor : negative command: locks; positive unlocks (black motor wire on M2A, red on M2B)
unsigned int motor_dir, motor_PWM;
// Serial << "outmotor received ID" << _motor_ID <<" cmd" << _command <<"\n";
switch (_motor_ID)
{
case MOTOR_CAM : motor_dir = PIN_DIR_MOTOR_CAM; motor_PWM = PIN_PWM_MOTOR_CAM; break;
case MOTOR_UNLOCKER : motor_dir = PIN_DIR_MOTOR_UNLOCKER; motor_PWM = PIN_PWM_MOTOR_UNLOCKER; break;
}
if (_command >= 1.0)
_command = 1.0;
if (_command <= -1.0)
_command = -1.0;
if (motor_dir != PIN_DIR_MOTOR_UNLOCKER)
if (_command < 0)
digitalWrite(motor_dir, 1);
else
digitalWrite(motor_dir, 0);
// unsigned int pwm = (unsigned int) (fabs(_command * 255));
// analogWrite (motor_PWM,pwm);
if (_command != 0)
digitalWrite (motor_PWM, 1);
else
digitalWrite (motor_PWM, 0);
// Serial << "writing " << motor_PWM << " command " <<pwm << "\n";
}
void ReadUserCommands()
{
/*
//Serial << "pushed " << InputButton.pushed() ;
//Serial << " switched " << InputButton.switched() ;
//Serial << " on " << InputButton.on() ;
if (InputButton.released())
Serial << " released " << "\n";
if (InputButton.pushed())
Serial << " pushed " << "\n";
return;
*/
if (isCarMoving())
return;
if (InputButton.released())
{
if (inhibit_first)
{
inhibit_first = 0;
return;
}
if (isCarLocked())
if (state == STATE_BOOT_LOCKED)
mode = MODE_IDLE;
else
mode = MODE_CLOSING;
else
{
// button pressed, car is unlocked, several cases:
if (mode == MODE_MANUAL_STOP)
{
if (mode_when_stopped_was == MODE_CLOSING)
mode = MODE_OPENING;
else if (mode_when_stopped_was == MODE_OPENING)
mode = MODE_CLOSING;
else
mode = MODE_IDLE;
}
else
{
if (mode != MODE_IDLE)
{
mode_when_stopped_was = mode;
mode = MODE_MANUAL_STOP;
}
else if (state == STATE_AT_TOP_END)
mode = MODE_CLOSING;
else
// in all other case, a pressed button is associated to open
mode = MODE_OPENING;
}
}
} // if lock_cmd_button
if (InputRemoteButton.released())
{
Serial << " $$$$$$ REMOTE \n";
if (mode != MODE_IDLE)
{
mode = MODE_MANUAL_STOP;
}
else
{
if (state == STATE_AT_TOP_END)
mode = MODE_CLOSING;
else
mode = MODE_OPENING;
}
}
if (old_mode != mode)
{
Serial << "New mode is " ;
switch (mode)
{
case MODE_IDLE : Serial << " IDLE \n"; break;
case MODE_OPENING : Serial << " MODE_OPENING \n"; motion_started = millis(); break;
case MODE_CLOSING : Serial << " MODE_CLOSING \n"; motion_started = millis(); break;
case MODE_SAFETY_CLOSING : Serial << " MODE_SAFETY_CLOSING \n"; break;
case MODE_MANUAL_STOP: Serial << " MODE_MANUAL_STOP\n"; break;
}
}
old_mode = mode;
}
boolean isCarLocked()
{
//TODO read dedicated input signal and define
// if car is locked
return false;
}
boolean isMotorRunning()
{
//return true;
bool in = digitalRead(MOTOR_RUNNING);
Serial << "in " << in <<"\n";
return in;
}
boolean isCarMoving()
{
//TODO read dedicated input signal and define
// if car is moving
return false;
}
void TestServo()
{
if (power)
PowerDrivesOn();
else
PowerDrivesOff();
return;
if (pos <= 0)
pos = 0;
if (pos >= 180)
pos = 180;
if (!off_servo)
myservo.write(pos);
}
void SetServo(int position_target)
{
static int current_target = -1;
if (position_target != current_target)
{
Serial << "sservo go to target" << position_target << " \n";
//return;
}
current_target = position_target;
if (!myservo.attached())
myservo.attach(PIN_PWM_SERVO);
myservo.write(position_target);
servo_stopped = false;
}
void EvaluateState()
{
static short count_debounce = 0;
//Read Servo feedback
current_pos = ScalePosition();
if (show_position)
Serial << "Current position " << current_pos << " \n";
boolean sw1, sw2;
sw1 = digitalRead (PIN_LOCK_SW1);
sw2 = digitalRead (PIN_LOCK_SW2);
if (show_switches)
Serial << "sw1 " << sw1 << " sw2 " << sw2 << "\n";
state = STATE_SWINGING;
if (sw1)
{
state = STATE_ENGAGED;
//Serial << "Engaged ! \n";
}
if (sw2)
{
state = STATE_BOOT_LOCKED;
//Serial << "Boot locked ! \n";
}
if (!sw1 && !sw2)
{
if ((abs (current_pos - SERVO_POSITION_TOP_END) <= POSITION_TOLERANCE - 4) || (current_pos <= (SERVO_POSITION_TOP_END)))
{
//Serial << "xount = " << count_debounce << "position = " << current_pos << "\n";
count_debounce ++;
if (count_debounce >= 10)
{
state = STATE_AT_TOP_END;
count_debounce = 10; // bound counter
}
}
else count_debounce --;
if (count_debounce < 0)
count_debounce = 0;
}
old_sw1 = sw1;
old_sw2 = sw2;
if (old_state != state)
{
Serial << " ==== New state is " ;
switch (state)
{
case STATE_BOOT_LOCKED : Serial << " STATE_BOOT_LOCKED \n"; break;
case STATE_ENGAGED : Serial << " STATE_ENGAGED \n"; break;
case STATE_SWINGING : Serial << " STATE_SWINGING \n"; break;
case STATE_AT_TOP_END : Serial << " STATE_AT_TOP_END \n"; break;
}
}
old_state = state;
}
unsigned int ScalePosition ()
{
//raw reading 1022 = top = 180°
// raw reading 980 = bottom = 0°
static int pos_average_steps = 0;
int j = 0;
bool displayout = false;
//1024 - anain
float scaledpos = analogRead (PIN_SERVO_FEEDBACK) / 1024.0 * 5.0; //in volts
raw_position [pos_average_steps] = scaledpos;
pos_average_steps++;
if (pos_average_steps >= ANALOG_AVERAGING_STEPS)
{
pos_average_steps = 0;
displayout = true;
}
//compute average
float average = 0;
for (j = 0; j < ANALOG_AVERAGING_STEPS; j++)
{
average = average + raw_position [j];
}
average = average / ANALOG_AVERAGING_STEPS;
unsigned int output;
float x = average;
//polynomial approximation in use with 10 turn potentiometer
average = 88.1593538351 * x * x * x * x - 869.9926535361 * x * x * x + 3208.9184567625 * x * x - 5385.4931352597 * x + 3698.1106094017;
output = (unsigned int) (average );
//if (displayout) Serial <<" Ana reading " << scaledpos <<"average "<< average << " pos corresponds to " <<output<< "\n";
return output;
}
void ReadKeyboardCmds()
{
//Check for manual commands
if (Serial.available() > 0)
{
Serial << "keyboard in \n";
char rx_byte = Serial.read(); // get the character
// ?
if (rx_byte == '?')
{
Serial << "B,b = bottom, T,t = top ; P,p = +5, M,m = -5; C,c = current\n" ;
}
switch (rx_byte)
{
case 'M':
case 'm': show_mode = !show_mode ; break;
case 'U':
case 'u': Serial << "Unlock cam\n"; PowerDrivesOn(); UnlockCam(); PowerDrivesOff(); break;
case 'L':
case 'l': Serial << "Lock cam\n"; PowerDrivesOn(); LockCam(); PowerDrivesOff(); break;
case 'P':
case 'p': Serial << "+5 \n" ; pos = pos + 5; break;
case 'T':
case 't': Serial << "go to top end \n" ; pos = 180; break;
case 'B':
case 'b': Serial << "go to bottom end \n" ; pos = 0; break;
case 'D':
case 'd': Serial << "show US distance \n"; show_us_distance = ! show_us_distance; break;
case 'C':
case 'c': Serial << "show current \n"; show_current_measure = ! show_current_measure; break;
case 'Z':
case 'z': Serial << "show position \n"; show_position = ! show_position; break;
case 'S':
case 's' : Serial << " STOP STOP at " << ScalePosition() << "\n"; StopServo(); break;
case 'a':
case 'A' : Serial << " cam motor positive \n"; OutMotor (MOTOR_CAM, 0.5); delay (1000); OutMotor (MOTOR_CAM, 0); break;
case 'q':
case 'Q' : Serial << " cam motor negative \n"; OutMotor (MOTOR_CAM, -0.5); delay (1000); OutMotor (MOTOR_CAM, 0); break;
case 'w':
case 'W' : Serial << " unlock motor \n"; OutMotor (MOTOR_UNLOCKER, 0.5); delay (1000); OutMotor (MOTOR_UNLOCKER, 0); break;
case '1' : mode = MODE_CLOSING; Serial << "closing keyboard command \n"; break;
case '2' : mode = MODE_OPENING; Serial << "opening keyboard command \n"; break;
case '3' : show_switches = !show_switches; break;
case 'n':
case 'N':
Serial << "power " << (power ? "ON" : "OFF") << "\n"; power = !power; break;
case 'X':
case 'x' : off_servo = !off_servo;
if (off_servo) myservo.detach();
else myservo.attach(9);
Serial << " SERVO AXIS IS " << (off_servo ? "OFF" : "ON") << "\n"; break;
}
}
}
void CurrentProtection()
{
int j;
static short int skip = 0;
int anain = analogRead(PIN_CURRENT_SENSE);
raw_current [current_av_steps] = ADCValueToCurrent(anain) ;
current_av_steps++;
if (current_av_steps >= ANALOG_AVERAGING_STEPS)
current_av_steps = 0;
//compute average
float average = 0;
for (j = 0; j < ANALOG_AVERAGING_STEPS; j++)
{
average = average + raw_current [j];
}
average = average / ANALOG_AVERAGING_STEPS;
float current_limit = CURRENT_LIMIT;
if (current_pos >= SERVO_POSITION_ENGAGEMENT_INCREASE_CURRENT)
{
current_limit += CURRENT_EXTRA_ALLOWANCE_LOCK;
//if (mode != MODE_IDLE)
// show_current_measure = true;
}
if (show_current_measure)
{
if (skip >= 30)
{
Serial << " current measure " << fabs(average) << " (A) LIMIT : " << current_limit << "\n";
skip = 0;
}
skip = skip + 1;
}
unsigned long deltat;
if ((fabs(average) >= current_limit) && (state == STATE_SWINGING) && (mode != MODE_OPENING) )
{
overcurrent_cnt++;
if (overcurrent_cnt >= OVERCURRENT_CONSECUTIVE_STEPS)
{
deltat = millis() - motion_started;
//Inhibit first two seconds after servo has started moving
if (deltat >= 2000)
{
Serial << "##### current limit reached " << fabs(average) << " (A) ; current position is " << current_pos << "\n";
StopServo();
show_current_measure = false;
if (mode == MODE_CLOSING)
// mode = MODE_SAFETY_CLOSING;
// else
mode == MODE_IDLE;
overcurrent_cnt = 0;
//delay (500);
}
}
}
else
overcurrent_cnt = 0;
}
float ADCValueToCurrent (long int adc_in)
{
float temp = adc_in;
float mv = (temp / 1023.0) * 5000.0;
return ((mv - 2500) / MV_PER_AMP);
}
bool FootSwitchSwing()
{
//return false;
static bool filter_one = true;
now_millis = millis();
if ((now_millis - old_millis) <= 30)
{
return false;
}
old_millis = now_millis;
dist = UltrasonDistance();
if ( (cycle != 0) && (now_millis - time_sig > 2000) )
{ cycle = 0;
Serial << " **** RESET !!\n";
filter_one = true;
}
if (abs (dist - old_dist) >= 1)
{
//Serial << " ## change ; dist : " << dist <<" old_dist: " <<old_dist <<"\n";
time_sig = now_millis;
switch (cycle)
{
case 0: if (( dist <= DIST_LOW)&& (dist >10)) //test > 10 required for noisy measures on gravel
{
cycle++;
t1 = time_sig;
Serial << "t1 : " << t1 << "\n";
} break;
case 1: if ( dist >= DIST_HIGH)
{
if (filter_one)
filter_one = false;
else
{
filter_one = true;
cycle++; t2 = time_sig; Serial << "t2 : " << t2 << " t2-t1: " << t2-t1 <<"\n";
}
}
break;
case 2: if (( dist <= DIST_LOW)&& (dist >10)) //test > 10 required for noisy measures on gravel
{
cycle++;
t3 = time_sig;
Serial << "t3 : " << t3 << " t3-t2: " << t3-t2 << "\n";
} break;
case 3: if ( dist >= DIST_HIGH)
{ if (filter_one)
{
filter_one = false;
}
else
{
filter_one = true;
cycle++; t4 = time_sig; Serial << "t4 : " << t4 << " t4-t3: " << t4-t3 << "\n";
}
}
break;
}
}
if (show_us_distance) Serial << "Swing ; distance : " << dist <<" cycle:" <<cycle <<" Mot-or on: " << digitalRead(MOTOR_RUNNING) <<"\n";
if (cycle == 4)
{
Serial << "t2 - t1: ==" << t2 - t1 << "== ; t3 - t2: **" << t3 - t2 << "** t4 - t3: %%" << t4 - t3 << "%%\n";
cycle = 0;
if ( ( (t2 - t1) <= 650) && ((t2 - t1) >= 80) &&
//( (t3 - t2) <= 1500) && ((t3 - t2) >= 200) &&
( (t4 - t3) <= 650) && ((t4 - t3) >= 80) )
{
Serial << "GOT IT !!!!!! \n";
return true;
}
}
old_dist = dist;
return false;
}
#define FOOT_KEEP_DELAY 400
float f_dist;
#define US_DIST_AVERAGING_STEPS 5
int raw_dist[US_DIST_AVERAGING_STEPS];
int UltrasonDistance()
{
int j = 0;
static int dist_av_steps = 0;
int _dist = sonar.ping_cm();
raw_dist [dist_av_steps] = _dist ;
dist_av_steps++;
if (dist_av_steps >= US_DIST_AVERAGING_STEPS)
dist_av_steps = 0;
//compute average
float f_dist = 0;
for (j = 0; j < US_DIST_AVERAGING_STEPS; j++)
{
f_dist = f_dist + raw_dist [j];
}
f_dist = f_dist / US_DIST_AVERAGING_STEPS;
return (int) f_dist;
}
bool FootSwitchKeep()
{
now_millis = millis();
if ((now_millis - old_millis) <= 30)
{