-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReedsShepp.cpp
80 lines (58 loc) · 2.52 KB
/
ReedsShepp.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
#include "ReedsShepp.h"
#include "Config.h"
#include <cmath>
#include <iostream>
#include <limits>
#include <ompl/base/State.h>
namespace ob = ompl::base;
ReedsShepp::ReedsShepp() : reedsSheppSpace(TURNING_RADIUS) {}
std::vector<Node> ReedsShepp::computePath(const Node &start, const Node &goal,
int numPoints) {
std::vector<Node> path;
ob::State *rsStart = reedsSheppSpace.allocState();
ob::State *rsGoal = reedsSheppSpace.allocState();
rsStart->as<ob::SE2StateSpace::StateType>()->setXY(start.x, start.y);
rsStart->as<ob::SE2StateSpace::StateType>()->setYaw(start.theta * M_PI /
180.0);
rsGoal->as<ob::SE2StateSpace::StateType>()->setXY(goal.x, goal.y);
rsGoal->as<ob::SE2StateSpace::StateType>()->setYaw(goal.theta * M_PI / 180.0);
ob::ReedsSheppStateSpace::ReedsSheppPath rsPath =
reedsSheppSpace.reedsShepp(rsStart, rsGoal);
double totalLength = rsPath.length();
if (totalLength == std::numeric_limits<double>::infinity()) {
reedsSheppSpace.freeState(rsStart);
reedsSheppSpace.freeState(rsGoal);
return path;
}
double stepSize = totalLength / numPoints;
for (int i = 0; i <= numPoints; ++i) {
double s = i * stepSize;
if (s > totalLength) {
s = totalLength;
}
ob::State *state = reedsSheppSpace.allocState();
reedsSheppSpace.interpolate(rsStart, rsGoal, s / totalLength, state);
double x = state->as<ob::SE2StateSpace::StateType>()->getX();
double y = state->as<ob::SE2StateSpace::StateType>()->getY();
double theta =
state->as<ob::SE2StateSpace::StateType>()->getYaw() * 180.0 / M_PI;
path.emplace_back(x, y, theta);
reedsSheppSpace.freeState(state);
}
reedsSheppSpace.freeState(rsStart);
reedsSheppSpace.freeState(rsGoal);
return path;
}
double ReedsShepp::distance(const Node &start, const Node &goal) {
ob::State *rsStart = reedsSheppSpace.allocState();
ob::State *rsGoal = reedsSheppSpace.allocState();
rsStart->as<ob::SE2StateSpace::StateType>()->setXY(start.x, start.y);
rsStart->as<ob::SE2StateSpace::StateType>()->setYaw(start.theta * M_PI /
180.0);
rsGoal->as<ob::SE2StateSpace::StateType>()->setXY(goal.x, goal.y);
rsGoal->as<ob::SE2StateSpace::StateType>()->setYaw(goal.theta * M_PI / 180.0);
double totalLength = reedsSheppSpace.reedsShepp(rsStart, rsGoal).length();
reedsSheppSpace.freeState(rsStart);
reedsSheppSpace.freeState(rsGoal);
return totalLength;
}