Skip to content

Commit

Permalink
[UI] Improve undefined-safety when sampling a trajectory. (SleipnirGr…
Browse files Browse the repository at this point in the history
  • Loading branch information
shueja authored Jan 7, 2025
1 parent e92edd4 commit 7a8e2ba
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/components/field/svg/FieldEventMarkers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class FieldEventMarkers extends Component<Props, State> {
return [];
}
const marked = sample(marker.from.timestamp, path.trajectory.samples);
if (marked === undefined) {
return <></>;
}
return (
<FieldEventMarker
key={marker.uuid}
Expand Down
2 changes: 1 addition & 1 deletion src/components/field/svg/InterpolatedRobot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class InterpolatedRobot extends Component<Props, State> {
return <></>;
}
const pose1 = sample(this.props.timestamp, trajectory);

if (pose1 === undefined) return <></>;
const headingPointSideLength =
targetSideLength *
Math.min(doc.robotConfig.bumper.length, doc.robotConfig.bumper.width);
Expand Down
7 changes: 5 additions & 2 deletions src/util/MathUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ export function interpolate(p1: Pose, p2: Pose, frac: number) {
export function sample(
timeSeconds: number,
m_states: Array<SwerveSample> | Array<DifferentialSample>
): Pose {
if (timeSeconds <= m_states[0].t) {
): Pose | undefined {
if (m_states.length == 0) {
return undefined;
}
if (m_states.length == 1 || timeSeconds <= m_states[0].t) {
return storeToPose(m_states[0]);
}
if (timeSeconds >= m_states[m_states.length - 1].t) {
Expand Down

0 comments on commit 7a8e2ba

Please sign in to comment.