/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-10-23 21:08:18 UTC
  • Revision ID: tim@ed.am-20131023210818-w3b4t7ddzxvu11mo
added "graphic" display to receiver test program

Show diffs side-by-side

added added

removed removed

10
10
//
11
11
 
12
12
 
13
 
// number of signal pulses to average (for damping)
14
 
#define DAMPING_SAMPLES 1
 
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
15
24
 
16
25
 
17
26
// set to the time that the last signal pulse was at
39
48
        Serial.begin( 9600 );
40
49
}
41
50
 
 
51
 
42
52
void loop()
43
53
{
44
54
        unsigned long last_pulse_off = 0;
45
 
        unsigned long intervals[ DAMPING_SAMPLES ] = {0};
 
55
        unsigned long intervals[ AVERAGE_SAMPLES ];     
46
56
        int interval_idx = 0;
47
57
 
 
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
 
48
72
        while( true )
49
73
        {
50
74
                // detect pulse falling-edge
59
83
                        {
60
84
                                // update interval buffer
61
85
                                intervals[ interval_idx ] = pulse_off - pulse_on;
62
 
                                if( ++interval_idx >= DAMPING_SAMPLES )
 
86
                                if( ++interval_idx >= AVERAGE_SAMPLES )
63
87
                                        interval_idx = 0;
64
88
 
65
89
                                got_pulse = true;
69
93
                }
70
94
 
71
95
                // display average?
72
 
                if( got_pulse )
 
96
                if( got_pulse &&
 
97
                        ( AVERAGE_DAMP || interval_idx == 0 ) )
73
98
                {
74
99
                        // calculate average
75
100
                        unsigned long ave = 0;
76
 
                        for( int a = 0; a < DAMPING_SAMPLES; a++ )
 
101
                        for( int a = 0; a < AVERAGE_SAMPLES; a++ )
77
102
                                ave += intervals[ a ];
78
 
                        ave /= DAMPING_SAMPLES;
 
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
79
115
 
80
116
                        // tell it like it is
81
 
                        Serial.print( ave );
82
 
                        Serial.print( "\r\n" );
 
117
                        Serial.println( ave );
 
118
 
 
119
#endif // GRAPH
83
120
                }               
84
121
        }
85
122
}