root / branches / encoders / code / lib / src / libdragonfly / motor.c @ 1345
History | View | Annotate | Download (2.65 KB)
1 | 7 | bcoltin | /*
|
---|---|---|---|
2 | motor.c - Contains functions necessary for activating and driving the
|
||
3 | H-bridge
|
||
4 | |||
5 | |||
6 | author: Robotics Club, Colony project
|
||
7 | |||
8 | much of this is taken from FWR's library, author: Tom Lauwers
|
||
9 | |||
10 | */
|
||
11 | |||
12 | #include "motor.h" |
||
13 | |||
14 | /**
|
||
15 | * @defgroup motors Motors
|
||
16 | * @brief Functions for controlling the motors.
|
||
17 | * Functions for controlling the motors. Found in motor.h.
|
||
18 | * @{
|
||
19 | **/
|
||
20 | |||
21 | /**
|
||
22 | * Initializes both motors so that they can be used with future
|
||
23 | * calls to motor1_set and motor2_set.
|
||
24 | *
|
||
25 | * @see motors_off, motor1_set, motor2_set
|
||
26 | **/
|
||
27 | void motors_init( void ) { |
||
28 | // Configure counter such that we use phase correct
|
||
29 | // PWM with 8-bit resolution
|
||
30 | PORTA &= 0x0F;
|
||
31 | DDRA |= 0xF0;
|
||
32 | DDRB |= 0x60;
|
||
33 | |||
34 | //timer 1A and 1B
|
||
35 | TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10); |
||
36 | TCCR1B = _BV(WGM12) | _BV(CS10); |
||
37 | // TCCR1A = 0xA1;
|
||
38 | // TCCR1B = 0x04;
|
||
39 | OCR1AH=0;
|
||
40 | OCR1AL=0;
|
||
41 | OCR1BH=0;
|
||
42 | OCR1BL=0;
|
||
43 | } |
||
44 | |||
45 | /**
|
||
46 | * Sets the speed and direction of motor1.
|
||
47 | * motors_init must be called before this function can be used.
|
||
48 | *
|
||
49 | * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
|
||
50 | * @param speed The speed the motor will run at, in the range 0-255.
|
||
51 | *
|
||
52 | * @see motor2_set, motors_init
|
||
53 | **/
|
||
54 | void motor1_set(int direction, int speed) { |
||
55 | |||
56 | if(direction == 0) { |
||
57 | // turn off PWM first if switching directions
|
||
58 | if((PORTA & 0x30) != 0x10) |
||
59 | { |
||
60 | OCR1A = 0;
|
||
61 | } |
||
62 | PORTA = (PORTA & 0xCF) | 0x10; |
||
63 | // PORTD |= 0x10;
|
||
64 | // PORTD &= 0xBF;
|
||
65 | } |
||
66 | else {
|
||
67 | // turn off PWM first if switching directions
|
||
68 | if((PORTA & 0x30) != 0x20) |
||
69 | { |
||
70 | OCR1A = 0;
|
||
71 | } |
||
72 | PORTA = (PORTA & 0xCF) | 0x20; |
||
73 | // PORTD |= 0x40;
|
||
74 | // PORTD &= 0xEF;
|
||
75 | } |
||
76 | |||
77 | // Set the timer to count up to speed, an 8-bit value
|
||
78 | OCR1AL = speed; |
||
79 | } |
||
80 | |||
81 | /**
|
||
82 | * Sets the speed and direction of motor2.
|
||
83 | * motors_init must be called before this function can be used.
|
||
84 | *
|
||
85 | * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
|
||
86 | * @param speed The speed the motor will run at, in the range 0-255.
|
||
87 | *
|
||
88 | * @see motor1_set, motors_init
|
||
89 | **/
|
||
90 | void motor2_set(int direction, int speed) { |
||
91 | if(direction == 0) { |
||
92 | // PORTD |= 0x20;
|
||
93 | // PORTD &= 0x7F;
|
||
94 | // turn off PWM first if switching directions
|
||
95 | if((PORTA & 0xC0) != 0x80) |
||
96 | { |
||
97 | OCR1B = 0;
|
||
98 | } |
||
99 | |||
100 | PORTA = (PORTA & 0x3F) | 0x80; |
||
101 | } |
||
102 | else {
|
||
103 | // PORTD |= 0x80;
|
||
104 | // PORTD &= 0xDF;
|
||
105 | |||
106 | // turn off PWM first if switching directions
|
||
107 | if((PORTA & 0xC0) != 0x40) |
||
108 | { |
||
109 | OCR1B = 0;
|
||
110 | } |
||
111 | |||
112 | PORTA = (PORTA & 0x3F) | 0x40; |
||
113 | } |
||
114 | OCR1BL = speed; |
||
115 | } |
||
116 | |||
117 | /**
|
||
118 | * Turns off both motors.
|
||
119 | *
|
||
120 | * @see motors_init
|
||
121 | **/
|
||
122 | void motors_off( void ) { |
||
123 | OCR1AL = 0x0;
|
||
124 | OCR1BL = 0x0;
|
||
125 | } |
||
126 | |||
127 | /**@}**///end defgroup |