Project

General

Profile

Revision 482

moved more old dirs

View differences:

trunk/code/projects/colonet/robot/dragonfly_wireless_relay/lights.h
1
/*
2
lights.h
3

  
4
Contains function prototypes, variables, and pins used by the Orbs and PWM
5

  
6
most of this is shamelessly copied from FWR's orb.h (Tom Lauwers and Steven Shamlian)
7

  
8
author: CMU Robotics Club, Colony Project
9

  
10
Change Log:
11
1.25.07 - KWoo
12
	Removed FF+ code. Cleaned up and commented all variables.
13

  
14
*/
15

  
16
#ifndef _LIGHTS_H_
17
#define _LIGHTS_H_
18

  
19
/***** Port and Pin Definitions ****/
20

  
21
//Orb Ports and Registers
22
#define ORB_PORT	PORTC
23
#define ORB_DDR		DDRC
24

  
25
//Orb Pins
26
#define ORB1_RED	0x00
27
#define ORB1_GREEN	0x01
28
#define ORB1_BLUE	0x02
29
#define ORB2_RED 	0x04
30
#define ORB2_GREEN 	0x05
31
#define ORB2_BLUE 	0x06
32

  
33
//ORB Colors
34
#define RED       0xE0
35
#define ORANGE    0xE4
36
#define YELLOW    0xE8
37
#define LIME      0x68
38
#define GREEN     0x1C
39
#define CYAN      0x1F
40
#define BLUE      0x03
41
#define PINK      0xA6 
42
#define PURPLE    0x41
43
#define MAGENTA   0xE3
44
#define WHITE     0xFE
45
#define ORB_OFF   0x00
46

  
47

  
48

  
49
#include <avr/interrupt.h>
50

  
51

  
52
#define ORB_RESET 1025
53

  
54
#define ORBPORT PORTC
55

  
56
#define ORBDDR DDRC
57

  
58
#define ORBMASK 0x77
59

  
60

  
61

  
62
/**** Function Prototypes ****/
63
//Enables counter and PWM for use with the orb.
64
void orb_init(void);
65

  
66
//Sets both orbs to a specified color 
67
void orb_set(unsigned char red_led, unsigned char green_led, unsigned char blue_led);
68
//Sets Orb1 to the specified color
69
void orb1_set(unsigned char red_led, unsigned char green_led, unsigned char blue_led); 
70
//Sets Orb2 to the specified color
71
void orb2_set(unsigned char red_led, unsigned char green_led, unsigned char blue_led);
72

  
73
//Sets both orbs to the same color using a color name
74
void orb_set_color(int col);
75
//Sets orb1 to a color using a color name
76
void orb1_set_color(int col);
77
//Sets orb2 to a color using a color name
78
void orb2_set_color(int col);
79

  
80
//Turns off the PWM and thus the Orbs
81
void orb_disable(void);
82
//Turns on the PWM and thus the Orbs
83
void orb_enable(void);
84

  
85
//Runs through some colors
86
void orb_tester(void);
87
//Set just one channel
88
void orb_set_angle(int servo, int angle);
89

  
90
#endif
trunk/code/projects/colonet/robot/dragonfly_wireless_relay/motor.c
1
/*
2
	motor.c - Contains functions necessary for activating and driving the 
3
	H-bridge
4
	
5
	
6
	author: Robotics Club, Colony project
7
	
8
	much of this is taken from FWR's library, author: Tom Lauwers
9
	
10
*/
11

  
12
#include "motor.h"
13

  
14
/*
15
motor initialization
16
initializes both motors
17

  
18
*/
19
void motors_init( void ) {
20

  
21

  
22
	// Configure counter such that we use phase correct
23
	// PWM with 8-bit resolution
24
	PORTA &= 0x0F;
25
	DDRA |= 0xF0;
26
	DDRB |= 0x60;
27

  
28
	//timer 1A and 1B
29
	TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10);
30
	TCCR1B = _BV(WGM12) | _BV(CS10);
31
//	TCCR1A = 0xA1;
32
//	TCCR1B = 0x04;
33
	OCR1AH=0;
34
	OCR1AL=0;
35
	OCR1BH=0;
36
	OCR1BL=0;
37
}
38

  
39
// The following functions set the motor direction and speed
40
void motor1_set(int direction, int speed) {
41

  
42
	if(direction == 0) {
43
	    // turn off PWM first if switching directions
44
		if((PORTA & 0x30) != 0x10)
45
		{
46
		  OCR1A = 0;
47
		}	
48
		PORTA = (PORTA & 0xCF) | 0x10;
49
//		PORTD |= 0x10;
50
//		PORTD &= 0xBF;
51
	}
52
	else {
53
	    // turn off PWM first if switching directions
54
		if((PORTA & 0x30) != 0x20)
55
		{
56
		  OCR1A = 0;
57
		}
58
		PORTA = (PORTA & 0xCF) | 0x20;
59
//		PORTD |= 0x40;
60
//		PORTD &= 0xEF;
61
	}
62
	
63
	// Set the timer to count up to speed, an 8-bit value
64
	OCR1AL = speed;
65

  
66

  
67
}
68

  
69
void motor2_set(int direction, int speed) {
70

  
71
	if(direction == 0) {
72
//		PORTD |= 0x20;
73
//		PORTD &= 0x7F;
74
	    // turn off PWM first if switching directions
75
		if((PORTA & 0xC0) != 0x80)
76
		{
77
		  OCR1B = 0;
78
		}		
79
		
80
		PORTA = (PORTA & 0x3F) | 0x80;
81
	}
82
	else {
83
//		PORTD |= 0x80;
84
//		PORTD &= 0xDF;
85

  
86
	    // turn off PWM first if switching directions
87
		if((PORTA & 0xC0) != 0x40)
88
		{
89
		  OCR1B = 0;
90
		}		
91
		
92
		PORTA = (PORTA & 0x3F) | 0x40;
93
	}
94
	OCR1BL = speed;
95

  
96
}
97

  
98
// Just turns off both motors
99
void motors_off( void ) {
100
	OCR1AL = 0x0;
101
	OCR1BL = 0x0;
102
}
trunk/code/projects/colonet/robot/dragonfly_wireless_relay/dragonflyWirelessRelay.c
1
/**
2
 * Eugene Marinelli
3
 * 4/5/07
4
 *
5
 * Robot wireless relay - dragonfly edition
6
 */
