Project

General

Profile

Revision 1516

Added by Brad Neuman over 14 years ago

created init_refactor branch for fixing the init functions without breaking everything

View differences:

branches/init_refactor/code/behaviors/BOM_test/testBot/Makefile
1
# this is a local makefile
2

  
3
# Relative path to the root directory (containing lib directory)
4
ifndef COLONYROOT
5
COLONYROOT = ..
6

  
7
# Target file name (without extension).
8
TARGET = main
9

  
10
# Uncomment this to use the wireless library
11
USE_WIRELESS = 1
12

  
13
# com1 = serial port. Use lpt1 to connect to parallel port.
14
AVRDUDE_PORT = $(shell if uname -s |grep -i w32 >/dev/null; then echo 'COM4:'; else echo '/dev/ttyUSB0'; fi)
15

  
16
else
17
COLONYROOT := ../$(COLONYROOT)
18
endif
19

  
20
include $(COLONYROOT)/Makefile
branches/init_refactor/code/behaviors/BOM_test/testBot/main.c
1
#include <dragonfly_lib.h>
2

  
3
int main(void)
4
{
5

  
6
	dragonfly_init(ALL_ON);
7
	
8
	int i, j, max = -1;
9
	int vals[16];
10

  
11
	orbs_set_color(RED, RED);
12
	delay_ms(2000);
13
	orbs_set_color(GREEN, GREEN);
14

  
15
	while (1) {
16

  
17
		/* Refresh and make sure the table is updated */
18
		bom_refresh(BOM_ALL);
19
		delay_ms(100);
20

  
21
		/* Record values into an array */
22
		for (i = 0; i < 16; i++) {
23
			vals[i] = bom_get(i);
24
			if (max < vals[i])
25
				max = vals[i];
26
		}
27

  
28
		/* Display results */
29
		for (i = 0; i < 16; i++) {
30
			
31
			usb_puti(vals[i]);
32
			usb_putc('\t');
33

  
34
			for (j = 0; j < (int)((max - vals[i]) / 5); j++) {
35
				usb_putc('#');
36
			}
37

  
38
			usb_putc('\n');
39

  
40
		}
41

  
42
		usb_puts("Max: ");
43
		usb_puti(bom_get_max());
44
		usb_putc('\n');
45
		usb_putc('\n');
46

  
47
		delay_ms(400);
48

  
49
	}
50

  
51
	return 0;
52
}
53

  
branches/init_refactor/code/behaviors/BOM_test/beaconBot/main.c
1
#include <dragonfly_lib.h>
2

  
3
int main(void)
4
{
5

  
6
	/* initialize components, set wireless channel */
7
	if (dragonfly_init(ANALOG|ORB|BOM) != 0) {
8
    orbs_set_color(YELLOW, YELLOW);
9
    while(1) { ; }
10
  }
11

  
12
	if (bom_on() != 0) {
13
    orbs_set_color(RED, RED);
14
    while(1) { ; }
15
  }
16
  
17
	
18
	while (1) {
19

  
20
		orbs_set_color(GREEN, BLUE);
21

  
22
		delay_ms(1000);
23

  
24
		orbs_set_color(BLUE, GREEN);
25

  
26
		delay_ms(1000);
27

  
28
	}
29

  
30
	return 0;
31
}
branches/init_refactor/code/behaviors/BOM_test/beaconBot/Makefile
1
# this is a local makefile
2

  
3
# Relative path to the root directory (containing lib directory)
4
ifndef COLONYROOT
5
COLONYROOT = ..
6

  
7
# Target file name (without extension).
8
TARGET = main
9

  
10
# Uncomment this to use the wireless library
11
USE_WIRELESS = 0
12

  
13
# com1 = serial port. Use lpt1 to connect to parallel port.
14
AVRDUDE_PORT = $(shell if uname -s |grep -i w32 >/dev/null; then echo 'COM7:'; else echo '/dev/ttyUSB0'; fi)
15

  
16
else
17
COLONYROOT := ../$(COLONYROOT)
18
endif
19

  
20
include $(COLONYROOT)/Makefile
branches/init_refactor/code/behaviors/BOM_test/Makefile
1
# this is a local makefile
2

  
3
# Relative path to the root directory (containing lib directory)
4
ifndef COLONYROOT
5
COLONYROOT = ..
6

  
7
# Target file name (without extension).
8
TARGET = main
9

  
10
# Uncomment this to use the wireless library
11
USE_WIRELESS = 1
12

  
13
# com1 = serial port. Use lpt1 to connect to parallel port.
14
AVRDUDE_PORT = $(shell if uname -s |grep -i w32 >/dev/null; then echo 'COM4:'; else echo '/dev/ttyUSB0'; fi)
15

  
16
else
17
COLONYROOT := ../$(COLONYROOT)
18
endif
19

  
20
include $(COLONYROOT)/Makefile
branches/init_refactor/code/behaviors/Makefile
1
# this is a local makefile
2

  
3
# Relative path to the root directory (containing lib directory)
4
ifndef COLONYROOT
5
COLONYROOT = ..
6

  
7
# Target file name (without extension).
8
TARGET = main
9

  
10
# Uncomment this to use the wireless library
11
USE_WIRELESS = 1
12

  
13
# com1 = serial port. Use lpt1 to connect to parallel port.
14
AVRDUDE_PORT = $(shell if uname -s |grep -i w32 >/dev/null; then echo 'COM4:'; else echo '/dev/ttyUSB0'; fi)
15

  
16
else
17
COLONYROOT := ../$(COLONYROOT)
18
endif
19

  
20
include $(COLONYROOT)/Makefile
branches/init_refactor/code/projects/libdragonfly/dragonfly_lib.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 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
    sei();
