/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.
9 by Tim Marston
updated comments in receiver tests
10
1 by Tim Marston
initial commit, and a couple of receiver test programs
11
7 by Tim Marston
added "graphic" display to receiver test program
12
// number of signal pulses to average
13
#define AVERAGE_SAMPLES 1
14
15
// should average be result, or damping?
16
#define AVERAGE_DAMP 1
17
18
// graphic display
10 by Tim Marston
fixed pulse-width test
19
#define GRAPH 0
7 by Tim Marston
added "graphic" display to receiver test program
20
#define GRAPH_MIN 1000
21
#define GRAPH_MAX 2000
22
#define GRAPH_SIZE 30
1 by Tim Marston
initial commit, and a couple of receiver test programs
23
24
25
// set to the time that the last signal pulse was at
26
static unsigned long _new_pulse_on = 0;
27
static unsigned long _new_pulse_off = 0;
28
29
30
// ISR to handle the PWM signal
31
void signal_handler()
32
{
33
	// record time
34
	if( digitalRead( 2 ) )
35
		_new_pulse_on = micros();
36
	else
37
		_new_pulse_off = micros();
38
}
39
40
41
void setup()
42
{
43
	// set up an interrupt handler on pin 2
44
	attachInterrupt( 0, signal_handler, CHANGE );
45
	digitalWrite( 2, LOW );
46
47
	Serial.begin( 9600 );
48
}
49
7 by Tim Marston
added "graphic" display to receiver test program
50
1 by Tim Marston
initial commit, and a couple of receiver test programs
51
void loop()
52
{
6 by Tim Marston
fixed receiver test code which is now working
53
	unsigned long last_pulse_off = 0;
7 by Tim Marston
added "graphic" display to receiver test program
54
	unsigned long intervals[ AVERAGE_SAMPLES ];	
1 by Tim Marston
initial commit, and a couple of receiver test programs
55
	int interval_idx = 0;
56
7 by Tim Marston
added "graphic" display to receiver test program
57
	// reset intervals
58
	for( int a = 0; a < AVERAGE_SAMPLES; a++ )
59
		intervals[ a ] = 0;
60
10 by Tim Marston
fixed pulse-width test
61
#if GRAPH
7 by Tim Marston
added "graphic" display to receiver test program
62
	// init graph
63
	char graph[ GRAPH_SIZE + 3 ];
64
	for( int a = 1; a < GRAPH_SIZE + 1; a++ )
65
		graph[ a ] = '-';
66
	graph[ 0 ] = '[';
67
	graph[ GRAPH_SIZE + 1 ] = ']';
68
	graph[ GRAPH_SIZE + 2 ] = 0;
69
#endif // GRAPH
70
1 by Tim Marston
initial commit, and a couple of receiver test programs
71
	while( true )
72
	{
73
		// detect pulse falling-edge
10 by Tim Marston
fixed pulse-width test
74
		noInterrupts();
6 by Tim Marston
fixed receiver test code which is now working
75
		unsigned long pulse_on = _new_pulse_on;
76
		unsigned long pulse_off = _new_pulse_off;
10 by Tim Marston
fixed pulse-width test
77
		interrupts();
78
		
1 by Tim Marston
initial commit, and a couple of receiver test programs
79
		bool got_pulse = false;
10 by Tim Marston
fixed pulse-width test
80
		if( pulse_off != last_pulse_off )
1 by Tim Marston
initial commit, and a couple of receiver test programs
81
		{
6 by Tim Marston
fixed receiver test code which is now working
82
			// sanity check
83
			unsigned long interval = pulse_off - pulse_on;
10 by Tim Marston
fixed pulse-width test
84
//			if( interval > 800 && interval < 3000 )
6 by Tim Marston
fixed receiver test code which is now working
85
			{
86
				// update interval buffer
87
				intervals[ interval_idx ] = pulse_off - pulse_on;
7 by Tim Marston
added "graphic" display to receiver test program
88
				if( ++interval_idx >= AVERAGE_SAMPLES )
6 by Tim Marston
fixed receiver test code which is now working
89
					interval_idx = 0;
90
91
				got_pulse = true;
92
			}
93
94
			last_pulse_off = pulse_off;
1 by Tim Marston
initial commit, and a couple of receiver test programs
95
		}
96
97
		// display average?
7 by Tim Marston
added "graphic" display to receiver test program
98
		if( got_pulse &&
99
			( AVERAGE_DAMP || interval_idx == 0 ) )
1 by Tim Marston
initial commit, and a couple of receiver test programs
100
		{
101
			// calculate average
6 by Tim Marston
fixed receiver test code which is now working
102
			unsigned long ave = 0;
7 by Tim Marston
added "graphic" display to receiver test program
103
			for( int a = 0; a < AVERAGE_SAMPLES; a++ )
1 by Tim Marston
initial commit, and a couple of receiver test programs
104
				ave += intervals[ a ];
7 by Tim Marston
added "graphic" display to receiver test program
105
			ave /= AVERAGE_SAMPLES;
106
10 by Tim Marston
fixed pulse-width test
107
#if GRAPH
7 by Tim Marston
added "graphic" display to receiver test program
108
109
			// draw graph
110
			int pos = ( GRAPH_SIZE ) *
111
				( ave - GRAPH_MIN ) / ( GRAPH_MAX - GRAPH_MIN );
112
			graph[ pos + 1 ] = '|';
113
			Serial.println( graph );
114
			graph[ pos + 1 ] = '-';
115
116
#else // GRAPH
1 by Tim Marston
initial commit, and a couple of receiver test programs
117
118
			// tell it like it is
7 by Tim Marston
added "graphic" display to receiver test program
119
			Serial.println( ave );
120
121
#endif // GRAPH
1 by Tim Marston
initial commit, and a couple of receiver test programs
122
		}		
123
	}
124
}