7

  
8
#include "dragonfly_lib.h"
9
#include <stdio.h>
10
#include <string.h>
11

  
12
int main(void)
13
{
14
  orb_init();
15
  orb_enable();
16
  usb_init();
17
  xbee_init();
18

  
19
  // orb_set_color(GREEN);
20
  orb_set_color(GREEN);
21

  
22
  //UCSR0B |= _BV(RXCIE);
23
  UCSR1B |= _BV(RXCIE);
24
  sei();
25

  
26
  while (1) {
27
    xbee_putc(usb_getc());
28
    orb_set_color(YELLOW);
29
  }
30
  
31
  return 0;
32
}
33

  
34
ISR(USART1_RX_vect)
35
{
36
  //char buf = UDR1;
37

  
38
  usb_putc(UDR1);
39
  //orb_set_color(RED);
40
}
trunk/code/projects/colonet/robot/dragonfly_wireless_relay/motor.h
1
/*
2
	motor.h - Contains the function prototypes for controlling motors
3
	author:  Tom Lauwers
4
*/
5

  
6
#ifndef _MOTOR_H
7
#define _MOTOR_H
8

  
9
#include <avr/io.h>
10

  
11
#define FORWARD 1
12
#define BACKWARD 0
13

  
14

  
15

  
16
// Function descriptions can be found in motor.c
17
void motors_init(void);
18
void motor1_set(int direction, int speed);
19
void motor2_set(int direction, int speed);
20
void motors_off(void);
21

  
22
#endif
trunk/code/projects/colonet/robot/dragonfly_wireless_relay/dragonfly_lib.c
1
#include <dragonfly_lib.h>
2

  
3
/* init_dragonfly - Initializes functions based on configuration parameters
4
   examples:
5

  
6
   init_dragonfly (0, 0, 0); - just initialize the digital IO (buttons, potentiometer)
7

  
8
   init_dragonfly (ANALOG | SERIAL | BUZZER, C0_C1_ANALOG, BAUD115200); 
9
   Initialize ADC and set C0 and C1 as analog inputs.  Initialize serial and set baud rate
10
   to 115200 bps.  Initialize the buzzer.
11
   
12
   init_dragonfly (MOTORS | ORB, 0, 0);
13
   Initialize motor driving and the color fading abilities of the ORB. */
14
   