71
    // Set directionality of various IO pins
72
    DDRG &= ~(_BV(PING0)|_BV(PING1));
73
    PORTG |= _BV(PING0)|_BV(PING1);
74
    
75
    if(config & ANALOG)
76
        analog_init(ADC_START);
77
    
78
    if(config & COMM)
79
    {
80
        //Defaults to 115200. Check serial.h for more information.
81
        usb_init();
82
        xbee_init();
83
    }
84
    
85
    if(config & BUZZER)
86
    {
87
        sei();
88
        buzzer_init();
89
    }
90
    
91
    if(config & ORB)
92
    {
93
        sei();
94
        orb_init();
95
    }
96
    
97
    if(config & MOTORS)
98
        motors_init();
99

  
100
    if(config & LCD)
101
        lcd_init();
102
    
103
    if(config & RANGE)
104
        range_init();
105
        
106
    if(config & BOM)
107
    {
108
        unsigned char bom_read = get_bom_type();
109
        if(bom_read == 0xFF) {
110
            //warn that bom initialization failed
111
            flash_red();
112
            return ERROR_INIT_FAILED;
113
        }
114
        else
115
          if (bom_init(bom_read) != 0)
116
            return ERROR_INIT_FAILED;
117
    }
118

  
119
	if (config & ENCODERS)
120
	{
121
		encoders_init();
122
	}
123

  
124
    // delay a bit for stability
125
    _delay_ms(1);
126
    
127
  return 0;
128
}
129

  
130
//flash lights red three times and restore ports
131
void flash_red(void)
132
{
133
    cli();
134
    char dd = DDRC;
135
    char po = PORTC;
136
    char i;
137
    DDRC = 0x77;
138
    for(i = 0; i<3; i++)
139
    {
140
        PORTC = 0x77;
141
        delay_ms(300);
142
        PORTC = 0x66;
143
        delay_ms(300);
144
    }
145
    
146
    DDRC = dd;
147
    PORTC = po;
148
    sei();
149
}
150
/** @} **/ //end defgroup
151

  
branches/init_refactor/code/projects/libdragonfly/dragonfly_lib.h
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.h
29
 * @brief Contains other include files
30
 * 
31
 * Include this file for all the functionality of libdragonfly.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35

  
36
#ifndef _DRAGONFLY_LIB_H_
37
#define _DRAGONFLY_LIB_H_
38

  
39
/**
40
 * @addtogroup dragonfly
41
 * @{
42
 **/
43

  
44
/** @brief Initialize the board **/
45
int dragonfly_init(int config);
46

  
47
/** @} **/ //end addtogroup
48

  
49
#include <inttypes.h>
50
#include <avr/io.h>
51
#include <avr/interrupt.h>
52
#include <util/delay.h>
53
#include <util/twi.h>
54

  
55
// This file is included from the libdragonfly directory because it seems to be
56
// missing from the AVR libc distribution.
57
#include "atomic.h"
58

  
59
#include "dragonfly_defs.h"
60
#include "analog.h"
61
#include "dio.h"
62
#include "time.h"
63
#include "lcd.h"
64
#include "lights.h"
65
#include "motor.h"
66
#include "serial.h"
67
#include "buzzer.h"
68
#include "rangefinder.h"
69
#include "bom.h"
70
#include "encoders.h"
71
#include "move.h"
72
#include "reset.h"
73
#include "math.h"
74
#include "eeprom.h"
75

  
76
#include <stddef.h>
77
#include <stdbool.h>
78

  
79
#endif
80

  
branches/init_refactor/code/projects/libdragonfly/bom.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 bom.c
29
 * @brief Implementation for using the BOM
30
 *
