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 = 0; // 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
46 // Variabili per lettura canale alettoni
47 byte ailPin = 2; // Calibrazione
49 // Variabili per autocalibrazione 0
50 int mid_point2 = 1500;
54 RGBLed ailerons(5,6,9); // Common Cat
55 // Transizione: Lampeggiatore
56 Lampeggiatore sxLamp(6); // Lampeggiatore
57 Lampeggiatore dxLamp(9); // Lampeggiatore
62 int mid_point = 1500 ; // centro del segnale, trimmato nel setup
63 const int deviation = 50 ; // deviazione dal punto medio
64 //per entrare nello stato successivo dal centro
66 // Led motore e altri:
70 unsigned long currentMillis; // timestamp reference per millis per tutto il loop
71 byte caso; // Random var
72 byte thrBit ; // Valore a 8bit per il throttle
74 // FSM gestione alettoni
75 enum { // Stati della FMS
77 sxin, // transizione a sx
79 dxin, // transizione a dx
84 unsigned long FSM_lastMillis = 0 ; // Timestamp per la FSM degli alettoni
85 unsigned long pausa = 400; // Pausa per la transizione durante gli stati 2, 4 della FSM
87 ///////////////////////////////////////////////////////////
92 attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168
93 attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168
95 // Funzione relativa a calibrazione:
96 //mid_point = calibraTrim(ailPin) + 10 ; // + LED di servizio per monitor calibrazione
97 mid_point = 1500 ; // + LED di servizio per monitor calibrazione
101 currentMillis = millis(); // Timestamp per tutto il loop
107 if (ail > mid_point + deviation + deviation /3) {
108 // extra margine per avere un po' di gioco
110 FSM_lastMillis = currentMillis;
112 else if (ail < mid_point - deviation - deviation / 3) {
114 FSM_lastMillis = currentMillis ;
120 sxLamp.Blink(pausa/2);
121 if (currentMillis - pausa > FSM_lastMillis ) {
128 if (ail < mid_point + deviation) {
131 else if (ail < mid_point - deviation) {
132 FSM_lastMillis = currentMillis;
139 dxLamp.Blink(pausa/2);
140 if (currentMillis - pausa > FSM_lastMillis ) {
147 if (ail > mid_point - deviation) {
150 else if (ail > mid_point + deviation) {
151 FSM_lastMillis = currentMillis;
162 else if (thr > 1900) {
163 // Throttle al massimo: LED laterali lampeggiano a caso,
164 // Sotto luminosita' a caso
165 caso = random(30, 250) ;
167 delay(caso); // Blocking!
171 thrBit = map(thr,1050, 1900, 0, 255);
172 motore.lSet(thrBit); // Luminosita' proporzionale al throttle
177 Serial.print((thr - 980) / 4);
178 Serial.print("\tail: ");
180 Serial.print("\t ailstate:");
181 Serial.println(ailstate);
188 attachInterrupt(0, chFall2, FALLING);
193 attachInterrupt(0, chRise2, RISING);
194 ail = micros() - chStart2;
199 attachInterrupt(1, chFall3, FALLING);
204 attachInterrupt(1, chRise3, RISING);
205 thr = micros() - chStart3;