15
void dragonfly_init(int config) 
16
{
17
	// Set directionality of various IO pins
18
	DDRG &= ~(_BV(PING0)|_BV(PING1));
19
	PORTG |= _BV(PING0)|_BV(PING1);
20
	
21
	if(config & ANALOG) {
22
		analog_init();
23
	}
24
	
25
	if(config & COMM) {
26
    //Defaults to 115200. Check serial.h for more information.
27
    sei();
28
    usb_init();
29
    xbee_init();
30
	}
31
	
32
	if(config & BUZZER) {
33
    sei();
34
		buzzer_init();
35
	}
36
	
37
	if(config & ORB) {
38
    sei();
39
		orb_init();
40
	}
41
	
42
	if(config & MOTORS) {
43
		motors_init();
44
	}
45
	
46
	if(config & SERVOS) {
47
		sei();
48
    //Servos not yet implemented
49
    //servo_init();
50
	}
51

  
52
	if(config & LCD) {
53
		lcd_init();
54
	}
55
	
56
	if(config & (SERVOS | ORB)) {
57
    sei();
58
    orb_init();
59
    //Servos not yet implemented
60
	}
61
	
62
	// delay a bit for stability
63
	_delay_ms(1);
64
}
65

  
66
void adc_gain_test(void);
67
void analog_test(void);
68
void batt_test(void);
69
void button_test(void);
70
void buzzer_test(void);
71
void dio_test(void);
72
void general_test(void);
73
void i2c_test(void);
74
void lcd_test(void);
75
void motor_test(void);
76
void orb_test(void);
77
void pullup_test(void);
78
void ranger_test(void);
79
void reset_test(void);
80
void ring_buffer_tester(void);
81
void rtc_test(void);
82
void rtc_test_pulse(void);
83
void serial_test(void);
84
void servo_orb_test(void);
85
void wl_receive_test(void);
86
void wl_send_test(void);
87
void wl_send_packet_test(void);
88
void move_test(void);
89

  
90
void move_test(void){
91
  range_init();
92
  motors_init();
93
  analog_init();
94
  while(1){
95
    move_avoid(200,0,50 );
96
  }
97
}  
98

  
99
void general_test(){
100
	int i;
101
	dragonfly_init(MOTORS|BUZZER|ORB|USB);
102

  
103
	while(1){
104
		if(button1_read()){
105

  
106
			for(i=0;i<17;i++){
107
				buzzer_set_val(i);
108
				delay_ms(100);
109
			}
110
			
111
			buzzer_off();
112
			
113
			motor1_set(1, 255);
114
			motor2_set(1, 255);
115
			
116
			delay_ms(3000);
117
			
118
			motor1_set(1, 200);
119
			
120
			delay_ms(1000);
121
			motor1_set(0, 255);
122
			motor2_set(0, 255);
123
			
124
			delay_ms(3000);
125
			motors_off();
126
		}
127
		if(button2_read()){
128
			orb_enable();
129
			orb1_set_color(RED);
130
			delay_ms(500);
131
			orb2_set_color(GREEN);
132
			delay_ms(500);
133
			orb1_set_color(YELLOW);
134
			delay_ms(500);
135
			orb2_set_color(CYAN);
136
			delay_ms(500);
137
			orb1_set_color(WHITE);
138
			delay_ms(500);
139
			orb2_set_color(WHITE);
140
			delay_ms(500);
141
			orb_disable();
142
			
143
		}
144
	
145
	}
146
}
147

  
148
void reset_test() {
149

  
150
    dragonfly_init(ORB|SERIAL);
151
    
152
    delay_ms(1000);
153
    usb_puti((int)DDRE);
154
    
155
    orb_set_color(RED);
156
    delay_ms(1000);
157
    
158
    orb_set_color(GREEN);
159
    delay_ms(1000);
160
    
161
    DDRE |= 0x80;
162
    
163
    reset();
164
    
165
    /*
166
    alternate implelmentation using external interrupts
167
    maintains register values on reset
168
    
169
    sei();
170
    EIMSK |= 0x80;
171
    EICRB |= 0xC0;
172
    DDRE |= 0x80;
173
    PORTE &= 0x7F;
174
    PORTE |= 0x80;
175
    */
176
    
177
    /*
178
    should not reach here
179
    */
180
    
181
    orb_set_color(BLUE);
182
    delay_ms(1000);
183
}
184

  
185
void ring_buffer_tester (void) {
186

  
187
	dragonfly_init(ALL_ON & ~ORB);
188
	orb_disable();
189

  
190
	RING_BUFFER_NEW(ring_buffer, 12, int, buffer);
191
	RING_BUFFER_INIT(buffer, 12);
192
	RING_BUFFER_CLEAR(buffer);
193
	
194
	for(int i=0;i<10;i++)
195
		RING_BUFFER_ADD(buffer, 0);
196
	
197
	int x;
198
	for(int i = 0; i < 100; i++) {
199
	
200
		RING_BUFFER_REMOVE(buffer, x);
201
		
202
		RING_BUFFER_ADD(buffer, i);
203
		
204
		usb_puti(i-x);
205
		usb_putc(' ');
206
	
207
	}
208
	
209
	usb_puts("\n\rfull? " );
210
	usb_puti(RING_BUFFER_FULL(buffer));
211
	
212
	while(!RING_BUFFER_FULL(buffer))
213
		RING_BUFFER_ADD(buffer, 0);
214
	
215
	usb_puts("\n\rfilling... full? " );
216
	usb_puti(RING_BUFFER_FULL(buffer));
217
	
218
	usb_puts("\n\rempty? " );
219
	usb_puti(RING_BUFFER_EMPTY(buffer));
220
	usb_puts("\n\rclearing... empty? ");
221
	RING_BUFFER_CLEAR(buffer);
222
	usb_puti(RING_BUFFER_EMPTY(buffer));
223
	
224
	usb_puts("\n\r");
225
	
226
	
227
}
228

  
229
#define WL_BAUD BAUD115200
230
//serial -- XBEE
231
//serial1 -- USB
232

  
233
void wl_send_test(void) {
234

  
235
	int i;
236
	
237
	usb_init();		//WIRED
238
	xbee_init();	//WIRELESS
239
	
240
	orb_init();
241
	sei();
242
	
243
	while(1) {
244
	
245
		for(i = 32; i <= 126; i++) {
246
			
247
			if(i%4) {
248
				orb1_set(255, 0, 0);
249
			}
250
			else {
251
				orb1_set(0,0,0);
252
			}
253
			
254
			usb_putc(i);		//wired
255
			xbee_putc(i);		//wireless
256
			delay_ms(100);
257
	
258
		}
259

  
260
	}
261

  
262
	return;
263
}
264

  
265
void wl_send_packet_test(void) {
266

  
267
	int i;
268
	
269
	usb_init();		//WIRED
270
	xbee_init();	//WIRELESS
271
	
272
	orb_init();
273
	sei();
274
	
275
	while(1) {
276
	
277
		for(i = 65; i <= 122; i++) {
278
			usb_putc(i);		//wired
279
			xbee_putc(i);		//wireless
280
      //delay_ms(100);
281
		}
282
    
283
    delay_ms(1000);
284

  
285
	}
286

  
287
	return;
288
}
289

  
290
volatile char c;
291
volatile char haveNew = 0;
292

  
293

  
294
void wl_receive_test(void) {
295

  
296

  
297
	usb_init();
298
	xbee_init();
299
	orb_init();
300
	
301
	
302
	c = '.';
303
	
304
  xbee_putc('r');
305
	
306
	
307
	UCSR1B |= _BV(RXCIE);
308
	UCSR0B |= _BV(RXCIE);
309
	sei();
310
	
311
	orb1_set(255, 0, 0);		//blue
312

  
313
	
314
	while(1)	 {
315
	
316
		if(haveNew) {
317
			xbee_putc(c);
318
			haveNew = 0;
319
		}
320
	}
321
	
322
	orb1_set(0, 0, 255);		//green
323
	
324
	while(1);
325

  
326
}
327
/*
328
ISR(USART1_RX_vect) {
329
	
330
	c = UDR1;
331
	haveNew = 1;
332
}
333

  
334
ISR(USART0_RX_vect) {
335
	
336
	c = UDR0;
337
	haveNew = 1;
338
}
339
*/
340
void buzzer_test(void)
341
{
342
	sei();
343
	buzzer_init();
344
	unsigned int i = 0;
345
	
346
	while(1) {
347
		buzzer_set_val(i);
348
		if (i == 20) {
349
			i = 0;
350
			delay_ms(250);
351
		} else {
352
			i++;
353
		}
354
		
355
		delay_ms(100);
356
	}
357
}
358

  
359
void adc_gain_test(void)
360
{
361
  analog_init();
362
  lcd_init();
363
  
364
  while(1)
365
  {
366
    
367
    lcd_putint(ADCH);
368
    delay_ms(100);
369
    lcd_clear_screen();
370
  }
371
  
372
  
373

  
374
}
375

  
376
void ranger_test( void )
377
{
378
	lcd_init();
379
	
380
	while(1) {
381
		lcd_clear_screen();
382
		lcd_putint(linearize_distance(read_distance(PIN_C0)));
383
		delay_ms(100);
384
	
385
	}
386

  
387
}
388

  
389

  
390
void button_test ( void) 
391
{
392
    dragonfly_init(ALL_ON);
393
    
394
    
395
    move(200,0);
396

  
397
	while(1) {
398
	
399
	
400
		if(button1_read()) {
401
			//lcd_putchar('1');
402
			usb_putc('1');
403
		} else if(button2_read()) {
404
			//lcd_putchar('2');
405
			usb_putc('2');
406
		}
407
		
408
	}
409

  
410

  
411

  
412
}
413
void motor_test (void) {
414

  
415
	motors_init();
416
	//led_init();
417
	
418
	//motor1_set(1, 255);
419
	//motor2_set(1, 255);
420
	move(200,0);
421
	
422
	delay_ms(5000);
423
	
424
	//motor1_set(0, 200);
425
	move(200,100);
426
	
427
	delay_ms(5000);
428
	
429
	//motor2_set(0, 170);
430
	move(200,0);
431
	
432
	delay_ms(5000);
433
	motors_off();
434
	//LED USER NO LONGER EXISTS
435
	//led_user(1);
436
	
437
	
438
	while(1);
439

  
440

  
441

  
442
}
443

  
444
void orb_test( void ) {
445
	int orbs[12] = { ORB_OFF, RED, ORANGE, YELLOW, LIME, GREEN, CYAN, BLUE, PINK, PURPLE, MAGENTA, WHITE };
446
	int orb_select = 0;
447
	lcd_init();
448
	orb_init();
449
	sei();
450
	orb2_set(254, 253, 252);
451
	orb1_set(251, 250, 249);
452
	while(1) {
453
	
454
		orb_set_color(orbs[orb_select]);
455
		/*orb_set1(251, 250, 249);
456
		delay_ms(2000);
457
		orb_set1(255, 1,2);
458
		delay_ms(1000);
459
		orb_set1(1, 255, 2);
460
		delay_ms(1000);
461
		orb_set1(1, 2, 255);
462
		*/
463
		delay_ms(3000);
464
		
465
		orb_select++;
466
		
467
		
468
		
469
		if(orb_select == 13) {
470
			orb_disable();
471
			delay_ms(2000);
472
			orb_enable();
473
			orb_select = 0;
474
		}
475
	
476
	/*
477
		for (int i = 0; i < 256; i++) {
478
			for (int j = 0; j < 256; j++) {
479
				for (int k = 0; k < 256; k++) {
480
					orb_set(i, j, k);
481
				}
482
			}
483
		}
484
	*/
485
	}
486
	return;
487

  
488
}
489

  
490
void servo_orb_test( void ) {
491
//	int orbs[13] = { ORB_OFF, RED, ORANGE, YELLOW, LIME, GREEN, CYAN, BLUE, PURPLE, PINK, PURPLE, MAGENTA, WHITE };
492
	int orb_select = 0;
493
	
494
	orb_init();
495
	sei();
496
	orb_set_angle(1,250);
497
	orb_set_angle(2,250);
498
	orb_set_angle(4,0);
499
	orb_set_angle(5,0);
500
	orb_set_angle(6,0);
501

  
502
	while(1) {
503
	
504
		//orb_set_color(orbs[orb_select]);
505
		
506
		orb_set_angle(0,orb_select+1);
507
	//	orb_set_angle(1,orb_select+1);
508
	//	orb_set_angle(2,orb_select+1);
509
		delay_ms(50);
510
		orb_select++;
511
		
512
		
513
		
514
		if(orb_select == 255) {
515
			
516
			orb_select = 0;
517
		}
518
	
519
	/*
520
		for (int i = 0; i < 256; i++) {
521
			for (int j = 0; j < 256; j++) {
522
				for (int k = 0; k < 256; k++) {
523
					orb_set(i, j, k);
524
				}
525
			}
526
		}
527
	*/
528
	}
529
	return;
530

  
531
}
532

  
533

  
534
void lcd_test(void) {
535
	int i, j;
536
	lcd_init();
537
	
538
	for(i = 32,j = 1; i <= 126; i++) {
539
		lcd_putchar(i);
540
		delay_ms(250);
541
        if(j > 50) {
542
            lcd_clear_screen();
543
            j = 0;
544
        }
545
        j++;
546
	}
547
	
548
	
549
	return;
550
	
551
}
552

  
553
void serial_test( void ) {
554
	int i;
555
	
556
	usb_init();
557

  
558
	for(i = 32; i <= 126; i++) {
559
		usb_putc(i);
560
		delay_ms(250);
561
	}
562

  
563
	return;
564
}
565

  
566
void pullup_test()
567
{
568
	lcd_init();
569
	lcd_putstr("pullup test");
570
	delay_ms(100);
571
	
572
	digital_pull_up(PIN_A4);
573
	digital_pull_up(PIN_A5);
574
	digital_pull_up(PIN_A6);
575
	digital_pull_up(PIN_A7);
576
	while(1){
577
		lcd_clear_screen();
578
		if(!digital_input(PIN_A4))
579
			lcd_putchar('0');
580
		else
581
			lcd_putchar(' ');
582
		if(!digital_input(PIN_A5))
583
			lcd_putchar('1');
584
		else
585
			lcd_putchar(' ');
586
		if(!digital_input(PIN_A6))
587
			lcd_putchar('2');
588
		else
589
			lcd_putchar(' ');
590
		if(!digital_input(PIN_A7))
591
			lcd_putchar('3');
592
		else
593
			lcd_putchar(' ');
594
		delay_ms(100);
595
	}
596
}
597

  
598
void batt_test()
599
{
600
	lcd_init();
601
  usb_init();
602

  
603
	analog_init();
604

  
605
	while(1){
606
    usb_puti(battery8());
607
    usb_putc(' ');
608
		delay_ms(10000);
609
	}
610
}
611

  
612

  
613
void analog_test(void){
614
  //int i;
615
  analog_init();
616
  usb_init();
617
  range_init();
618
  usb_puts(" information:\n");
619
  while(1){
620
    printAllBoms();
621
    /*for(i=0;i<16;i++){
622
      digital_output(MONK0,i&1);
623
      digital_output(MONK1, (i>>1)&1);
624
      digital_output(MONK2, (i>>2)&1);
625
      digital_output(MONK3, (i>>3)&1);
626
      usb_puts("Bom ");
627
      usb_puti(i);
628
      usb_puts(": ");
629
      usb_puti(analog8(MONKI));
630
      usb_puts("\n\r");
631
    }*/
632
  
633
    /*for(i=1;i<8;i++){
634
      usb_puts("Analog Ext ");
635
      usb_puti(i);
636
      usb_puts(":  ");
637
      usb_puti(analog8(i));
638
      usb_puts("\n\r");
639
    }
640
    
641
    usb_puts("IR1 val: ");
642
    usb_puti(read_distance(IR1));
643
    usb_puts("\n\rIR2 val: ");
644
    usb_puti(read_distance(IR2));
645
    usb_puts("\n\rIR3 val: ");
646
    usb_puti(read_distance(IR3));
647
    usb_puts("\n\rIR4 val: ");
648
    usb_puti(read_distance(IR4));
649
    usb_puts("\n\rIR5 val: ");
650
    usb_puti(read_distance(IR5));
651
    */
652
    delay_ms(2000);
653
  }
654
}
655

  
656
void dio_test()
657
{
658
	while(1)
659
	{
660
		digital_output(_PIN_E5,1);
661
		delay_ms(1000);
662
		digital_output(_PIN_E5,0);
663
		delay_ms(1000);
664
	}
665
}
666

  
667
void rtc_test_pulse() {
668
	usb_putc('s');
669
}
670

  
671
void rtc_test() {
672
	sei();
673
	usb_init();
674
	usb_putc('a');
675
	rtc_init(HALF_SECOND, &rtc_test_pulse);
676
}
677

  
678
#define RECEIVE 1
679

  
680
#define MY_ADDR (RECEIVE == 1 ? 0x01 : 0x02)
681
#define TARGET_ADDR (RECEIVE == 1 ? 0x02 : 0x01)
682

  
683
void i2c_test() {
684
  char c = 0;
685
  char i = 0;
686
  
687
  sei();
688
  i2c_init(MY_ADDR);
689
  usb_init();
690

  
691
  usb_putc('5');
692
  delay_ms(1000);
693
  
694
  while(1) {
695
    if (RECEIVE) {
696
      while(c != 'z') {
697
        if(!i2c_getc(&c)) {
698
          usb_putc(c);
699
          usb_putc(' ');
700
        }
701
      }
702
    } else {
703
    while(i < 26) {
704
      if(!i2c_putc(TARGET_ADDR, i + 0x61))
705
        i++;
706
      }
707
    }
708
  
709
    delay_ms(1000);
710
  
711
    if (!RECEIVE) {
712
      while(c != 'z') {
713
        if(!i2c_getc(&c)) {
714
          usb_putc(c);
715
          usb_putc(' ');
716
        }
717
      }
718
    } else {
719
      while(i < 26) {
720
        if(!i2c_putc(TARGET_ADDR, i + 0x61))
721
          i++;
722
      }
723
    }
724
    
725
    c = 0;
726
    i = 0;
727
  }
728
  while(1);
729
}
trunk/code/projects/colonet/robot/dragonfly_wireless_relay/i2c.c
1
/* I2C.c
2

  
3
*/
4

  
5
#include <avr/interrupt.h>
6
#include <util/twi.h>
7

  
8
#include "i2c.h"
9
#include "ring_buffer.h"
10

  
11

  
12
static int start_flag;
13
RING_BUFFER_NEW(i2c_buffer, 64, char, i2c_write_buff, i2c_addr_buff, i2c_read_buff);
14

  
15
void i2c_init(char address) {
16
	// Enable I2C and turn on the interrupt
17
  //TWCR = 0;
18
  
19
  /* initialize the buffers */
20
  RING_BUFFER_CLEAR(i2c_write_buff);
21
  RING_BUFFER_CLEAR(i2c_addr_buff);
22
  RING_BUFFER_CLEAR(i2c_read_buff);
23
  
24
	TWCR =  (_BV(TWEA) | _BV(TWEN) | _BV(TWIE));
25

  
26
  //Set bit rate 12 = 100kbit/s
27
  TWBR = 0x0C;
28
    
29
	TWAR = (address << 1);	//0b0000001 address, 0 for global commands to be off
30
	
31
  //usb_putint(TWAR);
32
  //i2cTXFlag = 0;
33
  //i2cRXFlag = 0;
34
}
35

  
36
char i2c_putc(char dest, char data) {
37
  if(RING_BUFFER_FULL(i2c_write_buff))
38
    return -1;
39
  
40
  cli();
41
  RING_BUFFER_ADD(i2c_write_buff, data);
42
  RING_BUFFER_ADD(i2c_addr_buff, dest << 1);
43
  sei();
44
  
45
	//Start bit
46
  if(!start_flag) {
47
    start_flag = 1;
48
    TWCR |= _BV(TWSTA);
49
    TWCR |= _BV(TWINT);
50
  }
51
  
52
  return 0;
53
}
54

  
55
char i2c_getc(char * c) {
56
  if(RING_BUFFER_EMPTY(i2c_read_buff))
57
    return -1;
58
  else
59
    RING_BUFFER_REMOVE(i2c_read_buff, *c);
60
  
61
  return 0;
62
}
63
 
