/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: dan
  • Date: 2016-04-10 13:36:22 UTC
  • Revision ID: dan-20160410133622-eagrslv007bj3loz
Updated Megashield project after long time

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