31
 * Contains functions for using the Bearing and Orientation Module (BOM)
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35

  
36
//#include "dragonfly_defs.h"
37
#include "bom.h"
38
#include "dio.h"
39
#include "serial.h"
40
#include "analog.h"
41

  
42
//On the original BOM1.0, the emmitter angular order does not match the analog mux order
43
//so you need to iterate through the mux index in the following order if you want to get
44
//the detector readings in order:
45
static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
46

  
47
// internal function prototypes
48
static void bom_select(char which);
49

  
50
/*
51
 Bk R Y (Analog)
52
---------
53
 Green
54
 Blue
55
 White
56
---------
57
 Blue
58
 White
59
*/
60

  
61

  
62
/*
63
the analog pin definitions from dio.h DO NOT work here,
64
so we must use PF0 from avrgcc (as opposed to _PIN_F0).
65
BUT the dio pin definitions from dio.h must be used (no PE...).
66

  
67
also, _PIN_E2 is initialized to high for some reason,
68
which turns the BOM on when the robot is turned on.
69
WORK-AROUND: call digital_output(_PIN_E2,0) at some point.
70

  
71
*/
72

  
73
#define MONKI PF0         //analog (yellow)
74
//------------------------//
75
#define MONKL _PIN_E2     //green
76
#define MONK1 _PIN_E3     //blue
77
#define MONK0 _PIN_E4     //white
78
//------------------------//
79
#define MONK3 _PIN_E6     //blue
80
#define MONK2 _PIN_E7     //white
81

  
82
#define BOM_VALUE_THRESHOLD 150 //200
83
#define NUM_BOM_LEDS 16
84

  
85
/*
86
  *The following pin definitions are for the BOM v1.5
87
  */
88

  
89
#define BOM_MODE	_PIN_E2	//dio0
90
#define BOM_STROBE	_PIN_E3	//dio1
91

  
92
#define BOM_DATA	_PIN_A0 //servo0
93
#define BOM_CLOCK	_PIN_A1	//servo1
94

  
95
#define BOM_S0		_PIN_E5	//dio3
96
#define BOM_S1		_PIN_E4	//dio2
97
#define BOM_S2		_PIN_E7	//dio4
98
#define BOM_S3		_PIN_E6	//dio5
99
#define BOM_OUT		PF0		//analog(yellow)
100

  
101
/**
102
 * @defgroup bom BOM (Bearing and Orientation Module)
103
 * @brief Functions for dealing with the BOM.
104
 *
105
 * The Bearing and Orientation Module / Barrel of Monkeys / BOM
106
 * is a custom sensor designed and built by the Colony Project.
107
 * It consists of a ring of 16 IR emitters and 16 IR detectors.
108
 * The BOM is most often use to determine the direction of other
109
 * robots. This module contains functions for controlling the BOM.
110
 *
111
 * Include bom.h to access these functions.
112
 *
113
 * @{
114
 **/
115

  
116
unsigned char bom_initd=0;
117

  
118
static unsigned int bom_val[NUM_BOM_LEDS];
119
static volatile char bom_type = BOM10;
120
static int select_pins[4];
121
static int analog_pin;
122

  
123
/**
124
 * Initializes the BOM.
125
 * Call bom_init before reading bom values or turning bom leds.
126
 *
127
 * @bugs INCOMPLETE - No utilization of BOM1.5 RSSI capability. Probably leave this out
128
 * until Cornell and Pras return
129
 *
130
 * @return 0 if init succesfull, an error code otherwise 
131
 *
132
 * @see bom_refresh, bom_leds_on, bom_leds_off
133
 **/
134
int bom_init(char type) {
135

  
136
    if(bom_initd)
137
      return ERROR_INIT_ALREADY_INITD;
138

  
139

  
140
    bom_type = type;
141
    
142
    switch(bom_type) {
143
    case BOM10:
144
		select_pins[0] = MONK0; 
145
		select_pins[1] = MONK1;
146
		select_pins[2] = MONK2;
147
		select_pins[3] = MONK3;
148
		analog_pin = MONKI;
149
        break;
150
    case BOM15:
151
        //Sets BOM1.5 to normal [BOM] mode
152
        digital_output(BOM_MODE, 0);
153
		select_pins[0] = BOM_S0; 
154
		select_pins[1] = BOM_S1;
155
		select_pins[2] = BOM_S2;
156
		select_pins[3] = BOM_S3;
157
		bom_set_leds(BOM_ALL);
158
		analog_pin = BOM_OUT;
159
        break;
160
    case RBOM:
161
        break;
162
    default:
163
      return -1;        
164
    //default:
165
    }
166

  
167
    bom_initd=1;
168
    return 0;
169
}
170

  
171
/**
172
 * Iterates through each bit in the bit_field. For each set bit, sets the corresponding bom select bits
173
 *    and updates the corresponding bom value with an analog_get8 reading.  analog_init and bom_init
174
 *    must be called for this to work. Must call this before reading BOM values!
175
 *
176
 *
177
 * @param bit_field specifies which elements in bom_val[] should be updated. Use BOM_ALL to refresh all values.
178
 *    Ex. if 0x0003 is passed, bom_val[0] and bom_val[1] will be updated.
179
 *
180
 * @return 0 if init succesfull, an error code otherwise
181
 *
182
 * @see bom_get
183
 **/
