Project

General

Profile

Revision 1842

Added by Dan Jacobs over 13 years ago

Some barcode code

View differences:

trunk/code/projects/linefollowing/lineFollow.h
47 47
int lineLocate(int* colors);
48 48

  
49 49
//Not implemented yet, returns ???
50
int updateBarCode();
50
void updateBarCode();
51 51

  
52 52
/*	min
53 53
	returns the minimum of two values
trunk/code/projects/linefollowing/main.c
9 9
	/* initialize components, set wireless channel */
10 10
	dragonfly_init(ALL_ON);
11 11
	lineFollow_init();
12
	int barCode;
12 13
	while(1)
13
		lineFollow();
14

  
14
	{
15
		barCode = lineFollow();
16
		switch (barCode)
17
		{
18
			case 0:
19
				straight();
20
				turnRight();
21
				break;
22
                        case 2:
23
	                        straight();
24
        	                break;
25
                        case 3:
26
	                        straight();
27
				turnLeft();
28
        	                break;
29
		}
30
	}
15 31
	return 0;
16 32
}
33
void turnRight()
34
{
35
motor_r_set(BACKWARD, 200);
36
motor_l_set(FORWARD, 200);
37
delay_ms(400);
38
}
39

  
40

  
41
void straight()
42
{
43
motor_r_set(FORWARD, 210);
44
motor_l_set(FORWARD, 210);
45
delay_ms(200);
46
move(0,0);
47
delay_ms(2000);
48
}
49

  
50
void turnLeft()
51
{
52
motor_l_set(BACKWARD, 200);
53
motor_r_set(FORWARD, 200);
54
delay_ms(400);
55
}
trunk/code/projects/linefollowing/lineFollow.c
1 1
#include "lineFollow.h"
2 2

  
3
#define NOBARCODE -2
4
#define CODESIZE 2
3 5

  
6
int countHi = 0;
7
int countLo = 0;
8
int maxAvg, avg;
9
int barCode[ CODESIZE ];
10
int barCodePosition=0;
4 11

  
5 12
void lineFollow_init()
6 13
{
......
24 31
	{
25 32
		if(lost++>20)
26 33
		{
27
			orb1_set_color(GREEN);
28 34
			orb2_set_color(GREEN);
29 35
			motors_off();
30 36
			return LINELOST;
......
34 40
	else
35 41
	{
36 42
		position*=30;
37
		orb1_set_color(GREEN);
38 43
		orb2_set_color(ORB_OFF);
39 44
		motorLeft(min(SPEED+position, 255));
40 45
		motorRight(min(SPEED-position, 255));
41 46
		lost=0;
42 47
	}
43
	return 0;
48
	updateBarCode();
49
	return getBarCode();
44 50
}
45 51

  
52
int getBarCode(){
53
	if(barCodePosition!=CODESIZE) return NOBARCODE ;
54
	int temp = 0;
55
	int i;
56
	for(i=0; i<CODESIZE; i++)
57
		temp += (barCode[i] << i);
58
	barCodePosition = 0;
59
	return temp;
60
}
46 61

  
47 62

  
48 63

  
......
90 105
}
91 106

  
92 107

  
93
int updateBarCode()
108
void updateBarCode()
94 109
{
95 110
	//maps the sensors to the analog input ports
96 111
	int ports[2] = {8,1};
97
	
98
	return analog_get10(ports[1]) | analog10(ports[2]);
112
	int current[2];
113
//	current[0] = analog_get10(8);
114
	current[1] = analog_get10(1);
115
//	usb_puti(analog_get10(8));
116
//	usb_putc('\t');
117
	usb_puti(analog_get10(1));
118
	usb_putc('\n');
119

  
120
	if(current[1]>500)
121
	{
122
		if(countHi++==0) 
123
		{
124
			avg = 500;
125
			maxAvg = 500;
126
		}
127
		else
128
		{
129
			avg = 3*avg + current[1];
130
			avg/=4;
131
			maxAvg = max(maxAvg, avg);
132
		}
133
	}
134
	else if(countHi>10)
135
	{
136
		if(countLo++>15)
137
		{
138
			countHi=countLo=0;
139
		//	if(maxAvg>825)orb1_set_color(RED);
140
		//	else orb1_set_color(BLUE);
141
			barCode[barCodePosition++] = (maxAvg > 825 ? 2:1)-1;
142
		}
143
	}
144
	else countHi/=2;
145
	if(countHi==0)countLo=0; 
99 146
}
100 147

  
101 148

  
102 149
int min(int x, int y){return x>y ? y : x;}
150
int max(int x, int y){return x<y ? y : x;}
103 151

  
104 152
void motorLeft(int speed){
105 153
	speed-=127;

Also available in: Unified diff