-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot_path.cpp
More file actions
70 lines (53 loc) · 1.34 KB
/
robot_path.cpp
File metadata and controls
70 lines (53 loc) · 1.34 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
#include "robot_path.h"
#include <cmath>
float distance(const float x1, const float y1, const float x2, const float y2)
{
float a,b;
a = (x1-x2);
b = (y1-y2);
return std::sqrt(a*a + b*b);
}
RobotPath::RobotPath()
{
}
void RobotPath::insertElem(const long time, const float x, const float y)
{
std::lock_guard<std::mutex> lock(this->mutex);
path.push_back(PathElem(time, x, y));
}
void RobotPath::reset()
{
std::lock_guard<std::mutex> lock(this->mutex);
path.clear();
}
RobotPath::PathElem::PathElem(const long time, const float x, const float y)
: time(time)
, x(x)
, y(y)
{
}
float RobotPath::pointDistance(const RobotPath::PathElem &a, const RobotPath::PathElem &b)
{
return distance(a.x, a.y, b.x, b.y);
}
float RobotPath::calculate_displacement()
{
std::lock_guard<std::mutex> lock(this->mutex);
return pointDistance(path.front(), path.back());
}
float RobotPath::calculate_path()
{
float total = 0;
std::lock_guard<std::mutex> lock(this->mutex);
std::list<PathElem>::const_iterator iter = path.begin();
if (iter == path.end())
return 0;
std::list<PathElem>::const_iterator next = path.begin();
next++;
while (next!= path.end()) {
total += pointDistance(*iter, *next);
iter++;
next++;
}
return total;
}