64
ISR(TWI_vect) {
65
  static char data_to_send;
66
  static char addr_to_send = -1;
67
  char addr, statusCode;
68
  
69
    //Get status code (only upper 5 bits)
70
    statusCode = (TWSR & 0xF8);
71
  
72
  switch (statusCode) {
73
    //Start sent successfully
74
    case TW_START:
75
    case TW_REP_START:
76
      //Send address and write
77
      //ring_buffer will not be empty
78
      RING_BUFFER_REMOVE(i2c_addr_buff, addr_to_send);
79
      RING_BUFFER_REMOVE(i2c_write_buff, data_to_send);
80
      
81
      TWDR = addr_to_send; /* first send the address */
82
      /* we will send the data next time around */
83
      
84
      //Turn off start bits
85
      TWCR &= ~_BV(TWSTA);
86
      break;
87
    //Address sent successfully
88
    case TW_MT_SLA_ACK:
89
      //Send byte
90
      TWDR = data_to_send;
91
      break;
92
    //Packet complete, close line
93
    case TW_MT_DATA_ACK:
94
      if(!RING_BUFFER_EMPTY(i2c_write_buff)) {
95
        RING_BUFFER_PEEK(i2c_addr_buff, addr);
96
        if(addr == addr_to_send) {
97
          RING_BUFFER_REMOVE(i2c_addr_buff, addr);
98
          RING_BUFFER_REMOVE(i2c_write_buff, TWDR);
99
          break;
100
        }
101
        else { /* no more bytes to this addr, bytes to send to new addr */
102
          /* doing the start signal again */
103
          TWCR |= _BV(TWSTA);
104
          break;
105
        }
106
      }
107
      
108
      /* there are no bytes to send */
109
      TWCR |= _BV(TWSTO);
110
      start_flag = 0;
111
      
112
      break;
113
    case TW_SR_SLA_ACK:
114
      break;
115
    case TW_SR_DATA_ACK:
116
      if(!RING_BUFFER_FULL(i2c_read_buff))
117
        RING_BUFFER_ADD(i2c_read_buff, TWDR);
118
      break;
119
    case TW_SR_STOP:
120
      break;
121
  }
122
      
123
  TWCR |= _BV(TWINT);
124
	
125
}
trunk/code/projects/colonet/robot/dragonfly_wireless_relay/serial.c
1
/*
2
	serial.c - Functions for using the RS232 serial port
3
	
4
	authors: Robotics Club, Colony Project, pkv
5
	much code taken from FWR's library, author: Tom Lauwers
6
*/
7

  
8
#include <avr/io.h>
9
#include <stdio.h>
10
#include "serial.h"
11

  
12
/* usb_init() - sets up AVR for communication over the usb serial port */
13
void usb_init() {
14
  //Set baud rate
15
  // - 115200 (both wired and wireless) is UBRR=8, U2X=1
16
  // - 9600 is U2X =1, UBRR = 107.
17
	#if (USB_BAUD == 115200)
18
    UBRR0H = 0x00;
19
    UBRR0L = 8;
20
    UCSR0A |= _BV(U2X0);
21
  #elif (USB_BAUD == 9600)
22
    UBRR0H = 0x00;
23
    UBRR0L = 107;
24
    UCSR0A |= _BV(U2X0);
25
  #else //Baud rate is defined in the header file, we should not get here
26
    return;
27
  #endif
28

  
29
  /*Enable receiver and transmitter */
30
	UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
31
	
32
	/* Set frame format: 8data, 1stop bit, asynchronous normal mode */
33
	UCSR0C |= (1<<UCSZ00) | (1<<UCSZ01);
34
  
35
  // if we have enabled the stdio stuff, then we init it here
36
  #ifdef USE_STDIO
37
    /* Open the stdio stream corresponding to this port */
38
    usb_fd = fdevopen(usb_putc, usb_getc);
39
  #endif
40
}
41

  
42
/* xbee_init() - sets up AVR for communication over the xbee serial port */
43
void xbee_init() {
44
  //Set baud rate
45
  // - 115200 (both wired and wireless) is UBRR=8, U2X=1
46
  // - 9600 is U2X =1, UBRR = 107.
47
	#if (XBEE_BAUD == 115200)
48
    UBRR1H = 0x00;
49
    UBRR1L = 8;
50
    UCSR1A |= _BV(U2X1);
51
  #elif (XBEE_BAUD == 9600)
52
    UBRR1H = 0x00;
53
    UBRR1L = 107;
54
    UCSR1A |= _BV(U2X1);
55
  #else //Baud rate is defined in the header file, we should not get here
56
    return;
57
  #endif
58

  
59
	//Enable receiver and transmitter
60
	UCSR1B |= (1<<RXEN1)|(1<<TXEN1);
61
	
62
	// Set frame format: 8data, 1stop bit, asynchronous normal mode
63
	UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);
