Skip to content

Commit

Permalink
Merge branch 'Mechanical-Advantage:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
sswadkar authored Feb 18, 2023
2 parents 77a2ebf + ef75dd0 commit df1fc5a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "advantagescope",
"productName": "AdvantageScope",
"version": "2.2.1",
"version": "2.2.2",
"description": "Robot telemetry application for FRC",
"author": {
"name": "FRC 6328",
Expand Down
32 changes: 19 additions & 13 deletions src/hub/Sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ export default class Sidebar {
childSpan.style.setProperty("--indent", (indent + this.INDENT_SIZE_PX).toString() + "px");
childSpan.hidden = true;

let firstExpand = true;
let setExpanded = (expanded: boolean) => {
// Update icon and span display
childSpan.hidden = !expanded;
closedIcon.style.display = expanded ? "none" : "initial";
openIcon.style.display = expanded ? "initial" : "none";
Expand All @@ -264,24 +266,28 @@ export default class Sidebar {
} else {
this.expandedFields.delete(fullTitle);
}

// Add children if first time
if (firstExpand) {
firstExpand = false;
let childKeys = Object.keys(field.children);
if (fullTitle == "/AdvantageKit") {
// Apply hidden and known keys
childKeys = childKeys
.filter((key) => !this.HIDDEN_KEYS.includes(key))
.sort((a, b) => this.sortKeys(a, b, true));
} else {
childKeys = childKeys.sort((a, b) => this.sortKeys(a, b));
}
childKeys.forEach((key) => {
this.addFields(key, fullTitle + "/" + key, field.children[key], childSpan, indent + this.INDENT_SIZE_PX);
});
}
};

closedIcon.addEventListener("click", () => setExpanded(true));
openIcon.addEventListener("click", () => setExpanded(false));
if (this.expandedFields.has(fullTitle)) setExpanded(true);

let childKeys = Object.keys(field.children);
if (fullTitle == "/AdvantageKit") {
// Apply hidden and known keys
childKeys = childKeys
.filter((key) => !this.HIDDEN_KEYS.includes(key))
.sort((a, b) => this.sortKeys(a, b, true));
} else {
childKeys = childKeys.sort((a, b) => this.sortKeys(a, b));
}
childKeys.forEach((key) => {
this.addFields(key, fullTitle + "/" + key, field.children[key], childSpan, indent + this.INDENT_SIZE_PX);
});
}
}

Expand Down
63 changes: 52 additions & 11 deletions src/hub/tabControllers/LineGraphController.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { AllColors } from "../../shared/Colors";
import { LineGraphState } from "../../shared/HubState";
import LoggableType from "../../shared/log/LoggableType";
import { getLogValueText } from "../../shared/log/LogUtil";
import { LogValueSetAny, LogValueSetNumber } from "../../shared/log/LogValueSets";
import TabType from "../../shared/TabType";
import { convertWithPreset, UnitConversionPreset } from "../../shared/units";
import { cleanFloat, scaleValue, shiftColor } from "../../shared/util";
import { LineGraphState } from "../../shared/HubState";
import { clampValue, cleanFloat, scaleValue, shiftColor } from "../../shared/util";
import ScrollSensor from "../ScrollSensor";
import { SelectionMode } from "../Selection";
import TabController from "../TabController";

