/elec/quadcopter

To get this branch, use:
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
//
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
12
// Receiver
13
// --------
14
//
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
15
// The 8 channels (from the RC receiver) need to be combined and fed to two
16
// interrupts on the Arduino.  They need to be combined in such a way as to
17
// alternate pulses between the two interrupts (which are alternately used to
18
// read the pulses).  Like this:
19
//
20
//                  ch1  ch3  ch5  ch7  ch2  ch4  ch6  ch8
21
//                   |    |    |    |    |    |    |    |
22
//                   ▼    ▼    ▼    ▼    ▼    ▼    ▼    ▼
23
//                   ¯    ¯    ¯    ¯    ¯    ¯    ¯    ¯
24
//            ___    |    |    |    |    |    |    |    |     ___
25
//    GND ---|___|---+----+----+----+    +----+----+----+----|___|--- GND
26
//             R                    |    |                     R
24 by Tim Marston
updated rc-interface comments
27
//         ________________________ | __ | _____________________
28
//         ARDUINO                  |    |
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
29
//                           pin 2  o    o  pin 3
30
//                    (interrupt 0)        (interrupt 1)
31
//
32
// The two resistors in the circuit are pull-down resistors (so, say 100kΩ).
33
// Without them, the Arduino is unable to read the pulse wave at all.
34
//
35
// Note that your receiver may not send pulses sequentially, in "channel order".
36
// If this turns out to be the case, you will need to arrange for the pulses to
24 by Tim Marston
updated rc-interface comments
37
// wire up the channels in an order whereby the pulses arrive alternately
38
// between the two interrupts and adjust the channel order in config.h.
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
39
//
40
// Motor control
41
// -------------
42
//
24 by Tim Marston
updated rc-interface comments
43
// The four motor control outputs should be fed directly to the ESCs.
44
//
45
//                            ESC  ESC  ESC  ESC
46
//                             o    o    o    o
47
//                             |    |    |    |
48
//         ___________________ | __ | __ | __ | _____
49
//         ARDUINO             |    |    |    |
50
//                      pin 4  o    o    o    o  pin 7
51
//
52
// SERIAL INTERFACE
53
// ----------------
54
//
55
// The serial interface (pins 0 and 1) talk a very simple protocol.  When a
56
// complete set of 8 channel values have been read, it writes 0xffff followed by
57
// the 8, 16-bit unsigned channel values.  It expects to recieve 0xffff,
58
// followed by 4, 16-bit unsigned channel values.  All channel values are in the
59
// range specified in config.h.
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
60
61
#include "config.h"
62
#include "receiver.h"
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
63
#include "motors.h"
18 by Tim Marston
completed (hopefully) the RC interface software
64
#include "comms.h"
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
65
66
28 by Tim Marston
rc-interface: added code to read from serial and set motor channel values
67
void flash_led()
68
{
69
	static bool on = false;
70
	on = !on;
71
	digitalWrite( LED_PIN, on? HIGH : LOW );
72
}
73
74
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
75
void setup()
76
{
28 by Tim Marston
rc-interface: added code to read from serial and set motor channel values
77
	// init subsystems
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
78
	Receiver::setup();
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
79
	Motors::setup();
18 by Tim Marston
completed (hopefully) the RC interface software
80
	Comms::setup();
81
82
	// led
83
	pinMode( LED_PIN, OUTPUT );
84
	digitalWrite( LED_PIN, LOW );
85
}
86
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
87
void loop()
88
{
28 by Tim Marston
rc-interface: added code to read from serial and set motor channel values
89
	int motor_values[ NUM_MOTORS ];
18 by Tim Marston
completed (hopefully) the RC interface software
90
	int channel_values[ NUM_CHANNELS ];
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
91
28 by Tim Marston
rc-interface: added code to read from serial and set motor channel values
92
	// reset the motor values
93
	for( int a = 0; a < NUM_MOTORS; a++ )
94
		motor_values[ a ] = 0;
95
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
96
	while( true )
97
	{
18 by Tim Marston
completed (hopefully) the RC interface software
98
		// have we read a whole set of channel values?
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
99
		if( Receiver::read_channels( channel_values ) )
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
100
		{
18 by Tim Marston
completed (hopefully) the RC interface software
101
			// yes, flash led!
102
			flash_led();
103
104
			// send channel values to main board
105
			Comms::write_channels( channel_values );
28 by Tim Marston
rc-interface: added code to read from serial and set motor channel values
106
		}
107
108
		// update motors
109
		Motors::update();
110
111
		// read channel values from main board
112
		if( Comms::read_channels( motor_values ) )
113
		{
18 by Tim Marston
completed (hopefully) the RC interface software
114
			// set motor speeds
28 by Tim Marston
rc-interface: added code to read from serial and set motor channel values
115
			Motors::set_values( motor_values );
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
116
		}
18 by Tim Marston
completed (hopefully) the RC interface software
117
118
		// update motors
119
		Motors::update();
16 by Tim Marston
added (unfinished) rc-interface module to src; added arduino.mk
120
	}
121
}