/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:
8
8
// microseconds.  It takes several measurements and prints it the average over
9
9
// serial.
10
10
//
11
 
// NOTES: Due to the way our receiver works (by varying the position of the
12
 
// rising edge of the pulse and keeping the falling edge fixed), it is
13
 
// sufficient for us to measure the pule width to determine the channel value
14
 
// (e.g., throttle position).  But it should be noted that this relies on a
15
 
// quirk of our receiver/transmitter and it is not truly measuring PPM.  Our
16
 
// next experiment is measuring PPM (../ppm-read).
17
 
 
18
 
// number of signal pulses to average
19
 
#define AVERAGE_SAMPLES 1
20
 
 
21
 
// should average be result, or damping?
22
 
#define AVERAGE_DAMP 1
23
 
 
24
 
// graphic display
25
 
#define GRAPH 1
26
 
#define GRAPH_MIN 1000
27
 
#define GRAPH_MAX 2000
28
 
#define GRAPH_SIZE 30
 
11
 
 
12
 
 
13
// number of signal pulses to average (for damping)
 
14
#define DAMPING_SAMPLES 1
29
15
 
30
16
 
31
17
// set to the time that the last signal pulse was at
53
39
        Serial.begin( 9600 );
54
40
}
55
41
 
56
 
 
57
42
void loop()
58
43
{
59
44
        unsigned long last_pulse_off = 0;
60
 
        unsigned long intervals[ AVERAGE_SAMPLES ];     
 
45
        unsigned long intervals[ DAMPING_SAMPLES ] = {0};
61
46
        int interval_idx = 0;
62
47
 
63
 
        // reset intervals
64
 
        for( int a = 0; a < AVERAGE_SAMPLES; a++ )
65
 
                intervals[ a ] = 0;
66
 
 
67
 
#ifdef GRAPH
68
 
        // init graph
69
 
        char graph[ GRAPH_SIZE + 3 ];
70
 
        for( int a = 1; a < GRAPH_SIZE + 1; a++ )
71
 
                graph[ a ] = '-';
72
 
        graph[ 0 ] = '[';
73
 
        graph[ GRAPH_SIZE + 1 ] = ']';
74
 
        graph[ GRAPH_SIZE + 2 ] = 0;
75
 
#endif // GRAPH
76
 
 
77
48
        while( true )
78
49
        {
79
50
                // detect pulse falling-edge
88
59
                        {
89
60
                                // update interval buffer
90
61
                                intervals[ interval_idx ] = pulse_off - pulse_on;
91
 
                                if( ++interval_idx >= AVERAGE_SAMPLES )
 
62
                                if( ++interval_idx >= DAMPING_SAMPLES )
92
63
                                        interval_idx = 0;
93
64
 
94
65
                                got_pulse = true;
98
69
                }
99
70
 
100
71
                // display average?
101
 
                if( got_pulse &&
102
 
                        ( AVERAGE_DAMP || interval_idx == 0 ) )
 
72
                if( got_pulse )
103
73
                {
104
74
                        // calculate average
105
75
                        unsigned long ave = 0;
106
 
                        for( int a = 0; a < AVERAGE_SAMPLES; a++ )
 
76
                        for( int a = 0; a < DAMPING_SAMPLES; a++ )
107
77
                                ave += intervals[ a ];
108
 
                        ave /= AVERAGE_SAMPLES;
109
 
 
110
 
#ifdef GRAPH
111
 
 
112
 
                        // draw graph
113
 
                        int pos = ( GRAPH_SIZE ) *
114
 
                                ( ave - GRAPH_MIN ) / ( GRAPH_MAX - GRAPH_MIN );
115
 
                        graph[ pos + 1 ] = '|';
116
 
                        Serial.println( graph );
117
 
                        graph[ pos + 1 ] = '-';
118
 
 
119
 
#else // GRAPH
 
78
                        ave /= DAMPING_SAMPLES;
120
79
 
121
80
                        // tell it like it is
122
 
                        Serial.println( ave );
123
 
 
124
 
#endif // GRAPH
 
81
                        Serial.print( ave );
 
82
                        Serial.print( "\r\n" );
125
83
                }               
126
84
        }
127
85
}