/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-11-13 22:39:08 UTC
  • Revision ID: tim@ed.am-20131113223908-0ohj8mmvdmqhzdku
updated comments in receiver tests

Show diffs side-by-side

added added

removed removed

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