Revision 1345
Final spline code for master/slave
updated outdated libdragonfly and libwireless
motor.c | ||
---|---|---|
1 |
/* |
|
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 |
*/ |
|
1 |
/** |
|
2 |
* Copyright (c) 2007 Colony Project |
|
3 |
* |
|
4 |
* Permission is hereby granted, free of charge, to any person |
|
5 |
* obtaining a copy of this software and associated documentation |
|
6 |
* files (the "Software"), to deal in the Software without |
|
7 |
* restriction, including without limitation the rights to use, |
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9 |
* copies of the Software, and to permit persons to whom the |
|
10 |
* Software is furnished to do so, subject to the following |
|
11 |
* conditions: |
|
12 |
* |
|
13 |
* The above copyright notice and this permission notice shall be |
|
14 |
* included in all copies or substantial portions of the Software. |
|
15 |
* |
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
23 |
* OTHER DEALINGS IN THE SOFTWARE. |
|
24 |
**/ |
|
11 | 25 |
|
26 |
|
|
27 |
/** |
|
28 |
* @file motor.c |
|
29 |
* @brief Motors |
|
30 |
* |
|
31 |
* Implementation of functions for controlling the motors. |
|
32 |
* |
|
33 |
* @author Colony Project, CMU Robotics Club |
|
34 |
* Much of this is taken from FWR's library, author: Tom Lauwers |
|
35 |
**/ |
|
36 |
|
|
12 | 37 |
#include "motor.h" |
13 | 38 |
|
14 | 39 |
/** |
... | ... | |
25 | 50 |
* @see motors_off, motor1_set, motor2_set |
26 | 51 |
**/ |
27 | 52 |
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;
|
|
53 |
// Configure counter such that we use phase correct
|
|
54 |
// PWM with 8-bit resolution
|
|
55 |
PORTA &= 0x0F;
|
|
56 |
DDRA |= 0xF0;
|
|
57 |
DDRB |= 0x60;
|
|
33 | 58 |
|
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;
|
|
59 |
//timer 1A and 1B
|
|
60 |
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10);
|
|
61 |
TCCR1B = _BV(WGM12) | _BV(CS10);
|
|
62 |
// TCCR1A = 0xA1;
|
|
63 |
// TCCR1B = 0x04;
|
|
64 |
OCR1AH=0;
|
|
65 |
OCR1AL=0;
|
|
66 |
OCR1BH=0;
|
|
67 |
OCR1BL=0;
|
|
43 | 68 |
} |
44 | 69 |
|
45 | 70 |
/** |
... | ... | |
53 | 78 |
**/ |
54 | 79 |
void motor1_set(int direction, int speed) { |
55 | 80 |
|
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 |
} |
|
81 |
if(direction == 0) { |
|
82 |
// turn off PWM first if switching directions |
|
83 |
if((PORTA & 0x30) != 0x10) { |
|
84 |
OCR1A = 0; |
|
85 |
} |
|
86 |
PORTA = (PORTA & 0xCF) | 0x10; |
|
87 |
// PORTD |= 0x10; |
|
88 |
// PORTD &= 0xBF; |
|
89 |
} else { |
|
90 |
// turn off PWM first if switching directions |
|
91 |
if((PORTA & 0x30) != 0x20) { |
|
92 |
OCR1A = 0; |
|
93 |
} |
|
94 |
PORTA = (PORTA & 0xCF) | 0x20; |
|
95 |
// PORTD |= 0x40; |
|
96 |
// PORTD &= 0xEF; |
|
97 |
} |
|
76 | 98 |
|
77 |
// Set the timer to count up to speed, an 8-bit value
|
|
78 |
OCR1AL = speed;
|
|
99 |
// Set the timer to count up to speed, an 8-bit value
|
|
100 |
OCR1AL = speed;
|
|
79 | 101 |
} |
80 | 102 |
|
81 | 103 |
/** |
... | ... | |
88 | 110 |
* @see motor1_set, motors_init |
89 | 111 |
**/ |
90 | 112 |
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 |
} |
|
113 |
if(direction == 0) { |
|
114 |
// PORTD |= 0x20; |
|
115 |
// PORTD &= 0x7F; |
|
116 |
// turn off PWM first if switching directions |
|
117 |
if((PORTA & 0xC0) != 0x80) { |
|
118 |
OCR1B = 0; |
|
119 |
} |
|
99 | 120 |
|
100 |
PORTA = (PORTA & 0x3F) | 0x80; |
|
101 |
} |
|
102 |
else { |
|
103 |
// PORTD |= 0x80; |
|
104 |
// PORTD &= 0xDF; |
|
121 |
PORTA = (PORTA & 0x3F) | 0x80; |
|
122 |
} else { |
|
123 |
// PORTD |= 0x80; |
|
124 |
// PORTD &= 0xDF; |
|
105 | 125 |
|
106 |
// turn off PWM first if switching directions |
|
107 |
if((PORTA & 0xC0) != 0x40) |
|
108 |
{ |
|
109 |
OCR1B = 0; |
|
110 |
} |
|
126 |
// turn off PWM first if switching directions |
|
127 |
if((PORTA & 0xC0) != 0x40) { |
|
128 |
OCR1B = 0; |
|
129 |
} |
|
111 | 130 |
|
112 |
PORTA = (PORTA & 0x3F) | 0x40;
|
|
113 |
}
|
|
114 |
OCR1BL = speed;
|
|
131 |
PORTA = (PORTA & 0x3F) | 0x40;
|
|
132 |
}
|
|
133 |
OCR1BL = speed;
|
|
115 | 134 |
} |
116 | 135 |
|
117 | 136 |
/** |
... | ... | |
120 | 139 |
* @see motors_init |
121 | 140 |
**/ |
122 | 141 |
void motors_off( void ) { |
123 |
OCR1AL = 0x0;
|
|
124 |
OCR1BL = 0x0;
|
|
142 |
OCR1AL = 0x0;
|
|
143 |
OCR1BL = 0x0;
|
|
125 | 144 |
} |
126 | 145 |
|
127 | 146 |
/**@}**///end defgroup |
Also available in: Unified diff