/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
36
33
// If this turns out to be the case, you will need to arrange for the pulses to
37
34
// wire up the channels in an order whereby the pulses alternatively alternately
38
35
// between the two interrupts and adjust the channel order below.
39
 
//
40
 
// Motor control
41
 
// -------------
42
 
//
43
 
// 
44
36
 
45
37
 
46
38
#include "config.h"
47
39
#include "receiver.h"
48
 
#include "motors.h"
49
 
#include "comms.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
50
49
 
51
50
 
52
51
void setup()
53
52
{
54
53
        Receiver::setup();
55
 
        Motors::setup();
56
 
        Comms::setup();
57
 
 
58
 
        // led
59
 
        pinMode( LED_PIN, OUTPUT );
60
 
        digitalWrite( LED_PIN, LOW );
 
54
        
 
55
        Serial.begin( 9600 );
61
56
}
62
57
 
63
58
 
64
 
void flash_led()
 
59
void draw_graph( unsigned long channel_values[] )
65
60
{
66
 
        static bool on = false;
67
 
        on = !on;
68
 
        digitalWrite( LED_PIN, on? HIGH : LOW );
 
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( "|" );
69
82
}
70
83
 
71
84
 
72
85
void loop()
73
86
{
74
 
        int channel_values[ NUM_CHANNELS ];
 
87
        unsigned long channel_values[ NUM_CHANNELS ];
75
88
 
76
89
        while( true )
77
90
        {
78
 
                // have we read a whole set of channel values?
79
91
                if( Receiver::read_channels( channel_values ) )
80
 
                {
81
 
                        // yes, flash led!
82
 
                        flash_led();
83
 
 
84
 
                        // send channel values to main board
85
 
                        Comms::write_channels( channel_values );
86
 
 
87
 
                        // set motor speeds
88
 
                        Motors::set_values( channel_values );
89
 
                }
90
 
 
91
 
                // update motors
92
 
                Motors::update();
 
92
                        draw_graph( channel_values );
93
93
        }
94
94
}