]> git.piffa.net Git - aerei/blob - aerei/daniele/fsm_switch/fsm_switch.ino
Aero, toggle on/off
[aerei] / aerei / daniele / fsm_switch / fsm_switch.ino
1 /* Aereo di Daniele
2
3 FSM: il throttle e' a posto
4 Prototipo: F8 Bearcat
5
6 Output:
7    2 LED PWM ai lati con lampeggio alternato
8    2 LED PWM alle estremita ali
9
10 Input:
11     2 interrupts per th e alettone
12     PIN 2:  alettone
13     PIN 3:  throttle
14     PIN A1: interruttore On/Off
15
16
17 NOTE: aggiunta libreria aero per gestione interruttore,
18           da testare.
19
20     TODO
21 * testare
22 * Vedere la calibrazione automatica
23 * Min e max a 1000 - 2000 per alettone
24
25 */
26
27 #include <common.h>
28 #include <aero.h>
29 # define dEBUG
30
31 // Instanziamo un LED fuori dal loop
32 Lampeggiatore left = 6;
33 Lampeggiatore right = 9;
34 Lampeggiatore codasx = 5;
35 Lampeggiatore codadx = 10;
36
37 Pwm pleft = 6;
38 Pwm pright = 9;
39 Pwm pcodasx = 5;
40 Pwm pcodadx = 10;
41
42 // Variabili per interrupt 0 si PIN 2
43 volatile unsigned int chValue2 = 1500; // Valore computato
44 volatile unsigned int chStart2 = 1500; // Inizio rilevamento
45
46 // Variabili per interrupt 1 su PIN 3
47 volatile unsigned int chValue3 = 1500; // Valore computato
48 volatile unsigned int chStart3 = 1500; // Inizio rilevamento
49
50 const byte chPin = A1; // PIN interruttore generale
51
52 // Variabili per autocalibrazione 0
53 const byte chPin2 = 2; // PIN per la calibrazione alettone
54 int mid_point2 = 1500;
55
56 // Variabili per autocalibrazione 1
57 const byte chPin3 = 3; // PIN per la calibrazione
58 int mid_point3 = 1000;
59
60 // Variabili
61 int caso ;
62 int thrBit;
63
64 void setup() {
65   // I PINs vengono impostati dal constructor al momento
66   // della dichiarazione dell'ogetto.
67 right.Invert();
68 codadx.Invert();
69
70   // HI -> LOW --> LOW -> HI
71   // per avere 2 LED che lampeggiano alternativamente
72     // Funzione relativa a calibrazione con pulsein:
73     mid_point2 =  calibraTrim(chPin2) ; // Calibrazione del TRIM attivo sul canale
74     //mid_point3 =  calibraTrim(chPin3) ; // Calibrazione del TRIM attivo sul canale
75     attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168
76     attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168
77 #ifdef dEBUG
78 Serial.begin(9600); 
79 #endif
80 }
81
82 void loop() {
83 if (toggleRead(A0)) { // Interruttore generale
84 // Gestione throttle
85     if (chValue3 < 1050) {
86         // IDLE
87 //pleft.Up(1000);
88 //pright.Up(1000);
89 pcodasx.UD(2000);
90 pcodadx.UD(2000);
91 pleft.lUp(1000);
92 pright.lDown(1000);
93
94
95     }
96     else if (chValue3 > 1900) {
97         // Throttle al massimo: LED laterali lampeggiano a caso,
98         // Sotto luminosita' a caso
99         caso = random(30, 250) ;
100 pleft.Set(caso); 
101 pright.Set(caso); 
102 pcodasx.Set(caso);
103 pcodadx.Set(caso);
104         delay(caso);
105     }
106     else {
107         // Throttle medio
108         thrBit = map(chValue3,1050, 1900, 0, 255);
109         codasx.Blink(1220 - 4 * thrBit );
110         codadx.Blink(1220 - 4 * thrBit );
111 left.Blink(map(chValue2,1000,2000,200,800 ));
112 right.Blink(map(chValue2,1000,2000,800,200 ));
113     }
114
115 #ifdef DEBUG
116     Serial.print("PIN2: ");
117     Serial.print(chValue2);
118     Serial.print(" -base: ");
119     Serial.print(mid_point2);
120
121     Serial.print(" |-| PIN3:");
122     Serial.print(chValue3);
123     Serial.print(" -base: ");
124     Serial.println(mid_point3);
125     delay(200);
126 #endif
127 } else { // Interruttore off
128 pleft.Set(0); 
129 pright.Set(0); 
130 pcodasx.Set(0);
131 pcodadx.Set(0);
132 }
133 }
134 // Functions
135 void chRise2() {
136     attachInterrupt(0, chFall2, FALLING);
137     chStart2 = micros();
138 }
139
140 void chFall2() {
141     attachInterrupt(0, chRise2, RISING);
142     chValue2 = micros() - chStart2;
143 }
144 // Seconod iterrupt
145 void chRise3() {
146     attachInterrupt(1, chFall3, FALLING);
147     chStart3 = micros();
148 }
149
150 void chFall3() {
151     attachInterrupt(1, chRise3, RISING);
152     chValue3 = micros() - chStart3;
153 }