From: eaman Date: Mon, 6 Mar 2017 22:16:12 +0000 (+0100) Subject: Aereo Palla prot X-Git-Url: http://git.piffa.net/web?p=aerei;a=commitdiff_plain;h=f3d032b8c8def1d285837a22298c76bca6121a7d Aereo Palla prot --- diff --git a/aerei/palla/ailerons_state_rgb/ailerons_state_rgb.ino b/aerei/palla/ailerons_state_rgb/ailerons_state_rgb.ino new file mode 100644 index 0000000..acb8857 --- /dev/null +++ b/aerei/palla/ailerons_state_rgb/ailerons_state_rgb.ino @@ -0,0 +1,169 @@ +/* Aereo Palla + +Pilotare un LED RGB in base al canale degli alettoni: +Questo sketch usa la funzione pulseIn(), +per versione con interrupts vedere esempio successivo. + += 3 stati + 2 transizioni: +- piatto +- roll a sx +- roll a dx + +TODO: +* clean up magic numbers + +*/ + +#include +#define dEBUG // Cambiare in DEBUG per il debug + +// Variabili: +unsigned long currentMillis; // timestamp reference per millis per tutto il loop + +// Un LED RGB +RGBLed ailerons(9,10,7,255); // Common Cat + +// Transizione: Pwm +Lampeggiatore sxLamp(10); // Lampeggiatore +Lampeggiatore dxLamp(9); // Lampeggiatore + + +// Variabili per lettura canale servo +int ail ; // Valore per ailerons +int ailIn ; // Valore rilevato del 4 Ch della RX +unsigned long ailTimer ; // millis per ail + + +// FSM gestione alettoni +enum { // Stati della FMS + middle, // centrale + sxin, // transizione a sx + sx, // sx + dxin, // transizione a dx + dx // dx +} ailstate = middle; + +// Vars FSM +unsigned long FSM_lastMillis = 0 ; // Timestamp per la FSM degli alettoni +unsigned long pausa = 1000; // Pausa per la transizione durante gli stati 2, 4 della FSM + +// Variabili per interrupt 0 si PIN 2: Throttle +volatile unsigned int chValue2 = 1500; // Valore computato +volatile unsigned int chStart2 = 1500; // Inizio rilevamento + +// Variabili per interrupt 1 su PIN 3: Ailerons +volatile unsigned int ail = 1500; // Valore computato +volatile unsigned int mid_point = 1560; // Inizio rilevamento + + +// Variabili per autocalibrazione 1 +const byte chPin3 = 3; // PIN per la calibrazione +int mid_point3 = 1000; + +// Vars Alettoni +int mid_point = 1560 ; // centro del segnale, trimmato nel setup +const int deviation = 50 ; // deviazione dal punto medio + //per entrare nello stato successivo dal centro + +/////////////////////////////////////////////////////////// +void setup() { + +// #define DEBUG + +#ifdef DEBUG + Serial.begin(9600); +#endif + +// Funzione relativa a calibrazione: +mid_point = calibraTrim(ailPin) ; // + LED di servizio per monitor calibrazione +} + +void loop() { + currentMillis = millis(); // Timestamp per tutto il loop + + + + switch (ailstate) { + case middle: + ailerons.White(); + // Alettoni piatti + if (ail > mid_point + deviation + deviation /3) { + // extra margine per avere un po' di gioco + ailstate = sxin; + FSM_lastMillis = currentMillis; + } + else if (ail < mid_point - deviation - deviation / 3) { + ailstate = dxin; + FSM_lastMillis = currentMillis ; + } ; + break; + + case sxin: + // Transizione a sx + sxLamp.Blink(200); + if (currentMillis - pausa > FSM_lastMillis ) { + ailstate = sx; + } + break; + + case sx: + // dx + ailerons.Green(); + if (ail < mid_point + deviation) { + ailstate = middle; + } + else if (ail < mid_point - deviation) { + FSM_lastMillis = currentMillis; + ailstate = dxin; + } ; + break; + + case dxin: + // Transizione a dx + dxLamp.Blink(200); + if (currentMillis - pausa > FSM_lastMillis ) { + ailstate = dx; + } + break; + + case dx: + // sx + ailerons.Blue(); + if (ail > mid_point - deviation) { + ailstate = middle; + } + else if (ail > mid_point + deviation) { + FSM_lastMillis = currentMillis; + ailstate = dxin; + } ; + break; + } +#ifdef DEBUG + Serial.print("ailIn: "); + Serial.print(ailIn); + Serial.print("\tail: "); + Serial.print(ail); + Serial.print("\t ailstate:"); + Serial.println(ailstate); +#endif +} +// Functions +void chRise2() { + attachInterrupt(0, chFall2, FALLING); + chStart2 = micros(); +} + +void chFall2() { + attachInterrupt(0, chRise2, RISING); + chValue2 = micros() - chStart2; +} +// Seconod iterrupt +void chRise3() { + attachInterrupt(1, chFall3, FALLING); + chStart3 = micros(); +} + +void chFall3() { + attachInterrupt(1, chRise3, RISING); + ail = micros() - chStart3; +} diff --git a/aerei/palla/prototipo/prototipo.ino b/aerei/palla/prototipo/prototipo.ino new file mode 100644 index 0000000..a03c08c --- /dev/null +++ b/aerei/palla/prototipo/prototipo.ino @@ -0,0 +1,107 @@ +/* Aereo di Palla + +Prototipo: F8 Bearcat + +Output: + 3 RGB PWM sotto + 1 PWM Motore +// 2 LED ai lati con lampeggio alternato +// 2 LED alle estremita ali + +Input: + 2 interrupts per throttle e alettone + PIN 2: alettone + PIN 3: throttle + + + TODO +* Vedere la calibrazione automatica +* Min e max a 1000 - 2000 per alettone + +*/ + +#include +# define DEBUG + +// Instanziamo un LED fuori dal loop + +RGBLed sotto(5,6,9); +Pwm motore(10); + +// Variabili per interrupt 0 si PIN 2: Throttle +volatile unsigned int chValue2 = 1500; // Valore computato +volatile unsigned int chStart2 = 1500; // Inizio rilevamento + +// Variabili per interrupt 1 su PIN 3: Ailerons +volatile unsigned int chValue3 = 1500; // Valore computato +volatile unsigned int chStart3 = 1500; // Inizio rilevamento + + +// Variabili per autocalibrazione 0 +const byte chPin2 = 2; // PIN per la calibrazione +int mid_point2 = 1000; + +// Variabili per autocalibrazione 1 +const byte chPin3 = 3; // PIN per la calibrazione +int mid_point3 = 1560; + + +void setup() { + + // HI -> LOW --> LOW -> HI + // per avere 2 LED che lampeggiano alternativamente + // Funzione relativa a calibrazione con pulsein: + //mid_point2 = calibraTrim(chPin2) ; // Calibrazione del TRIM attivo sul canale + //mid_point3 = calibraTrim(chPin3) ; // Calibrazione del TRIM attivo sul canale + attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168 + attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168 +#ifdef DEBUG +Serial.begin(9600); +#endif +} + +void loop() { + sotto.Red(); +delay(500); +sotto.Off(); + sotto.Green(); +delay(500); +sotto.Off(); + sotto.Blue(); +delay(500); +sotto.Off(); +motore.UD(3000); + +#ifdef DEBUG + Serial.print("PIN2: "); + Serial.print(chValue2); + Serial.print(" -base: "); + Serial.print(mid_point2); + + Serial.print(" |-| PIN3:"); + Serial.print(chValue3); + Serial.print(" -base: "); + Serial.println(mid_point3); + delay(200); +#endif +} +// Functions +void chRise2() { + attachInterrupt(0, chFall2, FALLING); + chStart2 = micros(); +} + +void chFall2() { + attachInterrupt(0, chRise2, RISING); + chValue2 = micros() - chStart2; +} +// Seconod iterrupt +void chRise3() { + attachInterrupt(1, chFall3, FALLING); + chStart3 = micros(); +} + +void chFall3() { + attachInterrupt(1, chRise3, RISING); + chValue3 = micros() - chStart3; +}