]> git.piffa.net Git - aerei/commitdiff
Merge branch 'master' of kim.andreamanni.com:/home/git/aerei
authoreaman <eaman@pad.piffa.net>
Tue, 21 Feb 2017 12:50:21 +0000 (13:50 +0100)
committereaman <eaman@pad.piffa.net>
Tue, 21 Feb 2017 12:50:21 +0000 (13:50 +0100)
20 files changed:
README.html
README.rst
README.txt
aerei/daniele/fsm/fsm.ino [new file with mode: 0644]
aerei/daniele/prototipo/prototipo.ino
aerei/yak/interrupts/interrupts.ino [new file with mode: 0644]
aerei/yak/prototipo/prototipo.ino [new file with mode: 0644]
aerei/zeta/zeta/zeta.ino
aerei/zeta/zeta_5_state/zeta_5_state.ino [deleted file]
aerei/zeta/zeta_prot/zeta_prot.ino
esempi/ailerons_state_interrupt/ailerons_state_interrupt.ino [new file with mode: 0644]
esempi/ailerons_state_rgb/ailerons_state_rgb.ino
esempi/ailerons_state_thrFSM/ailerons_state_thrFSM.ino [new file with mode: 0644]
esempi/base_th_3stati/base_th_3stati.ino
esempi/snippets/calibrazione_servo/calibrazione_servo.ino
esempi/snippets/lettura_servo_ch_pulse/lettura_servo_ch_pulse.ino
esempi/snippets/lettura_servo_ch_pulse_millis/lettura_servo_ch_pulse_millis.ino
esempi/snippets/lettura_servo_interrupt/lettura_servo_interrupt.ino
esempi/snippets/lettura_servo_interrupt_doppio/lettura_servo_interrupt_doppio.ino
libraries/common/common.cpp

index bf2269da68c9bb2fc91bd63b51db82145383697d..936a310d2f9e3ee7aeae223d31931dbe816a3783 100644 (file)
@@ -374,8 +374,8 @@ IDE l'intero repo <tt class="docutils literal">aerei_arduino</tt> come cartella
 copiare la cartella <tt class="docutils literal">libraries/common</tt> dentro <tt class="docutils literal">libraries</tt> dello sketchbook
 attuale.</p>
 </div>
-<div class="section" id="aggiornamenti">
-<h1>Aggiornamenti</h1>
+<div class="section" id="download">
+<h1>Download</h1>
 <dl class="docutils">
 <dt>Download tramite git:</dt>
 <dd>git clone git://git.andreamanni.com/aerei</dd>
@@ -391,12 +391,26 @@ git pull
 <li><a class="reference external" href="https://git.andreamanni.com/web?p=aerei">Interfaccia git Web</a></li>
 <li><a class="reference external" href="https://forum.piffa.net/viewforum.php?f=4&amp;sid=aef380599d890d6e7b9f92743fab9d7e">Foum di Discussione</a></li>
 </ul>
+<div class="section" id="libreria">
+<h2>Libreria</h2>
+<p>La sola libreria Common contenete gli oggetti base puo' essere scaricata direttamente nella vostra cartella libraries da:</p>
+<pre class="literal-block">
+git clone git://git.andreamanni.com/common
+</pre>
+<ul class="simple">
+<li><a class="reference external" href="https://git.andreamanni.com/web?p=common;a=snapshot;h=HEAD;sf=tgz">Download manuale archivio</a></li>
+<li><a class="reference external" href="https://aerei.piffa.net/repo/libraries/common/">Browse on line</a></li>
+<li><a class="reference external" href="https://git.andreamanni.com/web?p=common">Interfaccia git Web</a></li>
+</ul>
+</div>
 </div>
 <div class="section" id="links">
 <h1>Links</h1>
 <p>Risorse utili per utilizzare Arduino:</p>
 <ul class="simple">
 <li>Per poter programmare le schede Arduino: <a class="reference external" href="https://www.arduino.cc/en/Main/Software#">Arduino IDE</a></li>
+<li>Testi consigliati: <a class="reference external" href="https://lab.piffa.net/testi.html">https://lab.piffa.net/testi.html</a></li>
+<li>Risorse online: <a class="reference external" href="http://wiki.piffa.net/index.php/Guide">http://wiki.piffa.net/index.php/Guide</a></li>
 <li><dl class="first docutils">
 <dt>Risorse di Andrea: <a class="reference external" href="https://lab.piffa.net/">https://lab.piffa.net/</a></dt>
 <dd><ul class="first last">
@@ -406,8 +420,6 @@ git pull
 </dd>
 </dl>
 </li>
-<li>Testi consigliati: <a class="reference external" href="https://lab.piffa.net/testi.html">https://lab.piffa.net/testi.html</a></li>
-<li>Risorse online: <a class="reference external" href="http://wiki.piffa.net/index.php/Guide">http://wiki.piffa.net/index.php/Guide</a></li>
 </ul>
 </div>
 </div>
index 881a593b2048072bdc089a8c89bae1a303f75f11..88e033bd987db07c3ba24f3e73f4907098acd91a 100644 (file)
@@ -34,12 +34,13 @@ Per aggiornare all'ultima versione::
 Libreria
 ------------
 
