Project

General

Profile

Revision 1836

update the ir unit test

View differences:

trunk/code/projects/linefollowing/main.c
1 1
#include <dragonfly_lib.h>
2 2

  
3
#define WHITE			0
4
#define GREY			1
5
#define BLACK	 		2
6
#define COLOR_BUFFER		10
7
#define CENTER			3
8

  
3 9
int main(void)
4 10
{
5 11

  
6 12
	/* initialize components, set wireless channel */
7 13
	dragonfly_init(ALL_ON);
8 14
	
9
	int val;
15
	straightLineFollow();
10 16

  
11
	while (1) {
17
	return 0;
18
}
12 19

  
13
		usb_puts("Val: ");
14
		usb_puti(range_read_distance(IR2));
15
		usb_putc('\n');
20
//Converts an analog color to the ternary black, grey, white system.
21
//If the value is between two codes, the port is read again and the function is called again
22
int asssignColor(int analog)
23
{
24
	int color;
25
	
26
		if(analog < 341-COLOR_BUFFER)
27
			color = WHITE;
28
		else if (analog < 341+COLOR_BUFFER)
29
			color = assignColor(updateIR(port));
30
		else if (analog < 682-COLOR_BUFFER)
31
			color = GREY; 
32
		else if (analog < 682+COLOR_BUFFER)
33
			color = assignColor(updateIR(port));
34
		else
35
			color = BLACK;
36
			
37
		return color;
16 38

  
17
		delay_ms(200);
39
}
18 40

  
19
	}
41
//Gets the value of the nth IR sensor
42
int updateIR(int n)
43
{
44
	//maps the sensors to the analog input ports
45
	int ports[5] = {13, 12, 3, 2, 9};
20 46

  
21
	return 0;
47
	return analog10(ports[n]);
22 48
}
23 49

  
50
//Gets the value of the nth IR sensor
51
int updateBarCode()
52
{
53
	//maps the sensors to the analog input ports
54
	int ports[2] = {8,1};
55
	
56
	return analog10(ports[1]) | analog10(ports[2];
57
}
58

  
59
// updates all of the values, and assigns them new colors
60
int* updateLine(int* values)
61
{	
62
	int i;
63

  
64
	for(i = 0; i<5; i++)
65
		values[i] = assignColor(i, updateIR(i)); 
66
		
67
	return values;
68
}
69

  
70
// updates all of the values, and assigns them new colors
71
int* updateLine(int* values)
72
{	
73
	int i;
74
	
75
	values[i] = assignColor(i, updateIR(i)); 
76
	
77
	return values;
78
}
79

  
80

  
81
// given an array of ints this function will take the arrays weighted average. High numbers indicate that a left turn is needed. Low numberes indicate that a right turn is needed.
82
int lineLocate(int[] colors)
83
{
84
	int i;
85
	int wsum = 0;
86

  
87
	for(i = 0; i<5; i++)
88
		wsum += (i+1)*colors[i];
89

  
90
	return (wsum/15) -1;
91
}
92

  
93
//The robot will follow a line until it is interrupted by a barcode.
94
int straightLineFollow()
95
{
96
	int colors[5];
97
	int location = 0;
98
	
99

  
100
	while(1)
101
	{	
102
		colors  = updateLine(colors);
103
		location = lineLocate(colors); 	
104
		
105
		//turn left
106
		if(location > 3)
107
		{
108
			orb1_set_color(GREEN);
109
			orb2_set_color(ORB_OFF);
110
		}
111
		//turn right
112
		else if(location < 3)
113
		{
114
			orb1_set_color(ORB_OFF);
115
			orb2_set_color(GREEN);
116
		}
117
	}
118
}

Also available in: Unified diff