-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensor.cpp
More file actions
180 lines (147 loc) · 4.97 KB
/
Copy pathSensor.cpp
File metadata and controls
180 lines (147 loc) · 4.97 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "Sensor.h"
Sensor::Sensor(int distanceToCenter, Direction orientation){
this->distanceToCenter = distanceToCenter;
this->orientation = orientation;
this->vectorIndex = 0;
this->threadSafeLock = false;
// Fill vector with dummy measurements
for(int i = 0; i < SENSOR_MAX_VECTOR_SIZE; i++)
this->measurementVector.push_back(0);
Logger::getInstance().log("Sensor object constructed");
}
Sensor::~Sensor(){
Logger::getInstance().log("Sensor object destroyed");
}
void Sensor::update(int measurement){
// Find index of the oldest measurement
int indexToOverwrite;
if(this->vectorIndex - 1 == -1)
indexToOverwrite = SENSOR_MAX_VECTOR_SIZE - 1;
else indexToOverwrite = this->vectorIndex - 1;
// Overwrite oldest measurement
this->measurementVector[indexToOverwrite] = measurement + this->distanceToCenter;
// Update vectorIndex to point to the latest measurement
this->vectorIndex = indexToOverwrite;
}
float Sensor::calcAverage(){
int index = this->vectorIndex;
for(int i = 0; i < SENSOR_MAX_VECTOR_SIZE; i++){
// Gets the index to use
if(i != 0)
index = this->nextIndex(index);
// If last measurement is not deviating
// TODO
}
}
int Sensor::getLastMeasurement(){
return this->measurementVector[vectorIndex];
}
int Sensor::getPrevMeasurement(){
// Find index of previous index
int indexOfPrevMeasurement;
if(vectorIndex + 1 == SENSOR_MAX_VECTOR_SIZE)
indexOfPrevMeasurement = 0;
else indexOfPrevMeasurement = vectorIndex + 1;
return this->measurementVector[indexOfPrevMeasurement];
}
Direction Sensor::getOrientation(){
return this->orientation;
}
bool Sensor::isLastMeasurementDeviating(){
// isDeviating when last measurement is higher/lower then the previous measurement by SENSOR_DEVIATION_THRESHOLD
int prevMeasurement = this->getPrevMeasurement();
if(this->getLastMeasurement() > (prevMeasurement + SENSOR_DEVIATION_THRESHOLD) || this->getLastMeasurement() < (prevMeasurement - SENSOR_DEVIATION_THRESHOLD))
return true;
return false;
}
CoordSet Sensor::getWallPos(const CoordSet currentPos, const Direction k9Orientation){
CoordSet wallPos;
// Calculate point in wall based on sensor orientation, k9 orientation and the current position
if(this->orientation == NORTH){ // Sensor orientation
switch(k9Orientation){ // k9 orientation
case NORTH: wallPos.x = currentPos.x;
wallPos.y = currentPos.y + this->getLastMeasurement();
break;
case EAST: wallPos.x = currentPos.x + this->getLastMeasurement();
wallPos.y = currentPos.y;
break;
case SOUTH: wallPos.x = currentPos.x;
wallPos.y = currentPos.y - this->getLastMeasurement();
break;
case WEST: wallPos.x = currentPos.x - this->getLastMeasurement();
wallPos.y = currentPos.y;
break;
}
}
if(this->orientation == EAST){ // Sensor orientation
switch(k9Orientation){ // k9 orientation
case NORTH: wallPos.x = currentPos.x + this->getLastMeasurement();
wallPos.y = currentPos.y;
break;
case EAST: wallPos.x = currentPos.x;
wallPos.y = currentPos.y - this->getLastMeasurement();
break;
case SOUTH: wallPos.x = currentPos.x - this->getLastMeasurement();
wallPos.y = currentPos.y;
break;
case WEST: wallPos.x = currentPos.x;
wallPos.y = currentPos.y + this->getLastMeasurement();
break;
}
}
if(this->orientation == SOUTH){ // Sensor orientation
switch(k9Orientation){ // k9 orientation
case NORTH: wallPos.x = currentPos.x;
wallPos.y = currentPos.y - this->getLastMeasurement();
break;
case EAST: wallPos.x = currentPos.x - this->getLastMeasurement();
wallPos.y = currentPos.y;
break;
case SOUTH: wallPos.x = currentPos.x;
wallPos.y = currentPos.y + this->getLastMeasurement();
break;
case WEST: wallPos.x = currentPos.x + this->getLastMeasurement();
wallPos.y = currentPos.y;
break;
}
}
if(this->orientation == WEST){ // Sensor orientation
switch(k9Orientation){ // k9 orientation
case NORTH: wallPos.x = currentPos.x - this->getLastMeasurement();
wallPos.y = currentPos.y;
break;
case EAST: wallPos.x = currentPos.x;
wallPos.y = currentPos.y + this->getLastMeasurement();
break;
case SOUTH: wallPos.x = currentPos.x + this->getLastMeasurement();
wallPos.y = currentPos.y;
break;
case WEST: wallPos.x = currentPos.x;
wallPos.y = currentPos.y - this->getLastMeasurement();
break;
}
}
return wallPos;
}
bool Sensor::getThreadSafeLock(){
return this->threadSafeLock;
}
void Sensor::setThreadSafeLock(bool lock){
this->threadSafeLock = lock;
}
int Sensor::nextIndex(int lastIndex){
int nextIndex = lastIndex++;
// Check if nextIndex is not pointing outside the array, if it does, nextIndex is 0
if(nextIndex < SENSOR_MAX_VECTOR_SIZE)
return nextIndex;
else
return 0;
}
int Sensor::prevIndex(int lastIndex){
int prevIndex = lastIndex--;
// Check if prevIndex is not pointing outside the array, if it does, prevIndex is SENSOR_MAX_VECTOR_SIZE - 1
if(prevIndex > -1)
return prevIndex;
else
return SENSOR_MAX_VECTOR_SIZE - 1;
}