184
int bom_refresh(int bit_field) {
185
    int i;
186
    int loop_was_running = 0;
187

  
188
    if(!bom_initd)
189
      return ERROR_LIBRARY_NOT_INITD;
190

  
191
    
192
    //Check analog loop status
193
    if(analog_loop_status() == ADC_LOOP_RUNNING) {
194
      loop_was_running = 1;
195
      analog_stop_loop();
196
    }
197
    
198
    //Read BOM values
199
    for(i = 0; i < NUM_BOM_LEDS; i++) {
200
      if(bit_field & 0x1) {
201
	bom_select(i);
202
	bom_val[i] = analog_get8(analog_pin);
203
      }
204
      bit_field = bit_field >> 1;
205
    }
206
    
207
    //Restore analog loop status
208
    if(loop_was_running)
209
      analog_start_loop();
210

  
211
    return 0;
212
}
213

  
214
/**
215
 * Gets the bom reading from bom_val[which].  Call bom_refresh beforehand to read new bom values.
216
 *
217
 * @pre must call bom refresh first
218
 *
219
 * @param which which bom value to return
220
 *
221
 * @return the bom value, -1 on error
222
 *
223
 * see bom_refresh
224
 **/
225
int bom_get(int which) {
226
    if(!bom_initd)
227
      return -1;
228

  
229
    return bom_val[which];
230
}
231

  
232
/** 
233
 * Compares all the values in bom_val[] and returns the index to the lowest (max) value element.
234
 *
235
 * @pre must call bom refresh
236
 * @return index to the lowest (max) bom value element.  -1 if no value is lower than
237
 *    BOM_VALUE_THRESHOLD, -2 if the bom is not initialized
238
 **/
239
int bom_get_max(void) {
240
    int i, lowest_val, lowest_i;
241

  
242
    if(!bom_initd)
243
      return -2;
244

  
245
    lowest_i = -1;
246
    lowest_val = 255;
247
    for(i = 0; i < NUM_BOM_LEDS; i++) {
248
        if(bom_val[i] < lowest_val) {
249
            lowest_val = bom_val[i];
250
            lowest_i = i;
251
        }
252
    }
253
    
254
    if(lowest_val < BOM_VALUE_THRESHOLD)
255
        return lowest_i;
256
    else
257
        return -1;
258
}
259

  
260
/** 
261
 * Computes the weighted average of all the bom readings to estimate the position (and distance) of another robot.
262
 *
263
 * @pre must call bom refresh
264
 * @param dist  pointer to int in which to return the estimated distance to the other robot
265
 * @return estimated position of the max bom value element as a fixed point value analogous to 10 times the
266
 *        index of the max bom value.  -1 if no value is lower than BOM_VALUE_THRESHOLD, -2 if the bom is not initialized
267
 **/
268
int bom_get_max10(int *dist) {
269
    int i, max;
270
    long long mean = 0, sum = 0;
271

  
272
    if(!bom_initd)
273
      return ERROR_LIBRARY_NOT_INITD;
274

  
275

  
276
    max = bom_get_max();
277
    if (max < 0)
278
    {
279
        if (dist)
280
        {
281
            *dist = -1;
282
        }
283
        return -1;
284
    }
285
    /* Record values into an array */
286
    for (i = 0; i < NUM_BOM_LEDS; i++) {
287
        int idx = ((i + (NUM_BOM_LEDS/2 - max) + NUM_BOM_LEDS) % NUM_BOM_LEDS) - (NUM_BOM_LEDS/2 - max);
288
        int val = 255 - bom_val[i];
289
        mean += idx * val;
290
        sum += val;
291
    }
292
    mean = (mean * 10) / sum;
293
    mean = (mean + NUM_BOM_LEDS*10) % (NUM_BOM_LEDS*10);
294

  
295
    if (dist)
296
    {
297
        *dist = 50 - sum/48;
298
    }
299

  
300
    return mean;
301
}
302

  
303
/**
304
 * Iterates through each bit in the bit_field. If the bit is set, the corresponding emitter will
305
 *    be enabled to turn on when bom_on() is called.
306
 *    bom_init must be called for this to work. Does nothing if a BOM1.0 is installed
307
 *
308
 * @param bit_field specifies which leds should be turned on when bom_on is called.  Use BOM_ALL to turn on all bom leds.
309
 *    Ex. if 0x0005 is passed, leds 0 and 2 will be turned on.
310
 *
311
 * @return 0 if init succesfull, an error code otherwise
312
 *
313
 **/
