/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: 2013-10-30 22:41:50 UTC
  • Revision ID: tim@ed.am-20131030224150-t6xs1l783m2yg6rd
updated comments in receiver test programs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// motor.cc
3
 
//
4
 
 
5
 
 
6
 
#include "motors.h"
7
 
#include "config.h"
8
 
#include <Arduino.h>
9
 
#include <limits.h>
10
 
 
11
 
 
12
 
static int channels_[ NUM_MOTORS ];
13
 
 
14
 
 
15
 
void Motors::setup()
16
 
{
17
 
        // setup outputs
18
 
        for( int pin = FIRST_PIN; pin < FIRST_PIN + NUM_CHANNELS; pin++ ) {
19
 
                pinMode( pin, OUTPUT );
20
 
                digitalWrite( pin, LOW );
21
 
        }
22
 
 
23
 
        // reset channel values
24
 
        for( int a = 0; a < NUM_MOTORS; a++ )
25
 
                channels_[ a ] = 0;
26
 
}
27
 
 
28
 
 
29
 
void Motors::set_values( int channel_values[] )
30
 
{
31
 
        for( int a = 0; a < NUM_MOTORS; a++ )
32
 
                channels_[ a ] = channel_values[ a ];
33
 
}
34
 
 
35
 
 
36
 
void Motors::update()
37
 
{
38
 
        static int event = NUM_CHANNELS * 2;
39
 
        unsigned long now = micros();
40
 
        static unsigned long frame_start = now - FRAME_DURATION;
41
 
        static unsigned long next_event_at;
42
 
 
43
 
        if( now >= next_event_at )
44
 
        {
45
 
                // action event
46
 
                digitalWrite( FIRST_PIN + ( event / 2 ), ( event & 1 )? LOW : HIGH );
47
 
 
48
 
                // move to next event
49
 
                if( ++event >= NUM_CHANNELS * 2 ) {
50
 
                        event = 0;
51
 
                        frame_start += FRAME_DURATION;
52
 
                }
53
 
 
54
 
                // calculate the time that the next event will occur
55
 
                next_event_at = frame_start + ( event / 2 ) * CHANNEL_INTERVAL;
56
 
                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
 
                // }
73
 
        }
74
 
}