export default class LineGraphController implements TabController {
private MIN_ZOOM_TIME = 0.05;
private ZOOM_BASE = 1.001;
private MIN_AXIS_RANGE = 1e-5;
private MAX_AXIS_RANGE = 1e9;
private MAX_VALUE = 1e9;

private CONTENT: HTMLElement;
private LEGEND_ITEM_TEMPLATE: HTMLElement;
Expand Down Expand Up @@ -555,12 +558,38 @@ export default class LineGraphController implements TabController {
if (lockedRange != null) {
targetRange = lockedRange;
} else if (valueRange != null && marginProportion != null) {
let margin = (valueRange[1] - valueRange[0]) * marginProportion;
targetRange = [valueRange[0] - margin, valueRange[1] + margin];
if (targetRange[0] == targetRange[1]) {
targetRange[0]--;
targetRange[1]++;
// Apply extreme limits
let adjustedValueRange = [...valueRange];
if (adjustedValueRange[0] > this.MAX_VALUE) {
adjustedValueRange[0] = this.MAX_VALUE;
}
if (adjustedValueRange[1] > this.MAX_VALUE) {
adjustedValueRange[1] = this.MAX_VALUE;
}
if (adjustedValueRange[0] < -this.MAX_VALUE) {
adjustedValueRange[0] = -this.MAX_VALUE;
}
if (adjustedValueRange[1] < -this.MAX_VALUE) {
adjustedValueRange[1] = -this.MAX_VALUE;
}
if (adjustedValueRange[0] == adjustedValueRange[1]) {
adjustedValueRange[0]--;
adjustedValueRange[1]++;
}
if (adjustedValueRange[1] - adjustedValueRange[0] > this.MAX_AXIS_RANGE) {
if (adjustedValueRange[0] + this.MAX_AXIS_RANGE < this.MAX_VALUE) {
adjustedValueRange[1] = adjustedValueRange[0] + this.MAX_AXIS_RANGE;
} else {
adjustedValueRange[0] = adjustedValueRange[1] - this.MAX_AXIS_RANGE;
}
}
if (adjustedValueRange[1] - adjustedValueRange[0] < this.MIN_AXIS_RANGE) {
adjustedValueRange[1] = adjustedValueRange[0] + this.MIN_AXIS_RANGE;
}

// Calculate target range with margin
let margin = (adjustedValueRange[1] - adjustedValueRange[0]) * marginProportion;
targetRange = [adjustedValueRange[0] - margin, adjustedValueRange[1] + margin];
}

// How many steps?
Expand Down Expand Up @@ -836,7 +865,11 @@ export default class LineGraphController implements TabController {
context.moveTo(
graphLeft + graphWidth,
scaleValue(
convertWithPreset(data.values[data.values.length - 1], unitConversion),
clampValue(
convertWithPreset(data.values[data.values.length - 1], unitConversion),
-this.MAX_VALUE,
this.MAX_VALUE
),
[axis.min, axis.max],
[graphTop + graphHeightOpen, graphTop]
)
Expand All @@ -848,7 +881,11 @@ export default class LineGraphController implements TabController {
let x = scaleValue(data.timestamps[i], this.timestampRange, [graphLeft, graphLeft + graphWidth]);

// Render start of current data point
let convertedValue = convertWithPreset(data.values[i], unitConversion);
let convertedValue = clampValue(
convertWithPreset(data.values[i], unitConversion),
-this.MAX_VALUE,
this.MAX_VALUE
);
context.lineTo(x, scaleValue(convertedValue, [axis.min, axis.max], [graphTop + graphHeightOpen, graphTop]));

// Find previous data point and vertical range
Expand All @@ -857,7 +894,11 @@ export default class LineGraphController implements TabController {
let vertRange = [convertedValue, convertedValue];
do {
i--;
let convertedValue = convertWithPreset(data.values[i], unitConversion);
let convertedValue = clampValue(
convertWithPreset(data.values[i], unitConversion),
-this.MAX_VALUE,
this.MAX_VALUE
);
if (convertedValue < vertRange[0]) vertRange[0] = convertedValue;
if (convertedValue > vertRange[1]) vertRange[1] = convertedValue;
newX = Math.floor(
Expand All @@ -875,7 +916,7 @@ export default class LineGraphController implements TabController {
context.moveTo(
x,
scaleValue(
convertWithPreset(data.values[i], unitConversion),
clampValue(convertWithPreset(data.values[i], unitConversion), -this.MAX_VALUE, this.MAX_VALUE),
[axis.min, axis.max],
[graphTop + graphHeightOpen, graphTop]
)
Expand Down
2 changes: 2 additions & 0 deletions src/shared/log/LogField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export default class LogField {

/** Inserts a new value at the correct index. */
private putData(timestamp: number, value: any) {
if (value === null) return;

// Check if the timestamp already exists
if (this.data.timestamps.includes(timestamp)) {
this.data.values[this.data.timestamps.indexOf(timestamp)] = value;
Expand Down
5 changes: 5 additions & 0 deletions src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export function scaleValue(value: number, oldRange: [number, number], newRange:
return ((value - oldRange[0]) / (oldRange[1] - oldRange[0])) * (newRange[1] - newRange[0]) + newRange[0];
}

/** Clamps a value to a range. */
export function clampValue(value: number, min: number, max: number) {
return Math.min(Math.max(value, min), max);
}

/**
* Applys a transform to a pixel value.
* @param originPx The origin pixel in image coordinates.
Expand Down

0 comments on commit df1fc5a

Please sign in to comment.