314
int bom_set_leds(int bit_field) {
315
    int i;
316
    unsigned int mask = 1<<(NUM_BOM_LEDS-1);
317

  
318
    if(!bom_initd)
319
      return ERROR_LIBRARY_NOT_INITD;
320

  
321
    switch(bom_type) {
322
    case BOM10:
323
        //TODO: put an assert here to alert the user that this should not be called
324
        break;
325
		
326
    case BOM15:
327
	    for(i=NUM_BOM_LEDS; i>0; i--)
328
	    {
329
		    //set the current bit, sending MSB first
330
		    digital_output(BOM_DATA, bit_field&mask);
331
		    //then pulse the clock
332
		    digital_output(BOM_CLOCK, 1);
333
		    digital_output(BOM_CLOCK, 0);
334
			mask = mask>>1;
335
	    }
336
        break;
337
		
338
    case RBOM:
339
        //add rbom code here
340
        break;
341
    }
342

  
343
    return 0;
344
}
345

  
346

  
347
/**
348
 * (DEPRECATED) Returns the direction of the maximum BOM reading,
349
 * as an integer in the range 0-15. 0 indicates to the
350
 * robot's right, while the rest of the sensors are
351
 * numbered counterclockwise. This is useful for determining
352
 * the direction of a robot flashing its BOM, of only one
353
 * robot is currently doing so. analog_init must be called
354
 * before this function can be used.
355
 *
356
 * @return the direction of the maximum BOM reading
357
 *
358
 * @see analog_init
359
 **/
360
int get_max_bom(void) {
361
    bom_refresh(BOM_ALL);
362
    return bom_get_max();
363
}
364

  
365
/**
366
 * Flashes the BOM.  If using a BOM1.5, only the emitters that have been enabled using
367
 * bom_set_leds will turn on.
368
 * 
369
 * @return 0 if init succesfull, an error code otherwise
370
 *
371
 * @see bom_off, bom_set_leds
372
 **/
373
int bom_on(void)
374
{
375
  if(!bom_initd)
376
    return ERROR_LIBRARY_NOT_INITD;
377

  
378
  switch(bom_type) {
379
  case BOM10:
380
	digital_output(MONKL, 1);
381
	break;
382
  case BOM15:
383
	digital_output(BOM_STROBE, 1);
384
	break;
385
  case RBOM:
386
	break;
387
  default:
388
    return -1;
389
  }
390

  
391
  return 0;
392
}
393

  
394
/**
395
 * Turns off all bom leds.
396
 * 
397
 * @return 0 if init succesfull, an error code otherwise
398
 *
399
 * @see bom_on
400
 **/
401
int bom_off(void)
402
{
403
  if(!bom_initd)
404
    return ERROR_LIBRARY_NOT_INITD;
405

  
406
  switch(bom_type) {
407
  case BOM10:
408
	digital_output(MONKL, 0);
409
	break;
410
  case BOM15:
411
	digital_output(BOM_STROBE, 0);
412
	break;
413
  case RBOM:
414
	break;
415
  }
416

  
417
  return 0;
418
}
419

  
420
/** @} **/ //end group
421

  
422
//select a detector to read
423
static void bom_select(char which) {
424
	if(bom_type == BOM10)
425
	  which = lookup[(int)which];
426
	
427
    if (which&8)
428
      digital_output(select_pins[3], 1);
429
    else
430
      digital_output(select_pins[3], 0);
431

  
432
    if (which&4)
433
      digital_output(select_pins[2], 1);
434
    else
435
      digital_output(select_pins[2], 0);
436

  
437
    if (which&2)
438
      digital_output(select_pins[1], 1);
439
    else
440
      digital_output(select_pins[1], 0);
441

  
442
    if (which&1)
443
      digital_output(select_pins[0], 1);
444
    else
445
      digital_output(select_pins[0], 0);
446
	
447
}
branches/init_refactor/code/projects/libdragonfly/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 "dragonfly_defs.h"
38
#include "motor.h"
39

  
40
/**
41
 * @defgroup motors Motors
42
 * @brief Functions for controlling the motors.
43
 * Functions for controlling the motors. Found in motor.h.
44
 * @{
45
 **/
46

  
47
unsigned char motors_initd=0;
48

  
49
/**
50
 * Initializes both motors so that they can be used with future
51
 * calls to motor1_set and motor2_set.
52
 *
53
 * @return 0 if init succesfull, an error code otherwise
54
 *
55
 * @see motors_off, motor1_set, motor2_set
56
 **/
57
int motors_init( void ) {
58

  
59
  if(motors_initd)
60
    return ERROR_INIT_ALREADY_INITD;
61

  
62

  
63
  // Configure counter such that we use phase correct
64
  // PWM with 8-bit resolution
65
  PORTA &= 0x0F;
66
  DDRA |= 0xF0;
67
  DDRB |= 0x60;
68

  
69
  //timer 1A and 1B
70
  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10);
71
  TCCR1B = _BV(WGM12) | _BV(CS10);
72
  //	TCCR1A = 0xA1;