-La sola libreria Common contenete gli oggetti base puo' essere scaricata direttamente nella vostra cartella libraries da:
+La sola libreria Common contenete gli oggetti base puo' essere scaricata direttamente nella vostra cartella libraries da::
+
     git clone git://git.andreamanni.com/common
 
-* `Download manuale archivio <https://git.andreamanni.com/web?p=common;a=snapshot;h=HEAD;sf=tgz>`_
-* `Browse on line <https://aerei.piffa.net/repo/libraries/common/>`_
-* `Interfaccia git Web <https://git.andreamanni.com/web?p=common>`_
+* `Download manuale archivio <https://git.andreamanni.com/web?p=common;a=snapshot;h=HEAD;sf=tgz>`__
+* `Browse on line <https://aerei.piffa.net/repo/libraries/common/>`__
+* `Interfaccia git Web <https://git.andreamanni.com/web?p=common>`__
 
 
 Links
@@ -47,8 +48,8 @@ Links
 Risorse utili per utilizzare Arduino:
 
 * Per poter programmare le schede Arduino: `Arduino IDE <https://www.arduino.cc/en/Main/Software#>`_
+* Testi consigliati: https://lab.piffa.net/testi.html
+* Risorse online: http://wiki.piffa.net/index.php/Guide
 * Risorse di Andrea: https://lab.piffa.net/
     * Wiki: http://wiki.piffa.net/
     * Forum: https://forum.piffa.net/
-* Testi consigliati: https://lab.piffa.net/testi.html
-* Risorse online: http://wiki.piffa.net/index.php/Guide
index 564ce41ec2d778d61edd1f45d8edcd888e752297..a4155db3a98c17d71953dfffd7cee7b45d80485e 100644 (file)
@@ -12,37 +12,52 @@ Setup
    sketchbook, oppure copiare la cartella libraries/common dentro
    libraries dello sketchbook attuale.
 
-Aggiornamenti
+Download
 
    Download tramite git:
           git clone git://git.andreamanni.com/aerei
 
    Per aggiornare all'ultima versione:
-        cd ~/aerei
-        git pull
+cd ~/aerei
+git pull
 
-   Download manuale archivio:
-          [1]https://git.andreamanni.com/web?p=aerei;a=snapshot;h=HEAD;sf=
-          tgz
+     * [1]Download manuale archivio
+     * [2]Browse on line
+     * [3]Interfaccia git Web
+     * [4]Foum di Discussione
 
-   Browse on line:
-          [2]https://aerei.piffa.net/repo/
+Libreria
 
-   Interfaccia git Web:
-          [3]https://git.andreamanni.com/web?p=aerei
+   La sola libreria Common contenete gli oggetti base puo' essere
+   scaricata direttamente nella vostra cartella libraries da:
+git clone git://git.andreamanni.com/common
 
-   Discussioni:
-          [4]https://forum.piffa.net/viewforum.php?f=4&sid=aef380599d890d6
-          e7b9f92743fab9d7e
+     * [5]Download manuale archivio
+     * [6]Browse on line
+     * [7]Interfaccia git Web
 
 Links
 
    Risorse utili per utilizzare Arduino:
-     * Per poter programmare le schede Arduino: Arduino IDE:
-       [5]https://www.arduino.cc/en/Main/Software#
-     * Risorse di Andrea: [6]https://lab.piffa.net/
-       - Wiki: [7]http://wiki.piffa.net/
-       - Forum: [8]https://forum.piffa.net/
+     * Per poter programmare le schede Arduino: [8]Arduino IDE
      * Testi consigliati: [9]https://lab.piffa.net/testi.html
      * Risorse online: [10]http://wiki.piffa.net/index.php/Guide
