/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

Lines of Context:
9
9
#include <limits.h>
10
10
 
11
11
 
12
 
static int channels_[ NUM_MOTORS ];
13
 
 
14
 
 
15
12
void Motors::setup()
16
13
{
17
14
        // setup outputs
18
15
        for( int pin = FIRST_PIN; pin < FIRST_PIN + NUM_CHANNELS; pin++ ) {
19
16
                pinMode( pin, OUTPUT );
20
17
                digitalWrite( pin, LOW );
21
 
        }
22
 
 
23
 
        // reset channel values
24
 
        for( int a = 0; a < NUM_MOTORS; a++ )
25
 
                channels_[ a ] = 0;
 
18
        }       
26
19
}
27
20
 
28
21
 
29
 
void Motors::set_values( int channel_values[] )
 
22
static signed long calculate_duration( unsigned long then, unsigned long now )
30
23
{
31
 
        for( int a = 0; a < NUM_MOTORS; a++ )
32
 
                channels_[ a ] = channel_values[ a ];
 
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;
33
31
}
34
32
 
35
33
 
36
 
void Motors::update()
 
34
void Motors::update_channels( int channels[] )
37
35
{
38
36
        static int event = NUM_CHANNELS * 2;
39
37
        unsigned long now = micros();
54
52
                // calculate the time that the next event will occur
55
53
                next_event_at = frame_start + ( event / 2 ) * CHANNEL_INTERVAL;
56
54
                if( event & 1 )
57
 
                        next_event_at += MIN_PULSE_WIDTH + channels_[ event / 2 ] *
58
 
                                ( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
59
 
 
60
 
                // Serial.print( event / 2 );
61
 
                // Serial.print( ( event & 1 )? 'v' : '^' );
62
 
                // Serial.print( " " );
63
 
                // Serial.println( next_event_at );
64
 
 
65
 
                // if( event == 1 ) {
66
 
                //      unsigned long width = MIN_PULSE_WIDTH +
67
 
                //              (unsigned long)channels_[ event / 2 ] *
68
 
                //              ( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
69
 
                //      Serial.print( channels_[ event / 2 ] );
70
 
                //      Serial.print( " " );
71
 
                //      Serial.println( width );
72
 
                // }
 
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 );
 
70
                }
73
71
        }
74
72
}