64
  
65
  // if we have enabled the stdio stuff, then we init it here
66
  #ifdef USE_STDIO
67
    /* Open the stdio stream corresponding to this port */
68
    xbee_fd = fdevopen(xbee_putc, xbee_getc);
69
  #endif
70
}
71

  
72
/* usb_putc(c) - waits until the usb serial tx buffer is clear, then 
73
  puts the character c into the buffer */
74
int usb_putc(char c) 
75
{
76
  // Wait until buffer is clear for sending
77
  loop_until_bit_is_set(UCSR0A, UDRE0);
78
	
79
  // Load buffer with your character
80
  UDR0 = c;
81
  return 0;
82
}
83

  
84
/* xbee_putc(c) - waits until the xbee serial tx buffer is clear, then 
85
  puts the character c into the buffer */
86
int xbee_putc(char c) 
87
{
88
  // Wait until buffer is clear for sending
89
  loop_until_bit_is_set(UCSR1A, UDRE1);
90
	
91
  // Load buffer with your character
92
  UDR1 = c;
93
  return 0;
94
}
95

  
96
/* usb_puts(c) - writes each character from the string to the usb */
97
int usb_puts(char *s)
98
{
99
	char *t = s;
100
	while (*t != 0)
101
	{
102
		usb_putc(*t);
103
		t++;
104
	}
105
  return 0;
106
}
107

  
108

  
109
/* usb_getc() - waits until the usb serial rx buffer is not empty, then 
110
  returns the first character in the buffer */