-
+     * Risorse di Andrea: [11]https://lab.piffa.net/
+               o Wiki: [12]http://wiki.piffa.net/
+               o Forum: [13]https://forum.piffa.net/
+
+References
+
+   1. https://git.andreamanni.com/web?p=aerei;a=snapshot;h=HEAD;sf=tgz
+   2. https://aerei.piffa.net/repo/
+   3. https://git.andreamanni.com/web?p=aerei
+   4. https://forum.piffa.net/viewforum.php?f=4&sid=aef380599d890d6e7b9f92743fab9d7e
+   5. https://git.andreamanni.com/web?p=common;a=snapshot;h=HEAD;sf=tgz
+   6. https://aerei.piffa.net/repo/libraries/common/
+   7. https://git.andreamanni.com/web?p=common
+   8. https://www.arduino.cc/en/Main/Software
+   9. https://lab.piffa.net/testi.html
+  10. http://wiki.piffa.net/index.php/Guide
+  11. https://lab.piffa.net/
+  12. http://wiki.piffa.net/
+  13. https://forum.piffa.net/
diff --git a/aerei/daniele/fsm/fsm.ino b/aerei/daniele/fsm/fsm.ino
new file mode 100644 (file)
index 0000000..10ef329
--- /dev/null
@@ -0,0 +1,140 @@
+/* Aereo di Daniele
+
+FSM: il throttle e' a posto
+Prototipo: F8 Bearcat
+
+Output:
+   2 LED PWM ai lati con lampeggio alternato
+   2 LED PWM alle estremita ali
+
+Input:
+    2 interrupts per th e alettone
+    PIN 2:  alettone
+    PIN 3:  throttle
+
+
+    TODO
+* Vedere la calibrazione automatica
+* Min e max a 1000 - 2000 per alettone
+
+TODO: 
+Aggiungere FSM per alettone: lampeggi alternati
+in base a chValue2
+*/
+
+#include <common.h>
+# define dEBUG
+
+// Instanziamo un LED fuori dal loop
+Lampeggiatore left = 6;
+Lampeggiatore right = 9;
+Lampeggiatore codasx = 5;
+Lampeggiatore codadx = 10;
+
+Pwm pleft = 6;
+Pwm pright = 9;
+Pwm pcodasx = 5;
+Pwm pcodadx = 10;
+
+// Variabili per interrupt 0 si PIN 2
+volatile unsigned int chValue2 = 1500; // Valore computato
+volatile unsigned int chStart2 = 1500; // Inizio rilevamento
+
+// Variabili per interrupt 1 su PIN 3
+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 alettone
+int mid_point2 = 1500;
+
+// Variabili per autocalibrazione 1
+const byte chPin3 = 3; // PIN per la calibrazione
+int mid_point3 = 1000;
+
+// Variabili
+int caso ;
+int thrBit;
+
+void setup() {
+  // I PINs vengono impostati dal constructor al momento
+  // della dichiarazione dell'ogetto.
+right.Invert();
+codadx.Invert();
+
+  // 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() {
+left.Blink(map(chValue2,1000,2000,200,800 );
+right.Blink(map(chValue2,1000,2000,800,200 );
+//codasx.Blink();
+//codadx.Blink();
+
+// Gestione throttle
+    if (chValue3 < 1050) {
+        // IDLE
+//pleft.Up(1000);
+//pright.Up(1000);
+pcodasx.UD(2000);
+pcodadx.UD(2000);
+
+    }
+    else if (chValue3 > 1900) {
+        // Throttle al massimo: LED laterali lampeggiano a caso,
+        // Sotto luminosita' a caso
+        caso = random(30, 250) ;
+codasx.Swap();
+codadx.Swap();
+        delay(caso);
+    }
+    else {
+        // Throttle medio
+        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);
+    }
+#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;
+}
index 7e4b2941a4a50e40b30a4e8271baff4399e9a2f1..dffeaa52be622c7599df2e3f3847070d1c10e637 100644 (file)
@@ -1,10 +1,10 @@
 /* Aereo di Daniele
 
-Prototipo:
+Prototipo: F8 Bearcat
 
 Output:
-   2 LED ai lati con lampeggio alternato
-   1 LED PWM per motore
+   2 LED PWM ai lati con lampeggio alternato
+   2 LED PWM alle estremita ali
 
 Input:
     2 interrupts per th e alettone
@@ -22,9 +22,15 @@ Input:
 # define DEBUG
 
 // Instanziamo un LED fuori dal loop
-Lampeggiatore left = 5;
-Lampeggiatore right = 6;
-Lampeggiatore coda = 9;
+Lampeggiatore left = 6;
+Lampeggiatore right = 9;
+Lampeggiatore codasx = 5;
+Lampeggiatore codadx = 10;
+
+//Pwm pleft = 6;
+//Pwm pright = 9;
+//Pwm pcodasx = 5;
+//Pwm pcodadx = 10;
 
 // Variabili per interrupt 0 si PIN 2
 volatile unsigned int chValue2 = 1500; // Valore computato
@@ -46,6 +52,8 @@ int mid_point3 = 1000;
 void setup() {
   // I PINs vengono impostati dal constructor al momento
   // della dichiarazione dell'ogetto.
+right.Invert();
+codadx.Invert();
 
   // HI -> LOW --> LOW -> HI
   // per avere 2 LED che lampeggiano alternativamente
@@ -60,6 +68,10 @@ Serial.begin(9600);
 }
 
 void loop() {
+left.Blink(300);
+right.Blink(300);
+codasx.Blink();
+codadx.Blink();
 
 #ifdef DEBUG
     Serial.print("PIN2: ");
diff --git a/aerei/yak/interrupts/interrupts.ino b/aerei/yak/interrupts/interrupts.ino
new file mode 100644 (file)
index 0000000..5973e8d
--- /dev/null
@@ -0,0 +1,93 @@
+/* Yak 980 di A.
+
+   Prototipo:
+
+OUTPUT:
+   1 PWM motore: 10
+   2 LED ai lati con lampeggio alternato
+   1 LED in coda lampeggio a freq doppia
+   3 Sequenza di LED da 3 unita' ripetuta 3 volte
+
+INPUT:
+
+   * Thtottle: PIN 2
+   * Ailerons: PIN 3
+
+Notes: abbondante usi di map().
+NO FSM.
+
+*/
+
+#include <common.h>
+#define dEBUG
+
+// Variabili per interrupt 0 si PIN 2
+volatile unsigned int chValue2 = 1500; // Valore computato
+volatile unsigned int chStart2 = 1500; // Inizio rilevamento
+
+// Variabili per interrupt 1 su PIN 3
+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 = 980;
+
+// Variabili per autocalibrazione 1
+const byte chPin3 = 3; // PIN per la calibrazione
+int mid_point3 = 1500;
+
+// Instanziamo un LED fuori dal loop
+Lampeggiatore left = 4;
+Lampeggiatore right = 11;
+Lampeggiatore coda = 6;
+Pwm motore = 10;
+Lampeggiatore stato = 13;
+byte pins[] = {  // PIN dei LED che compongono la sequenza
+       7,8,9 
+}; 
+Sequenza seq = Sequenza(pins,3);
+
+void setup() {
+left.Invert();
+    // Funzione relativa a calibrazione con pulsein:
+    mid_point3 =  calibraTrim(chPin3) + 8; // La calibrazione in genere non serve
+    // su throttle, il valore minimo e' costante e non c'e' TRIM
+
+    attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168
+    attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168
+
+} ;
+
+
+void loop() {
+stato.Blink(); // Onboard LED per vedere se la scheda e' accesa
+
+  right.Blink(map(chValue3,980,2020,800,200));
+  left.Blink(map(chValue3,980,2020,200,800));
+  coda.Blink(chValue2 /4);
+  motore.lSet((chValue2 -980) / 4);
+  seq.Update(map(chValue2,980,2000,300,40));
+
+}
+
+// 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;
+}
diff --git a/aerei/yak/prototipo/prototipo.ino b/aerei/yak/prototipo/prototipo.ino
new file mode 100644 (file)
index 0000000..9b94212
--- /dev/null
@@ -0,0 +1,40 @@
+/* Yak 980 di A.
+
+   Prototipo base:
+OUTPUT:
+   1 PWM motore: 10
+   2 LED ai lati con lampeggio alternato
+   1 LED in coda lampeggio a freq doppia
+   3 Sequenza di LED da 3 unita' ripetuta 3 volte
+*/
+
+#include <common.h>
+
+// Instanziamo un LED fuori dal loop
+Pwm pLeft = 11;
+Lampeggiatore right = 4;
+Lampeggiatore coda = 6;
+Pwm motore = 10;
+byte pins[] = {  // PIN dei LED che compongono la sequenza
+       7,8,9 
+}; 
+
+Sequenza seq = Sequenza(pins,3);
+
+void setup() {
+  // I PINs vengono impostati dal constructor al momento
+  // della dichiarazione dell'ogetto.
+
+ // right.Invert() ;  // Opzionale: inverte l'ordine del lampeggio da
+  // HI -> LOW --> LOW -> HI
+  // per avere 2 LED che lampeggiano alternativamente
+}
+
+void loop() {
+//  left.Blink();   // Lampeggia con un default di 1sec (0.5 HI 0.5 LOW)
+  right.Blink();
+  pLeft.UD(2000);
+  coda.Blink();
+  motore.UD(2000);
+  seq.Update(200);
+}
index 7d0fbdbe962f27ecd033686046a545812425a594..9eeb455045a1a4fdc4dca5105d0823731f6ecb84 100644 (file)
@@ -1,7 +1,10 @@
 /* Zeta
 
-Nota: rivedere i PWM per come calcolano le pause:
-non possono schendere sotto a 1ms
+Versione con solo il throttle come input via pulseIn.
+
+Aggiunta striscia RGB sotto.
+
+
 */
 
 #include <common.h>
