/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-01-16 21:42:06 UTC
  • Revision ID: tim@ed.am-20130116214206-5mw4cgsya1h0yiyq
updated arduino.mk

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 (for damping)
14
 
#define DAMPING_SAMPLES 1
 
13
// number of signal pulses to average
 
14
#define SIGNAL_SAMPLES 10
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_off = 0;
45
 
        unsigned long intervals[ DAMPING_SAMPLES ] = {0};
 
44
        unsigned long last_pulse = 0;
 
45
        unsigned long intervals[ SIGNAL_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 pulse_on = _new_pulse_on;
52
 
                unsigned long pulse_off = _new_pulse_off;
 
51
                unsigned long new_pulse_on = _new_pulse_on;
 
52
                unsigned long new_pulse_off = _new_pulse_off;
53
53
                bool got_pulse = false;
54
 
                if( pulse_off > last_pulse_off )
 
54
                if( new_pulse_off > last_pulse )
55
55
                {
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;
 
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;
69
63
                }
70
64
 
71
65
                // display average?
72
 
                if( got_pulse )
 
66
                if( interval_idx == 0 && got_pulse )
73
67
                {
74
68
                        // calculate average
75
 
                        unsigned long ave = 0;
76
 
                        for( int a = 0; a < DAMPING_SAMPLES; a++ )
 
69
                        double ave = 0;
 
70
                        for( int a = 0; a < SIGNAL_SAMPLES; a++ )
77
71
                                ave += intervals[ a ];
78
 
                        ave /= DAMPING_SAMPLES;
 
72
                        ave /= SIGNAL_SAMPLES;
79
73
 
80
74
                        // tell it like it is
81
 
                        Serial.print( ave );
82
 
                        Serial.print( "\r\n" );
 
75
                        Serial.println( round( ave ) );
83
76
                }               
84
77
        }
85
78
}