-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtargetdetection.c
61 lines (55 loc) · 2.23 KB
/
targetdetection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
This program detects the target (which outputs an IR signal), using an IR sensor to test the functionality of the IR sensor.
The robot with spin until it senses the the IR signal, then it will stop
*/
#pragma config(Sensor, in1, irsignal, sensorReflection)
#pragma config(Sensor, dgtl1, button, sensorTouch)
#pragma config(Sensor, dgtl5, redlight, sensorDigitalOut)
#pragma config(Motor, port1, rightmotor, tmotorVex393_HBridge, openLoop)
#pragma config(Motor, port2, leftmotor, tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
//function to determine if the IR sensor is detecting the target, returns true if we are, false if not
bool detection(){
//initialize 2 variables for the values recorded by IR sensor
int min = SensorValue[irsignal];
int max = SensorValue[irsignal];
/*
Since the value read by the IR sensor varies even when it's not detecting the target,
and it varies more when we sense the target, we need to find when the there is more variance.
To do this, we go through a quick loop (0.05 * 5 seconds) and retrieve the maximum and minimum read values during the 5 increments of the loop.
If these values have a variance past 80 (number determined through trial and error), we are detecting the target and should return true.
If not, we are not detecting the target, and the function returns false
*/
int i = 0;
while( i<5 ){
if( SensorValue[irsignal] < min ) {
min = SensorValue[irsignal];
}else if( SensorValue[irsignal] > max ) {
max = SensorValue[irsignal];
}
wait1Msec(50);
i++;
}
//difference between max and min is the variance
if ( (max-min)>80 ){
return true;
}else{
return false;
}
}
task main(){
//initialize the process by clicking the button
while( SensorValue[button] == 0 ){
motor[leftmotor] = 0;
motor[rightmotor] = 0;
}
//continuously call the detection function until it detects the siganl
while(!detection()){
//robot spins
motor[rightmotor] = 30;
motor[leftmotor] = -30;
}
//IR signal faces the target, stop spinning
motor[rightmotor] = 0;
motor[leftmotor] = 0;
}