Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / libdragonfly / motor.c @ 943

History | View | Annotate | Download (2.88 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
#include <avr/io.h>
40

    
41
void motors_init( void ) {
42
  // Configure counter such that we use phase correct
43
  // PWM with 8-bit resolution
44
  PORTA &= 0x0F;
45
  DDRA |= 0xF0;
46
  DDRB |= 0x60;
47

    
48
  //timer 1A and 1B
49
  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10);
50
  TCCR1B = _BV(WGM12) | _BV(CS10);
51
  //        TCCR1A = 0xA1;
52
  //        TCCR1B = 0x04;
53
  OCR1AH=0;
54
  OCR1AL=0;
55
  OCR1BH=0;
56
  OCR1BL=0;
57
}
58

    
59
void motor1_set(int direction, int speed) {
60

    
61
  if(direction == 0) {
62
    // turn off PWM first if switching directions
63
    if((PORTA & 0x30) != 0x10) {
64
      OCR1A = 0;
65
    }        
66
    PORTA = (PORTA & 0xCF) | 0x10;
67
    //                PORTD |= 0x10;
68
    //                PORTD &= 0xBF;
69
  } else {
70
    // turn off PWM first if switching directions
71
    if((PORTA & 0x30) != 0x20) {
72
      OCR1A = 0;
73
    }
74
    PORTA = (PORTA & 0xCF) | 0x20;
75
    //                PORTD |= 0x40;
76
    //                PORTD &= 0xEF;
77
  }
78
        
79
  // Set the timer to count up to speed, an 8-bit value
80
  OCR1AL = speed;
81
}
82

    
83
void motor2_set(int direction, int speed) {
84
  if(direction == 0) {
85
    //                PORTD |= 0x20;
86
    //                PORTD &= 0x7F;
87
    // turn off PWM first if switching directions
88
    if((PORTA & 0xC0) != 0x80) {
89
      OCR1B = 0;
90
    }                
91
                
92
    PORTA = (PORTA & 0x3F) | 0x80;
93
  } else {
94
    //                PORTD |= 0x80;
95
    //                PORTD &= 0xDF;
96

    
97
    // turn off PWM first if switching directions
98
    if((PORTA & 0xC0) != 0x40) {
99
      OCR1B = 0;
100
    }                
101
                
102
    PORTA = (PORTA & 0x3F) | 0x40;
103
  }
104
  OCR1BL = speed;
105
}
106

    
107
void motors_off( void ) {
108
  OCR1AL = 0x0;
109
  OCR1BL = 0x0;
110
}
111