root / branches / simulator / projects / libdragonfly / motor.c @ 891
History | View | Annotate | Download (2.86 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 |
void motors_init( void ) { |
40 |
// Configure counter such that we use phase correct
|
41 |
// PWM with 8-bit resolution
|
42 |
PORTA &= 0x0F;
|
43 |
DDRA |= 0xF0;
|
44 |
DDRB |= 0x60;
|
45 |
|
46 |
//timer 1A and 1B
|
47 |
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10); |
48 |
TCCR1B = _BV(WGM12) | _BV(CS10); |
49 |
// TCCR1A = 0xA1;
|
50 |
// TCCR1B = 0x04;
|
51 |
OCR1AH=0;
|
52 |
OCR1AL=0;
|
53 |
OCR1BH=0;
|
54 |
OCR1BL=0;
|
55 |
} |
56 |
|
57 |
void motor1_set(int direction, int speed) { |
58 |
|
59 |
if(direction == 0) { |
60 |
// turn off PWM first if switching directions
|
61 |
if((PORTA & 0x30) != 0x10) { |
62 |
OCR1A = 0;
|
63 |
} |
64 |
PORTA = (PORTA & 0xCF) | 0x10; |
65 |
// PORTD |= 0x10;
|
66 |
// PORTD &= 0xBF;
|
67 |
} else {
|
68 |
// turn off PWM first if switching directions
|
69 |
if((PORTA & 0x30) != 0x20) { |
70 |
OCR1A = 0;
|
71 |
} |
72 |
PORTA = (PORTA & 0xCF) | 0x20; |
73 |
// PORTD |= 0x40;
|
74 |
// PORTD &= 0xEF;
|
75 |
} |
76 |
|
77 |
// Set the timer to count up to speed, an 8-bit value
|
78 |
OCR1AL = speed; |
79 |
} |
80 |
|
81 |
void motor2_set(int direction, int speed) { |
82 |
if(direction == 0) { |
83 |
// PORTD |= 0x20;
|
84 |
// PORTD &= 0x7F;
|
85 |
// turn off PWM first if switching directions
|
86 |
if((PORTA & 0xC0) != 0x80) { |
87 |
OCR1B = 0;
|
88 |
} |
89 |
|
90 |
PORTA = (PORTA & 0x3F) | 0x80; |
91 |
} else {
|
92 |
// PORTD |= 0x80;
|
93 |
// PORTD &= 0xDF;
|
94 |
|
95 |
// turn off PWM first if switching directions
|
96 |
if((PORTA & 0xC0) != 0x40) { |
97 |
OCR1B = 0;
|
98 |
} |
99 |
|
100 |
PORTA = (PORTA & 0x3F) | 0x40; |
101 |
} |
102 |
OCR1BL = speed; |
103 |
} |
104 |
|
105 |
void motors_off( void ) { |
106 |
OCR1AL = 0x0;
|
107 |
OCR1BL = 0x0;
|
108 |
} |
109 |
|