Project

General

Profile

Revision 1851

Added by Dan Jacobs over 12 years ago

Added turning

View differences:

trunk/code/projects/linefollowing/lineFollow.h
8 8
#define BLACK	 		2
9 9
#define CENTER			3
10 10
#define NOLINE			-42
11
#define SPEED			170
12 11
#define LINELOST		-1
13 12

  
14 13
/* 	lineFollow_init
......
21 20
	Must call lineFollow first
22 21
	Must be called inside a loop
23 22
*/
24
int lineFollow();
23
int lineFollow(int speed);
25 24

  
26
/*	assignColor
27
	Converts an analog color to WHITE or BLACK
25
/*	turnLeft and turnRight
26
	Must be called inside a loop
27
	returns 0 when complete
28 28
*/
29
int assignColor(int port, int analog);
29
int turnLeft();
30
int turnRight();
30 31

  
31
/*	updateIR
32
	Gets the value of the nth line sensor
33
*/
34
int updateIR(int n);
35

  
36 32
/*	updateLine
33
	Reads in the analog values
37 34
	Fills the given array with WHITE
38 35
	or BLACK representing the line
39 36
*/
trunk/code/projects/linefollowing/main.c
12 12
	int barCode;
13 13
	while(1)
14 14
	{
15
		barCode = lineFollow();
15
		//for(int q=0; q<500; q++)
16
			barCode = lineFollow(200);
17
		if(barCode==-25)while(turnLeft());
18
		continue;
16 19
		if(barCode != -2 && barCode != LINELOST)
17 20
		{
18 21
			usb_puti(barCode);
......
53 56
	}
54 57
	return 0;
55 58
}
56
void turnRight()
59
void right()
57 60
{
58 61
motor_r_set(BACKWARD, 200);
59 62
motor_l_set(FORWARD, 200);
......
70 73
delay_ms(2000);
71 74
}
72 75

  
73
void turnLeft()
76
void left()
74 77
{
75 78
motor_l_set(BACKWARD, 200);
76 79
motor_r_set(FORWARD, 200);
trunk/code/projects/linefollowing/lineFollow.c
2 2

  
3 3
#define NOBARCODE -2
4 4
#define CODESIZE 5 
5
#define INTERSECTION -25
6
#define FULL_LINE -26
5 7

  
6 8
int countHi = 0;
7 9
int countLo = 0;
......
9 11
int barCode[ CODESIZE ];
10 12
int barCodePosition=0;
11 13

  
14
int turnDistance=0;
15
int intersection=0;
16
int disableBarCode=0;
17

  
18

  
12 19
void lineFollow_init()
13 20
{
14 21
	analog_init(0);
15 22
	lost = 0;
23
	intersection=0;
24
	disableBarCode=0;
16 25
}
17 26

  
18 27

  
19 28

  
20
int lineFollow()
29
int lineFollow(int speed)
21 30
{
22
	int colors[6];
31
	int colors[5];
23 32
	int position;
24 33
	
25 34

  
......
36 45
			return LINELOST;
37 46
		}
38 47
	}
48
	else if(position == FULL_LINE)
49
	{
50
		if(intersection++>4)
51
		{
52
			orb2_set_color(RED);
53
			barCodePosition=0;
54
			disableBarCode=15;
55
			return INTERSECTION;
56
		}
57
	}
39 58
	//on line
40 59
	else
41 60
	{
42 61
		position*=30;
43 62
		orb2_set_color(ORB_OFF);
44
		motorLeft(min(SPEED+position, 150));
45
		motorRight(min(SPEED-position, 150));
63
		motorLeft(min(speed+position, 255));
64
		motorRight(min(speed-position, 255));
46 65
		lost=0;
66
		intersection=0;
47 67
	}
68

  
69
	if(disableBarCode)
70
	{
71
		disableBarCode--;
72
		return NOBARCODE;
73
	}
48 74
	updateBarCode();
49 75
	return getBarCode();
50 76
}
51 77

  
52
int getBarCode(){
78

  
79
int turnLeft()
80
{
81
	motor_l_set(BACKWARD, 200);
82
	motor_r_set(FORWARD, 200);
83
	int colors[5];
84
	updateLine(colors);
85
	int position = lineLocate(colors);
86
	if(position==NOLINE)turnDistance++;
87
	else if(turnDistance>10) return 0;//turnDistance = 0;
88
	return 1;
89
}
90

  
91
int turnRight()
92
{
93
        motor_r_set(BACKWARD, 200);
94
        motor_l_set(FORWARD, 200);
95
        int colors[5];
96
        updateLine(colors);
97
        int position = lineLocate(colors);
98
        if(position==NOLINE)turnDistance++;
99
        else if(turnDistance>10) return 0;//turnDistance = 0;
100
        return 1;
101
}
102

  
103

  
104

  
105

  
106
int getBarCode()
107
{
53 108
	if(barCodePosition!=CODESIZE) return NOBARCODE ;
54 109
	int temp = 0;
55 110
	int i;
......
63 118

  
64 119
void updateLine(int* values)
65 120
{	
121
	int ports[5] = {13, 12, 3, 2, 9};
66 122
	for(int i = 0; i<5; i++)
67
		values[i] = assignColor(i, updateIR(i));
123
		values[i] = analog_get10(ports[i])<150 ? WHITE : BLACK;
68 124
}
69 125

  
70 126

  
71
int updateIR(int n)
72
{
73
	//maps the sensors to the analog input ports
74
	int ports[5] = {13, 12, 3, 2, 9};
75 127

  
76
	return analog_get10(ports[n]);
77
}
78

  
79

  
80

  
81
int assignColor(int port, int analog)
82
{
83
	if (analog < 150)
84
		return  WHITE;
85
	else
86
		return  BLACK;
87
}
88

  
89

  
90 128
int lineLocate(int* colors)
91 129
{
92 130
	int i;
......
99 137
		wsum += (i)*colors[i];
100 138
	}
101 139
	if(count==0)
102
			return NOLINE;	
103
		
140
		return NOLINE;	
141
	if(count==5)
142
		return FULL_LINE;
104 143
	return (wsum/count)-4;
105 144
}
106 145

  
......
138 177
			countHi=countLo=0;
139 178
			if(maxAvg>825)orb1_set_color(RED);
140 179
			else orb1_set_color(BLUE);
141
			barCode[barCodePosition++] = (maxAvg > 825 ? 2:1)-1;
180
			barCode[barCodePosition++] = maxAvg > 825 ? 1:0;
142 181
		}
143 182
	}
144 183
	else countHi/=2;
trunk/code/projects/linefollowing/Makefile
11 11
USE_WIRELESS = 1
12 12

  
13 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/tty.usbserial-*'; fi)
14
AVRDUDE_PORT = $(shell if uname -s |grep -i w32 >/dev/null; then echo 'COM4:'; else echo '/dev/ttyUSB0'; fi)
15 15

  
16 16
else
17 17
COLONYROOT := ../$(COLONYROOT)

Also available in: Unified diff