73
  //	TCCR1B = 0x04;
74
  OCR1AH=0;
75
  OCR1AL=0;
76
  OCR1BH=0;
77
  OCR1BL=0;
78

  
79
  motors_initd=1;
80
  return 0;
81
}
82

  
83
/**
84
 * Sets the speed and direction of the left motor.
85
 * motors_init must be called before this function can be used.
86
 *
87
 * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
88
 * @param speed The speed the motor will run at, in the range 0-255.
89
 * 
90
 * @return 0 if init succesfull, an error code otherwise
91
 *
92
 * @see motor_r_set, motors_init
93
 **/
94
int motor_l_set(int direction, int speed) {
95
  if(!motors_initd)
96
    return ERROR_LIBRARY_NOT_INITD;
97

  
98
  if(direction == 0) {
99
    // turn off PWM first if switching directions
100
    if((PORTA & 0x30) != 0x10) {
101
      OCR1A = 0;
102
    }	
103
    PORTA = (PORTA & 0xCF) | 0x10;
104
    //		PORTD |= 0x10;
105
    //		PORTD &= 0xBF;
106
  } else {
107
    // turn off PWM first if switching directions
108
    if((PORTA & 0x30) != 0x20) {
109
      OCR1A = 0;
110
    }
111
    PORTA = (PORTA & 0xCF) | 0x20;
112
    //		PORTD |= 0x40;
113
    //		PORTD &= 0xEF;
114
  }
115
	
116
  // Set the timer to count up to speed, an 8-bit value
117
  OCR1AL = speed;
118

  
119
  return 0;
120
}
121

  
122
/**
123
 * Sets the speed and direction of the right motor.
124
 * motors_init must be called before this function can be used.
125
 *
126
 * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
127
 * @param speed The speed the motor will run at, in the range 0-255.
128
 *
129
 * @return 0 if init succesfull, an error code otherwise
130
 *
131
 * @see motor_l_set, motors_init
132
 **/
133
int motor_r_set(int direction, int speed) {
134
  if(!motors_initd)
135
    return ERROR_LIBRARY_NOT_INITD;
136

  
137
  if(direction == 0) {
138
    //		PORTD |= 0x20;
139
    //		PORTD &= 0x7F;
140
    // turn off PWM first if switching directions
141
    if((PORTA & 0xC0) != 0x80) {
142
      OCR1B = 0;
143
    }		
144
		
145
    PORTA = (PORTA & 0x3F) | 0x80;
146
  } else {
147
    //		PORTD |= 0x80;
148
    //		PORTD &= 0xDF;
149

  
150
    // turn off PWM first if switching directions
151
    if((PORTA & 0xC0) != 0x40) {
152
      OCR1B = 0;
153
    }		
154
		
155
    PORTA = (PORTA & 0x3F) | 0x40;
156
  }
157
  OCR1BL = speed;
158

  
159
  return 0;
160
}
161

  
162
/**
163
 * Sets the speed and direction of motor1.
164
 * motors_init must be called before this function can be used.
165
 *
166
 * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
167
 * @param speed The speed the motor will run at, in the range 0-255.
168
 *
169
 * @return 0 if init succesfull, an error code otherwise
170
 *
171
 * @see motor2_set, motors_init
172
 **/
173
int motor1_set(int direction, int speed) {
174
  return motor_l_set(direction, speed);
175
}
176

  
177
/**
178
 * Sets the speed and direction of motor2.
179
 * motors_init must be called before this function can be used.
180
 *
181
 * @param direction Either FORWARD or BACKWARD to set the direction of rotation.
182
 * @param speed The speed the motor will run at, in the range 0-255.
183
 *
184
 * @return 0 if init succesfull, an error code otherwise
185
 *
186
 * @see motor2_set, motors_init
187
 **/
188
int motor2_set(int direction, int speed) {
189
  return motor_r_set(direction, speed);
190
}
191

  
192

  
193
/**
194
 * Turns off both motors.
195
 *
196
 * @return 0 if init succesfull, an error code otherwise
197
 *
198
 * @see motors_init
199
 **/
200
int motors_off( void ) {
201
  if(!motors_initd)
202
    return ERROR_LIBRARY_NOT_INITD;
203

  
204
  OCR1AL = 0x0;
205
  OCR1BL = 0x0;
206

  
207
  return 0;
208
}
209

  
210
/**@}**///end defgroup
211

  
branches/init_refactor/code/projects/libdragonfly/motor.h
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.h
29
 * @brief Contains definitions for controlling the motors
30
 *
31
 * Contains definitions and functions for controlling
32
 * the motors.
33
 *
34
 * @author Colony Project, CMU Robotics Club
35
 * Based on Tom Lauwer's Firefly Library
36
 **/
