]> git.piffa.net Git - aerei/blob - esempi/state_throttle/state_throttle.ino
23e3a2d0fafbc948a2b1f2d0b6d53f04dc33dc62
[aerei] / esempi / state_throttle / state_throttle.ino
1 /* Throttle state machine
2
3 Idle, throttle e averburner effect basati sono sul canale throttle.   
4
5 FSM con dispatcher esterno.
6
7
8 */
9
10 #include <common.h>
11
12 enum  { // Stati della FMS
13   idle,    // Throttle a zero
14   normal,  // Th normale
15   full,    // Th massimo
16 } state  ;
17
18 // Due LED con lampeggio alternato:
19 Lampeggiatore right = 3;
20 Lampeggiatore left = 5;
21 Pwm motor = 7;
22
23 const byte thrPin = A3;
24 byte thr ; // Valore a 8bit per il throttle
25 int thrIn ; // Valore rilevato del 3 Ch della RX 
26
27
28
29 void setup() {
30   left.Invert() ; // Parte da stato invertito rispetto al default
31   randomSeed(analogRead(0));
32 }
33
34 void loop() {
35
36  // Lettura Throttle channel: FAKE con un potenziometro
37  //thrIn = analogRead(3);
38  //thr = constrain(thrIn / 4 , 0, 255) ;
39
40  // Lettura Throttle channel
41  thrIn = pulseIn(thrPin, HIGH, 25000);
42  thr = constrain(map(thrIn, 983, 2000, 0, 255), 0, 255) ; // 983 potrebbe campbiare con 
43            // con un altra ricevente, fare una calibrazione nel caso.
44
45  // FMS dispatcher
46  if ( thr < 10 ) {
47    state = idle ;
48  } else if ( thr > 245 ) {
49    state = full ;
50  } else {
51    state = normal ;
52  }
53
54  switch (state) {
55    case idle:
56        // Lampeggia i lati fissi e fa un PWD Up/Down col motore
57      right.Blink();
58      left.Blink();
59      motore.UD(1000);
60      break;
61
62    case normal:
63      // right e left lampeggiano alternativamente in base al th,
64      // il motore e' acceso con correzione di luminosita' in proporzione al th
65      right.Blink(1120 - 4 * thr ); // lampeggio piu' rapido in base al Th
66      left.Blink(1120 - 4 * thr );
67      motore.lSet(thr);
68      break;
69
70    case full:
71      // lampeggi e PWM a caso
72      right.Swap();
73      left.Swap();
74      motore.lSet(random(0,255);
75      delay(random(20, 100));
76      break;
77   }
78