/elec/quadcopter

To get this branch, use:
bzr branch http://bzr.ed.am/elec/quadcopter

« back to all changes in this revision

Viewing changes to test/receiver/pulse-width/main.ino

  • Committer: Tim Marston
  • Date: 2013-01-16 21:42:06 UTC
  • Revision ID: tim@ed.am-20130116214206-5mw4cgsya1h0yiyq
updated arduino.mk

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
 
13
13
// number of signal pulses to average
14
 
#define AVERAGE_SAMPLES 1
15
 
 
16
 
// should average be result, or damping?
17
 
#define AVERAGE_DAMP 1
18
 
 
19
 
// graphic display
20
 
#define GRAPH 1
21
 
#define GRAPH_MIN 1000
22
 
#define GRAPH_MAX 2000
23
 
#define GRAPH_SIZE 30
 
14
#define SIGNAL_SAMPLES 10
24
15
 
25
16
 
26
17
// set to the time that the last signal pulse was at
48
39
        Serial.begin( 9600 );
49
40
}
50
41
 
51
 
 
52
42
void loop()
53
43
{
54
 
        unsigned long last_pulse_off = 0;
55
 
        unsigned long intervals[ AVERAGE_SAMPLES ];     
 
44
        unsigned long last_pulse = 0;
 
45
        unsigned long intervals[ SIGNAL_SAMPLES ] = {0};
56
46
        int interval_idx = 0;
57
47
 
58
 
        // reset intervals
59
 
        for( int a = 0; a < AVERAGE_SAMPLES; a++ )
60
 
                intervals[ a ] = 0;
61
 
 
62
 
#ifdef GRAPH
63
 
        // init graph
64
 
        char graph[ GRAPH_SIZE + 3 ];
65
 
        for( int a = 1; a < GRAPH_SIZE + 1; a++ )
66
 
                graph[ a ] = '-';
67
 
        graph[ 0 ] = '[';
68
 
        graph[ GRAPH_SIZE + 1 ] = ']';
69
 
        graph[ GRAPH_SIZE + 2 ] = 0;
70
 
#endif // GRAPH
71
 
 
72
48
        while( true )
73
49
        {
74
50
                // detect pulse falling-edge
75
 
                unsigned long pulse_on = _new_pulse_on;
76
 
                unsigned long pulse_off = _new_pulse_off;
 
51
                unsigned long new_pulse_on = _new_pulse_on;
 
52
                unsigned long new_pulse_off = _new_pulse_off;
77
53
                bool got_pulse = false;
78
 
                if( pulse_off > last_pulse_off )
 
54
                if( new_pulse_off > last_pulse )
79
55
                {
80
 
                        // sanity check
81
 
                        unsigned long interval = pulse_off - pulse_on;
82
 
                        if( interval > 800 && interval < 3000 )
83
 
                        {
84
 
                                // update interval buffer
85
 
                                intervals[ interval_idx ] = pulse_off - pulse_on;
86
 
                                if( ++interval_idx >= AVERAGE_SAMPLES )
87
 
                                        interval_idx = 0;
88
 
 
89
 
                                got_pulse = true;
90
 
                        }
91
 
 
92
 
                        last_pulse_off = pulse_off;
 
56
                        // update interval buffer
 
57
                        intervals[ interval_idx ] = new_pulse_off - new_pulse_on;
 
58
                        if( ++interval_idx >= SIGNAL_SAMPLES )
 
59
                                interval_idx = 0;
 
60
 
 
61
                        last_pulse = new_pulse_off;
 
62
                        got_pulse = true;
93
63
                }
94
64
 
95
65
                // display average?
96
 
                if( got_pulse &&
97
 
                        ( AVERAGE_DAMP || interval_idx == 0 ) )
 
66
                if( interval_idx == 0 && got_pulse )
98
67
                {
99
68
                        // calculate average
100
 
                        unsigned long ave = 0;
101
 
                        for( int a = 0; a < AVERAGE_SAMPLES; a++ )
 
69
                        double ave = 0;
 
70
                        for( int a = 0; a < SIGNAL_SAMPLES; a++ )
102
71
                                ave += intervals[ a ];
103
 
                        ave /= AVERAGE_SAMPLES;
104
 
 
105
 
#ifdef GRAPH
106
 
 
107
 
                        // draw graph
108
 
                        int pos = ( GRAPH_SIZE ) *
109
 
                                ( ave - GRAPH_MIN ) / ( GRAPH_MAX - GRAPH_MIN );
110
 
                        graph[ pos + 1 ] = '|';
111
 
                        Serial.println( graph );
112
 
                        graph[ pos + 1 ] = '-';
113
 
 
114
 
#else // GRAPH
 
72
                        ave /= SIGNAL_SAMPLES;
115
73
 
116
74
                        // tell it like it is
117
 
                        Serial.println( ave );
118
 
 
119
 
#endif // GRAPH
 
75
                        Serial.println( round( ave ) );
120
76
                }               
121
77
        }
122
78
}