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