Project

General

Profile

Statistics
| Revision:

root / branches / init_refactor / code / projects / libdragonfly / dragonfly_lib.c @ 1543

History | View | Annotate | Download (5.22 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 dragonfly_lib.c
29
 * @brief Dragonfly initialization
30
 *
31
 * Contains implementation of dragonfly_init.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35

    
36
#include "dragonfly_lib.h"
37

    
38
/* init_dragonfly - Initializes functions based on configuration parameters
39
   examples:
40

41
   init_dragonfly (0, 0, 0); - just initialize the digital IO (buttons, potentiometer)
42

43
   init_dragonfly (ANALOG | SERIAL | BUZZER, C0_C1_ANALOG, BAUD115200); 
44
   Initialize ADC and set C0 and C1 as analog inputs.  Initialize serial and set baud rate
45
   to 115200 bps.  Initialize the buzzer.
46
   
47
   init_dragonfly (MOTORS | ORB, 0, 0);
48
   Initialize motor driving and the color fading abilities of the ORB. */
49

    
50
/**
51
 * @defgroup dragonfly Dragonfly
52
 * @brief General Dragonfly Functions
53
 * General functions for the dragonfly. Include
54
 * dragonfly_lib.h to access these functions.
55
 *
56
 * @{
57
 **/
58

    
59
/**
60
 * Initializes the components specified by config.
61
 * 
62
 * @see analog_init, usb_init, xbee_init, buzzer_init,
63
 * bom_init, orb_init, motors_init, lcd_init, encoders_init
64
 **/
65
 
66
void flash_red(void);
67

    
68
int dragonfly_init(int config) 
69
{
70
  int ret = 0;
71

    
72
  DRAGONFLY_DEBUG_PRINT("in dragonfly_init\r\n");
73

    
74
    sei();
75
    // Set directionality of various IO pins
76
    DDRG &= ~(_BV(PING0)|_BV(PING1));
77
    PORTG |= _BV(PING0)|_BV(PING1);
78
    
79
    if(config & ANALOG) {
80
      if((ret = analog_init(ADC_START))) {
81
        DRAGONFLY_DEBUG_PRINT("analog_init returned ");
82
        DRAGONFLY_DEBUG_PRINT_INT(ret);
83
        DRAGONFLY_DEBUG_PRINT_STR("\r\n");
84
      }
85
      /*TODO: return error already init'd or maybe something else if
86
        one of these inits returns that? */
87
    }
88
    
89
    if(config & COMM)
90
    {
91
        //Defaults to 115200. Check serial.h for more information.
92
      if((ret =  usb_init())) {
93
        DRAGONFLY_DEBUG_PRINT("usb_init returned ");
94
        DRAGONFLY_DEBUG_PRINT_INT(ret);
95
        DRAGONFLY_DEBUG_PRINT_STR("\r\n");
96
      }
97
      if((ret =  xbee_init())) {
98
        DRAGONFLY_DEBUG_PRINT("xbee_init returned ");
99
        DRAGONFLY_DEBUG_PRINT_INT(ret);
100
        DRAGONFLY_DEBUG_PRINT_STR("\r\n");
101
      }
102
    }
103
    
104
    if(config & BUZZER)
105
    {
106
      //sei(); This is already done above
107
      buzzer_init();
108
    }
109
    
110
    if(config & ORB)
111
    {
112
      //sei(); This is already done above
113
      orb_init();
114
    }
115
    
116
    if(config & MOTORS) {
117
      if((ret =  motors_init())) {
118
        DRAGONFLY_DEBUG_PRINT("motors_init returned ");
119
        DRAGONFLY_DEBUG_PRINT_INT(ret);
120
        DRAGONFLY_DEBUG_PRINT_STR("\r\n");
121
      }
122
    }
123

    
124
    if(config & LCD) {
125
      lcd_init();
126
    }
127
    
128
    if(config & RANGE) {
129
      
130
      if((ret =  range_init())) {
131
        DRAGONFLY_DEBUG_PRINT("range_init returned ");
132
        DRAGONFLY_DEBUG_PRINT_INT(ret);
133
        DRAGONFLY_DEBUG_PRINT_STR("\r\n");
134
      }
135
    }
136
        
137
    if(config & BOM)
138
    {
139
        unsigned char bom_read = get_bom_type();
140
        DRAGONFLY_DEBUG_PRINT("Got BOM type ");
141
        DRAGONFLY_DEBUG_PRINT_INT(bom_read);
142
        DRAGONFLY_DEBUG_PRINT_STR("\r\n");
143

    
144
        if(bom_read == 0xFF) {
145
          //warn that bom initialization failed
146
          DRAGONFLY_DEBUG_PRINT("WARNING: invalid BOM type!\r\n");
147
          flash_red();
148
          return ERROR_INIT_FAILED;
149
        }
150
        else {
151
          if ((ret = bom_init(bom_read))) {
152
            DRAGONFLY_DEBUG_PRINT("bom_init returned ");
153
            DRAGONFLY_DEBUG_PRINT_INT(ret);
154
            DRAGONFLY_DEBUG_PRINT_STR("\r\n");
155
            return ERROR_INIT_FAILED;
156
          }
157
        }
158
    }
159

    
160
    if (config & ENCODERS)
161
    {
162
      if((ret =  encoders_init())) {
163
        DRAGONFLY_DEBUG_PRINT("encoders_init returned ");
164
        DRAGONFLY_DEBUG_PRINT_INT(ret);
165
        DRAGONFLY_DEBUG_PRINT_STR("\r\n");
166
      }
167

    
168
    }
169

    
170
    DRAGONFLY_DEBUG_PRINT("dragonfly_init complete\r\n");
171

    
172
    // delay a bit for stability
173
    _delay_ms(1);
174
    
175
  return 0;
176
}
177

    
178
//flash lights red three times and restore ports
179
void flash_red(void)
180
{
181
    cli();
182
    char dd = DDRC;
183
    char po = PORTC;
184
    char i;
185
    DDRC = 0x77;
186
    for(i = 0; i<3; i++)
187
    {
188
        PORTC = 0x77;
189
        delay_ms(300);
190
        PORTC = 0x66;
191
        delay_ms(300);
192
    }
193
    
194
    DDRC = dd;
195
    PORTC = po;
196
    sei();
197
}
198
/** @} **/ //end defgroup
199