/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 20:08:35 UTC
  • Revision ID: tim@ed.am-20131023200835-zqu4je2gn0y79car
fixed receiver test code which is now working

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