-
Notifications
You must be signed in to change notification settings - Fork 0
/
lasersensor.cpp
155 lines (130 loc) · 4.37 KB
/
lasersensor.cpp
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
#include <stdio.h>
#include <math.h>
#include "includes/lasersensor.h"
#include "includes/robotconnector.h"
#define MAX_DISTANCE_FOR_CONNECTED_POINTS 0.1
#define MAX_PILLAR_SIZE 0.1
#define ANGLE(x) ((double)x / 180.0 * M_PI)
static inline point<double> getPointFromLaser(const double dist, const double angle)
{
point<double> newPoint;
newPoint.x = dist * cos(angle);
newPoint.y = dist * sin(angle);
return newPoint;
}
static double getLength(const point<double> p)
{
return sqrt(pow(p.x, 2) + pow(p.y, 2));
}
static double distanceBetweenPoints(const point<double> a, const point<double> b)
{
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
static point<double> getPointFromLaserIndex(const int index)
{
const double laserAngle = ((double) LASER_SEARCH_ANGLE / MAX_LASER_COUNT) * index;
return getPointFromLaser(laserpar[index], ANGLE(laserAngle));
}
static std::vector<std::vector<point<double>>*>* getUnknownLaserObjects(const int startIndex, const int endIndex)
{
std::vector<std::vector<point<double>>*>* unknownObjects = new std::vector<std::vector<point<double>>*>;
bool isLaserUsed[MAX_LASER_COUNT] = { 0 };
for (int i = startIndex; i < endIndex; ++i)
{
if (laserpar[i] > MIN_LASER_DISTANCE && !isLaserUsed[i])
{
std::vector<point<double>> *objectPositions = new std::vector<point<double>>;
objectPositions->push_back(getPointFromLaserIndex(i));
isLaserUsed[i] = true;
for (int z = i + 1; z < endIndex; ++z)
{
if (laserpar[z] > MIN_LASER_DISTANCE)
{
const point<double> obstaclePos = getPointFromLaserIndex(z);
if (distanceBetweenPoints(objectPositions->back(), obstaclePos) <= MAX_DISTANCE_FOR_CONNECTED_POINTS * getLength(objectPositions->back()))
{
objectPositions->push_back(obstaclePos);
isLaserUsed[z] = true;
}
}
}
if (objectPositions->size() > 1)
{
unknownObjects->push_back(objectPositions);
}
else
{
delete objectPositions;
}
}
}
return unknownObjects;
}
static laserObjects* categorizeUnknownLaserObjects(const std::vector<std::vector<point<double>>*>& unknownObjects)
{
laserObjects* objects = new laserObjects;
for (unsigned int unknownObjectIndex = 0; unknownObjectIndex < unknownObjects.size(); ++unknownObjectIndex)
{
const std::vector<point<double>> unknownObject = *unknownObjects[unknownObjectIndex];
//shitty solution but it works for straight walls
const bool isPillar = distanceBetweenPoints(unknownObject.front(), unknownObject.back()) <= MAX_PILLAR_SIZE;
if (isPillar)
{
pillar* newPillar = new pillar;
point<double> pointsSum = { 0 };
for (unsigned int i = 0; i < unknownObject.size(); ++i)
{
pointsSum = pointsSum + unknownObject[i];
}
pointsSum = pointsSum / unknownObject.size();
newPillar->pos = pointsSum;
point<double> nearestPos = unknownObject[0];
for (unsigned int i = 1; i < unknownObject.size(); ++i)
{
if (getLength(nearestPos) > getLength(unknownObject[i]))
{
nearestPos = unknownObject[i];
}
}
newPillar->nearestPos = nearestPos;
newPillar->points = new point<double>[unknownObject.size()];
std::copy(unknownObject.begin(), unknownObject.end(), newPillar->points);
newPillar->pointsCount = unknownObject.size();
objects->pillars.push_back(newPillar);
}
else
{
wall* newWall = new wall;
newWall->startPos = unknownObject.front();
newWall->endPos = unknownObject.back();
point<double> nearestPos = unknownObject[0];
for (unsigned int i = 1; i < unknownObject.size(); ++i)
{
if (getLength(nearestPos) > getLength(unknownObject[i]))
{
nearestPos = unknownObject[i];
}
}
newWall->nearestPos = nearestPos;
newWall->points = new point<double>[unknownObject.size()];
std::copy(unknownObject.begin(), unknownObject.end(), newWall->points);
newWall->pointsCount = unknownObject.size();
objects->walls.push_back(newWall);
}
delete unknownObjects[unknownObjectIndex];
}
return objects;
}
laserObjects* getLaserObjects(const int startAngle, const int searchAngle)
{
const int startIndex = 0;
const int endIndex = MAX_LASER_COUNT;
const std::vector<std::vector<point<double>>*>* unknownObjects = getUnknownLaserObjects(startIndex, endIndex);
laserObjects* categorizedObjects = categorizeUnknownLaserObjects(*unknownObjects);
delete unknownObjects;
return categorizedObjects;
}
double getLaserDistance(enum LaserDistance l)
{
return laserpar[l];
}