Repository navigation
Expand file tree
/
Copy pathSensorSystem.pde
More file actions
101 lines (88 loc) · 3.85 KB
/
Copy pathSensorSystem.pde
File metadata and controls
101 lines (88 loc) · 3.85 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class SensorSystem {
//SensorSystem - alle bilens sensorer - ogå dem der ikke bruges af "hjernen"
//wall detectors
float sensorMag = 50;
float sensorAngle = PI*2/8;
PVector anchorPos = new PVector();
PVector sensorVectorFront = new PVector(0, sensorMag);
PVector sensorVectorLeft = new PVector(0, sensorMag);
PVector sensorVectorRight = new PVector(0, sensorMag);
boolean frontSensorSignal = false;
boolean leftSensorSignal = false;
boolean rightSensorSignal = false;
//crash detection
int whiteSensorFrameCount = 0; //udenfor banen
//clockwise rotation detection
PVector centerToCarVector = new PVector();
float lastRotationAngle = -1;
float clockWiseRotationFrameCounter = 0;
//lapTime calculation
boolean lastGreenDetection;
int lastTimeInFrames = 0;
int lapTimeInFrames = 10000;
void displaySensors() {
strokeWeight(0.5);
if (frontSensorSignal) {
fill(255, 0, 0);
ellipse(anchorPos.x+sensorVectorFront.x, anchorPos.y+sensorVectorFront.y, 8, 8);
}
if (leftSensorSignal) {
fill(255, 0, 0);
ellipse( anchorPos.x+sensorVectorLeft.x, anchorPos.y+sensorVectorLeft.y, 8, 8);
}
if (rightSensorSignal) {
fill(255, 0, 0);
ellipse( anchorPos.x+sensorVectorRight.x, anchorPos.y+sensorVectorRight.y, 8, 8);
}
line(anchorPos.x, anchorPos.y, anchorPos.x+sensorVectorFront.x, anchorPos.y+sensorVectorFront.y);
line(anchorPos.x, anchorPos.y, anchorPos.x+sensorVectorLeft.x, anchorPos.y+sensorVectorLeft.y);
line(anchorPos.x, anchorPos.y, anchorPos.x+sensorVectorRight.x, anchorPos.y+sensorVectorRight.y);
strokeWeight(2);
if (whiteSensorFrameCount>0) {
fill(whiteSensorFrameCount*10, 0, 0);
} else {
fill(0, clockWiseRotationFrameCounter, 0);
}
ellipse(anchorPos.x, anchorPos.y, 10, 10);
}
void updateSensorsignals(PVector pos, PVector vel) {
//Collision detectors
frontSensorSignal = get(int(pos.x+sensorVectorFront.x), int(pos.y+sensorVectorFront.y))==-1?true:false;
leftSensorSignal = get(int(pos.x+sensorVectorLeft.x), int(pos.y+sensorVectorLeft.y))==-1?true:false;
rightSensorSignal = get(int(pos.x+sensorVectorRight.x), int(pos.y+sensorVectorRight.y))==-1?true:false;
//Crash detector
color color_car_position = get(int(pos.x), int(pos.y));
if (color_car_position ==-1) {
whiteSensorFrameCount = whiteSensorFrameCount+1;
}
//Laptime calculation
boolean currentGreenDetection =false;
if (red(color_car_position)==0 && blue(color_car_position)==0 && green(color_car_position)!=0) {//den grønne målstreg er detekteret
currentGreenDetection = true;
}
if (lastGreenDetection && !currentGreenDetection) { //sidst grønt - nu ikke -vi har passeret målstregen
lapTimeInFrames = frameCount - lastTimeInFrames; //LAPTIME BEREGNES - frames nu - frames sidst
lastTimeInFrames = frameCount;
}
lastGreenDetection = currentGreenDetection; //Husker om der var grønt sidst
//count clockWiseRotationFrameCounter
centerToCarVector.set((height/2)-pos.x, (width/2)-pos.y);
float currentRotationAngle = centerToCarVector.heading();
float deltaHeading = lastRotationAngle - centerToCarVector.heading();
clockWiseRotationFrameCounter = deltaHeading>0 ? clockWiseRotationFrameCounter + 1 : clockWiseRotationFrameCounter -1;
lastRotationAngle = currentRotationAngle;
updateSensorVectors(vel);
anchorPos.set(pos.x,pos.y);
}
void updateSensorVectors(PVector vel) {
if (vel.mag()!=0) {
sensorVectorFront.set(vel);
sensorVectorFront.normalize();
sensorVectorFront.mult(sensorMag);
}
sensorVectorLeft.set(sensorVectorFront);
sensorVectorLeft.rotate(-sensorAngle);
sensorVectorRight.set(sensorVectorFront);
sensorVectorRight.rotate(sensorAngle);
}
}