From 9f5e79fda4d0dcdfd222d3f2fefcef31cd49b504 Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Thu, 23 Feb 2017 19:50:46 +0100 Subject: [PATCH] Anto + Dani --- aerei/antonino/bugatti_fsm/bugatti_fsm.ino | 215 +++++++++++++++++ .../bugatti_fsm_mix/bugatti_fsm_mix.ino | 226 ++++++++++++++++++ .../bugatti_fsm_prot/bugatti_fsm_prot.ino | 205 ++++++++++++++++ aerei/daniele/fsm/fsm.ino | 15 +- .../ailerons_state_interrupt.ino | 34 +-- 5 files changed, 673 insertions(+), 22 deletions(-) create mode 100644 aerei/antonino/bugatti_fsm/bugatti_fsm.ino create mode 100644 aerei/antonino/bugatti_fsm_mix/bugatti_fsm_mix.ino create mode 100644 aerei/antonino/bugatti_fsm_prot/bugatti_fsm_prot.ino diff --git a/aerei/antonino/bugatti_fsm/bugatti_fsm.ino b/aerei/antonino/bugatti_fsm/bugatti_fsm.ino new file mode 100644 index 0000000..5099f7f --- /dev/null +++ b/aerei/antonino/bugatti_fsm/bugatti_fsm.ino @@ -0,0 +1,215 @@ +/* Bugatti di Antonino + +Outputs: + 2 LED / Strisce laterali che lampeggiano alternativamente + 1 LED in PWM per il motore + 1 Striscia RGB sotto per tutta la lunghezza delle ali + +Inputs: + Lettura del canale Throttle (3) con la funzione Pulsein + Lettura alettoni con interrupt 0 (PIN2) + +TODO: +* Cambiare il PIN del throttle su A5 da A3 +* attaccare il canale degli alettoni al pin2 +* guardare che tipo di RGB e', anodo o cat +* a full throttle RGB fa un Rand, vedere che non vada in conflitto con la sec FSM + +*/ + +#include +#define dEBUG + +// LED disponibili +Lampeggiatore left = 7; +Lampeggiatore right = 8; +Pwm motore = 3; + +// RGB +RGBLed ailerons(6,5,9); + // Transizione: lampeggiatori sui PIN RGB + Lampeggiatore sxLamp(5); // Lampeggiatore + Lampeggiatore dxLamp(9); // Lampeggiatore +//Pwm rsotto = 6; +//Pwm gsotto = 5; +//Pwm bsotto = 3; + + +// Var thr +//////////////// !!!! cambiare thrIn +const byte thrPin = A5; // PIN collegato al CH3 +byte thr ; // Throttle a 8bit +int thrIn ; // Valore del th in ingresso dal servo + +// Variabili per interrupt 0 su PIN 2 +volatile unsigned int ail = 1500; // Valore computato +volatile unsigned int chStart2 = 1500; // Inizio rilevamento + +// Variabili per autocalibrazione 0 +const byte chPin2 = 2; // PIN per la calibrazione +int mid_point2 = 1500; + +// 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 + + +// 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 = 600; // Pausa per la transizione durante gli stati 2, 4 della FSM + +// Variabili comuni: +unsigned long currentMillis; // timestamp reference per millis per tutto il loop +byte caso ; // Valore random + + +void setup() { + // I PINs vengono impostati dal constructor al momento + // della dichiarazione dell'ogetto. + pinMode(thrPin,INPUT); + right.Invert() ; // Opzionale: inverte l'ordine del lampeggio da + + attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168 + + randomSeed(analogRead(0)); + + // Test iniziale dei LED per verifica contatti: + left.High(); + right.High(); + ailerons.White(); + motore.Set(255); + delay(4000); + + +mid_point = calibraTrim(chPin2) + 8 ; // + LED di servizio per monitor calibrazione +#ifdef DEBUG + Serial.begin(9600); +#endif +} + +void loop() { +currentMillis = millis(); // Timestamp per tutto il loop + +// Lettura CH3 + thrIn = pulseIn(thrPin, HIGH, 25000); + // Hint: thrIn andrebbe calibrato son un Serial.write + if (thrIn != 0) { +thr = map(thrIn, 960, 2000, 0, 255); +}; + + +// Gestione throttle + if (thr >= 0 && thr < 15) { + // IDLE + + right.Blink(); + left.Blink(); + motore.UD(2000); + } else if (thr < 245) { + // Throttle medio + + right.Blink(1120 - 4 * thr ); + left.Blink(1120 - 4 * thr ); + motore.lSet(thr); // Luminosita' proporzionale al throttle + } else { + // Throttle al massimo: LED laterali lampeggiano a caso, + // Sotto luminosita' a caso + + caso = random(20, 240) ; + right.Swap(); + left.Swap(); + motore.lSet(caso); +// TODO: check this + ailerons.Rand(); + delay(caso); + } + + + + //// Ailerons: + 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; + ailerons.Off(); + FSM_lastMillis = currentMillis; + } + else if (ail < mid_point - deviation - deviation / 3) { + ailstate = dxin; + ailerons.Off(); + FSM_lastMillis = currentMillis ; + } ; + break; + + case sxin: + // Transizione a sx + sxLamp.Blink(150); + if (currentMillis - pausa > FSM_lastMillis ) { + ailstate = sx; + } + break; + + case sx: + 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(150); + if (currentMillis - pausa > FSM_lastMillis ) { + ailstate = dx; + } + break; + + case dx: + 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(thrIn); + Serial.print("\tthr: "); +Serial.print(thr); + Serial.print("\tail: "); + Serial.print(ail); + Serial.print("\t ailstate:"); + Serial.println(ailstate); +#endif +} +// ISRs +void chRise2() { + attachInterrupt(0, chFall2, FALLING); + chStart2 = micros(); +} + +void chFall2() { + attachInterrupt(0, chRise2, RISING); + ail = micros() - chStart2; +} diff --git a/aerei/antonino/bugatti_fsm_mix/bugatti_fsm_mix.ino b/aerei/antonino/bugatti_fsm_mix/bugatti_fsm_mix.ino new file mode 100644 index 0000000..4c03df0 --- /dev/null +++ b/aerei/antonino/bugatti_fsm_mix/bugatti_fsm_mix.ino @@ -0,0 +1,226 @@ +/* Bugatti di Antonino + +Outputs: + 2 LED / Strisce laterali che lampeggiano alternativamente + 1 LED in PWM per il motore + 1 Striscia RGB sotto per tutta la lunghezza delle ali + +Inputs: + Lettura del canale Throttle (3) con la funzione Pulsein + Lettura alettoni con interrupt 0 (PIN2) + +TODO: +* Cambiare il PIN del throttle su A5 da A3 +* attaccare il canale degli alettoni al pin2 +* guardare che tipo di RGB e', anodo o cat +* a full throttle RGB fa un Rand, vedere che non vada in conflitto con la sec FSM + +*/ + +#include +#define dEBUG + +// LED disponibili +Lampeggiatore left = 7; +Lampeggiatore right = 8; +Pwm motore = 3; + +// RGB +RGBLed ailerons(6,5,9); +// Transizione: lampeggiatori sui PIN RGB +Lampeggiatore sxLamp(5); // Lampeggiatore +Lampeggiatore dxLamp(9); // Lampeggiatore +Pwm rsotto = 6; +Pwm gsotto = 5; +Pwm bsotto = 3; + + +// Var thr +//////////////// !!!! cambiare thrIn +const byte thrPin = A5; // PIN collegato al CH3 +byte thr ; // Throttle a 8bit +int thrIn ; // Valore del th in ingresso dal servo + +// Variabili per interrupt 0 su PIN 2 +volatile unsigned int ail = 1500; // Valore computato +volatile unsigned int chStart2 = 1500; // Inizio rilevamento + +// Variabili per autocalibrazione 0 +const byte chPin2 = 2; // PIN per la calibrazione +int mid_point2 = 1500; + +// 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 + + +// 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 = 600; // Pausa per la transizione durante gli stati 2, 4 della FSM + +// Variabili comuni: +unsigned long currentMillis; // timestamp reference per millis per tutto il loop +byte caso ; // Valore random + + +void setup() { + // I PINs vengono impostati dal constructor al momento + // della dichiarazione dell'ogetto. + pinMode(thrPin,INPUT); + right.Invert() ; // Opzionale: inverte l'ordine del lampeggio da + + attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168 + + randomSeed(analogRead(0)); + + // Test iniziale dei LED per verifica contatti: + left.High(); + right.High(); + ailerons.White(); + motore.Set(255); + delay(4000); + + + mid_point = calibraTrim(chPin2) + 8 ; // + LED di servizio per monitor calibrazione +#ifdef DEBUG + Serial.begin(9600); +#endif +} + +void loop() { + currentMillis = millis(); // Timestamp per tutto il loop + +// Lettura CH3 + thrIn = pulseIn(thrPin, HIGH, 25000); + // Hint: thrIn andrebbe calibrato son un Serial.write + if (thrIn != 0) { + thr = map(thrIn, 960, 2000, 0, 255); + }; + + +// Gestione throttle + if (thr >= 0 && thr < 15) { + // IDLE + + right.Blink(); + left.Blink(); + motore.UD(2000); + // RGB + rsotto.lDown(3000); + gsotto.lUp(1000); + bsotto.lup(2000); + + + } else if (thr < 245) { + // Throttle medio + + right.Blink(1120 - 4 * thr ); + left.Blink(1120 - 4 * thr ); + motore.lSet(thr); // Luminosita' proporzionale al throttle + + //// Ailerons: + switch (ailstate) { + case middle: + // ailerons.White(); + rsotto.UD(2000); + gsotto.UD(2000); + bsotto.UD(2000); + // Alettoni piatti + if (ail > mid_point + deviation + deviation /3) { + // extra margine per avere un po' di gioco + ailstate = sxin; + ailerons.Off(); + FSM_lastMillis = currentMillis; + } + else if (ail < mid_point - deviation - deviation / 3) { + ailstate = dxin; + ailerons.Off(); + FSM_lastMillis = currentMillis ; + } ; + break; + + case sxin: + // Transizione a sx + sxLamp.Blink(150); + if (currentMillis - pausa > FSM_lastMillis ) { + ailstate = sx; + } + break; + + case sx: + 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(150); + if (currentMillis - pausa > FSM_lastMillis ) { + ailstate = dx; + } + break; + + case dx: + ailerons.Blue(); + if (ail > mid_point - deviation) { + ailstate = middle; + } + else if (ail > mid_point + deviation) { + FSM_lastMillis = currentMillis; + ailstate = dxin; + } ; + break; + }; + + } else { + // Throttle al massimo: LED laterali lampeggiano a caso, + // Sotto luminosita' a caso + + caso = random(20, 240) ; + right.Swap(); + left.Swap(); + motore.lSet(caso); +// TODO: check this + ailerons.Rand(); + delay(caso); + } + + + + +#ifdef DEBUG + Serial.print(thrIn); + Serial.print("\tthr: "); + Serial.print(thr); + Serial.print("\tail: "); + Serial.print(ail); + Serial.print("\t ailstate:"); + Serial.println(ailstate); +#endif +} +// ISRs +void chRise2() { + attachInterrupt(0, chFall2, FALLING); + chStart2 = micros(); +} + +void chFall2() { + attachInterrupt(0, chRise2, RISING); + ail = micros() - chStart2; +} diff --git a/aerei/antonino/bugatti_fsm_prot/bugatti_fsm_prot.ino b/aerei/antonino/bugatti_fsm_prot/bugatti_fsm_prot.ino new file mode 100644 index 0000000..1e309f8 --- /dev/null +++ b/aerei/antonino/bugatti_fsm_prot/bugatti_fsm_prot.ino @@ -0,0 +1,205 @@ +/* Bugatti di Antonino + +Outputs: + 2 LED / Strisce laterali che lampeggiano alternativamente + 1 LED in PWM per il motore + 1 Striscia RGB sotto per tutta la lunghezza delle ali + +Inputs: + Lettura del canale Throttle (3) con la funzione Pulsein + Lettura alettoni con interrupt 0 (PIN2) + +*/ + +#include +#define DEBUG + +// LED disponibili +Lampeggiatore left = 7; +Lampeggiatore right = 8; +Pwm motore = 3; + +// RGB +RGBLed ailerons(11,10,9,255); // Common Cat + // Transizione: lampeggiatori sui PIN RGB + Lampeggiatore sxLamp(10); // Lampeggiatore + Lampeggiatore dxLamp(9); // Lampeggiatore +//Pwm rsotto = 6; +//Pwm gsotto = 5; +//Pwm bsotto = 3; + + +// Var thr +//////////////// !!!! cambiare thrIn +const byte thrPin = A5; // PIN collegato al CH3 +byte thr ; // Throttle a 8bit +int thrIn ; // Valore del th in ingresso dal servo + +// Variabili per interrupt 0 su PIN 2 +volatile unsigned int ail = 1500; // Valore computato +volatile unsigned int chStart2 = 1500; // Inizio rilevamento + +// Variabili per autocalibrazione 0 +const byte chPin2 = 2; // PIN per la calibrazione +int mid_point2 = 1500; + +// 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 + + +// 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 = 600; // Pausa per la transizione durante gli stati 2, 4 della FSM + +// Variabili comuni: +unsigned long currentMillis; // timestamp reference per millis per tutto il loop +byte caso ; // Valore random + + +void setup() { + // I PINs vengono impostati dal constructor al momento + // della dichiarazione dell'ogetto. + pinMode(thrPin,INPUT); + right.Invert() ; // Opzionale: inverte l'ordine del lampeggio da + + attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168 + + randomSeed(analogRead(0)); + + // Test iniziale dei LED per verifica contatti: + left.High(); + right.High(); + ailerons.White(); + motore.Set(255); + delay(4000); + + +#ifdef DEBUG + Serial.begin(9600); +#endif +} + +void loop() { +currentMillis = millis(); // Timestamp per tutto il loop + +// Lettura CH3 + thrIn = pulseIn(thrPin, HIGH, 25000); + // Hint: thrIn andrebbe calibrato son un Serial.write + if (thrIn != 0) { +thr = map(thrIn, 960, 2000, 0, 255); +}; + +// Gestione throttle + if (thr >= 0 && thr < 15) { + // IDLE + + right.Blink(); + left.Blink(); + motore.UD(2000); + } else if (thr < 245) { + // Throttle medio + + right.Blink(1120 - 4 * thr ); + left.Blink(1120 - 4 * thr ); + motore.lSet(thr); // Luminosita' proporzionale al throttle + } else { + // Throttle al massimo: LED laterali lampeggiano a caso, + // Sotto luminosita' a caso + + caso = random(20, 240) ; + right.Swap(); + left.Swap(); + motore.lSet(caso); + delay(caso); + } + + + + //// Ailerons: + 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; + ailerons.Off(); + FSM_lastMillis = currentMillis; + } + else if (ail < mid_point - deviation - deviation / 3) { + ailstate = dxin; + ailerons.Off(); + FSM_lastMillis = currentMillis ; + } ; + break; + + case sxin: + // Transizione a sx + sxLamp.Blink(150); + if (currentMillis - pausa > FSM_lastMillis ) { + ailstate = sx; + } + break; + + case sx: + 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(150); + if (currentMillis - pausa > FSM_lastMillis ) { + ailstate = dx; + } + break; + + case dx: + 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(thrIn); + Serial.print("\tthr: "); +Serial.print(thr); + Serial.print("\tail: "); + Serial.print(ail); + Serial.print("\t ailstate:"); + Serial.println(ailstate); +#endif +} +// ISRs +void chRise2() { + attachInterrupt(0, chFall2, FALLING); + chStart2 = micros(); +} + +void chFall2() { + attachInterrupt(0, chRise2, RISING); + ail = micros() - chStart2; +} diff --git a/aerei/daniele/fsm/fsm.ino b/aerei/daniele/fsm/fsm.ino index 10ef329..98ee6c9 100644 --- a/aerei/daniele/fsm/fsm.ino +++ b/aerei/daniele/fsm/fsm.ino @@ -75,8 +75,6 @@ Serial.begin(9600); } void loop() { -left.Blink(map(chValue2,1000,2000,200,800 ); -right.Blink(map(chValue2,1000,2000,800,200 ); //codasx.Blink(); //codadx.Blink(); @@ -87,14 +85,19 @@ right.Blink(map(chValue2,1000,2000,800,200 ); //pright.Up(1000); pcodasx.UD(2000); pcodadx.UD(2000); +pleft.lUp(1000); +pright.lDown(1000); + } else if (chValue3 > 1900) { // Throttle al massimo: LED laterali lampeggiano a caso, // Sotto luminosita' a caso caso = random(30, 250) ; -codasx.Swap(); -codadx.Swap(); +pleft.Set(); +pright.Set(); +pcodasx.Set(); +pcodadx.Set(); delay(caso); } else { @@ -102,8 +105,8 @@ codadx.Swap(); thrBit = map(chValue3,1050, 1900, 0, 255); codasx.Blink(1220 - 4 * thrBit ); codadx.Blink(1220 - 4 * thrBit ); - //left.Blink(chValue2 - 300); - //right.Blink(chValue2 - 300); +left.Blink(map(chValue2,1000,2000,200,800 )); +right.Blink(map(chValue2,1000,2000,800,200 )); } #ifdef DEBUG Serial.print("PIN2: "); diff --git a/esempi/ailerons_state_interrupt/ailerons_state_interrupt.ino b/esempi/ailerons_state_interrupt/ailerons_state_interrupt.ino index 58948e4..295208f 100644 --- a/esempi/ailerons_state_interrupt/ailerons_state_interrupt.ino +++ b/esempi/ailerons_state_interrupt/ailerons_state_interrupt.ino @@ -20,7 +20,7 @@ FSM per alettoni - piatto -> sx - piatto -> dx - +Note: TODO: * clean up magic numbers @@ -42,18 +42,14 @@ volatile unsigned int chStart3 = 1500; // Inizio rilevamento const byte chPin2 = 3; // PIN per la calibrazione int mid_point2 = 1500; +// LEDS +Pwm motore = 7; -// Variabili: -unsigned long currentMillis; // timestamp reference per millis per tutto il loop - -Pwm motore = 11; - -// Un LED RGB +//RGB RGBLed ailerons(11,10,9,255); // Common Cat - -// Transizione: Pwm -Lampeggiatore sxLamp(10); // Lampeggiatore -Lampeggiatore dxLamp(9); // Lampeggiatore + // Transizione: lampeggiatori sui PIN RGB + Lampeggiatore sxLamp(10); // Lampeggiatore + Lampeggiatore dxLamp(9); // Lampeggiatore // Variabili per lettura canale servo @@ -76,7 +72,11 @@ enum { // Stati della FMS // Vars FSM unsigned long FSM_lastMillis = 0 ; // Timestamp per la FSM degli alettoni -unsigned long pausa = 500; // Pausa per la transizione durante gli stati 2, 4 della FSM +unsigned long pausa = 600; // Pausa per la transizione durante gli stati 2, 4 della FSM + +// Variabili comuni: +unsigned long currentMillis; // timestamp reference per millis per tutto il loop + /////////////////////////////////////////////////////////// void setup() { @@ -100,24 +100,26 @@ mid_point = calibraTrim(ailPin) + 10 ; // + LED di servizio per monitor calibra void loop() { currentMillis = millis(); // Timestamp per tutto il loop - switch (ailstate) { + 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; + ailerons.Off(); FSM_lastMillis = currentMillis; } else if (ail < mid_point - deviation - deviation / 3) { ailstate = dxin; + ailerons.Off(); FSM_lastMillis = currentMillis ; } ; break; case sxin: // Transizione a sx - sxLamp.Blink(200); + sxLamp.Blink(150); if (currentMillis - pausa > FSM_lastMillis ) { ailstate = sx; } @@ -136,7 +138,7 @@ void loop() { case dxin: // Transizione a dx - dxLamp.Blink(200); + dxLamp.Blink(150); if (currentMillis - pausa > FSM_lastMillis ) { ailstate = dx; } @@ -165,7 +167,7 @@ Serial.print((thr - 980) / 4); Serial.println(ailstate); #endif } -// Functions +// ISRs void chRise2() { attachInterrupt(0, chFall2, FALLING); chStart2 = micros(); -- 2.39.2