Skip to content

Commit

Permalink
Linting: enforce camelCasing
Browse files Browse the repository at this point in the history
  • Loading branch information
miklschmidt committed Oct 28, 2024
1 parent 758de8f commit eb902f4
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 95 deletions.
17 changes: 16 additions & 1 deletion src/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@
// "unused-imports/no-unused-imports": "error",
"prettier/prettier": ["warn", { "endOfLine": "auto" }],
"react/display-name": "off",
"no-console": "error"
"no-console": "error",
"camelcase": ["warn", {
"allow": [
"driver_.+",
"run_current",
"sense_resistor",
"(z|travel|square_corner)_(velocity|accel)",
"print_stats",
"^.+_pin$",
"dual_carriage",
"stepper_*",
"uart_address",
"spi_bus",
"response_template" // used in klipper jsonRPC.
]
}]
}
}
],
Expand Down
6 changes: 3 additions & 3 deletions src/app/analysis/_worker/graph-comparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ export const pairPeaks = (
let i = 0;
while (unpairedPeaks1.length > 0 && unpairedPeaks2.length > 0 && i < unpairedPeaks1.length * unpairedPeaks2.length) {
i++;
let min_distance = threshold + 1;
let minDistance = threshold + 1;
let pair: [Peak, Peak] | null = null;
for (const p1 of unpairedPeaks1) {
for (const p2 of unpairedPeaks2) {
const distance = Math.abs(p1.freq - p2.freq);
if (distance < min_distance) {
min_distance = distance;
if (distance < minDistance) {
minDistance = distance;
pair = [p1, p2];
}
}
Expand Down
50 changes: 25 additions & 25 deletions src/app/analysis/_worker/input-shaper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,27 @@ const throwIfCancelled = (signal: AbortSignal) => {
};

async function getShaperSmoothing(shaper: Shaper, accel: number = 5000, scv: number = 5): Promise<number> {
const half_accel = accel * 0.5;
const halfAccel = accel * 0.5;

const [A, T] = shaper;
const inv_D = 1 / sumArray(A);
const invD = 1 / sumArray(A);
const n = T.length;
// Calculate input shaper shift
const ts = sumArray(A.map((a, i) => a * T[i])) * inv_D;
const ts = sumArray(A.map((a, i) => a * T[i])) * invD;

// Calculate offset for 90 and 180 degrees turn
let offset_90 = 0;
let offset_180 = 0;
let offset90 = 0;
let offset180 = 0;
for (let i = 0; i < n; i++) {
if (T[i] >= ts) {
// Calculate offset for one of the axes
offset_90 += A[i] * (scv + half_accel * (T[i] - ts)) * (T[i] - ts);
offset90 += A[i] * (scv + halfAccel * (T[i] - ts)) * (T[i] - ts);
}
offset_180 += A[i] * half_accel * (T[i] - ts) ** 2;
offset180 += A[i] * halfAccel * (T[i] - ts) ** 2;
}
offset_90 *= inv_D * Math.sqrt(2);
offset_180 *= inv_D;
return Math.max(offset_90, offset_180);
offset90 *= invD * Math.sqrt(2);
offset180 *= invD;
return Math.max(offset90, offset180);
}

async function bisect(fn: (x: number) => Promise<boolean>): Promise<number> {
Expand Down Expand Up @@ -177,36 +177,36 @@ async function findShaperMaxAccel(shaper: Shaper, scv: number): Promise<number>
return maxAccel;
}

function estimateShaper(shaper: Shaper, test_damping_ratio: number, test_freqs: Tensor1D): Tensor1D {
function estimateShaper(shaper: Shaper, testDampingRatio: number, testFreqs: Tensor1D): Tensor1D {
return tidy(() => {
const A = tensor1d(shaper[0]);
const T = tensor1d(shaper[1]);
const inv_D = div(1, sum(A));
const omega = mul<Tensor1D>(mul<Tensor1D>(test_freqs, 2), Math.PI);
const damping = mul<Tensor1D>(omega, test_damping_ratio);
const omega_d = mul<Tensor1D>(omega, Math.sqrt(1 - test_damping_ratio ** 2));
const invD = div(1, sum(A));
const omega = mul<Tensor1D>(mul<Tensor1D>(testFreqs, 2), Math.PI);
const damping = mul<Tensor1D>(omega, testDampingRatio);
const omegaD = mul<Tensor1D>(omega, Math.sqrt(1 - testDampingRatio ** 2));
const W = mul(A, exp(outerProduct(mul<Tensor1D>(damping, -1), sub<Tensor1D>(slice(T, T.size - 1, 1), T))));
const S = mul(W, sin(outerProduct(omega_d, T)));
const C = mul(W, cos(outerProduct(omega_d, T)));
return mul(sqrt(sum(stack([pow(sum(S, 1), 2), pow(sum(C, 1), 2)]), 0)), inv_D);
const S = mul(W, sin(outerProduct(omegaD, T)));
const C = mul(W, cos(outerProduct(omegaD, T)));
return mul(sqrt(sum(stack([pow(sum(S, 1), 2), pow(sum(C, 1), 2)]), 0)), invD);
});
}

async function estimateRemainingVibrations(
shaper: Shaper,
test_damping_ratio: number,
freq_bins: Tensor1D,
testDampingRatio: number,
freqBins: Tensor1D,
psd: Tensor1D,
): Promise<[number, Tensor1D]> {
const vals = estimateShaper(shaper, test_damping_ratio, freq_bins);
const vals = estimateShaper(shaper, testDampingRatio, freqBins);
const res = (await tidy(() => {
// The input shaper can only reduce the amplitude of vibrations by
// SHAPER_VIBRATION_REDUCTION times, so all vibrations below that
// threshold can be ignored
const vibr_threshold = div(max(psd), shaperDefaults.SHAPER_VIBRATION_REDUCTION);
const remaining_vibrations = sum(max(sub(mul(vals, psd), vibr_threshold), 0));
const all_vibrations = sum(max(sub(psd, vibr_threshold), 0));
return div(remaining_vibrations, all_vibrations);
const vibrThreshold = div(max(psd), shaperDefaults.SHAPER_VIBRATION_REDUCTION);
const remainingVibrations = sum(max(sub(mul(vals, psd), vibrThreshold), 0));
const allVibrations = sum(max(sub(psd, vibrThreshold), 0));
return div(remainingVibrations, allVibrations);
}).array()) as number;
return [res, vals];
}
Expand Down
92 changes: 46 additions & 46 deletions src/app/analysis/_worker/shapers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,98 +26,98 @@ export function inputShaperCfg(
return { name, initFunc, minFreq, color };
}

export function get_none_shaper(): Shaper {
export function getNoneShaper(): Shaper {
return [[], []];
}

export function get_zv_shaper(shaper_freq: number, damping_ratio: number): Shaper {
const df = Math.sqrt(1 - damping_ratio ** 2);
const K = Math.exp((-damping_ratio * Math.PI) / df);
const t_d = 1 / (shaper_freq * df);
export function getZvShaper(shaperFreq: number, dampingRatio: number): Shaper {
const df = Math.sqrt(1 - dampingRatio ** 2);
const K = Math.exp((-dampingRatio * Math.PI) / df);
const tD = 1 / (shaperFreq * df);
const A = [1, K];
const T = [0, 0.5 * t_d];
const T = [0, 0.5 * tD];
return [A, T];
}

export function get_zvd_shaper(shaper_freq: number, damping_ratio: number): Shaper {
const df = Math.sqrt(1 - damping_ratio ** 2);
const K = Math.exp((-damping_ratio * Math.PI) / df);
const t_d = 1 / (shaper_freq * df);
export function getZvdShaper(shaperFreq: number, dampingRatio: number): Shaper {
const df = Math.sqrt(1 - dampingRatio ** 2);
const K = Math.exp((-dampingRatio * Math.PI) / df);
const tD = 1 / (shaperFreq * df);
const A = [1, 2 * K, K ** 2];
const T = [0, 0.5 * t_d, t_d];
const T = [0, 0.5 * tD, tD];
return [A, T];
}

export function get_mzv_shaper(shaper_freq: number, damping_ratio: number): Shaper {
const df = Math.sqrt(1 - damping_ratio ** 2);
const K = Math.exp((-0.75 * damping_ratio * Math.PI) / df);
const t_d = 1 / (shaper_freq * df);
export function getMzvShaper(shaperFreq: number, dampingRatio: number): Shaper {
const df = Math.sqrt(1 - dampingRatio ** 2);
const K = Math.exp((-0.75 * dampingRatio * Math.PI) / df);
const tD = 1 / (shaperFreq * df);

const a1 = 1 - 1 / Math.sqrt(2);
const a2 = (Math.sqrt(2) - 1) * K;
const a3 = a1 * K * K;

const A = [a1, a2, a3];
const T = [0, 0.375 * t_d, 0.75 * t_d];
const T = [0, 0.375 * tD, 0.75 * tD];
return [A, T];
}

export function get_ei_shaper(shaper_freq: number, damping_ratio: number): Shaper {
const v_tol = 1 / shaperDefaults.SHAPER_VIBRATION_REDUCTION; // vibration tolerance
const df = Math.sqrt(1 - damping_ratio ** 2);
const K = Math.exp((-damping_ratio * Math.PI) / df);
const t_d = 1 / (shaper_freq * df);
export function getEiShaper(shaperFreq: number, dampingRatio: number): Shaper {
const vTol = 1 / shaperDefaults.SHAPER_VIBRATION_REDUCTION; // vibration tolerance
const df = Math.sqrt(1 - dampingRatio ** 2);
const K = Math.exp((-dampingRatio * Math.PI) / df);
const tD = 1 / (shaperFreq * df);

const a1 = 0.25 * (1 + v_tol);
const a2 = 0.5 * (1 - v_tol) * K;
const a1 = 0.25 * (1 + vTol);
const a2 = 0.5 * (1 - vTol) * K;
const a3 = a1 * K * K;

const A = [a1, a2, a3];
const T = [0, 0.5 * t_d, t_d];
const T = [0, 0.5 * tD, tD];
return [A, T];
}

export function get_2hump_ei_shaper(shaper_freq: number, damping_ratio: number): Shaper {
const v_tol = 1 / shaperDefaults.SHAPER_VIBRATION_REDUCTION; // vibration tolerance
const df = Math.sqrt(1 - damping_ratio ** 2);
const K = Math.exp((-damping_ratio * Math.PI) / df);
const t_d = 1 / (shaper_freq * df);
export function get2HumpEiShaper(shaperFreq: number, dampingRatio: number): Shaper {
const vTol = 1 / shaperDefaults.SHAPER_VIBRATION_REDUCTION; // vibration tolerance
const df = Math.sqrt(1 - dampingRatio ** 2);
const K = Math.exp((-dampingRatio * Math.PI) / df);
const tD = 1 / (shaperFreq * df);

const V2 = v_tol ** 2;
const V2 = vTol ** 2;
const X = Math.pow(V2 * (Math.sqrt(1 - V2) + 1), 1 / 3);
const a1 = (3 * X * X + 2 * X + 3 * V2) / (16 * X);
const a2 = (0.5 - a1) * K;
const a3 = a2 * K;
const a4 = a1 * K * K * K;

const A = [a1, a2, a3, a4];
const T = [0, 0.5 * t_d, t_d, 1.5 * t_d];
const T = [0, 0.5 * tD, tD, 1.5 * tD];
return [A, T];
}

export function get_3hump_ei_shaper(shaper_freq: number, damping_ratio: number): Shaper {
const v_tol = 1 / shaperDefaults.SHAPER_VIBRATION_REDUCTION; // vibration tolerance
const df = Math.sqrt(1 - damping_ratio ** 2);
const K = Math.exp((-damping_ratio * Math.PI) / df);
const t_d = 1 / (shaper_freq * df);
export function get3HumpEiShaper(shaperFreq: number, dampingRatio: number): Shaper {
const vTol = 1 / shaperDefaults.SHAPER_VIBRATION_REDUCTION; // vibration tolerance
const df = Math.sqrt(1 - dampingRatio ** 2);
const K = Math.exp((-dampingRatio * Math.PI) / df);
const tD = 1 / (shaperFreq * df);

const K2 = K * K;
const a1 = 0.0625 * (1 + 3 * v_tol + 2 * Math.sqrt(2 * (v_tol + 1) * v_tol));
const a2 = 0.25 * (1 - v_tol) * K;
const a3 = (0.5 * (1 + v_tol) - 2 * a1) * K2;
const a1 = 0.0625 * (1 + 3 * vTol + 2 * Math.sqrt(2 * (vTol + 1) * vTol));
const a2 = 0.25 * (1 - vTol) * K;
const a3 = (0.5 * (1 + vTol) - 2 * a1) * K2;
const a4 = a2 * K2;
const a5 = a1 * K2 * K2;

const A = [a1, a2, a3, a4, a5];
const T = [0, 0.5 * t_d, t_d, 1.5 * t_d, 2 * t_d];
const T = [0, 0.5 * tD, tD, 1.5 * tD, 2 * tD];
return [A, T];
}

export const INPUT_SHAPERS = [
inputShaperCfg('zv', get_zv_shaper, 21, 'blue'),
inputShaperCfg('mzv', get_mzv_shaper, 23, 'rose'),
inputShaperCfg('zvd', get_zvd_shaper, 29, 'lime'),
inputShaperCfg('ei', get_ei_shaper, 29, 'amber'),
inputShaperCfg('2hump_ei', get_2hump_ei_shaper, 39, 'pink'),
inputShaperCfg('3hump_ei', get_3hump_ei_shaper, 48, 'violet'),
inputShaperCfg('zv', getZvShaper, 21, 'blue'),
inputShaperCfg('mzv', getMzvShaper, 23, 'rose'),
inputShaperCfg('zvd', getZvdShaper, 29, 'lime'),
inputShaperCfg('ei', getEiShaper, 29, 'amber'),
inputShaperCfg('2hump_ei', get2HumpEiShaper, 39, 'pink'),
inputShaperCfg('3hump_ei', get3HumpEiShaper, 48, 'violet'),
];
18 changes: 9 additions & 9 deletions src/app/analysis/periodogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function powerSpectralDensity(
throw new Error('Unknown scaling');
}

let scaling_factor: number = _scaling === 'none' ? 1 : 2;
let scalingFactor: number = _scaling === 'none' ? 1 : 2;
const win = tidy(() => tfSignal.hannWindow(fftSize));
let windowLossCompensationFactor = (await tidy(() => div(div(1.0, sum(pow(win, 2))), sampleRate)).array()) as number;

Expand Down Expand Up @@ -77,7 +77,7 @@ export async function powerSpectralDensity(
power *= windowLossCompensationFactor;
// Don't scale DC or Nyquist by 2
if (_scaling == 'psd' && i > 0 && nextFrequency < shaperDefaults.MAX_FREQ) {
power *= scaling_factor;
power *= scalingFactor;
}
if (power > maxPower) {
maxPower = power;
Expand Down Expand Up @@ -115,16 +115,16 @@ export const welch = async (PSDs: PSD[]): Promise<TypedArrayPSD> => {
// }

// Compute average PSD
let num_estimates = PSDs[0].estimates.length;
let avg = new Float64Array(num_estimates).fill(0);
let numEstimates = PSDs[0].estimates.length;
let avg = new Float64Array(numEstimates).fill(0);
for (let p = 0; p < PSDs.length; p++) {
for (let i = 0; i < num_estimates; i++) {
for (let i = 0; i < numEstimates; i++) {
avg[i] += PSDs[p].estimates[i];
}
}
let maxPower = 0;
let minPower = 0;
for (let i = 0; i < num_estimates; i++) {
for (let i = 0; i < numEstimates; i++) {
avg[i] = avg[i] / PSDs.length;
if (avg[i] > maxPower) {
maxPower = avg[i];
Expand All @@ -142,10 +142,10 @@ export const welch = async (PSDs: PSD[]): Promise<TypedArrayPSD> => {
};

export const sumPSDs = (PSDs: TypedArrayPSD[]): TypedArrayPSD => {
let num_estimates = PSDs[0].estimates.length;
let sum = new Float64Array(num_estimates).fill(0);
let numEstimates = PSDs[0].estimates.length;
let sum = new Float64Array(numEstimates).fill(0);
for (let p = 0; p < PSDs.length; p++) {
for (let i = 0; i < num_estimates; i++) {
for (let i = 0; i < numEstimates; i++) {
sum[i] += PSDs[p].estimates[i];
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/calibration/focus-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type CameraSettingsProps = {

export const FocusControls: React.FC<CameraSettingsProps> = (props) => {
const G = useGcodeCommand();
const live_position = usePrinterObjectSubscription((res) => {
const livePosition = usePrinterObjectSubscription((res) => {
return {
z: res.motion_report.live_position?.[2],
};
Expand Down Expand Up @@ -126,8 +126,8 @@ export const FocusControls: React.FC<CameraSettingsProps> = (props) => {
>
<span className="inline h-5 w-5 flex-shrink-0 font-sans text-base font-semibold leading-[18px]">Z</span>{' '}
<span className="inline">
{(live_position?.z ?? 0) >= 0 && '+'}
{live_position?.z?.toFixed(2) ?? '??'}
{(livePosition?.z ?? 0) >= 0 && '+'}
{livePosition?.z?.toFixed(2) ?? '??'}
</span>
</span>
</div>
Expand Down
5 changes: 2 additions & 3 deletions src/app/calibration/toolbars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const useToolbarState = (props: { zoom: number }) => {
clearTempZoomExpand();
}, [clearTempZoomExpand]);

const live_position = usePrinterObjectSubscription((res) => {
const livePosition = usePrinterObjectSubscription((res) => {
return {
x: res.motion_report.live_position?.[0],
y: res.motion_report.live_position?.[1],
Expand All @@ -99,7 +99,7 @@ const useToolbarState = (props: { zoom: number }) => {
const isZOffsetProbeVisible = hasZOffsetProbe && isVaocStarted;

return {
live_position,
livePosition,
tool: t0?.active ? 'T0' : t1?.active ? 'T1' : null,
isStartingVaoc,
setIsStartingVaoc,
Expand Down Expand Up @@ -141,7 +141,6 @@ export const Toolbars: React.FC<ToolbarsProps> = (props) => {
isFullscreened,
} = props;
const {
live_position,
tool,
isStartingVaoc,
setIsStartingVaoc,
Expand Down
4 changes: 2 additions & 2 deletions src/server/helpers/iw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ const parseCell = (cell: string): Network => {
* @param {function} callback The callback function.
*
*/
const parseScan = (show_hidden: boolean) => {
const parseScan = (showHidden: boolean) => {
return function ({ stdout, stderr }: { stdout: string; stderr: string }) {
if (show_hidden) {
if (showHidden) {
return stdout
.split(/(^|\n)(?=BSS )/)
.map(parseCell)
Expand Down
Loading

0 comments on commit eb902f4

Please sign in to comment.