-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalADStarAK.java
More file actions
158 lines (132 loc) · 5.32 KB
/
LocalADStarAK.java
File metadata and controls
158 lines (132 loc) · 5.32 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
package frc.robot.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.littletonrobotics.junction.LogTable;
import org.littletonrobotics.junction.Logger;
import org.littletonrobotics.junction.inputs.LoggableInputs;
import com.pathplanner.lib.path.GoalEndState;
import com.pathplanner.lib.path.PathConstraints;
import com.pathplanner.lib.path.PathPlannerPath;
import com.pathplanner.lib.path.PathPoint;
import com.pathplanner.lib.pathfinding.LocalADStar;
import com.pathplanner.lib.pathfinding.Pathfinder;
import edu.wpi.first.math.Pair;
import edu.wpi.first.math.geometry.Translation2d;
public class LocalADStarAK implements Pathfinder {
private final ADStarIO io = new ADStarIO();
/**
* Get if a new path has been calculated since the last time a path was
* retrieved
*
* @return True if a new path is available
*/
@Override
public boolean isNewPathAvailable() {
if (!Logger.hasReplaySource()) {
io.updateIsNewPathAvailable();
}
Logger.processInputs("LocalADStarAK", io);
return io.isNewPathAvailable;
}
/**
* Get the most recently calculated path
*
* @param constraints The path constraints to use when creating the path
* @param goalEndState The goal end state to use when creating the path
* @return The PathPlannerPath created from the points calculated by the
* pathfinder
*/
@Override
public PathPlannerPath getCurrentPath(PathConstraints constraints, GoalEndState goalEndState) {
if (!Logger.hasReplaySource()) {
io.updateCurrentPathPoints(constraints, goalEndState);
}
Logger.processInputs("LocalADStarAK", io);
if (io.currentPathPoints.isEmpty()) {
return null;
}
return PathPlannerPath.fromPathPoints(io.currentPathPoints, constraints, goalEndState);
}
/**
* Set the start position to pathfind from
*
* @param startPosition Start position on the field. If this is within an
* obstacle it will be
* moved to the nearest non-obstacle node.
*/
@Override
public void setStartPosition(Translation2d startPosition) {
if (!Logger.hasReplaySource()) {
io.adStar.setStartPosition(startPosition);
}
}
/**
* Set the goal position to pathfind to
*
* @param goalPosition Goal position on the field. f this is within an obstacle
* it will be moved
* to the nearest non-obstacle node.
*/
@Override
public void setGoalPosition(Translation2d goalPosition) {
if (!Logger.hasReplaySource()) {
io.adStar.setGoalPosition(goalPosition);
}
}
/**
* Set the dynamic obstacles that should be avoided while pathfinding.
*
* @param obs A List of Translation2d pairs representing obstacles.
* Each Translation2d represents
* opposite corners of a bounding box.
* @param currentRobotPos The current position of the robot. This is needed to
* change the start
* position of the path to properly avoid obstacles
*/
@Override
public void setDynamicObstacles(
List<Pair<Translation2d, Translation2d>> obs, Translation2d currentRobotPos) {
if (!Logger.hasReplaySource()) {
io.adStar.setDynamicObstacles(obs, currentRobotPos);
}
}
private static class ADStarIO implements LoggableInputs {
public LocalADStar adStar = new LocalADStar();
public boolean isNewPathAvailable = false;
public List<PathPoint> currentPathPoints = Collections.emptyList();
@Override
public void toLog(LogTable table) {
table.put("IsNewPathAvailable", isNewPathAvailable);
double[] pointsLogged = new double[currentPathPoints.size() * 2];
int idx = 0;
for (PathPoint point : currentPathPoints) {
pointsLogged[idx] = point.position.getX();
pointsLogged[idx + 1] = point.position.getY();
idx += 2;
}
table.put("CurrentPathPoints", pointsLogged);
}
@Override
public void fromLog(LogTable table) {
isNewPathAvailable = table.get("IsNewPathAvailable", false);
double[] pointsLogged = table.get("CurrentPathPoints", new double[0]);
List<PathPoint> pathPoints = new ArrayList<>();
for (int i = 0; i < pointsLogged.length; i += 2) {
pathPoints.add(new PathPoint(new Translation2d(pointsLogged[i], pointsLogged[i + 1]), null));
}
currentPathPoints = pathPoints;
}
public void updateIsNewPathAvailable() {
isNewPathAvailable = adStar.isNewPathAvailable();
}
public void updateCurrentPathPoints(PathConstraints constraints, GoalEndState goalEndState) {
PathPlannerPath currentPath = adStar.getCurrentPath(constraints, goalEndState);
if (currentPath != null) {
currentPathPoints = currentPath.getAllPathPoints();
} else {
currentPathPoints = Collections.emptyList();
}
}
}
}