diff --git a/aerei/zeta/zeta_5_state/zeta_5_state.ino b/aerei/zeta/zeta_5_state/zeta_5_state.ino
deleted file mode 100644 (file)
index cae5347..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-/* Zeta test
- *  le funzioni finali sono da controllare
-
-
-*/
-
-#include <common.h>
-enum states_available { // Stati della FMS
-  idle,    // Throttle a zero
-  normal,  // Th normale
-  full,    // Th massimo
-};
-states_available state  ;
-
-// Due LED con lampeggio alternato:
-Lampeggiatore right = 3;
-Lampeggiatore left = 5;
-const byte rtail = 6;
-const byte ltail = 9;
-const byte thrPin = A3;
-byte thr ;
-int thrIn ;
-
-Pwm rwhite = 3;
-Pwm lwhite = 5;
-Pwm rtp = 6 ;
-Pwm ltp = 9 ;
-
-
-void setup() {
-  left.Invert() ; // Parte da stato invertito rispetto al default
-  pinMode(rtail, OUTPUT);
-  pinMode(ltail, OUTPUT);
-  pinMode(thrPin, INPUT);
- // Warning: serial breaks PWM
-   Serial.begin(9600);
-
-  randomSeed(analogRead(0));
-}
-
-void loop() {
-
-  thrIn = pulseIn(thrPin, HIGH, 25000);
-  thr = constrain(map(thrIn, 983, 2000, 0, 255), 0, 255) ;
-
-// FMS dispatcher
-  if ( thr < 10 ) {
-    state = idle ;
-  } else if ( thr > 245 ) {
-    state = full ;
-  } else {
-    state = normal ;
-  }
-
-  switch (state) {
-    case idle:
-rwhite.Up(200);
-lwhite.Up(200);
-ltp.Up(200);
-rtp.Up(200);
-      break;
-
-    case normal:
-      // Due LED con lampeggio alternato:
-      right.Blink(1120 - 4 * thr );
-      left.Blink(1120 - 4 * thr );
-      analogWrite(rtail, thr);
-      analogWrite(ltail, thr);
-      break;
-      
-    case full:
-      // Due LED con lampeggio alternato:
-      right.Blink(1120 - 4 * thr );
-      left.Blink(1120 - 4 * thr );
-      digitalWrite(rtail, !digitalRead(rtail));
-      digitalWrite(ltail, !digitalRead(ltail));
-      delay(random(20, 100));
-      break;
-  }
-
-  
-  Serial.println(thrIn);
-  Serial.print("\t thr:");
-  Serial.print(thr);
-  Serial.print("\t state:");
-  Serial.println(state);
-
-}
index 7243b22c7805a11b8f8c4427adb138a95b50b9a5..5e2402411329b728fbeded783be7ee01f2b43ce3 100644 (file)
@@ -1,82 +1,28 @@
 /* Zeta prototipo
 
-   Sketch da breadboard, il throttle e' simulato con un potenziometro
-   o con una RX.
-
+Test dei vari LED
 */
 
 #include <common.h>
