|
14 | 14 | import org.team2363.helixnavigator.document.waypoint.HWaypoint;
|
15 | 15 | import org.team2363.helixnavigator.ui.prompts.RobotConfigDialog;
|
16 | 16 | import org.team2363.helixnavigator.ui.prompts.TransformDialog;
|
| 17 | +import org.team2363.helixtrajectory.HolonomicPath; |
| 18 | +import org.team2363.helixtrajectory.HolonomicTrajectory; |
| 19 | +import org.team2363.helixtrajectory.InvalidPathException; |
| 20 | +import org.team2363.helixtrajectory.Obstacle; |
| 21 | +import org.team2363.helixtrajectory.OptimalTrajectoryGenerator; |
| 22 | +import org.team2363.helixtrajectory.PluginLoadException; |
| 23 | +import org.team2363.helixtrajectory.SwerveDrivetrain; |
| 24 | +import org.team2363.helixtrajectory.TrajectoryGenerationException; |
17 | 25 |
|
18 | 26 | import com.jlbabilino.json.InvalidJSONTranslationConfiguration;
|
19 | 27 | import com.jlbabilino.json.JSONArray;
|
|
27 | 35 | import com.jlbabilino.json.JSONSerializerException;
|
28 | 36 |
|
29 | 37 | import javafx.beans.property.BooleanProperty;
|
| 38 | +import javafx.beans.property.ObjectProperty; |
30 | 39 | import javafx.beans.property.SimpleBooleanProperty;
|
| 40 | +import javafx.beans.property.SimpleObjectProperty; |
31 | 41 | import javafx.beans.value.ObservableValue;
|
| 42 | +import javafx.concurrent.Service; |
| 43 | +import javafx.concurrent.Task; |
32 | 44 | import javafx.geometry.Point2D;
|
33 | 45 | import javafx.scene.input.Clipboard;
|
34 | 46 | import javafx.scene.input.ClipboardContent;
|
@@ -382,6 +394,66 @@ public void paste() {
|
382 | 394 | }
|
383 | 395 | }
|
384 | 396 |
|
| 397 | + private static class TrajectoryGenerationService extends Service<HolonomicTrajectory> { |
| 398 | + |
| 399 | + // private final ObjectProperty<SwerveDrivetrain> drive = new SimpleObjectProperty<>(this, "drive", null); |
| 400 | + // private final ObjectProperty<HolonomicPath> path = new SimpleObjectProperty<>(this, "path", null); |
| 401 | + private SwerveDrivetrain drive = null; |
| 402 | + private HolonomicPath path = null; |
| 403 | + |
| 404 | + TrajectoryGenerationService() { |
| 405 | + } |
| 406 | + |
| 407 | + @Override |
| 408 | + protected Task<HolonomicTrajectory> createTask() { |
| 409 | + Task<HolonomicTrajectory> optimizeTask = new Task<HolonomicTrajectory>() { |
| 410 | + @Override |
| 411 | + protected HolonomicTrajectory call() throws PluginLoadException, InvalidPathException, TrajectoryGenerationException { |
| 412 | + System.out.println("Path optimizing: " + path.toString()); |
| 413 | + if (drive != null && path != null) { |
| 414 | + return OptimalTrajectoryGenerator.generate(drive, path); |
| 415 | + } else { |
| 416 | + throw new TrajectoryGenerationException("No path specified for optimization service."); |
| 417 | + } |
| 418 | + // System.out.println("Error generating path: " + e.getMessage() + System.lineSeparator()); |
| 419 | + } |
| 420 | + }; |
| 421 | + return optimizeTask; |
| 422 | + } |
| 423 | + } |
| 424 | + |
| 425 | + private static final TrajectoryGenerationService SERVICE = new TrajectoryGenerationService(); |
| 426 | + public void generateTrajectory() { |
| 427 | + if (documentManager.getIsDocumentOpen() && documentManager.getDocument().isPathSelected()) { |
| 428 | + HDocument hDocument = documentManager.getDocument(); |
| 429 | + HPath hPath = documentManager.getDocument().getSelectedPath(); |
| 430 | + SwerveDrivetrain drive = hDocument.getRobotConfiguration().toDrive(); |
| 431 | + List<Obstacle> obstacles = new ArrayList<>(hPath.getObstacles().size()); |
| 432 | + for (int i = 0; i < hPath.getObstacles().size(); i++) { |
| 433 | + obstacles.add(hPath.getObstacles().get(i).toObstacle()); |
| 434 | + } |
| 435 | + HolonomicPath path = hPath.toPath(obstacles); |
| 436 | + SERVICE.drive = drive; |
| 437 | + SERVICE.path = path; |
| 438 | + SERVICE.setOnSucceeded(workerState -> { |
| 439 | + HolonomicTrajectory traj = (HolonomicTrajectory) workerState.getSource().getValue(); |
| 440 | + hPath.setTrajectory(HTrajectory.fromTrajectory(traj)); |
| 441 | + }); |
| 442 | + if (!SERVICE.isRunning()) { |
| 443 | + SERVICE.restart(); |
| 444 | + } |
| 445 | + } |
| 446 | + } |
| 447 | + public void toggleTrajectoryGeneration() { |
| 448 | + if (SERVICE.isRunning()) { |
| 449 | + System.out.println("Cancelling trajopt"); |
| 450 | + SERVICE.cancel(); |
| 451 | + } else { |
| 452 | + System.out.println("Restarting trajopt"); |
| 453 | + generateTrajectory(); |
| 454 | + } |
| 455 | + } |
| 456 | + |
385 | 457 | private static final Rotate ROTATE_90_CLOCKWISE = new Rotate(-90);
|
386 | 458 | private static final Rotate ROTATE_90_COUNTERCLOCKWISE = new Rotate(90);
|
387 | 459 | private static final Rotate ROTATE_180 = new Rotate(180);
|
|
0 commit comments