Skip to content

Commit 5c205e1

Browse files
committed
reorganize and restructure
1 parent 82a15da commit 5c205e1

File tree

5 files changed

+363
-245
lines changed

5 files changed

+363
-245
lines changed
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
package com.github.mittyrobotics.pathfollowing;
2+
3+
import java.util.ArrayList;
4+
5+
public class Path {
6+
/**
7+
* Associated parametric and motion profile parameters
8+
*/
9+
protected Parametric parametric;
10+
protected double maxAcceleration, maxVelocity, startVelocity, endVelocity, maxDeceleration, maxAngularVelocity;
11+
12+
/**
13+
* Conversion constants
14+
*/
15+
public static final double TO_METERS = 0.0254;
16+
public static final double TO_INCHES = 39.3700787401;
17+
18+
/**
19+
* Various helper variables
20+
*/
21+
protected double prevVelocity, distanceTraveled, closestPointT, distanceToEnd, maxVelocityToEnd, velocity, purePursuitRadius;
22+
protected boolean turnRight;
23+
protected ArrayList<Vector2D> previewVelocities = new ArrayList<>();
24+
25+
/**
26+
* Create a new path with motion profile
27+
* @param parametric parametric associated with path
28+
* @param maxAcceleration max acceleration of motion profile
29+
* @param maxDeceleration max deceleration of motion profile
30+
* @param maxVelocity max velocity of motion profile
31+
* @param maxAngularVelocity max angular velocity of motion profile
32+
* @param startVelocity starting velocity of motion profile
33+
* @param endVelocity desired ending of motion profile
34+
*/
35+
public Path(Parametric parametric, double maxAcceleration, double maxDeceleration, double maxVelocity, double maxAngularVelocity, double startVelocity, double endVelocity) {
36+
this.parametric = parametric;
37+
this.maxAcceleration = maxAcceleration;
38+
this.maxDeceleration = maxDeceleration;
39+
this.maxVelocity = maxVelocity;
40+
this.maxAngularVelocity = maxAngularVelocity;
41+
this.endVelocity = endVelocity;
42+
43+
this.startVelocity = startVelocity;
44+
this.prevVelocity = startVelocity;
45+
46+
distanceToEnd = parametric.getLength();
47+
}
48+
49+
/**
50+
* Create a new path with motion profile, max angular velocity is infinity, max deceleration is equal to max acceleration
51+
* @param parametric parametric associated with path
52+
* @param maxAcceleration max acceleration of motion profile
53+
* @param maxVelocity max velocity of motion profile
54+
* @param startVelocity starting velocity of motion profile
55+
* @param endVelocity desired ending of motion profile
56+
*/
57+
public Path(Parametric parametric, double maxAcceleration, double maxVelocity, double startVelocity, double endVelocity) {
58+
this(parametric, maxAcceleration, maxAcceleration, maxVelocity, Double.POSITIVE_INFINITY, startVelocity, endVelocity);
59+
}
60+
61+
/**
62+
* Create a new path with motion profile, max angular velocity is infinity, max deceleration is equal to max acceleration, starting and ending velocities are 0
63+
* @param parametric parametric associated with path
64+
* @param maxAcceleration max acceleration of motion profile
65+
* @param maxVelocity max velocity of motion profile
66+
*/
67+
public Path(Parametric parametric, double maxAcceleration, double maxVelocity) {
68+
this(parametric, maxAcceleration, maxVelocity, 0, 0);
69+
}
70+
71+
/**
72+
* Clear previewed velocities that have passed
73+
*/
74+
public void clearOldPreviewed() {
75+
previewVelocities.removeIf(previewVelocity -> previewVelocity.getY() <= distanceTraveled);
76+
}
77+
78+
/**
79+
* Returns the maximum possible velocity from velocity previews
80+
* @return the maximum possible velocity from velocity previews
81+
*/
82+
public double getMaxVelocityFromPreviews() {
83+
double min = Double.POSITIVE_INFINITY;
84+
85+
for(Vector2D vel : previewVelocities) {
86+
//get max possible current velocity given future velocity and distance from future velocity
87+
min = Math.min(min, maxVelocityFromDistance(vel.y-distanceTraveled, vel.x, maxDeceleration));
88+
}
89+
90+
return min;
91+
}
92+
93+
/**
94+
* Returns maximum current velocity given curvature
95+
* @param t current t parameter
96+
* @return maximum current velocity given curvature
97+
*/
98+
public double maxVelocityFromT(double t) {
99+
return maxVelocityFromRadius(1/(getCurvature(t)));
100+
}
101+
102+
/**
103+
* Return the distance needed to get from current velocity to end velocity
104+
* @param curVelocity current velocity
105+
* @param endVelocity desired end velocity
106+
* @param maxDeceleration maximum deceleration of the motion profile
107+
* @return the distance needed to get from current velocity to end velocity
108+
*/
109+
public double distanceToSlowdown(double curVelocity, double endVelocity, double maxDeceleration) {
110+
return (curVelocity * curVelocity - endVelocity * endVelocity) / 2 * maxDeceleration;
111+
}
112+
113+
/**
114+
* Return the distance of a {@link Pose2D} from the spline
115+
* @param parametric parametric to get distance from
116+
* @param robotPose {@link Pose2D} to get distance of
117+
* @param newtonsSteps number of steps to run Newton's method
118+
* @return the distance of a {@link Pose2D} from the spline
119+
*/
120+
public double distanceFromSpline(Parametric parametric, Pose2D robotPose, int newtonsSteps) {
121+
closestPointT = parametric.findClosestPointOnSpline(robotPose.getPosition(), newtonsSteps, 5);
122+
123+
return parametric.getPoint(closestPointT).distance(robotPose.getPosition());
124+
}
125+
126+
/**
127+
* Returns the curvature of the parametric at t
128+
* @param t t parameter to get curvature at
129+
* @return the curvature of the parametric at t
130+
*/
131+
public double getCurvature(double t) {
132+
return parametric.getCurvature(t);
133+
}
134+
135+
/**
136+
* Returns the curvature of the parametric at the closest point
137+
* @return the curvature of the parametric at the closest point
138+
*/
139+
public double getCurvature() {
140+
return parametric.getCurvature(closestPointT);
141+
}
142+
143+
/**
144+
* Returns the maximum possible velocity based on a distance from a desired velocity
145+
* @param distance distance from point of desired velocity
146+
* @param endVelocity desired velocity at point of calculation
147+
* @param maxDeceleration maximum deceleration of the motion profile
148+
* @return the maximum possible velocity based on a distance from a desired velocity
149+
*/
150+
public double maxVelocityFromDistance(double distance, double endVelocity, double maxDeceleration) {
151+
//vf^2 = vi^2 + 2ad, find vi (deceleration = -a)
152+
if(distance > 0) return Math.sqrt(endVelocity * endVelocity + 2 * maxDeceleration * distance);
153+
else return 0;
154+
}
155+
156+
/**
157+
* Returns maximum possible linear velocity given the radius of circle at a point
158+
* @param radius the radius of the circle
159+
* @return maximum possible linear velocity given the radius of circle at a point
160+
*/
161+
public double maxVelocityFromRadius(double radius) {
162+
if(Double.isInfinite(maxAngularVelocity)) return Double.POSITIVE_INFINITY;
163+
//l = w*r
164+
else return Math.abs(radius * maxAngularVelocity);
165+
}
166+
167+
/**
168+
* Returns the angular velocity at a point given the linear velocity
169+
* @param t t parameter of the point
170+
* @param linearVelocity the linear velocity
171+
* @return the angular velocity at a point given the linear velocity
172+
*/
173+
public double getAngularVelocityAtPoint(double t, double linearVelocity) {
174+
return linearVelocity * getCurvature(t);
175+
}
176+
177+
/**
178+
* Returns whether the path is finished based on the end threshold
179+
* @param robotPosition current robot {@link Pose2D}
180+
* @param threshold end threshold in meters
181+
* @return whether the path is finished based on the end threshold
182+
*/
183+
public boolean isFinished(Pose2D robotPosition, double threshold) {
184+
return (robotPosition.getPosition().distance(parametric.getPoint(1.0)) <= threshold) || distanceToEnd <= 0;
185+
}
186+
187+
/**
188+
* Returns the {@link Parametric} associated with the path
189+
* @return the {@link Parametric} associated with the path
190+
*/
191+
public Parametric getParametric() { return parametric; }
192+
193+
/**
194+
* Returns the maximum acceleration of the motion profile
195+
* @return the maximum acceleration of the motion profile
196+
*/
197+
public double getMaxAcceleration() {
198+
return maxAcceleration;
199+
}
200+
201+
/**
202+
* Returns the maximum velocity of the motion profile
203+
* @return the maximum velocity of the motion profile
204+
*/
205+
public double getMaxVelocity() {
206+
return maxVelocity;
207+
}
208+
209+
/**
210+
* Returns the start velocity of the motion profile
211+
* @return the start velocity of the motion profile
212+
*/
213+
public double getStartVelocity() {
214+
return startVelocity;
215+
}
216+
217+
/**
218+
* Returns the end velocity of the motion profile
219+
* @return the end velocity of the motion profile
220+
*/
221+
public double getEndVelocity() {
222+
return endVelocity;
223+
}
224+
225+
/**
226+
* Returns the maximum deceleration of the motion profile
227+
* @return the maximum deceleration of the motion profile
228+
*/
229+
public double getMaxDeceleration() {
230+
return maxDeceleration;
231+
}
232+
233+
/**
234+
* Returns the maximum angular velocity of the motion profile
235+
* @return the maximum angular velocity of the motion profile
236+
*/
237+
public double getMaxAngularVelocity() {
238+
return maxAngularVelocity;
239+
}
240+
241+
/**
242+
* Returns the radius of the tangent circle to the path at a t parameter
243+
* @param t t parameter to get radius of
244+
* @return the radius of the tangent circle to the path at a t parameter
245+
*/
246+
public double getRadius(double t) {
247+
return 1/getCurvature(t);
248+
}
249+
250+
}

0 commit comments

Comments
 (0)