-enum  { // Stati della FMS
-  idle,    // Throttle a zero
-  normal,  // Th normale
-  full,    // Th massimo
-} state  ;
-
-// Due LED con lampeggio alternato:
-Lampeggiatore right = 13;
-Pwm rpwm = 3;
-
-// Variabili
-const byte thrPin = A3;
-byte thr ;
-int thrIn ;
 
 
+// Due LED con lampeggio alternato:
+Lampeggiatore right = 3;
+Lampeggiatore left = 5;
+const byte rtail = 6;
+const byte ltail = 9;
 
 void setup() {
-  Serial.begin(9600);
-  pinMode(A3, INPUT);
-  randomSeed(analogRead(0));
+  left.Invert() ; // Parte da stato invertito rispetto al default
+  pinMode(rtail,OUTPUT);
+  pinMode(ltail,OUTPUT);
+    
 }
 
 void loop() {
-
-  // Utilizzando un potenziometro
-  //  thrIn = analogRead(3);
-  //  thr = constrain(thrIn / 4 , 0, 255) ;
-
-  // Utilizzando una RX
-  thrIn = pulseIn(thrPin, HIGH, 25000);
-  // Hint: thrIn andrebbe calibrato son un Serial.write
-  thr = constrain(map(thrIn, 960, 2000, 0, 255), 0, 255);
-
-  // FMS dispatcher
-  if ( thr < 10 ) {
-    state = idle ;
-  } else if ( thr > 245 ) {
-    state = full ;
-  } else {
-    state = normal ;
-  }
-
-  switch (state) {
-    case idle:
-      rpwm.UD(2000);
-      right.Blink();
-      break;
-
-    case normal:
-      // Due LED con lampeggio alternato:
-      right.Blink(1120 - 4 * thr );
-      rpwm.lSet(thr);
-      break;
-
-    case full:
-      digitalWrite(3, HIGH);
-      rpwm.Set(0);
-      right.Swap();
-      delay(50);
-      rpwm.Set(255);
-      right.Swap();
-      delay(50);
-      break;
-  }
-
-
-  Serial.print(thrIn);
-  Serial.print("\t thr:");
-  Serial.print(thr);
-  Serial.print("\t state:");
-  Serial.println(state);
-  //  delay(200);
+  // Due LED con lampeggio alternato:
+  right.Blink();
+  left.Blink();
+  digitalWrite(rtail,HIGH);
+  digitalWrite(ltail,HIGH);
 }
