/elec/quadcopter

To get this branch, use:
bzr branch http://bzr.ed.am/elec/quadcopter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// main.ino
//
// This software is intended to be run on a small Arduino (e.g., a Pro Mini),
// whose purpose is dedicated to talking to the RC system.  That is to say, it
// listens to the PPM signal from the RC receiver and produces a compatible
// single for the ESCs to control the motors.  It additionally talks to the main
// board via a trivial serial protocol.  In so doing, this software relieves the
// main board (and software running on it) of the task of interfacing with any
// of the RC system.
//
// The 8 channels (from the RC receiver) need to be combined and fed to two
// interrupts on the Arduino.  They need to be combined in such a way as to
// alternate pulses between the two interrupts (which are alternately used to
// read the pulses).  Like this:
//
//                  ch1  ch3  ch5  ch7  ch2  ch4  ch6  ch8
//                   |    |    |    |    |    |    |    |
//                   ▼    ▼    ▼    ▼    ▼    ▼    ▼    ▼
//                   ¯    ¯    ¯    ¯    ¯    ¯    ¯    ¯
//            ___    |    |    |    |    |    |    |    |     ___
//    GND ---|___|---+----+----+----+    +----+----+----+----|___|--- GND
//             R                    |    |                     R
//             ____________________ | __ | _____________________
//                                  |    |
//                           pin 2  o    o  pin 3
//                    (interrupt 0)        (interrupt 1)
//
// The two resistors in the circuit are pull-down resistors (so, say 100kΩ).
// Without them, the Arduino is unable to read the pulse wave at all.
//
// Note that your receiver may not send pulses sequentially, in "channel order".
// If this turns out to be the case, you will need to arrange for the pulses to
// wire up the channels in an order whereby the pulses alternatively alternately
// between the two interrupts and adjust the channel order below.


#include "config.h"
#include "receiver.h"


// the width of the display of a single channel (in chars)
#define GRAPH_SIZE 7

// the channel's expected range for use in drawing (should be similar to
// {MAX,MIN}_PULSE_WIDTH values)
#define GRAPH_MIN 1000
#define GRAPH_MAX 2000


void setup()
{
	Receiver::setup();
	
	Serial.begin( 9600 );
}


void draw_graph( unsigned long channel_values[] )
{
	// init graph
	static char graph[ GRAPH_SIZE + 2 ];
	static char inited_graph = false;
	if( !inited_graph ) {
		for( int a = 1; a < GRAPH_SIZE + 1; a++ )
			graph[ a ] = '_';
		graph[ 0 ] = '|';
		graph[ GRAPH_SIZE + 1 ] = 0;
		inited_graph = true;
	}

	// draw channels
	for( int a = 0; a < NUM_CHANNELS; a++ ) {
		unsigned long value = max( 0,
			min( channel_values[ a ], GRAPH_MAX ) - GRAPH_MIN );
		int pos = ( GRAPH_SIZE ) * value / ( GRAPH_MAX - GRAPH_MIN );
		graph[ pos + 1 ] = '^';
		Serial.print( graph );
		graph[ pos + 1 ] = '_';
	}
	Serial.println( "|" );
}


void loop()
{
	unsigned long channel_values[ NUM_CHANNELS ];

	while( true )
	{
		if( Receiver::read_channels( channel_values ) )
			draw_graph( channel_values );
	}
}