/elec/quadcopter

To get this branch, use:
bzr branch http://bzr.ed.am/elec/quadcopter
1 by Tim Marston
initial commit, and a couple of receiver test programs
1
//
2
// main.ino
3
//
4
// Testing reading from the receiver.  We're expecting a PWM signal, on
5
// interrupt 0 (which is pin 2 on an Arduino Uno).
6
//
7
// This program tries to measure the width of the signal pulses in
8
// microseconds.  It takes several measurements and prints it the average over
9
// serial.
10
//
11
12
6 by Tim Marston
fixed receiver test code which is now working
13
// number of signal pulses to average (for damping)
14
#define DAMPING_SAMPLES 1
1 by Tim Marston
initial commit, and a couple of receiver test programs
15
16
17
// set to the time that the last signal pulse was at
18
static unsigned long _new_pulse_on = 0;
19
static unsigned long _new_pulse_off = 0;
20
21
22
// ISR to handle the PWM signal
23
void signal_handler()
24
{
25
	// record time
26
	if( digitalRead( 2 ) )
27
		_new_pulse_on = micros();
28
	else
29
		_new_pulse_off = micros();
30
}
31
32
33
void setup()
34
{
35
	// set up an interrupt handler on pin 2
36
	attachInterrupt( 0, signal_handler, CHANGE );
37
	digitalWrite( 2, LOW );
38
39
	Serial.begin( 9600 );
40
}
41
42
void loop()
43
{
6 by Tim Marston
fixed receiver test code which is now working
44
	unsigned long last_pulse_off = 0;
45
	unsigned long intervals[ DAMPING_SAMPLES ] = {0};
1 by Tim Marston
initial commit, and a couple of receiver test programs
46
	int interval_idx = 0;
47
48
	while( true )
49
	{
50
		// detect pulse falling-edge
6 by Tim Marston
fixed receiver test code which is now working
51
		unsigned long pulse_on = _new_pulse_on;
52
		unsigned long pulse_off = _new_pulse_off;
1 by Tim Marston
initial commit, and a couple of receiver test programs
53
		bool got_pulse = false;
6 by Tim Marston
fixed receiver test code which is now working
54
		if( pulse_off > last_pulse_off )
1 by Tim Marston
initial commit, and a couple of receiver test programs
55
		{
6 by Tim Marston
fixed receiver test code which is now working
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;
1 by Tim Marston
initial commit, and a couple of receiver test programs
69
		}
70
71
		// display average?
6 by Tim Marston
fixed receiver test code which is now working
72
		if( got_pulse )
1 by Tim Marston
initial commit, and a couple of receiver test programs
73
		{
74
			// calculate average
6 by Tim Marston
fixed receiver test code which is now working
75
			unsigned long ave = 0;
76
			for( int a = 0; a < DAMPING_SAMPLES; a++ )
1 by Tim Marston
initial commit, and a couple of receiver test programs
77
				ave += intervals[ a ];
6 by Tim Marston
fixed receiver test code which is now working
78
			ave /= DAMPING_SAMPLES;
1 by Tim Marston
initial commit, and a couple of receiver test programs
79
80
			// tell it like it is
6 by Tim Marston
fixed receiver test code which is now working
81
			Serial.print( ave );
82
			Serial.print( "\r\n" );
1 by Tim Marston
initial commit, and a couple of receiver test programs
83
		}		
84
	}
85
}