root / branches / init_refactor / code / projects / libdragonfly / motor.c @ 1543
History | View | Annotate | Download (5.68 KB)
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 |
**/
|
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 |
|
37 |
#include "dragonfly_defs.h" |
38 |
#include "motor.h" |
39 |
|
40 |
/**
|
41 |
* @defgroup motors Motors
|
42 |
* @brief Functions for controlling the motors.
|
43 |
* Functions for controlling the motors. Found in motor.h.
|
44 |
* @{
|
45 |
**/
|
46 |
|
47 |
unsigned char motors_initd=0; |
48 |
|
49 |
/**
|
50 |
* Initializes both motors so that they can be used with future
|
51 |
* calls to motor1_set and motor2_set.
|
52 |
*
|
53 |
* @return 0 if init succesfull, an error code otherwise
|
54 |
*
|
55 |
* @see motors_off, motor1_set, motor2_set
|
56 |
**/
|
57 |
int motors_init( void ) { |
58 |
|
59 |
if(motors_initd) {
|
60 |
DRAGONFLY_DEBUG_PRINT("ERROR: motors already init'd\r\n");
|
61 |
return ERROR_INIT_ALREADY_INITD;
|
62 |
} |
63 |
|
64 |
|
65 |
// Configure counter such that we use phase correct
|
66 |
// PWM with 8-bit resolution
|
67 |
PORTA &= 0x0F;
|
68 |
DDRA |= 0xF0;
|
69 |
DDRB |= 0x60;
|
70 |
|
71 |
//timer 1A and 1B
|
72 |
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10); |
73 |
TCCR1B = _BV(WGM12) | _BV(CS10); |
74 |
// TCCR1A = 0xA1;
|
75 |
// TCCR1B = 0x04;
|
76 |
OCR1AH=0;
|
77 |
OCR1AL=0;
|
78 |
OCR1BH=0;
|
79 |
OCR1BL=0;
|
80 |
|
81 |
motors_initd=1;
|
82 |
return 0; |
83 |
} |
84 |
|
85 |
/**
|
86 |
* Sets the speed and direction of the left motor.
|
87 |
* motors_init must be called before this function can be used.
|
88 |
*
|
89 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation.
|
90 |
* @param speed The speed the motor will run at, in the range 0-255.
|
91 |
*
|
92 |
* @return 0 if init succesfull, an error code otherwise
|
93 |
*
|
94 |
* @see motor_r_set, motors_init
|
95 |
**/
|
96 |
int motor_l_set(int direction, int speed) { |
97 |
if(!motors_initd) {
|
98 |
DRAGONFLY_DEBUG_PRINT("ERROR: motors not init'd\r\n");
|
99 |
return ERROR_LIBRARY_NOT_INITD;
|
100 |
} |
101 |
|
102 |
if(direction == 0) { |
103 |
// turn off PWM first if switching directions
|
104 |
if((PORTA & 0x30) != 0x10) { |
105 |
OCR1A = 0;
|
106 |
} |
107 |
PORTA = (PORTA & 0xCF) | 0x10; |
108 |
// PORTD |= 0x10;
|
109 |
// PORTD &= 0xBF;
|
110 |
} else {
|
111 |
// turn off PWM first if switching directions
|
112 |
if((PORTA & 0x30) != 0x20) { |
113 |
OCR1A = 0;
|
114 |
} |
115 |
PORTA = (PORTA & 0xCF) | 0x20; |
116 |
// PORTD |= 0x40;
|
117 |
// PORTD &= 0xEF;
|
118 |
} |
119 |
|
120 |
// Set the timer to count up to speed, an 8-bit value
|
121 |
OCR1AL = speed; |
122 |
|
123 |
return 0; |
124 |
} |
125 |
|
126 |
/**
|
127 |
* Sets the speed and direction of the right motor.
|
128 |
* motors_init must be called before this function can be used.
|
129 |
*
|
130 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation.
|
131 |
* @param speed The speed the motor will run at, in the range 0-255.
|
132 |
*
|
133 |
* @return 0 if init succesfull, an error code otherwise
|
134 |
*
|
135 |
* @see motor_l_set, motors_init
|
136 |
**/
|
137 |
int motor_r_set(int direction, int speed) { |
138 |
if(!motors_initd) {
|
139 |
DRAGONFLY_DEBUG_PRINT("ERROR: encoders not init'd\r\n");
|
140 |
return ERROR_LIBRARY_NOT_INITD;
|
141 |
} |
142 |
|
143 |
if(direction == 0) { |
144 |
// PORTD |= 0x20;
|
145 |
// PORTD &= 0x7F;
|
146 |
// turn off PWM first if switching directions
|
147 |
if((PORTA & 0xC0) != 0x80) { |
148 |
OCR1B = 0;
|
149 |
} |
150 |
|
151 |
PORTA = (PORTA & 0x3F) | 0x80; |
152 |
} else {
|
153 |
// PORTD |= 0x80;
|
154 |
// PORTD &= 0xDF;
|
155 |
|
156 |
// turn off PWM first if switching directions
|
157 |
if((PORTA & 0xC0) != 0x40) { |
158 |
OCR1B = 0;
|
159 |
} |
160 |
|
161 |
PORTA = (PORTA & 0x3F) | 0x40; |
162 |
} |
163 |
OCR1BL = speed; |
164 |
|
165 |
return 0; |
166 |
} |
167 |
|
168 |
/**
|
169 |
* Sets the speed and direction of motor1.
|
170 |
* motors_init must be called before this function can be used.
|
171 |
*
|
172 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation.
|
173 |
* @param speed The speed the motor will run at, in the range 0-255.
|
174 |
*
|
175 |
* @return 0 if init succesfull, an error code otherwise
|
176 |
*
|
177 |
* @see motor2_set, motors_init
|
178 |
**/
|
179 |
int motor1_set(int direction, int speed) { |
180 |
return motor_l_set(direction, speed);
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Sets the speed and direction of motor2.
|
185 |
* motors_init must be called before this function can be used.
|
186 |
*
|
187 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation.
|
188 |
* @param speed The speed the motor will run at, in the range 0-255.
|
189 |
*
|
190 |
* @return 0 if init succesfull, an error code otherwise
|
191 |
*
|
192 |
* @see motor2_set, motors_init
|
193 |
**/
|
194 |
int motor2_set(int direction, int speed) { |
195 |
return motor_r_set(direction, speed);
|
196 |
} |
197 |
|
198 |
|
199 |
/**
|
200 |
* Turns off both motors.
|
201 |
*
|
202 |
* @return 0 if init succesfull, an error code otherwise
|
203 |
*
|
204 |
* @see motors_init
|
205 |
**/
|
206 |
int motors_off( void ) { |
207 |
if(!motors_initd) {
|
208 |
DRAGONFLY_DEBUG_PRINT("ERROR: encoders not init'd\r\n");
|
209 |
return ERROR_LIBRARY_NOT_INITD;
|
210 |
} |
211 |
|
212 |
OCR1AL = 0x0;
|
213 |
OCR1BL = 0x0;
|
214 |
|
215 |
return 0; |
216 |
} |
217 |
|
218 |
/**@}**///end defgroup |
219 |
|