bzr branch
http://bzr.ed.am/elec/quadcopter
16
by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk |
1 |
// |
2 |
// main.ino |
|
3 |
// |
|
4 |
// This software is intended to be run on a small Arduino (e.g., a Pro Mini), |
|
5 |
// whose purpose is dedicated to talking to the RC system. That is to say, it |
|
6 |
// listens to the PPM signal from the RC receiver and produces a compatible |
|
7 |
// single for the ESCs to control the motors. It additionally talks to the main |
|
8 |
// board via a trivial serial protocol. In so doing, this software relieves the |
|
9 |
// main board (and software running on it) of the task of interfacing with any |
|
10 |
// of the RC system. |
|
11 |
// |
|
12 |
// The 8 channels (from the RC receiver) need to be combined and fed to two |
|
13 |
// interrupts on the Arduino. They need to be combined in such a way as to |
|
14 |
// alternate pulses between the two interrupts (which are alternately used to |
|
15 |
// read the pulses). Like this: |
|
16 |
// |
|
17 |
// ch1 ch3 ch5 ch7 ch2 ch4 ch6 ch8 |
|
18 |
// | | | | | | | | |
|
19 |
// ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ |
|
20 |
// ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ |
|
21 |
// ___ | | | | | | | | ___ |
|
22 |
// GND ---|___|---+----+----+----+ +----+----+----+----|___|--- GND |
|
23 |
// R | | R |
|
24 |
// ____________________ | __ | _____________________ |
|
25 |
// | | |
|
26 |
// pin 2 o o pin 3 |
|
27 |
// (interrupt 0) (interrupt 1) |
|
28 |
// |
|
29 |
// The two resistors in the circuit are pull-down resistors (so, say 100kΩ). |
|
30 |
// Without them, the Arduino is unable to read the pulse wave at all. |
|
31 |
// |
|
32 |
// Note that your receiver may not send pulses sequentially, in "channel order". |
|
33 |
// If this turns out to be the case, you will need to arrange for the pulses to |
|
34 |
// wire up the channels in an order whereby the pulses alternatively alternately |
|
35 |
// between the two interrupts and adjust the channel order below. |
|
36 |
||
37 |
||
38 |
#include "config.h" |
|
39 |
#include "receiver.h" |
|
40 |
||
41 |
||
42 |
// the width of the display of a single channel (in chars) |
|
43 |
#define GRAPH_SIZE 7 |
|
44 |
||
45 |
// the channel's expected range for use in drawing (should be similar to |
|
46 |
// {MAX,MIN}_PULSE_WIDTH values) |
|
47 |
#define GRAPH_MIN 1000 |
|
48 |
#define GRAPH_MAX 2000 |
|
49 |
||
50 |
||
51 |
void setup() |
|
52 |
{ |
|
53 |
Receiver::setup(); |
|
54 |
||
55 |
Serial.begin( 9600 ); |
|
56 |
} |
|
57 |
||
58 |
||
59 |
void draw_graph( unsigned long channel_values[] ) |
|
60 |
{ |
|
61 |
// init graph |
|
62 |
static char graph[ GRAPH_SIZE + 2 ]; |
|
63 |
static char inited_graph = false; |
|
64 |
if( !inited_graph ) { |
|
65 |
for( int a = 1; a < GRAPH_SIZE + 1; a++ ) |
|
66 |
graph[ a ] = '_'; |
|
67 |
graph[ 0 ] = '|'; |
|
68 |
graph[ GRAPH_SIZE + 1 ] = 0; |
|
69 |
inited_graph = true; |
|
70 |
} |
|
71 |
||
72 |
// draw channels |
|
73 |
for( int a = 0; a < NUM_CHANNELS; a++ ) { |
|
74 |
unsigned long value = max( 0, |
|
75 |
min( channel_values[ a ], GRAPH_MAX ) - GRAPH_MIN ); |
|
76 |
int pos = ( GRAPH_SIZE ) * value / ( GRAPH_MAX - GRAPH_MIN ); |
|
77 |
graph[ pos + 1 ] = '^'; |
|
78 |
Serial.print( graph ); |
|
79 |
graph[ pos + 1 ] = '_'; |
|
80 |
} |
|
81 |
Serial.println( "|" ); |
|
82 |
} |
|
83 |
||
84 |
||
85 |
void loop() |
|
86 |
{ |
|
87 |
unsigned long channel_values[ NUM_CHANNELS ]; |
|
88 |
||
89 |
while( true ) |
|
90 |
{ |
|
91 |
if( Receiver::read_channels( channel_values ) ) |
|
92 |
draw_graph( channel_values ); |
|
93 |
} |
|
94 |
} |