diff --git a/esempi/ailerons_state_interrupt/ailerons_state_interrupt.ino b/esempi/ailerons_state_interrupt/ailerons_state_interrupt.ino
new file mode 100644 (file)
index 0000000..58948e4
--- /dev/null
@@ -0,0 +1,187 @@
+/* Ailerons state machine
+            Serial.print(mid_point);
+
+Pilotare un LED RGB in base al canale degli alettoni:
+Questo sketch usa 2 interrupts per thr e alettoni.
+
+INPUT:
+PIN 2   : throttle
+PIN 3   : alettoni
+
+OUTPUT:
+RGB Alettoni
+Motore PWM
+
+FSM per alettoni
+= 3 stati + 2 transizioni:
+- piatto
+- roll a sx
+- roll a dx
+- piatto -> sx
+- piatto -> dx
+
+
+
+TODO:
+* clean up magic numbers
+
+*/
+
+#include <common.h>
+#define dEBUG
+
+// Variabili per interrupt 0 si PIN 2
+volatile unsigned int thr = 1500; // Valore computato
+volatile unsigned int chStart2 = 1500; // Inizio rilevamento
+
+// Variabili per interrupt 1 su PIN 3
+volatile unsigned int ail = 1500; // Valore computato
+volatile unsigned int chStart3 = 1500; // Inizio rilevamento
+
+// Variabili per autocalibrazione 0
+const byte chPin2 = 3; // PIN per la calibrazione
+int mid_point2 = 1500;
+
+
+// Variabili:
+unsigned long currentMillis; // timestamp reference per millis per tutto il loop
+
+Pwm motore = 11;
+
+// Un LED RGB
+RGBLed ailerons(11,10,9,255); // Common Cat
+
+// Transizione: Pwm
+Lampeggiatore sxLamp(10); // Lampeggiatore
+Lampeggiatore dxLamp(9); // Lampeggiatore
+
+
+// Variabili per lettura canale servo
+byte ailPin = 3; // Calibrazione
+
+// 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 = 500;  // Pausa per la transizione durante gli stati 2, 4 della FSM
+
+///////////////////////////////////////////////////////////
+void setup() {
+
+
+#ifdef DEBUG
+   Serial.begin(9600);
+#endif
+
+    attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168
+    attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168
+
+
+// Funzione relativa a calibrazione:
+mid_point =  calibraTrim(ailPin) + 10 ; // + LED di servizio per monitor calibrazione
+//Serial.print(mid_point);
+//while(1) {
+//}
+}
+
+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:
+        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:
+        ailerons.Blue();
+        if (ail > mid_point - deviation) {
+            ailstate = middle;
+        }
+        else if (ail > mid_point + deviation) {
+            FSM_lastMillis = currentMillis;
+            ailstate = dxin;
+        } ;
+        break;
+    }
+
+// PWM Motore
+motore.lSet((thr - 980) / 4); // 980 = minimo
+        
+#ifdef DEBUG
+Serial.print((thr - 980) / 4);
+    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);
+    thr = micros() - chStart2;
+}
+// Seconod iterrupt
+void chRise3() {
+    attachInterrupt(1, chFall3, FALLING);
+    chStart3 = micros();
+}
+
+void chFall3() {
+    attachInterrupt(1, chRise3, RISING);
+    ail = micros() - chStart3;
+}
index 3ffa994f3e425ba1cb9a7fbc181392836020fbb4..f81251372c79f773d220a3000df41e44579fd129 100644 (file)
@@ -1,21 +1,21 @@
 /* Ailerons state machine
 
 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
 
-NOTE: uso di goto all'interno dell FSM.
-
 TODO:
-
 * clean up magic numbers
 
 */
 
 #include <common.h>
+#define dEBUG // Cambiare in DEBUG per il debug
 
 // Variabili:
 unsigned long currentMillis; // timestamp reference per millis per tutto il loop
@@ -24,8 +24,8 @@ unsigned long currentMillis; // timestamp reference per millis per tutto il loop
 RGBLed ailerons(11,10,9,255); // Common Cat
 
 // Transizione: Pwm
-Pwm sxLamp(10); // Lampeggiatore
-Pwm dxLamp(9); // Lampeggiatore
+Lampeggiatore sxLamp(10); // Lampeggiatore
+Lampeggiatore dxLamp(9); // Lampeggiatore
 
 
 // Variabili per lettura canale servo
