/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-29 22:33:55 UTC
  • Revision ID: tim@ed.am-20140129223355-irft5up0wdiq61if
switch rc-interface (and test app) to Arduino Pro Mini

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
16
15
void Motors::setup()
17
16
{
18
17
        // setup outputs
19
 
        for( int pin = FIRST_PIN; pin < FIRST_PIN + NUM_MOTORS; pin++ ) {
 
18
        for( int pin = FIRST_PIN; pin < FIRST_PIN + NUM_CHANNELS; pin++ ) {
20
19
                pinMode( pin, OUTPUT );
21
20
                digitalWrite( pin, LOW );
22
21
        }
36
35
 
37
36
void Motors::update()
38
37
{
39
 
        static int event = NUM_MOTORS * 2;
 
38
        static int event = NUM_CHANNELS * 2;
40
39
        unsigned long now = micros();
41
40
        static unsigned long frame_start = now - FRAME_DURATION;
42
 
        static unsigned long next_event_at = 0;
 
41
        static unsigned long next_event_at;
43
42
 
44
43
        if( now >= next_event_at )
45
44
        {
46
45
                // action event
47
 
                if( event < NUM_MOTORS * 2 )
48
 
                        digitalWrite( FIRST_PIN + ( event / 2 ),
49
 
                                ( event & 1 )? LOW : HIGH );
 
46
                digitalWrite( FIRST_PIN + ( event / 2 ), ( event & 1 )? LOW : HIGH );
50
47
 
51
48
                // move to next event
52
 
                if( ++event >= NUM_MOTORS * 2 ) {
 
49
                if( ++event >= NUM_CHANNELS * 2 ) {
53
50
                        event = 0;
54
51
                        frame_start += FRAME_DURATION;
55
52
                }
74
71
                //      Serial.println( width );
75
72
                // }
76
73
        }
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();
88
 
                }
89
 
        }
90
 
        
91
74
}