37

  
38
#ifndef _MOTOR_H
39
#define _MOTOR_H
40

  
41
#include <avr/io.h>
42

  
43
/**
44
 * @addtogroup motors
45
 * @{
46
 **/
47

  
48
/** @brief make the motors go forwards **/
49
#define FORWARD 1
50
/** @brief make the motors go backwards **/
51
#define BACKWARD 0
52

  
53
/** @brief Initialize the motors **/
54
int motors_init(void);
55
/** @brief Set speed and direction of motor1 
56
 *  @deprecated use the left motor function instead. it's more intuitive and easier to read.**/
57
int motor1_set(int direction, int speed);
58
/** @brief Set speed and direction of motor2 
59
 *  @deprecated use the right motor function instead. it's more intuitive and easier to read.**/
60
int motor2_set(int direction, int speed);
61
/** @brief Set speed and direction of left motor **/
62
int motor_l_set(int direction, int speed);
63
/** @brief Set speed and direction of right motor **/
64
int motor_r_set(int direction, int speed);
65
/** @brief Turn the motors off **/
66
int motors_off(void);
67

  
68
/**@}**/ // end addtogroup
69

  
70
#endif
71

  
branches/init_refactor/code/projects/libdragonfly/serial.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 serial.c
29
 * @brief Serial Input and Output
30
 *
31
 * Implementation of functions for serial input and output.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35

  
36
#include <avr/io.h>
37

  
38
#include "dragonfly_defs.h"
39
#include "serial.h"
40

  
41
unsigned char usb_initd=0;
42
unsigned char xbee_initd=0;
43

  
44
#ifdef USE_STDIO
45

  
46
#include <stdio.h>
47

  
48
/**
49
 * For use with fprintf() and related stdio functions
50
 **/
51
FILE *usb_fd;
52

  
53
/**
54
 * For use with fprintf() and related stdio functions
55
 **/
56
FILE *xbee_fd;
57

  
58
#endif
59

  
60
/**
61
 * Initializes communication over the USB serial port.
62
 * This must be called before any other usb function
63
 * may be used.
64
 **/
65
int usb_init() {
66
  //Set baud rate
67
  // - 115200 (both wired and wireless) is UBRR=8, U2X=1
68
  // - 9600 is U2X =1, UBRR = 107.
69

  
70
  if(usb_initd) {
71
    return ERROR_INIT_ALREADY_INITD;
72
  }
73

  
74
#if (USB_BAUD == 115200)
75
  UBRR0H = 0x00;
76
  UBRR0L = 8;
77
  UCSR0A |= _BV(U2X0);
78
#elif (USB_BAUD == 9600)
79
  UBRR0H = 0x00;
80
  UBRR0L = 103;
81
  UCSR0A |= _BV(U2X0);
82
#else //Baud rate is defined in the header file, we should not get here
83
  return 0;
84
#endif
85

  
86
  /*Enable receiver and transmitter */
87
  UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
88
	
89
  /* Set frame format: 8data, 1stop bit, asynchronous normal mode */
90
  UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
91
  
92
  // if we have enabled the stdio stuff, then we init it here
93
#ifdef USE_STDIO
94
  /* Open the stdio stream corresponding to this port */
95
  usb_fd = fdevopen(usb_putc, usb_getc);
96
#endif
97

  
98
  usb_initd = 1;
99
  return 0;
100

  
101
}
102

  
103
/**
104
 * Initializes communication over the XBee.
105
 * This must be called before any other xbee function
106
 * may be used.
107
 **/
108
int xbee_init() {
109

  
110
  if(xbee_initd) {
111
    return ERROR_INIT_ALREADY_INITD;
112
  }
113

  
114
  //Set baud rate
115
  // - 115200 (both wired and wireless) is UBRR=8, U2X=1
116
  // - 9600 is U2X =1, UBRR = 107.
117
#if (XBEE_BAUD == 115200)
118
  UBRR1H = 0x00;
119
  UBRR1L = 8;
120
  UCSR1A |= _BV(U2X1);
121
#elif (XBEE_BAUD == 9600)
122
  UBRR1H = 0x00;
123
  UBRR1L = 103;
124
  UCSR1A |= _BV(U2X1);
125
#else //Baud rate is defined in the header file, we should not get here
126
  return 0;
127
#endif
128

  
129
  //Enable receiver and transmitter
130
  UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
131
	
132
  // Set frame format: 8data, 1stop bit, asynchronous normal mode
133
  UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
134
  
135
  // if we have enabled the stdio stuff, then we init it here
136
#ifdef USE_STDIO
137
  /* Open the stdio stream corresponding to this port */
138
  xbee_fd = fdevopen(xbee_putc, xbee_getc);
139
#endif
140

  
141
  xbee_initd = 1;
142
  return 0;
143

  
144
}
145

  
146
/**
147
 * Sends a character over USB.
148
 *
149
 * @param c the character to send
150
 * @return 0 for success, nonzero for failure
151
 **/
