Skip to content

Commit

Permalink
Display a different icon if the trajectory is changed since being gen…
Browse files Browse the repository at this point in the history
…erated (#1093)
  • Loading branch information
shueja authored Jan 3, 2025
1 parent d2557b9 commit cee8e40
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src-core/src/file_management/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type DeployPath = Arc<Mutex<PathBuf>>;
type TrajectoryFileName = String;

mod diagnostics;
mod formatter;

pub mod formatter;
pub mod upgrader;

pub use diagnostics::{create_diagnostic_file, get_log_lines};
Expand Down
74 changes: 67 additions & 7 deletions src-core/src/spec/trajectory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use trajoptlib::{DifferentialTrajectorySample, SwerveTrajectorySample};

use super::{upgraders::upgrade_traj_file, Expr, SnapshottableType};

#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
/// A waypoint parameter.
pub struct Waypoint<T: SnapshottableType> {
Expand Down Expand Up @@ -53,7 +53,7 @@ impl<T: SnapshottableType> Waypoint<T> {
}

/// A waypoint identifier.
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum WaypointID {
/// The first waypoint.
Expand Down Expand Up @@ -106,7 +106,7 @@ pub enum ConstraintScope {
}

/// A constraint on the robot's motion.
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
#[serde(tag = "type", content = "props")]
pub enum ConstraintData<T: SnapshottableType> {
/// A constraint on the maximum velocity.
Expand Down Expand Up @@ -211,7 +211,7 @@ impl<T: SnapshottableType> ConstraintData<T> {
}

/// A constraint on the robot's motion and where it applies.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub struct Constraint<T: SnapshottableType> {
/// The waypoint the constraint starts at.
pub from: WaypointID,
Expand All @@ -237,7 +237,7 @@ impl<T: SnapshottableType> Constraint<T> {
}

/// A constraint on the robot's motion and where it applies.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub struct ConstraintIDX<T: SnapshottableType> {
/// The index of the waypoint the constraint starts at.
pub from: usize,
Expand All @@ -252,7 +252,7 @@ pub struct ConstraintIDX<T: SnapshottableType> {

/// A sample of the robot's state at a point in time during the trajectory.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum Sample {
/// A sample for a swerve drive.
Expand Down Expand Up @@ -363,7 +363,7 @@ pub enum DriveType {
}

/// The parameters used for generating a trajectory.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Parameters<T: SnapshottableType> {
/// The waypoints the robot will pass through or use for initial guess.
Expand Down Expand Up @@ -441,6 +441,15 @@ impl TrajectoryFile {
let val = upgrade_traj_file(serde_json::from_str(content)?)?;
serde_json::from_value(val).map_err(Into::into)
}

pub fn up_to_date(&self) -> bool {
// Can't use is_some_and due to its move semantics.
if let Some(snap) = &self.snapshot {
snap == &self.params.snapshot()
} else {
false
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -482,3 +491,54 @@ pub enum PplibCommand {
commands: Vec<PplibCommand>,
},
}

#[cfg(test)]
mod tests {
use crate::spec::TRAJ_SCHEMA_VERSION;

use super::*;
fn test_trajectory() -> TrajectoryFile {
let parameters = Parameters::<Expr> {
waypoints: vec![Waypoint::<Expr> {
x: Expr::fill_in_value(0.0, "m"),
y: Expr::fill_in_value(0.0, "m"),
heading: Expr::fill_in_value(0.0, "rad"),
intervals: 20,
split: false,
fix_translation: false,
fix_heading: false,
override_intervals: false,
is_initial_guess: false,
}],
constraints: vec![],
target_dt: Expr::fill_in_value(0.05, "s"),
};
TrajectoryFile {
name: "Test".to_string(),
version: TRAJ_SCHEMA_VERSION,
snapshot: Some(parameters.snapshot()),
params: parameters,
trajectory: Trajectory {
sample_type: Some(DriveType::Swerve),
waypoints: Vec::new(),
samples: Vec::new(),
splits: Vec::new(),
},
events: Vec::new(),
}
}
#[test]
fn snapshot_equality() {
assert!(test_trajectory().up_to_date());
}

#[test]
fn snapshot_equality_through_serde() -> serde_json::Result<()> {
use crate::file_management::formatter;
let trajectory = test_trajectory();
let serde_trajectory = formatter::to_string_pretty(&trajectory)?;
let deser_trajectory = TrajectoryFile::from_content(serde_trajectory.as_str());
assert!(deser_trajectory.is_ok_and(|t| t.up_to_date()));
Ok(())
}
}
5 changes: 5 additions & 0 deletions src-tauri/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ pub async fn delete_trajectory(
debug_result!(file_management::delete_trajectory_file(&resources, trajectory).await);
}

#[tauri::command]
pub async fn trajectory_up_to_date(trajectory: TrajectoryFile) -> bool {
trajectory.up_to_date()
}

#[tauri::command]
pub async fn set_deploy_root(app_handle: tauri::AppHandle, dir: String) {
let resources = app_handle.state::<WritingResources>();
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/tauri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ pub fn run_tauri(project: Option<PathBuf>) {
open_project_dialog,
read_trajectory,
rename_trajectory,
trajectory_up_to_date,
set_deploy_root,
get_deploy_root,
requested_file,
Expand Down
26 changes: 24 additions & 2 deletions src/components/sidebar/PathSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { KeyboardArrowDown, Route, Settings } from "@mui/icons-material";
import {
KeyboardArrowDown,
Route,
Settings,
ShapeLine
} from "@mui/icons-material";
import DeleteIcon from "@mui/icons-material/Delete";
import {
CircularProgress,
Expand Down Expand Up @@ -83,6 +88,7 @@ class PathSelectorOption extends Component<OptionProps, OptionState> {
this.searchForName("");
const selected = this.props.uuid == doc.pathlist.activePathUUID;
const name = this.getPath().name;
const upToDate = this.getPath().ui.upToDate;
if (name != this.state.name && !this.state.renaming) {
this.state.name = name;
}
Expand All @@ -105,13 +111,29 @@ class PathSelectorOption extends Component<OptionProps, OptionState> {
}}
variant="indeterminate"
></CircularProgress>
) : (
) : upToDate ? (
<Route
className={styles.SidebarIcon}
htmlColor={
selected ? "var(--select-yellow)" : "var(--accent-purple)"
}
/>
) : (
<IconButton
className={styles.SidebarIcon}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
doc.generatePath(this.getPath().uuid);
}}
>
<ShapeLine
className={styles.SidebarIcon}
htmlColor={
selected ? "var(--select-yellow)" : "var(--accent-purple)"
}
></ShapeLine>
</IconButton>
)}

<TextField
Expand Down
3 changes: 3 additions & 0 deletions src/components/sidebar/Sidebar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@
height: var(--icon-size);
width: var(--icon-size);
}
.SidebarItem button.SidebarIcon {
margin: 0;
}
.SidebarItem .SidebarVerticalLine {
border-left: solid gray 1px;
height: calc(100% + var(--grid-gap));
Expand Down
8 changes: 6 additions & 2 deletions src/document/path/HolonomicPathStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ChoreoPathStore } from "./ChoreoPathStore";
import { ChoreoTrajectoryStore } from "./ChoreoTrajectoryStore";
import { PathUIStore } from "./PathUIStore";
import { findUUIDIndex } from "./utils";
import { Commands } from "../tauriCommands";
export function waypointIDToText(
id: WaypointUUID | undefined,
points: IHolonomicWaypointStore[]
Expand Down Expand Up @@ -60,7 +61,6 @@ export const HolonomicPathStore = types
name: "",
uuid: types.identifier
})

.views((self) => {
return {
canGenerate(): boolean {
Expand Down Expand Up @@ -162,6 +162,7 @@ export const HolonomicPathStore = types
}
});
self.setSnapshot(ser.snapshot);
self.ui.setUpToDate(true);
},
deserialize(ser: Trajectory) {
self.name = ser.name;
Expand Down Expand Up @@ -191,7 +192,10 @@ export const HolonomicPathStore = types
() => {
return self.serialize;
},
(_value) => {
(ser) => {
Commands.trajectoryUpToDate(ser).then((upToDate) =>
self.ui.setUpToDate(upToDate)
);
exporter(self.uuid);
}
);
Expand Down
8 changes: 7 additions & 1 deletion src/document/path/PathUIStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ export const PathUIStore = types
Array<SwerveSample> | Array<DifferentialSample>
>([]),
generating: false,
generationIterationNumber: 0
generationIterationNumber: 0,
upToDate: false
})
.actions((self) => ({
setUpToDate(upToDate: boolean) {
getEnv<Env>(self).withoutUndo(() => {
self.upToDate = upToDate;
});
},
setVisibleWaypointsStart(start: number) {
if (start <= self.visibleWaypointsEnd) {
self.visibleWaypointsStart = start;
Expand Down
7 changes: 7 additions & 0 deletions src/document/tauriCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ export const Commands = {
deleteTrajectory: (trajectory: Trajectory) =>
invoke<void>("delete_trajectory", { trajectory }),

/**
* Returns if the `Trajectory` parameters and snapshot are equivalent.
* @param trajectory The `Trajectory` to check
* @returns true if the parameters and snapshots are equivalent, false if not.
*/
trajectoryUpToDate: (trajectory: Trajectory) =>
invoke<boolean>("trajectory_up_to_date", { trajectory }),
/**
* If the application was opened via CLI and a file was specified, this will return the path of that file.
*
Expand Down

0 comments on commit cee8e40

Please sign in to comment.