/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/motors.cc

  • Committer: Tim Marston
  • Date: 2014-01-15 22:18:56 UTC
  • Revision ID: tim@ed.am-20140115221856-lcqttlborwi7ot7k
rc-interface: mostly completed it (untested, but rx and tx code should work)

Show diffs side-by-side

added added

removed removed

5
5
 
6
6
#include "motors.h"
7
7
#include "config.h"
8
 
#include "common.h"
9
8
#include <Arduino.h>
10
9
#include <limits.h>
11
10
 
12
11
 
13
 
static int channels_[ NUM_MOTORS ];
14
 
 
15
 
 
16
12
void Motors::setup()
17
13
{
18
14
        // setup outputs
19
 
        for( int pin = FIRST_PIN; pin < FIRST_PIN + NUM_MOTORS; pin++ ) {
 
15
        for( int pin = FIRST_PIN; pin < FIRST_PIN + NUM_CHANNELS; pin++ ) {
20
16
                pinMode( pin, OUTPUT );
21
17
                digitalWrite( pin, LOW );
22
 
        }
23
 
 
24
 
        // reset channel values
25
 
        for( int a = 0; a < NUM_MOTORS; a++ )
26
 
                channels_[ a ] = 0;
27
 
}
28
 
 
29
 
 
30
 
void Motors::set_values( int channel_values[] )
31
 
{
32
 
        for( int a = 0; a < NUM_MOTORS; a++ )
33
 
                channels_[ a ] = channel_values[ a ];
34
 
}
35
 
 
36
 
 
37
 
void Motors::update()
38
 
{
39
 
        static int event = NUM_MOTORS * 2;
 
18
        }       
 
19
}
 
20
 
 
21
 
 
22
static signed long calculate_duration( unsigned long then, unsigned long now )
 
23
{
 
24
        // does it look like now has overflowed (and wrapped)?
 
25
        if( now < then && now < ( ULONG_MAX / 2 ) && then > ( ULONG_MAX / 2 ) )
 
26
                return now + ( ULONG_MAX - then );
 
27
        
 
28
        // else, calculate duration
 
29
        else
 
30
                return now - then;
 
31
}
 
32
 
 
33
 
 
34
void Motors::update_channels( int channels[] )
 
35
{
 
36
        static int event = NUM_CHANNELS * 2;
40
37
        unsigned long now = micros();
41
38
        static unsigned long frame_start = now - FRAME_DURATION;
42
 
        static unsigned long next_event_at = 0;
 
39
        static unsigned long next_event_at;
43
40
 
44
41
        if( now >= next_event_at )
45
42
        {
46
43
                // action event
47
 
                if( event < NUM_MOTORS * 2 )
48
 
                        digitalWrite( FIRST_PIN + ( event / 2 ),
49
 
                                ( event & 1 )? LOW : HIGH );
 
44
                digitalWrite( FIRST_PIN + ( event / 2 ), ( event & 1 )? LOW : HIGH );
50
45
 
51
46
                // move to next event
52
 
                if( ++event >= NUM_MOTORS * 2 ) {
 
47
                if( ++event >= NUM_CHANNELS * 2 ) {
53
48
                        event = 0;
54
49
                        frame_start += FRAME_DURATION;
55
50
                }
57
52
                // calculate the time that the next event will occur
58
53
                next_event_at = frame_start + ( event / 2 ) * CHANNEL_INTERVAL;
59
54
                if( event & 1 )
60
 
                        next_event_at += MIN_PULSE_WIDTH + channels_[ event / 2 ] *
61
 
                                ( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
62
 
 
63
 
                // Serial.print( event / 2 );
64
 
                // Serial.print( ( event & 1 )? 'v' : '^' );
65
 
                // Serial.print( " " );
66
 
                // Serial.println( next_event_at );
67
 
 
68
 
                // if( event == 1 ) {
69
 
                //      unsigned long width = MIN_PULSE_WIDTH +
70
 
                //              (unsigned long)channels_[ event / 2 ] *
71
 
                //              ( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
72
 
                //      Serial.print( channels_[ event / 2 ] );
73
 
                //      Serial.print( " " );
74
 
                //      Serial.println( width );
75
 
                // }
76
 
        }
77
 
 
78
 
        // else, if an event is coming up soon, sleep and service the event to make
79
 
        // sure that no other code runs and blocks us!
80
 
        else
81
 
        {
82
 
                long delay = calculate_duration( now, next_event_at );
83
 
                if( delay < 250 )
84
 
                {
85
 
                        if( delay > 20 )
86
 
                                delayMicroseconds( delay - 20 );
87
 
                        update();
 
55
                        next_event_at += MIN_PULSE_WIDTH + channels[ event / 2 ] *
 
56
                                ( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
 
57
 
 
58
//              Serial.print( event / 2 );
 
59
//              Serial.print( ( event & 1 )? 'v' : '^' );
 
60
//              Serial.print( " " );
 
61
//              Serial.println( next_event_at );
 
62
 
 
63
                if( event == 1 ) {
 
64
                        unsigned long width = MIN_PULSE_WIDTH +
 
65
                                (unsigned long)channels[ event / 2 ] *
 
66
                                ( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
 
67
                        Serial.print( channels[ event / 2 ] );
 
68
                        Serial.print( " " );
 
69
                        Serial.println( width );
88
70
                }
89
71
        }
90
 
        
91
72
}