152
int usb_putc(char c) {
153

  
154
  if(!usb_initd)
155
    return ERROR_LIBRARY_NOT_INITD;
156

  
157
  // Wait until buffer is clear for sending
158
  loop_until_bit_is_set(UCSR0A, UDRE0);
159
	
160
  // Load buffer with your character
161
  UDR0 = c;
162
  return 0;
163
}
164

  
165
/**
166
 * Sends a character to the XBee.
167
 *
168
 * @param c the character to send
169
 * @return 0 for success, nonzero for failure
170
 **/
171
int xbee_putc(char c) {
172

  
173
  if(!xbee_initd)
174
    return ERROR_LIBRARY_NOT_INITD;
175

  
176
  // Wait until buffer is clear for sending
177
  loop_until_bit_is_set(UCSR1A, UDRE1);
178
	
179
  // Load buffer with your character
180
  UDR1 = c;
181
  return 0;
182
}
183

  
184
/**
185
 * Sends a sequence of characters over USB.
186
 *
187
 * @param s the string to send
188
 * @return 0 for success, nonzero for failure
189
 **/
190
int usb_puts(char *s) {
191
  char *t = s;
192

  
193
  if(!usb_initd)
194
    return ERROR_LIBRARY_NOT_INITD;
195

  
196
  while (*t != 0) {
197
    usb_putc(*t);
198
    t++;
199
  }
200
  return 0;
201
}
202

  
203
/**
204
 * Sends a sequence of characters from program space over USB.
205
 *
206
 * @param s the string to send
207
 *
208
 * @return 0 if init succesfull, an error code otherwise
209
 **/
210
int usb_puts_P (PGM_P s) {
211
    char buf;
212

  
213
    if(!usb_initd)
214
      return ERROR_LIBRARY_NOT_INITD;
215
	
216
    while (memcpy_P (&buf, s, sizeof (char)), buf!=0) {
217
        usb_putc (buf);
218
        s++;
219
    }
220

  
221
    return 0;
222
}
223

  
224

  
225

  
226
/**
227
 * Returns the first character in the buffer received from USB.
228
 * This function blocks execution until a character has been received.
229
 * xbee_init must be called before this function may be used.
230
 * 
231
 * @return the first character in the usb buffer, -1 on error
232
 *
233
 * @see usb_init, usb_getc_nb
234
 **/
235
int usb_getc(void) {
236

  
237
  if(!usb_initd)
238
    return -1;
239

  
240
  // Wait for the receive buffer to be filled
241
  loop_until_bit_is_set(UCSR0A, RXC0);
242
	
243
  // Read the receive buffer
244
  return UDR0;
245
}
246

  
247
/**
248
 * Returns the first character in the buffer received from USB.
249
 * This function blocks execution until a character has been
250
 * received. xbee_init must be called before this function
251
 * may be used.
252
 * 
253
 * @return the first character in the xbee buffer, -1 on error
254
 * 
255
 * @see xbee_init, xbee_getc_nb
256
 **/
257
int xbee_getc(void) {
258

  
259
  if(!usb_initd)
260
    return -1;
261

  
262
  // Wait for the receive buffer to be filled
263
  loop_until_bit_is_set(UCSR1A, RXC1);
264
	
265
  // Read the receive buffer
266
  return UDR1;
267
}
268

  
269
/**
270
 * Non blocking version of usb_getc. If a character is present in the buffer,
271
 * it is returned, otherwise -1 is returned immediately. usb_init must be
272
 * called before this function can be used.
273
 *
274
 * @param c the received character. This will be set if a character has
275
 * been received.
276
 * 
277
 * @return -1 if no character is available, 0 otherwise, positive for error
278
 * 
279
 * @see usb_init, usb_getc
280
 **/
281
int usb_getc_nb(char *c) {
282

  
283
  if(!usb_initd)
284
    return ERROR_LIBRARY_NOT_INITD;
285

  
286
  // check if the receive buffer is filled
287
  if (UCSR0A & _BV(RXC0)) {
288
    // Read the receive buffer
289
    (*c) = UDR0;
290
    return 0;
291
  } else {
292
    // Return empty
293
    return -1;
294
  }
295
}
296

  
297
/**
298
 * Non blocking version of xbee_getc. If a character is present in the buffer,
299
 * it is returned, otherwise -1 is returned immediately. xbee_init
300
 * must be called before this function can be used.
301
 *
302
 * @param c the received character. This will be set if a character has
303
 * been received.
304
 * 
305
 * @return -1 if no character is available, 0 otherwise, positive for error
306
 *
307
 * @see xbee_init, xbee_getc
308
 **/
309
int xbee_getc_nb(char *c) {
310
  if(!xbee_initd)
311
    return ERROR_LIBRARY_NOT_INITD;
312

  
313
  // check if the receive buffer is filled
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff