root / branches / encoders / code / projects / libdragonfly / motor.c @ 1345
History | View | Annotate | Download (3.87 KB)
1 | 1345 | chihsiuh | /**
|
---|---|---|---|
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 | 1345 | chihsiuh | |
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 | 8 | bcoltin | #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 | /**
|
||
47 | * Initializes both motors so that they can be used with future
|
||
48 | * calls to motor1_set and motor2_set.
|
||
49 | *
|
||
50 | * @see motors_off, motor1_set, motor2_set
|
||
51 | **/
|
||
52 | void motors_init( void ) { |
||
53 | 1345 | chihsiuh | // Configure counter such that we use phase correct
|
54 | // PWM with 8-bit resolution
|
||
55 | PORTA &= 0x0F;
|
||
56 | DDRA |= 0xF0;
|
||
57 | DDRB |= 0x60;
|
||
58 | 8 | bcoltin | |
59 | 1345 | chihsiuh | //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;
|
||
68 | 8 | bcoltin | } |
69 | |||
70 | /**
|
||
71 | * Sets the speed and direction of motor1.
|
||
72 | * motors_init must be called before this function can be used.
|
||
73 | *
|
||
74 | * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
|
||
75 | * @param speed The speed the motor will run at, in the range 0-255.
|
||
76 | *
|
||
77 | * @see motor2_set, motors_init
|
||
78 | **/
|
||
79 | void motor1_set(int direction, int speed) { |
||
80 | |||
81 | 1345 | chihsiuh | 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 | } |
||
98 | 8 | bcoltin | |
99 | 1345 | chihsiuh | // Set the timer to count up to speed, an 8-bit value
|
100 | OCR1AL = speed; |
||
101 | 8 | bcoltin | } |
102 | |||
103 | /**
|
||
104 | * Sets the speed and direction of motor2.
|
||
105 | * motors_init must be called before this function can be used.
|
||
106 | *
|
||
107 | * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
|
||
108 | * @param speed The speed the motor will run at, in the range 0-255.
|
||
109 | *
|
||
110 | * @see motor1_set, motors_init
|
||
111 | **/
|
||
112 | void motor2_set(int direction, int speed) { |
||
113 | 1345 | chihsiuh | 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 | } |
||
120 | 8 | bcoltin | |
121 | 1345 | chihsiuh | PORTA = (PORTA & 0x3F) | 0x80; |
122 | } else {
|
||
123 | // PORTD |= 0x80;
|
||
124 | // PORTD &= 0xDF;
|
||
125 | 8 | bcoltin | |
126 | 1345 | chihsiuh | // turn off PWM first if switching directions
|
127 | if((PORTA & 0xC0) != 0x40) { |
||
128 | OCR1B = 0;
|
||
129 | } |
||
130 | 8 | bcoltin | |
131 | 1345 | chihsiuh | PORTA = (PORTA & 0x3F) | 0x40; |
132 | } |
||
133 | OCR1BL = speed; |
||
134 | 8 | bcoltin | } |
135 | |||
136 | /**
|
||
137 | * Turns off both motors.
|
||
138 | *
|
||
139 | * @see motors_init
|
||
140 | **/
|
||
141 | void motors_off( void ) { |
||
142 | 1345 | chihsiuh | OCR1AL = 0x0;
|
143 | OCR1BL = 0x0;
|
||
144 | 8 | bcoltin | } |
145 | |||
146 | /**@}**///end defgroup |