/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-21 00:05:58 UTC
  • Revision ID: tim@ed.am-20131121000558-06xtcb4fue72td7q
updated 8-channel pulse width reading test program's comments

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
10
 
12
11
 
13
12
// number of signal pulses to average
14
 
#define SIGNAL_SAMPLES 10
 
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 0
 
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
 
        unsigned long last_pulse = 0;
45
 
        unsigned long intervals[ SIGNAL_SAMPLES ] = {0};
 
53
        unsigned long last_pulse_off = 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
#if 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
51
 
                unsigned long new_pulse_on = _new_pulse_on;
52
 
                unsigned long new_pulse_off = _new_pulse_off;
 
74
                noInterrupts();
 
75
                unsigned long pulse_on = _new_pulse_on;
 
76
                unsigned long pulse_off = _new_pulse_off;
 
77
                interrupts();
 
78
                
53
79
                bool got_pulse = false;
54
 
                if( new_pulse_off > last_pulse )
 
80
                if( pulse_off != last_pulse_off )
55
81
                {
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;
 
82
                        // sanity check
 
83
                        unsigned long interval = pulse_off - pulse_on;
 
84
//                      if( interval > 800 && interval < 3000 )
 
85
                        {
 
86
                                // update interval buffer
 
87
                                intervals[ interval_idx ] = pulse_off - pulse_on;
 
88
                                if( ++interval_idx >= AVERAGE_SAMPLES )
 
89
                                        interval_idx = 0;
 
90
 
 
91
                                got_pulse = true;
 
92
                        }
 
93
 
 
94
                        last_pulse_off = pulse_off;
63
95
                }
64
96
 
65
97
                // display average?
66
 
                if( interval_idx == 0 && got_pulse )
 
98
                if( got_pulse &&
 
99
                        ( AVERAGE_DAMP || interval_idx == 0 ) )
67
100
                {
68
101
                        // calculate average
69
 
                        double ave = 0;
70
 
                        for( int a = 0; a < SIGNAL_SAMPLES; a++ )
 
102
                        unsigned long ave = 0;
 
103
                        for( int a = 0; a < AVERAGE_SAMPLES; a++ )
71
104
                                ave += intervals[ a ];
72
 
                        ave /= SIGNAL_SAMPLES;
 
105
                        ave /= AVERAGE_SAMPLES;
 
106
 
 
107
#if GRAPH
 
108
 
 
109
                        // draw graph
 
110
                        int pos = ( GRAPH_SIZE ) *
 
111
                                ( ave - GRAPH_MIN ) / ( GRAPH_MAX - GRAPH_MIN );
 
112
                        graph[ pos + 1 ] = '|';
 
113
                        Serial.println( graph );
 
114
                        graph[ pos + 1 ] = '-';
 
115
 
 
116
#else // GRAPH
73
117
 
74
118
                        // tell it like it is
75
 
                        Serial.println( round( ave ) );
 
119
                        Serial.println( ave );
 
120
 
 
121
#endif // GRAPH
76
122
                }               
77
123
        }
78
124
}