/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 frequency of the signal pulses in
8
// microseconds.  It takes several measurements and prints out the average over
9
// serial.
10
//
8 by Tim Marston
updated comments in receiver test programs
11
// NOTES: This test is not very useful.  Our second experiment was measuring
12
// pulse width (../pulse-width).
1 by Tim Marston
initial commit, and a couple of receiver test programs
13
14
// number of signal pulses to average
15
#define SIGNAL_SAMPLES 10
16
17
18
// set to the time that the last signal pulse was at
19
static volatile unsigned long _new_pulse_at = 0;
20
21
22
// ISR to handle the PWM signal
23
void signal_handler()
24
{
25
	// record time
26
	_new_pulse_at = micros();
27
}
28
29
30
void setup()
31
{
32
	Serial.begin( 9600 );
33
34
	// set up an interrupt handler on pin 2
35
	attachInterrupt( 0, signal_handler, RISING );
36
}
37
38
void loop()
39
{
40
	unsigned long last_pulse = 0;
41
	unsigned long intervals[ SIGNAL_SAMPLES ] = {0};
42
	int interval_idx = 0;
43
44
	while( true )
45
	{
46
		// detect pulse
47
		unsigned long new_pulse = _new_pulse_at;
48
		bool got_pulse = false;
49
		if( new_pulse < last_pulse )
50
			last_pulse = new_pulse;
51
		if( new_pulse > last_pulse )
52
		{
53
			// check interval
54
			unsigned long interval = new_pulse - last_pulse;
6 by Tim Marston
fixed receiver test code which is now working
55
			if( false && interval < 300 )
1 by Tim Marston
initial commit, and a couple of receiver test programs
56
			{
57
				Serial.print( "[" );
58
				Serial.print( last_pulse );
59
				Serial.print( "," );
60
				Serial.print( new_pulse );
61
				Serial.print( "]" );
62
			}
63
//			if( interval > 19000 && interval < 20500 )
64
//			{
65
				// update interval buffer
66
				intervals[ interval_idx ] = interval;
67
				if( ++interval_idx >= SIGNAL_SAMPLES )
68
					interval_idx = 0;
69
70
				got_pulse = true;
71
//			}
72
73
			last_pulse = new_pulse;
74
		}
75
76
		// display average?
77
		if( interval_idx == 0 && got_pulse )
78
		{
79
			// calculate average
6 by Tim Marston
fixed receiver test code which is now working
80
			long interval = 0;
81
			for( int a = 0; a < SIGNAL_SAMPLES; a++ )
82
				interval += intervals[ a ];
83
			
84
			Serial.print( intervals[ 0 ] );
85
			Serial.println();
1 by Tim Marston
initial commit, and a couple of receiver test programs
86
		}		
87
	}
88
}