@@ -62,8 +62,8 @@ void setup() {
    Serial.begin(9600);
 #endif
 
-    // Funzione relativa a calibrazione:
-    mid_point =  calibraTrim(ailPin) ; // + LED di servizio per monitor calibrazione
+// Funzione relativa a calibrazione:
+mid_point =  calibraTrim(ailPin) ; // + LED di servizio per monitor calibrazione
 }
 
 void loop() {
@@ -99,7 +99,7 @@ void loop() {
 
     case sxin:
         // Transizione a sx
-        sxLamp.(200);
+        sxLamp.Blink(200);
         if (currentMillis - pausa > FSM_lastMillis ) {
             ailstate = sx;
         }
@@ -119,7 +119,7 @@ void loop() {
 
     case dxin:
         // Transizione a dx
-        dxLamp.(200);
+        dxLamp.Blink(200);
         if (currentMillis - pausa > FSM_lastMillis ) {
             ailstate = dx;
         }
diff --git a/esempi/ailerons_state_thrFSM/ailerons_state_thrFSM.ino b/esempi/ailerons_state_thrFSM/ailerons_state_thrFSM.ino
new file mode 100644 (file)
index 0000000..743bcc1
--- /dev/null
@@ -0,0 +1,229 @@
+/* Ailerons state machine
+            Serial.print(mid_point);
+
+Pilotare un LED RGB in base al canale degli alettoni:
+Questo sketch usa 2 interrupts per thr e alettoni.
+
+INPUT:
+PIN 2   : throttle
+PIN 3   : alettoni
+
+OUTPUT:
+ailerons       RGB Alettoni 
+motore         Motore PWM
+left, right 2 Lampeggiatori PWM laterali + PWM
+
+FSM per alettoni
+= 3 stati + 2 transizioni:
+- piatto
+- roll a sx
+- roll a dx
+- piatto -> sx
+- piatto -> dx
+
+
+Ciclo if per gestione di 3 stati del motore:
+- idle
+- middle 
+- max
+
+
+TODO:
+* Da testare! Mai provato.
+
+*/
+
+#include <common.h>
+#define DEBUG
+
+// Variabili per interrupt 0 si PIN 2
+volatile unsigned int thr = 1500; // Valore computato
+volatile unsigned int chStart2 = 1500; // Inizio rilevamento
+
+// Variabili per interrupt 1 su PIN 3
+volatile unsigned int ail = 1500; // Valore computato
+volatile unsigned int chStart3 = 1500; // Inizio rilevamento
+
+// Variabili per autocalibrazione 0
+const byte chPin2 = 3; // PIN per la calibrazione
+int mid_point2 = 1500;
+
+// LEDs:
+Pwm motore = 11;
+
+// LED RGB alettoni
+RGBLed ailerons(5,6,9,255); // Common Cat
+// Transizione: Pwm
+Lampeggiatore sxLamp(6); // Lampeggiatore
+Lampeggiatore dxLamp(9); // Lampeggiatore
+
+
+// Variabili per lettura canale servo
+byte ailPin = 3; // Calibrazione
+
+// 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
+
+// Led motore e altri:
+Lampeggiatore left = 10;
+Lampeggiatore right = 12;
+Pwm sotto = 9;
+
+// Quando il Throttle e' in IDE facciamo un PWM anche sui laterali
+Pwm lpwm = 10 ;
+Pwm rpwm = 12;
+
+// Variabili:
+unsigned long currentMillis; // timestamp reference per millis per tutto il loop
+byte caso; // Random var
+byte thrBit ; // Valore a 8bit per il throttle
+
+
+
+
+// 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 = 500;  // Pausa per la transizione durante gli stati 2, 4 della FSM
+
+///////////////////////////////////////////////////////////
+void setup() {
+
+
+#ifdef DEBUG
+   Serial.begin(9600);
+#endif
+
+    attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168
+    attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168
+
+
+// Funzione relativa a calibrazione:
+mid_point =  calibraTrim(ailPin) + 10 ; // + LED di servizio per monitor calibrazione
+//Serial.print(mid_point);
+//while(1) {
+//}
+}
+
+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:
+        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:
+        ailerons.Blue();
+        if (ail > mid_point - deviation) {
+            ailstate = middle;
+        }
+        else if (ail > mid_point + deviation) {
+            FSM_lastMillis = currentMillis;
+            ailstate = dxin;
+        } ;
+        break;
+    }
+
+// Gestione throttle
+    if (thr < 1050) {
+        // IDLE
+        rpwm.UD(2000);
+        lpwm.UD(2000);
+        motore.lDown(1500);
+    }
+    else if (thr > 1900) {
+        // Throttle al massimo: LED laterali lampeggiano a caso,
+        // Sotto luminosita' a caso
+        caso = random(30, 250) ;
+        right.Swap();
+        left.Swap();
+        motore.lSet(caso);
+        delay(caso); // Blocking!
+    }
+    else {
+        // Throttle medio
+        thrBit = map(thr,1050, 1900, 0, 255);
+        right.Blink(1220 - 4 * thrBit );
+        left.Blink(1220 - 4 * thrBit );
+        motore.lSet(thrBit);   // Luminosita' proporzionale al throttle
+    }
+
+        
+#ifdef DEBUG
+Serial.print((thr - 980) / 4);
+    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);
+    thr = micros() - chStart2;
+}
+// Seconod iterrupt
+void chRise3() {
+    attachInterrupt(1, chFall3, FALLING);
+    chStart3 = micros();
+}
+
+void chFall3() {
+    attachInterrupt(1, chRise3, RISING);
+    ail = micros() - chStart3;
+}
index e865f0a4012565b807318a442f67ae5d90f74b45..22ea4c24c0c4305721d7dcf01d2fa4f7331ec5fd 100644 (file)
 
 // LED disponibili
 Lampeggiatore left = 10;
-Lampeggiatore right = 11;
+Lampeggiatore right = 12;
 Pwm sotto = 9;
 
 // Quando il Throttle e' in IDE facciamo un PWM anche sui laterali
 Pwm lpwm = 10 ;
-Pwm rpwm = 11;
+Pwm rpwm = 12;
 
 // Variabili
 const byte thrPin = 3; // PIN collegato al CH3
index fdea33825ea27a05cbb58eb248236d0366a1d238..f636fd5607cc6b831c2ba423b4e7913e7007d826 100644 (file)
@@ -10,6 +10,10 @@ Nota: questa funzione usa PULSEIN in modo da poter lavorare su
 qualunque PIN, considerando che in genere viene eseguita 
 una sola volta nel SETUP.
 
+Si noti che se il canale viene poi letto con un interrupt e non con pulseIn
+statisticamente bisogna incrementare (almeno sulle mie RX) il valore ottenuto
+di circa 10.
+
 Se si vuole qualcosa non blocking sostituire PULSEIN con 
 un interrupt.
 
