/elec/quadcopter

To get this branch, use:
bzr branch http://bzr.ed.am/elec/quadcopter
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
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
18 by Tim Marston
completed (hopefully) the RC interface software
12
static int channels_[ NUM_MOTORS ];
13
14
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
15
void Motors::setup()
16
{
17
	// setup outputs
21 by Tim Marston
fixed some bugs in rc-interface introduced in the conversion from the motors
18
	for( int pin = FIRST_PIN; pin < FIRST_PIN + NUM_MOTORS; pin++ ) {
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
19
		pinMode( pin, OUTPUT );
20
		digitalWrite( pin, LOW );
18 by Tim Marston
completed (hopefully) the RC interface software
21
	}
22
23
	// reset channel values
24
	for( int a = 0; a < NUM_MOTORS; a++ )
25
		channels_[ a ] = 0;
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
26
}
27
28
18 by Tim Marston
completed (hopefully) the RC interface software
29
void Motors::set_values( int channel_values[] )
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
30
{
18 by Tim Marston
completed (hopefully) the RC interface software
31
	for( int a = 0; a < NUM_MOTORS; a++ )
32
		channels_[ a ] = channel_values[ a ];
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
33
}
34
35
18 by Tim Marston
completed (hopefully) the RC interface software
36
void Motors::update()
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
37
{
21 by Tim Marston
fixed some bugs in rc-interface introduced in the conversion from the motors
38
	static int event = NUM_MOTORS * 2;
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
39
	unsigned long now = micros();
40
	static unsigned long frame_start = now - FRAME_DURATION;
21 by Tim Marston
fixed some bugs in rc-interface introduced in the conversion from the motors
41
	static unsigned long next_event_at = 0;
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
42
43
	if( now >= next_event_at )
44
	{
45
		// action event
21 by Tim Marston
fixed some bugs in rc-interface introduced in the conversion from the motors
46
		if( event < NUM_MOTORS * 2 )
47
			digitalWrite( FIRST_PIN + ( event / 2 ),
48
				( event & 1 )? LOW : HIGH );
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
49
50
		// move to next event
21 by Tim Marston
fixed some bugs in rc-interface introduced in the conversion from the motors
51
		if( ++event >= NUM_MOTORS * 2 ) {
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
52
			event = 0;
53
			frame_start += FRAME_DURATION;
54
		}
55
56
		// calculate the time that the next event will occur
57
		next_event_at = frame_start + ( event / 2 ) * CHANNEL_INTERVAL;
58
		if( event & 1 )
18 by Tim Marston
completed (hopefully) the RC interface software
59
			next_event_at += MIN_PULSE_WIDTH + channels_[ event / 2 ] *
60
				( MAX_PULSE_WIDTH - MIN_PULSE_WIDTH ) / MAX_CHANNEL_VALUE;
61
62
		// Serial.print( event / 2 );
63
		// Serial.print( ( event & 1 )? 'v' : '^' );
64
		// Serial.print( " " );
65
		// Serial.println( next_event_at );
66
67
		// if( event == 1 ) {
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 );
74
		// }
17 by Tim Marston
rc-interface: mostly completed it (untested, but rx and tx code should work)
75
	}
76
}