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 |
|
19 |
#define GRAPH 1 |
|
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 |
||
61 |
#ifdef GRAPH |
|
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 |
|
6
by Tim Marston
fixed receiver test code which is now working |
74 |
unsigned long pulse_on = _new_pulse_on; |
75 |
unsigned long pulse_off = _new_pulse_off; |
|
1
by Tim Marston
initial commit, and a couple of receiver test programs |
76 |
bool got_pulse = false; |
6
by Tim Marston
fixed receiver test code which is now working |
77 |
if( pulse_off > last_pulse_off ) |
1
by Tim Marston
initial commit, and a couple of receiver test programs |
78 |
{ |
6
by Tim Marston
fixed receiver test code which is now working |
79 |
// sanity check |
80 |
unsigned long interval = pulse_off - pulse_on; |
|
81 |
if( interval > 800 && interval < 3000 ) |
|
82 |
{ |
|
83 |
// update interval buffer |
|
84 |
intervals[ interval_idx ] = pulse_off - pulse_on; |
|
7
by Tim Marston
added "graphic" display to receiver test program |
85 |
if( ++interval_idx >= AVERAGE_SAMPLES ) |
6
by Tim Marston
fixed receiver test code which is now working |
86 |
interval_idx = 0; |
87 |
||
88 |
got_pulse = true; |
|
89 |
} |
|
90 |
||
91 |
last_pulse_off = pulse_off; |
|
1
by Tim Marston
initial commit, and a couple of receiver test programs |
92 |
} |
93 |
||
94 |
// display average? |
|
7
by Tim Marston
added "graphic" display to receiver test program |
95 |
if( got_pulse && |
96 |
( AVERAGE_DAMP || interval_idx == 0 ) ) |
|
1
by Tim Marston
initial commit, and a couple of receiver test programs |
97 |
{ |
98 |
// calculate average |
|
6
by Tim Marston
fixed receiver test code which is now working |
99 |
unsigned long ave = 0; |
7
by Tim Marston
added "graphic" display to receiver test program |
100 |
for( int a = 0; a < AVERAGE_SAMPLES; a++ ) |
1
by Tim Marston
initial commit, and a couple of receiver test programs |
101 |
ave += intervals[ a ]; |
7
by Tim Marston
added "graphic" display to receiver test program |
102 |
ave /= AVERAGE_SAMPLES; |
103 |
||
104 |
#ifdef GRAPH |
|
105 |
||
106 |
// draw graph |
|
107 |
int pos = ( GRAPH_SIZE ) * |
|
108 |
( ave - GRAPH_MIN ) / ( GRAPH_MAX - GRAPH_MIN ); |
|
109 |
graph[ pos + 1 ] = '|'; |
|
110 |
Serial.println( graph ); |
|
111 |
graph[ pos + 1 ] = '-'; |
|
112 |
||
113 |
#else // GRAPH |
|
1
by Tim Marston
initial commit, and a couple of receiver test programs |
114 |
|
115 |
// tell it like it is |
|
7
by Tim Marston
added "graphic" display to receiver test program |
116 |
Serial.println( ave ); |
117 |
||
118 |
#endif // GRAPH |
|
1
by Tim Marston
initial commit, and a couple of receiver test programs |
119 |
} |
120 |
} |
|
121 |
} |