@@ -25,7 +29,7 @@ void setup() {
     Serial.begin(9600); // Se abilitato la calibrazione viene tracciata su seriale
     // Abilitando anche DEBUG nella libreria
 
-    mid_point =  calibraTrim(ailPin) ; // Pin a cui e' collegato il canale
+    mid_point =  calibraTrim(ailPin) + 8; // Pin a cui e' collegato il canale
 //mid_point =  calibraTrim(ailPin,11) ; // + LED di servizio per calibrazione
 
 Serial.print("Valore calibrato: ");
index ccd6c68a2d13fd46fb1fade4e383b40d23a8d156..269db74abcdb3c02e7e0d4e7a244bb2f730e9f5e 100644 (file)
@@ -16,7 +16,7 @@
 
 
 // Variabili
-const byte chPin = A4; // PIN su cui e' collegato il canale
+const byte chPin = 2; // PIN su cui e' collegato il canale
 unsigned int chIn = 1500; // Valore catturato
 unsigned int chValue = 1500; // Valore computato
 // Attenzione che pulsein e' blocking
@@ -26,22 +26,24 @@ int mid_point = 1500;
 
 void setup() {
     // Funzione relativa a calibrazione:
-    mid_point =  calibraTrim(chPin) - 10; // Con pulse in si toglie 10
-Serial.begin(9600); // Warning: interrupts e serial potrebbero dare problemi
+    mid_point =  calibraTrim(chPin)  ; 
 
-} ;
+Serial.begin(9600); // Warning: interrupts e serial potrebbero dare problemi
+};
 
 void loop() {
-
-// Lettura ailerons channel ogni 200ms
+// Lettura canale
         chIn = pulseIn(chPin, HIGH, 25000);
-        if (chIn != 0 && chIn > 1000 && chIn <2000)  {
-            // get only resonable values
+        if (chIn > 1000 && chIn <2000)  {
+            // Scartiamo valori fuori range, talvolta pero' alcune RX
+            // hanno il minimo un po' sotto a 1000
             chValue = chIn;
     };
 
-
 // do something with    chValue
+
+    Serial.print(chIn);
+    Serial.print(" > chValue= ");
     Serial.print(chValue);
     Serial.print(" - base: ");
     Serial.println(mid_point);
index 81cdd7758f66aa01de3925c3e33315f4e927e8c6..b25af091039f77d81a2d5150f0ec7dc928744d7b 100644 (file)
 unsigned long currentMillis; // timestamp reference per millis per tutto il loop
 
 // Variabili
-const byte chPin = A4; // PIN su cui e' collegato il canale
+const byte chPin = 2; // PIN su cui e' collegato il canale
 long unsigned chStamp = 0; // Timestamp per
 unsigned int chIn = 1500; // Valore catturato
 unsigned int chValue = 1500; // Valore computato
 unsigned int freq = 200 ; // Ogni quanti millisecondi leggere il valore
+int mid_point = 1500; // Punto medio per calibrazione
 // Attenzione che pulsein e' blocking
 
 void setup() {
-    // Funzione relativa a calibrazione:
-//    mid_point =  calibraTrim(chPin) +10 ; // Con pulse in c'e' una traslazione ~10
+// Funzione relativa a calibrazione: per il throttle si puo' evitare
+    mid_point =  calibraTrim(chPin)  ; 
+
 #ifdef DEBUG
 Serial.begin(9600); // Warning: interrupts e serial potrebbero dare problemi
-} ;
 #endif
+} ;
 
 void loop() {
     currentMillis = millis(); // Timestamp per tutto il loop
index 2be4d729b27011f649356c492ea73c3ca379c87d..6f795bd36c1cfdebf0bb2dc9f4c9edcf6e131ea5 100644 (file)
@@ -18,13 +18,16 @@ const byte chPin = 2; // PIN per la calibrazione
 int mid_point = 1500;
 
 void setup() {
-    // Funzione relativa a calibrazione con pulsein:
-//    mid_point =  calibraTrim(chPin) ; // Calibrazione del TRIM attivo sul canale
-    attachInterrupt(0, chRise, RISING); // PIN 2 su 328p / 168
+// Funzione relativa a calibrazione con pulsein:
+ mid_point =  calibraTrim(chPin) + 8; // Calibrazione del TRIM con pulseIn:
+ // se si usa poi un interrupt si aggiunge ~10
+
+attachInterrupt(0, chRise, RISING); // PIN 2 su 328p / 168
+
 #ifdef DEBUG
 Serial.begin(9600); // Warning: interrupts e serial potrebbero dare problemi
-} ;
 #endif
+} ;
 
 void loop() {
 #ifdef DEBUG
index 7626b7058c310cf63453f8fb9e401a8fc51aabad..1c966f79a6c6a4d52e6e8baca93fa27980ac3ebe 100644 (file)
@@ -28,12 +28,16 @@ int mid_point3 = 1500;
 
 void setup() {
     // 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
+    mid_point2 =  calibraTrim(chPin2) + 8 ; // Calibrazione del TRIM attivo sul canale
+    mid_point3 =  calibraTrim(chPin3) + 8; // La calibrazione in genere non serve
+    // su throttle, il valore minimo e' costante e non c'e' TRIM
+
     attachInterrupt(0, chRise2, RISING); // PIN 2 su 328p / 168
     attachInterrupt(1, chRise3, RISING); // PIN 3 su 328p / 168
+
 #ifdef DEBUG
 Serial.begin(9600); 
+Serial.flush();
 #endif
 } ;
 
index e696ff4321e279a9cfd7bc98e632980c78e7c21b..e99f202804a45134155d354388719b26f8b1d0de 100644 (file)
@@ -417,7 +417,7 @@ int calibraTrim(int pin, const byte ledPin) {
     Serial.println(middle / 10 + 10);
     Serial.flush() ;
 #endif 
-    return(middle / 10 + 10) ;
+    return(middle / 10 ) ;
 // END calibrazione
 };