root / branches / init_refactor / code / projects / libdragonfly / motor.c @ 1543
History | View | Annotate | Download (5.68 KB)
1 | 241 | bcoltin | /**
|
---|---|---|---|
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 | 8 | bcoltin | |
26 | 241 | bcoltin | |
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 | 1462 | dsschult | #include "dragonfly_defs.h" |
38 | 8 | bcoltin | #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 | 1461 | bneuman | unsigned char motors_initd=0; |
48 | |||
49 | 8 | bcoltin | /**
|
50 | * Initializes both motors so that they can be used with future
|
||
51 | * calls to motor1_set and motor2_set.
|
||
52 | *
|
||
53 | 1461 | bneuman | * @return 0 if init succesfull, an error code otherwise
|
54 | *
|
||
55 | 8 | bcoltin | * @see motors_off, motor1_set, motor2_set
|
56 | **/
|
||
57 | 1461 | bneuman | int motors_init( void ) { |
58 | |||
59 | 1543 | bneuman | if(motors_initd) {
|
60 | DRAGONFLY_DEBUG_PRINT("ERROR: motors already init'd\r\n");
|
||
61 | 1461 | bneuman | return ERROR_INIT_ALREADY_INITD;
|
62 | 1543 | bneuman | } |
63 | 1461 | bneuman | |
64 | |||
65 | 380 | jknichel | // Configure counter such that we use phase correct
|
66 | // PWM with 8-bit resolution
|
||
67 | PORTA &= 0x0F;
|
||
68 | DDRA |= 0xF0;
|
||
69 | DDRB |= 0x60;
|
||
70 | 8 | bcoltin | |
71 | 380 | jknichel | //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 | 1461 | bneuman | |
81 | motors_initd=1;
|
||
82 | return 0; |
||
83 | 8 | bcoltin | } |
84 | |||
85 | /**
|
||
86 | 1412 | emullini | * Sets the speed and direction of the left motor.
|
87 | 8 | bcoltin | * 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 | 1461 | bneuman | * @return 0 if init succesfull, an error code otherwise
|
93 | *
|
||
94 | 1412 | emullini | * @see motor_r_set, motors_init
|
95 | 8 | bcoltin | **/
|
96 | 1461 | bneuman | int motor_l_set(int direction, int speed) { |
97 | 1543 | bneuman | if(!motors_initd) {
|
98 | DRAGONFLY_DEBUG_PRINT("ERROR: motors not init'd\r\n");
|
||
99 | 1461 | bneuman | return ERROR_LIBRARY_NOT_INITD;
|
100 | 1543 | bneuman | } |
101 | 8 | bcoltin | |
102 | 380 | jknichel | 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 | 8 | bcoltin | |
120 | 380 | jknichel | // Set the timer to count up to speed, an 8-bit value
|
121 | OCR1AL = speed; |
||
122 | 1461 | bneuman | |
123 | return 0; |
||
124 | 8 | bcoltin | } |
125 | |||
126 | /**
|
||
127 | 1412 | emullini | * Sets the speed and direction of the right motor.
|
128 | 8 | bcoltin | * 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 | 1461 | bneuman | * @return 0 if init succesfull, an error code otherwise
|
134 | *
|
||
135 | 1412 | emullini | * @see motor_l_set, motors_init
|
136 | 8 | bcoltin | **/
|
137 | 1461 | bneuman | int motor_r_set(int direction, int speed) { |
138 | 1543 | bneuman | if(!motors_initd) {
|
139 | DRAGONFLY_DEBUG_PRINT("ERROR: encoders not init'd\r\n");
|
||
140 | 1461 | bneuman | return ERROR_LIBRARY_NOT_INITD;
|
141 | 1543 | bneuman | } |
142 | 1461 | bneuman | |
143 | 380 | jknichel | 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 | 8 | bcoltin | |
151 | 380 | jknichel | PORTA = (PORTA & 0x3F) | 0x80; |
152 | } else {
|
||
153 | // PORTD |= 0x80;
|
||
154 | // PORTD &= 0xDF;
|
||
155 | 8 | bcoltin | |
156 | 380 | jknichel | // turn off PWM first if switching directions
|
157 | if((PORTA & 0xC0) != 0x40) { |
||
158 | OCR1B = 0;
|
||
159 | } |
||
160 | 8 | bcoltin | |
161 | 380 | jknichel | PORTA = (PORTA & 0x3F) | 0x40; |
162 | } |
||
163 | OCR1BL = speed; |
||
164 | 1461 | bneuman | |
165 | return 0; |
||
166 | 8 | bcoltin | } |
167 | |||
168 | /**
|
||
169 | 1412 | emullini | * 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 | 1461 | bneuman | * @return 0 if init succesfull, an error code otherwise
|
176 | *
|
||
177 | 1412 | emullini | * @see motor2_set, motors_init
|
178 | **/
|
||
179 | 1461 | bneuman | int motor1_set(int direction, int speed) { |
180 | return motor_l_set(direction, speed);
|
||
181 | 1412 | emullini | } |
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 | 1461 | bneuman | * @return 0 if init succesfull, an error code otherwise
|
191 | *
|
||
192 | 1412 | emullini | * @see motor2_set, motors_init
|
193 | **/
|
||
194 | 1461 | bneuman | int motor2_set(int direction, int speed) { |
195 | return motor_r_set(direction, speed);
|
||
196 | 1412 | emullini | } |
197 | |||
198 | |||
199 | /**
|
||
200 | 8 | bcoltin | * Turns off both motors.
|
201 | *
|
||
202 | 1461 | bneuman | * @return 0 if init succesfull, an error code otherwise
|
203 | *
|
||
204 | 8 | bcoltin | * @see motors_init
|
205 | **/
|
||
206 | 1461 | bneuman | int motors_off( void ) { |
207 | 1543 | bneuman | if(!motors_initd) {
|
208 | DRAGONFLY_DEBUG_PRINT("ERROR: encoders not init'd\r\n");
|
||
209 | 1461 | bneuman | return ERROR_LIBRARY_NOT_INITD;
|
210 | 1543 | bneuman | } |
211 | 1461 | bneuman | |
212 | 380 | jknichel | OCR1AL = 0x0;
|
213 | OCR1BL = 0x0;
|
||
214 | 1461 | bneuman | |
215 | return 0; |
||
216 | 8 | bcoltin | } |
217 | |||
218 | /**@}**///end defgroup |