111
int usb_getc(void)
112
{
113
  // Wait for the receive buffer to be filled
114
  loop_until_bit_is_set(UCSR0A, RXC0);
115
	
116
	// Read the receive buffer
117
	return UDR0;
118
}
119

  
120
/* xbee_getc() - waits until the xbee serial rx buffer is not empty, then 
121
  returns the first character in the buffer */
122
int xbee_getc(void)
123
{
124
	// Wait for the receive buffer to be filled
125
    loop_until_bit_is_set(UCSR1A, RXC1);
126
	
127
	// Read the receive buffer
128
	return UDR1;
129
}
130

  
131
/* usb_getc_nb(*c) - checks if the usb serial rx buffer is empty or not:
132
  -If it is empty, c is unchanged and -1 is returned.  
133
  -If is not empty, the first character in the buffer is loaded into c,
134
    and 0 is returned */
135
int usb_getc_nb(char *c)
136
{
137
	// check if the receive buffer is filled
138
  if (UCSR0A & _BV(RXC0)) {
139
    // Read the receive buffer
140
    (*c) = UDR0;
141
    return 0;
142
  }
143
  else {
144
    // Return empty
145
    return -1;
146
	}
147
}
148

  
149
/* xbee_getc_nb(*c) - checks if the xbee serial rx buffer is empty or not:
150
  -If it is empty, c is unchanged and -1 is returned.  
151
  -If is not empty, the first character in the buffer is loaded into c,
152
    and 0 is returned */
