/elec/quadcopter

To get this branch, use:
bzr branch http://bzr.ed.am/elec/quadcopter

« back to all changes in this revision

Viewing changes to src/rc-interface/main.ino

  • Committer: Tim Marston
  • Date: 2014-01-08 22:38:10 UTC
  • Revision ID: tim@ed.am-20140108223810-o1mhgns6tath533c
added (unfinished) rc-interface module to src; added arduino.mk

Show diffs side-by-side

added added

removed removed

9
9
// main board (and software running on it) of the task of interfacing with any
10
10
// of the RC system.
11
11
//
12
 
// Receiver
13
 
// --------
14
 
//
15
12
// The 8 channels (from the RC receiver) need to be combined and fed to two
16
13
// interrupts on the Arduino.  They need to be combined in such a way as to
17
14
// alternate pulses between the two interrupts (which are alternately used to
24
21
//            ___    |    |    |    |    |    |    |    |     ___
25
22
//    GND ---|___|---+----+----+----+    +----+----+----+----|___|--- GND
26
23
//             R                    |    |                     R
27
 
//         ________________________ | __ | _____________________
28
 
//         ARDUINO                  |    |
 
24
//             ____________________ | __ | _____________________
 
25
//                                  |    |
29
26
//                           pin 2  o    o  pin 3
30
27
//                    (interrupt 0)        (interrupt 1)
31
28
//
34
31
//
35
32
// Note that your receiver may not send pulses sequentially, in "channel order".
36
33
// If this turns out to be the case, you will need to arrange for the pulses to
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.
39
 
//
40
 
// Motor control
41
 
// -------------
42
 
//
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.
 
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
 
60
37
 
61
38
#include "config.h"
62
39
#include "receiver.h"
63
 
#include "motors.h"
64
 
#include "comms.h"
65
 
 
66
 
 
67
 
void flash_led()
68
 
{
69
 
        static bool on = false;
70
 
        on = !on;
71
 
        digitalWrite( LED_PIN, on? HIGH : LOW );
72
 
}
 
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
73
49
 
74
50
 
75
51
void setup()
76
52
{
77
 
        // init subsystems
78
53
        Receiver::setup();
79
 
        Motors::setup();
80
 
        Comms::setup();
81
 
 
82
 
        // led
83
 
        pinMode( LED_PIN, OUTPUT );
84
 
        digitalWrite( LED_PIN, LOW );
85
 
}
 
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
 
86
84
 
87
85
void loop()
88
86
{
89
 
        int motor_values[ NUM_MOTORS ];
90
 
        int channel_values[ NUM_CHANNELS ];
91
 
 
92
 
        // reset the motor values
93
 
        for( int a = 0; a < NUM_MOTORS; a++ )
94
 
                motor_values[ a ] = 0;
 
87
        unsigned long channel_values[ NUM_CHANNELS ];
95
88
 
96
89
        while( true )
97
90
        {
98
 
                // have we read a whole set of channel values?
99
91
                if( Receiver::read_channels( channel_values ) )
100
 
                {
101
 
                        // yes, flash led!
102
 
                        flash_led();
103
 
 
104
 
                        // send channel values to main board
105
 
                        Comms::write_channels( 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
 
                {
114
 
                        // set motor speeds
115
 
                        Motors::set_values( motor_values );
116
 
                }
117
 
 
118
 
                // update motors
119
 
                Motors::update();
 
92
                        draw_graph( channel_values );
120
93
        }
121
94
}