12
static int channels_[ NUM_MOTORS ];
12
15
void Motors::setup()
15
for( int pin = FIRST_PIN; pin < FIRST_PIN + NUM_CHANNELS; pin++ ) {
18
for( int pin = FIRST_PIN; pin < FIRST_PIN + NUM_MOTORS; pin++ ) {
16
19
pinMode( pin, OUTPUT );
17
20
digitalWrite( pin, LOW );
22
static signed long calculate_duration( unsigned long then, unsigned long now )
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 );
28
// else, calculate duration
34
void Motors::update_channels( int channels[] )
36
static int event = NUM_CHANNELS * 2;
23
// reset channel values
24
for( int a = 0; a < NUM_MOTORS; a++ )
29
void Motors::set_values( int channel_values[] )
31
for( int a = 0; a < NUM_MOTORS; a++ )
32
channels_[ a ] = channel_values[ a ];
38
static int event = NUM_MOTORS * 2;
37
39
unsigned long now = micros();
38
40
static unsigned long frame_start = now - FRAME_DURATION;
39
static unsigned long next_event_at;
41
static unsigned long next_event_at = 0;
41
43
if( now >= next_event_at )
44
digitalWrite( FIRST_PIN + ( event / 2 ), ( event & 1 )? LOW : HIGH );
46
if( event < NUM_MOTORS * 2 )
47
digitalWrite( FIRST_PIN + ( event / 2 ),
48
( event & 1 )? LOW : HIGH );
46
50
// move to next event
47
if( ++event >= NUM_CHANNELS * 2 ) {
51
if( ++event >= NUM_MOTORS * 2 ) {
49
53
frame_start += FRAME_DURATION;
52
56
// calculate the time that the next event will occur
53
57
next_event_at = frame_start + ( event / 2 ) * CHANNEL_INTERVAL;
55
next_event_at += MIN_PULSE_WIDTH + channels[ event / 2 ] *
56
( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
58
// Serial.print( event / 2 );
59
// Serial.print( ( event & 1 )? 'v' : '^' );
60
// Serial.print( " " );
61
// Serial.println( next_event_at );
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 ] );
69
Serial.println( width );
59
next_event_at += MIN_PULSE_WIDTH + channels_[ event / 2 ] *
60
( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
62
// Serial.print( event / 2 );
63
// Serial.print( ( event & 1 )? 'v' : '^' );
64
// Serial.print( " " );
65
// Serial.println( next_event_at );
68
// unsigned long width = MIN_PULSE_WIDTH +
69
// (unsigned long)channels_[ event / 2 ] *
70
// ( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
71
// Serial.print( channels_[ event / 2 ] );
72
// Serial.print( " " );
73
// Serial.println( width );