153
int xbee_getc_nb(char *c)
154
{
155
	// check if the receive buffer is filled
156
  if (UCSR1A & _BV(RXC1)) {
157
    // Read the receive buffer
158
    (*c) = UDR1;
159
    return 0;
160
  }
161
  else {
162
    // Return empty
163
    return -1;
164
	}
165
}
166

  
167

  
168
/*
169
prints an int to serial
170

  
171
code adapted from Chris Efstathiou's code (hendrix@otenet.gr)
172
uses usb_putc
173
*/
174
int usb_puti(int value ) {
175
	unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; 
176

  
177
        /* convert int to ascii  */ 
178
        if(value<0) { usb_putc('-'); value=-value; }    
179
        do { position--; *(usb_data+position)=(value%radix)+'0'; value/=radix;  } while(value); 
180

  
181
    
182
        /* start displaying the number */
183
        for(;position<=(sizeof(usb_data)-1);position++)
184
          {
185
            
186
            usb_putc(usb_data[position]);
187
          }
188

  
189
	return 0;
190
}
trunk/code/projects/colonet/robot/dragonfly_wireless_relay/dragonfly_lib.h
1
/*
2
dragonfly_lib.h
3

  
4
author: CMU Robotics Club, Colony project
5
much of this is adapted from the firefly library (author: Tom Lauwers)
6
*/
7

  
8

  
9
#ifndef _DRAGONFLY_LIB_H_
10
#define _DRAGONFLY_LIB_H_
11

  
12

  
13
// Configuration definitions
14
#define ANALOG 0x01
15
#define SERIAL 0x02
16
#define USB    0x02
17
#define COMM   0x02
18
#define ORB    0x04
19
#define MOTORS 0x08
20
#define SERVOS 0x10
21
#define I2C    0x20
22
#define BUZZER 0x40
23
#define LCD	   0x80
24
#define ALL_ON 0xFF
25

  
26
// initializes the board with the functions specified by config
27
void dragonfly_init(int config);
28

  
29
#include <inttypes.h>
30
#include <stdio.h>
31
#include <stdlib.h>
32
#include <avr/io.h>
33
#include <avr/interrupt.h>
34
#include <util/delay.h>
35
#include <util/twi.h>
36

  
37
#include <analog.h>
38
#include <dio.h>
39
#include <time.h>
40
#include <lcd.h>
41
#include <lights.h>
42
#include <motor.h>
43
#include <serial.h>
44
#include <buzzer.h>
45
#include <servo.h>
46
#include <rangefinder.h>
47
#include <bom.h>
48
#include <move.h>
49
#include <i2c.h>
50
#include <ring_buffer.h>
51
#include <reset.h>
52
#include <math.h>
53

  
54
#endif
trunk/code/projects/colonet/robot/dragonfly_wireless_relay/buzzer.c
1
/*
2
  buzzer.c - Contains the functions necessary for running the buzzer
3
  
4
  author: Robotics Club, Colony Project
5
  
6
  much taken from FWR's firefly library (author: Tom Lauwers)
7
*/
8

  
9
#include <avr/io.h>
10
#include <buzzer.h>
11
#include <time.h>
12

  
13
/*
14
initializes buffer
15
this function must be called before the buzzer can be used
16
*/
17
void buzzer_init( void )
18
{
19
//NOTE: This is mostly handled by buzzer_Set_val for now
20
	// Set pin B7 to output - B7 is buzzer pin
21
//	DDRB |= _BV(DDB7);
22
	
23
		
24
	//Fast PWM Mode, Clear OCR0 on compare match and set at bottom, clock/64
25
//	TCCR2 = _BV(COM20) | _BV(WGM21)  | _BV(CS22);
26

  
27
}
28

  
29
// Set the buzzer value - takes a value from 0-255
30
// Higher values are lower frequency.  Take a look at 
31
// the buzzer frequency table to see how a given value
32
// correlates to a frequency.
33
void buzzer_set_val(unsigned int buzz_value)
34
{ 
35
	TCCR2 = _BV(COM20) | _BV(WGM21)  | _BV(CS22);
36
	DDRB |= _BV(DDB7);
37
  OCR2 = buzz_value;
38
}
39

  
40
// Set the buzzer frequency - takes any value and tries to find the nearest 
41
// buzzer frequency
42
void buzzer_set_freq(unsigned int buzz_freq)
43
{
44
  int buzz_value;
45
  
46
  buzz_value = 62500/buzz_freq - 1;
47

  
48
  if(buzz_value > 255){
49
    buzz_value = 255;
50
  }else if(buzz_value < 0){
51
    buzz_value = 0;
52
  }
53
  
54
  buzzer_set_val(buzz_value);
55
}
56

  
57
// Chirps the buzzer for a number of milliseconds
58
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq) 
59
{ 
60
  buzzer_set_freq(buzz_freq);
61
  delay_ms(ms);
62
  buzzer_off();
63
}
64

  
65
// Disables timer0 clock counting, turning off the buzzer
66
void buzzer_off()
67
{
68
  // Disable clock, to halt counter
69
  TCCR2 &= 0xF8;
70
  
71
  // Set buzzer pin low in case it's high
72
  PORTB &= 0xBF;
73
}
trunk/code/projects/colonet/robot/dragonfly_wireless_relay/i2c.h
1
#ifndef _I2C_H_
2
#define _I2C_H_
3

  
4
void i2c_init(char address);
5
char i2c_putc(char dest, char data);
6
char i2c_getc(char * c);
7

  
8
#endif
trunk/code/projects/colonet/robot/dragonfly_wireless_relay/serial.h
1
/*
2
	serial.h - Contains definitions and function prototypes for the RS232 serial port
3
  author(s): pkv
4
  
5
  Directions:
6
  Call the initialization function for the serial port you wish to use.  Then, use
7
  either the provided functions or the stdio functions (fprintf, etc) to read and
8
  write characters to the serial ports.
9
  
10
  UART Mapping:
11
    usb_*() -> UART0
12
    xbee_*() -> UART1
13
  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff