Project

General

Profile

Revision 276

cleaned up library - dos2unix and delete-trailing-whitespace

View differences:

motor.c
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 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
	if(direction == 0) {
82
	    // turn off PWM first if switching directions
83
		if((PORTA & 0x30) != 0x10)
84
		{
85
		  OCR1A = 0;
86
		}	
87
		PORTA = (PORTA & 0xCF) | 0x10;
88
//		PORTD |= 0x10;
89
//		PORTD &= 0xBF;
90
	}
91
	else {
92
	    // turn off PWM first if switching directions
93
		if((PORTA & 0x30) != 0x20)
94
		{
95
		  OCR1A = 0;
96
		}
97
		PORTA = (PORTA & 0xCF) | 0x20;
98
//		PORTD |= 0x40;
99
//		PORTD &= 0xEF;
100
	}
101
	
102
	// Set the timer to count up to speed, an 8-bit value
103
	OCR1AL = speed;
104
}
105

  
106
/**
107
 * Sets the speed and direction of motor2.
108
 * motors_init must be called before this function can be used.
109
 *
110
 * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
111
 * @param speed The speed the motor will run at, in the range 0-255.
112
 *
113
 * @see motor1_set, motors_init
114
 **/
115
void motor2_set(int direction, int speed) {
116
	if(direction == 0) {
117
//		PORTD |= 0x20;
118
//		PORTD &= 0x7F;
119
	    // turn off PWM first if switching directions
120
		if((PORTA & 0xC0) != 0x80)
121
		{
122
		  OCR1B = 0;
123
		}		
124
		
125
		PORTA = (PORTA & 0x3F) | 0x80;
126
	}
127
	else {
128
//		PORTD |= 0x80;
129
//		PORTD &= 0xDF;
130

  
131
	    // turn off PWM first if switching directions
132
		if((PORTA & 0xC0) != 0x40)
133
		{
134
		  OCR1B = 0;
135
		}		
136
		
137
		PORTA = (PORTA & 0x3F) | 0x40;
138
	}
139
	OCR1BL = speed;
140
}
141

  
142
/**
143
 * Turns off both motors.
144
 *
145
 * @see motors_init
146
 **/
147
void motors_off( void ) {
148
	OCR1AL = 0x0;
149
	OCR1BL = 0x0;
150
}
151

  
152
/**@}**///end defgroup
153

  
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 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
	if(direction == 0) {
82
	    // turn off PWM first if switching directions
83
		if((PORTA & 0x30) != 0x10)
84
		{
85
		  OCR1A = 0;
86
		}
87
		PORTA = (PORTA & 0xCF) | 0x10;
88
//		PORTD |= 0x10;
89
//		PORTD &= 0xBF;
90
	}
91
	else {
92
	    // turn off PWM first if switching directions
93
		if((PORTA & 0x30) != 0x20)
94
		{
95
		  OCR1A = 0;
96
		}
97
		PORTA = (PORTA & 0xCF) | 0x20;
98
//		PORTD |= 0x40;
99
//		PORTD &= 0xEF;
100
	}
101

  
102
	// Set the timer to count up to speed, an 8-bit value
103
	OCR1AL = speed;
104
}
105

  
106
/**
107
 * Sets the speed and direction of motor2.
108
 * motors_init must be called before this function can be used.
109
 *
110
 * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
111
 * @param speed The speed the motor will run at, in the range 0-255.
112
 *
113
 * @see motor1_set, motors_init
114
 **/
115
void motor2_set(int direction, int speed) {
116
	if(direction == 0) {
117
//		PORTD |= 0x20;
118
//		PORTD &= 0x7F;
119
	    // turn off PWM first if switching directions
120
		if((PORTA & 0xC0) != 0x80)
121
		{
122
		  OCR1B = 0;
123
		}
124

  
125
		PORTA = (PORTA & 0x3F) | 0x80;
126
	}
127
	else {
128
//		PORTD |= 0x80;
129
//		PORTD &= 0xDF;
130

  
131
	    // turn off PWM first if switching directions
132
		if((PORTA & 0xC0) != 0x40)
133
		{
134
		  OCR1B = 0;
135
		}
136

  
137
		PORTA = (PORTA & 0x3F) | 0x40;
138
	}
139
	OCR1BL = speed;
140
}
141

  
142
/**
143
 * Turns off both motors.
144
 *
145
 * @see motors_init
146
 **/
147
void motors_off( void ) {
148
	OCR1AL = 0x0;
149
	OCR1BL = 0x0;
150
}
151

  
152
/**@}**///end defgroup
153

  

Also available in: Unified diff