/elec/quadcopter

To get this branch, use:
bzr branch http://bzr.ed.am/elec/quadcopter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// main.ino
//
// Testing reading from the receiver.  We're expecting a PWM signal, on
// interrupt 0 (which is pin 2 on an Arduino Uno).
//
// This program tries to measure the width of the signal pulses in
// microseconds.  It takes several measurements and prints it the average over
// serial.
//
// NOTES: Due to the way our receiver works (by varying the position of the
// rising edge of the pulse and keeping the falling edge fixed), it is
// sufficient for us to measure the pule width to determine the channel value
// (e.g., throttle position).  But it should be noted that this relies on a
// quirk of our receiver/transmitter and it is not truly measuring PPM.  Our
// next experiment is measuring PPM (../ppm-read).

// number of signal pulses to average
#define AVERAGE_SAMPLES 1

// should average be result, or damping?
#define AVERAGE_DAMP 1

// graphic display
#define GRAPH 1
#define GRAPH_MIN 1000
#define GRAPH_MAX 2000
#define GRAPH_SIZE 30


// set to the time that the last signal pulse was at
static unsigned long _new_pulse_on = 0;
static unsigned long _new_pulse_off = 0;


// ISR to handle the PWM signal
void signal_handler()
{
	// record time
	if( digitalRead( 2 ) )
		_new_pulse_on = micros();
	else
		_new_pulse_off = micros();
}


void setup()
{
	// set up an interrupt handler on pin 2
	attachInterrupt( 0, signal_handler, CHANGE );
	digitalWrite( 2, LOW );

	Serial.begin( 9600 );
}


void loop()
{
	unsigned long last_pulse_off = 0;
	unsigned long intervals[ AVERAGE_SAMPLES ];	
	int interval_idx = 0;

	// reset intervals
	for( int a = 0; a < AVERAGE_SAMPLES; a++ )
		intervals[ a ] = 0;

#ifdef GRAPH
	// init graph
	char graph[ GRAPH_SIZE + 3 ];
	for( int a = 1; a < GRAPH_SIZE + 1; a++ )
		graph[ a ] = '-';
	graph[ 0 ] = '[';
	graph[ GRAPH_SIZE + 1 ] = ']';
	graph[ GRAPH_SIZE + 2 ] = 0;
#endif // GRAPH

	while( true )
	{
		// detect pulse falling-edge
		unsigned long pulse_on = _new_pulse_on;
		unsigned long pulse_off = _new_pulse_off;
		bool got_pulse = false;
		if( pulse_off > last_pulse_off )
		{
			// sanity check
			unsigned long interval = pulse_off - pulse_on;
			if( interval > 800 && interval < 3000 )
			{
				// update interval buffer
				intervals[ interval_idx ] = pulse_off - pulse_on;
				if( ++interval_idx >= AVERAGE_SAMPLES )
					interval_idx = 0;

				got_pulse = true;
			}

			last_pulse_off = pulse_off;
		}

		// display average?
		if( got_pulse &&
			( AVERAGE_DAMP || interval_idx == 0 ) )
		{
			// calculate average
			unsigned long ave = 0;
			for( int a = 0; a < AVERAGE_SAMPLES; a++ )
				ave += intervals[ a ];
			ave /= AVERAGE_SAMPLES;

#ifdef GRAPH

			// draw graph
			int pos = ( GRAPH_SIZE ) *
				( ave - GRAPH_MIN ) / ( GRAPH_MAX - GRAPH_MIN );
			graph[ pos + 1 ] = '|';
			Serial.println( graph );
			graph[ pos + 1 ] = '-';

#else // GRAPH

			// tell it like it is
			Serial.println( ave );

#endif // GRAPH
		}		
	}
}