/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:
10
10
//
11
11
 
12
12
 
13
 
// number of signal pulses to average
14
 
#define SIGNAL_SAMPLES 10
 
13
// number of signal pulses to average (for damping)
 
14
#define DAMPING_SAMPLES 1
15
15
 
16
16
 
17
17
// set to the time that the last signal pulse was at
41
41
 
42
42
void loop()
43
43
{
44
 
        unsigned long last_pulse = 0;
45
 
        unsigned long intervals[ SIGNAL_SAMPLES ] = {0};
 
44
        unsigned long last_pulse_off = 0;
 
45
        unsigned long intervals[ DAMPING_SAMPLES ] = {0};
46
46
        int interval_idx = 0;
47
47
 
48
48
        while( true )
49
49
        {
50
50
                // detect pulse falling-edge
51
 
                unsigned long new_pulse_on = _new_pulse_on;
52
 
                unsigned long new_pulse_off = _new_pulse_off;
 
51
                unsigned long pulse_on = _new_pulse_on;
 
52
                unsigned long pulse_off = _new_pulse_off;
53
53
                bool got_pulse = false;
54
 
                if( new_pulse_off > last_pulse )
 
54
                if( pulse_off > last_pulse_off )
55
55
                {
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;
 
56
                        // sanity check
 
57
                        unsigned long interval = pulse_off - pulse_on;
 
58
                        if( interval > 800 && interval < 3000 )
 
59
                        {
 
60
                                // update interval buffer
 
61
                                intervals[ interval_idx ] = pulse_off - pulse_on;
 
62
                                if( ++interval_idx >= DAMPING_SAMPLES )
 
63
                                        interval_idx = 0;
 
64
 
 
65
                                got_pulse = true;
 
66
                        }
 
67
 
 
68
                        last_pulse_off = pulse_off;
63
69
                }
64
70
 
65
71
                // display average?
66
 
                if( interval_idx == 0 && got_pulse )
 
72
                if( got_pulse )
67
73
                {
68
74
                        // calculate average
69
 
                        double ave = 0;
70
 
                        for( int a = 0; a < SIGNAL_SAMPLES; a++ )
 
75
                        unsigned long ave = 0;
 
76
                        for( int a = 0; a < DAMPING_SAMPLES; a++ )
71
77
                                ave += intervals[ a ];
72
 
                        ave /= SIGNAL_SAMPLES;
 
78
                        ave /= DAMPING_SAMPLES;
73
79
 
74
80
                        // tell it like it is
75
 
                        Serial.println( round( ave ) );
 
81
                        Serial.print( ave );
 
82
                        Serial.print( "\r\n" );
76
83
                }               
77
84
        }
78
85
}