1 /* Ailerons state machine
2 Serial.print(mid_point);
4 Pilotare un LED RGB in base al canale degli alettoni:
5 Questo sketch usa 2 interrupts per thr e alettoni.
14 left, right 2 Lampeggiatori PWM laterali + PWM
17 = 3 stati + 2 transizioni:
25 Ciclo if per gestione di 3 stati del motore:
32 * Da testare! Mai provato.
39 // Variabili per interrupt 0 si PIN 2
40 volatile unsigned int thr = 1500; // Valore computato
41 volatile unsigned int chStart2 = 1500; // Inizio rilevamento
43 // Variabili per interrupt 1 su PIN 3
44 volatile unsigned int ail = 1500; // Valore computato
45 volatile unsigned int chStart3 = 1500; // Inizio rilevamento
47 // Variabili per autocalibrazione 0
48 const byte chPin2 = 3; // PIN per la calibrazione
49 int mid_point2 = 1500;
55 RGBLed ailerons(5,6,9,255); // Common Cat
57 Lampeggiatore sxLamp(6); // Lampeggiatore
58 Lampeggiatore dxLamp(9); // Lampeggiatore
61 // Variabili per lettura canale servo
62 byte ailPin = 3; // Calibrazione
65 int mid_point = 1560 ; // centro del segnale, trimmato nel setup
66 const int deviation = 50 ; // deviazione dal punto medio
67 //per entrare nello stato successivo dal centro
69 // Led motore e altri:
70 Lampeggiatore left = 10;
71 Lampeggiatore right = 12;
74 // Quando il Throttle e' in IDE facciamo un PWM anche sui laterali
79 unsigned long currentMillis; // timestamp reference per millis per tutto il loop
80 byte caso; // Random var
81 byte thrBit ; // Valore a 8bit per il throttle
86 // FSM gestione alettoni
87 enum { // Stati della FMS
89 sxin, // transizione a sx
91 dxin, // transizione a dx
96 unsigned long FSM_lastMillis = 0 ; // Timestamp per la FSM degli alettoni
97 unsigned long pausa = 500; // Pausa per la transizione durante gli stati 2, 4 della FSM
99 ///////////////////////////////////////////////////////////
107 attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168
108 attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168
111 // Funzione relativa a calibrazione:
112 mid_point = calibraTrim(ailPin) + 10 ; // + LED di servizio per monitor calibrazione
113 //Serial.print(mid_point);
119 currentMillis = millis(); // Timestamp per tutto il loop
125 if (ail > mid_point + deviation + deviation /3) {
126 // extra margine per avere un po' di gioco
128 FSM_lastMillis = currentMillis;
130 else if (ail < mid_point - deviation - deviation / 3) {
132 FSM_lastMillis = currentMillis ;
139 if (currentMillis - pausa > FSM_lastMillis ) {
146 if (ail < mid_point + deviation) {
149 else if (ail < mid_point - deviation) {
150 FSM_lastMillis = currentMillis;
158 if (currentMillis - pausa > FSM_lastMillis ) {
165 if (ail > mid_point - deviation) {
168 else if (ail > mid_point + deviation) {
169 FSM_lastMillis = currentMillis;
182 else if (thr > 1900) {
183 // Throttle al massimo: LED laterali lampeggiano a caso,
184 // Sotto luminosita' a caso
185 caso = random(30, 250) ;
189 delay(caso); // Blocking!
193 thrBit = map(thr,1050, 1900, 0, 255);
194 right.Blink(1220 - 4 * thrBit );
195 left.Blink(1220 - 4 * thrBit );
196 motore.lSet(thrBit); // Luminosita' proporzionale al throttle
201 Serial.print((thr - 980) / 4);
202 Serial.print("\tail: ");
204 Serial.print("\t ailstate:");
205 Serial.println(ailstate);
212 attachInterrupt(0, chFall2, FALLING);
217 attachInterrupt(0, chRise2, RISING);
218 thr = micros() - chStart2;
222 attachInterrupt(1, chFall3, FALLING);
227 attachInterrupt(1, chRise3, RISING);
228 ail = micros() - chStart3;