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