Statistics
| Revision:

root / demos / hunter_prey / lib / src / libdragonfly / motor.c @ 1828

History | View | Annotate | Download (4.67 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
/**
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
  // Configure counter such that we use phase correct
54
  // PWM with 8-bit resolution
55
  PORTA &= 0x0F;
56
  DDRA |= 0xF0;
57
  DDRB |= 0x60;
58

    
59
  //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
}
69

    
70
/**
71
 * Sets the speed and direction of the left motor.
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 motor_r_set, motors_init
78
 **/
79
void motor_l_set(int direction, int speed) {
80

    
81
  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
        
99
  // Set the timer to count up to speed, an 8-bit value
100
  OCR1AL = speed;
101
}
102

    
103
/**
104
 * Sets the speed and direction of the right motor.
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 motor_l_set, motors_init
111
 **/
112
void motor_r_set(int direction, int speed) {
113
  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
                
121
    PORTA = (PORTA & 0x3F) | 0x80;
122
  } else {
123
    //                PORTD |= 0x80;
124
    //                PORTD &= 0xDF;
125

    
126
    // turn off PWM first if switching directions
127
    if((PORTA & 0xC0) != 0x40) {
128
      OCR1B = 0;
129
    }                
130
                
131
    PORTA = (PORTA & 0x3F) | 0x40;
132
  }
133
  OCR1BL = speed;
134
}
135

    
136
/**
137
 * Sets the speed and direction of motor1.
138
 * motors_init must be called before this function can be used.
139
 *
140
 * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
141
 * @param speed The speed the motor will run at, in the range 0-255.
142
 *
143
 * @see motor2_set, motors_init
144
 **/
145
void motor1_set(int direction, int speed) {
146
        motor_l_set(direction, speed);
147
}
148

    
149
/**
150
 * Sets the speed and direction of motor2.
151
 * motors_init must be called before this function can be used.
152
 *
153
 * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
154
 * @param speed The speed the motor will run at, in the range 0-255.
155
 *
156
 * @see motor2_set, motors_init
157
 **/
158
void motor2_set(int direction, int speed) {
159
        motor_r_set(direction, speed);
160
}
161

    
162

    
163
/**
164
 * Turns off both motors.
165
 *
166
 * @see motors_init
167
 **/
168
void motors_off( void ) {
169
  OCR1AL = 0x0;
170
  OCR1BL = 0x0;
171
}
172

    
173
/**@}**///end defgroup
174