Skip to content

Commit

Permalink
Merge pull request #2 from etskinner/feature/allow-initial
Browse files Browse the repository at this point in the history
allow initial tweaks
  • Loading branch information
etskinner authored Apr 18, 2024
2 parents 00717db + 5f562bf commit 632e51e
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 98 deletions.
6 changes: 5 additions & 1 deletion www/css/tablet.css
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ textarea {
.caltable {
width: 100%;
font-size: x-small !important;
height: 600px;
height: 530px;
}

.caltable textarea {
Expand Down Expand Up @@ -909,3 +909,7 @@ textarea {
text-align: left;
float: left;
}

.pad-left {
margin-left: 20px;
}
158 changes: 98 additions & 60 deletions www/js/calculatesCalibrationStuff.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
//This is the inital guess for how big the machine is. These numbers are wrong intensionally
const initialWidth = 3048 + 12
const initialHeight = 2200 - 14


//Establish initial guesses for the corners
var initialGuess = {
tl: { x: 0, y: initialHeight },
tr: { x: initialWidth, y: initialHeight },
tl: { x: 0, y: 2000 },
tr: { x: 3000, y: 2000 },
bl: { x: 0, y: 0 },
br: { x: initialWidth, y: 0 },
br: { x: 3000, y: 0 },
fitness: 100000000,
}

const centerX = initialWidth / 2
const centerY = initialHeight / 2

let result

/**------------------------------------Intro------------------------------------
*
* If you are reading this code to understand it then I would recommend starting
* at the bottom of the page and working your way up. The code is written in a
* functional style so the function definitions are at the top and the code that
* functional st‰yle so the function definitions are at the top and the code that
* actually runs is at the bottom. It was also written quickly and modified a lot
* so it is not very clean. I apologize for that.
*
Expand Down Expand Up @@ -491,14 +486,19 @@ function scaleMeasurementsBasedOnTension(measurements, guess) {
return newMeasurements
}

// breaks out of loop if set.
let interruptMaxFitness = false;
function findMaxFitness(measurements) {
// reject if already running
if (window.computing) {
console.log('Computation in progress, ignoring request');
return;
}
document.getElementById("caltable").dispatchEvent(new CustomEvent(CALIBRATION_EVENT, {
bubbles: true,
cancelable: true,
detail: {
started: true,
}
}));
// turn off compute button
window.computing = true;

Expand All @@ -507,73 +507,93 @@ function findMaxFitness(measurements) {
let totalCounter = 0;
var bestGuess = JSON.parse(JSON.stringify(initialGuess));

var messagesBox = document.querySelector('#messages');
const fitnessMessage = document.getElementById('fitnessMessage');
var messagesBox = document.querySelector("#messages");
const fitnessMessage = document.getElementById("fitnessMessage");

function loop() {
if (interruptMaxFitness) {
// breaks out of loop if set.
interruptMaxFitness = false;
window.computing = false;
function iterate() {
if (!window.computing) {
printGuess(messagesBox, bestGuess);
return;
}
clearCalCanvas();
currentGuess = computeLinesFitness(measurements, currentGuess);
if (stagnantCounter < 300 && totalCounter < 200000) {
//Clear the canvass
clearCalCanvas();

currentGuess = computeLinesFitness(measurements, currentGuess);

document.getElementById("caltable").dispatchEvent(new CustomEvent(CALIBRATION_EVENT, {
bubbles: true,
cancelable: true,
detail: {
progress: true,
currentGuess: currentGuess
}
}));

if (1/currentGuess.fitness > 1/bestGuess.fitness) {
bestGuess = JSON.parse(JSON.stringify(currentGuess));
BestGuess = bestGuess;
stagnantCounter = 0;
} else {
stagnantCounter++;
}
totalCounter++;
//console.log("Total Counter: " + totalCounter);

if (1/currentGuess.fitness > 1/bestGuess.fitness) {
bestGuess = JSON.parse(JSON.stringify(currentGuess));
BestGuess = bestGuess;
stagnantCounter = 0;
} else {
stagnantCounter++;
}
totalCounter++;

if(totalCounter % 100 == 0) {
const fitnessStatus = `Fitness: ${1 / bestGuess.fitness.toFixed(7)} in ${totalCounter}`;
fitnessMessage.innerText = fitnessStatus;
messagesBox.value += '\n' + fitnessStatus;
messagesBox.scrollTop;
messagesBox.scrollTop = messagesBox.scrollHeight;
}
if(totalCounter % 100 == 0) {
const fitnessStatus = `Fitness: ${1 / bestGuess.fitness.toFixed(7)} in ${totalCounter}`;
fitnessMessage.innerText = fitnessStatus;
messagesBox.value += '\n' + fitnessStatus;
messagesBox.scrollTop;
messagesBox.scrollTop = messagesBox.scrollHeight;
}

if (stagnantCounter < 100 && totalCounter < 200000) {
requestAnimationFrame(loop);
// Schedule the next iteration
setTimeout(iterate, 0);
} else {
document.getElementById("caltable").dispatchEvent(new CustomEvent(CALIBRATION_EVENT, {
bubbles: true,
cancelable: true,
detail: {
complete: true,
bestGuess: bestGuess
}
}));

// allow compute button to be pressed again
window.computing = false;
resetButtonsDisabled(false);
printGuess(messagesBox, bestGuess);

if(1/bestGuess.fitness > 0.5){
sendCommand('$/Maslow_tlX=' + bestGuess.tl.x.toFixed(1));
sendCommand('$/Maslow_tlY=' + bestGuess.tl.y.toFixed(1));
sendCommand('$/Maslow_trX=' + bestGuess.tr.x.toFixed(1));
sendCommand('$/Maslow_trY=' + bestGuess.tr.y.toFixed(1));
sendCommand('$/Maslow_blX=' + bestGuess.bl.x.toFixed(1));
sendCommand('$/Maslow_blY=' + bestGuess.bl.y.toFixed(1));
sendCommand('$/Maslow_brX=' + bestGuess.br.x.toFixed(1));
sendCommand('$/Maslow_brY=' + bestGuess.br.y.toFixed(1));
if (1 / bestGuess.fitness > 0.5) {
sendCommand("$/Maslow_tlX=" + bestGuess.tl.x.toFixed(1));
sendCommand("$/Maslow_tlY=" + bestGuess.tl.y.toFixed(1));
sendCommand("$/Maslow_trX=" + bestGuess.tr.x.toFixed(1));
sendCommand("$/Maslow_trY=" + bestGuess.tr.y.toFixed(1));
sendCommand("$/Maslow_blX=" + bestGuess.bl.x.toFixed(1));
sendCommand("$/Maslow_blY=" + bestGuess.bl.y.toFixed(1));
sendCommand("$/Maslow_brX=" + bestGuess.br.x.toFixed(1));
sendCommand("$/Maslow_brY=" + bestGuess.br.y.toFixed(1));
refreshSettings(current_setting_filter);
saveMaslowYaml();

messagesBox.value += '\nThese values have been automatically saved for you.';
messagesBox.value += "\nYou MUST restart your machine for them to take effect...I know that is annoying, it's getting fixed ASAP. ";
messagesBox.scrollTop
messagesBox.value +=
"\nThese values have been automatically saved for you.";
messagesBox.value +=
"\nYou MUST restart your machine for them to take effect...I know that is annoying, it's getting fixed ASAP. ";
messagesBox.scrollTop;
messagesBox.scrollTop = messagesBox.scrollHeight;

//This restarts the esp32 to prevent you from trying to move the machine after calibration
setTimeout(function() {
sendCommand('$System/Control=RESTART');
// This restarts the esp32 to prevent you from trying to move the machine after calibration
setTimeout(function () {
sendCommand("$System/Control=RESTART");
}, 2000);
}

// allow compute button to be pressed again
windowcomputing = false;
resetButtonsDisabled(false);
}
}

loop();
return bestGuess;
// Start the iteration
iterate();
}


Expand Down Expand Up @@ -607,4 +627,22 @@ function printGuess(messagesBox, bestGuess) {

}

const CALIBRATION_EVENT = 'calibration';

const CAL_STARTED_EVENT = new CustomEvent(CALIBRATION_EVENT,
{bubbles: true, cancelable: true, detail: {
complete: true
}});

const CAL_DONE_EVENT = new CustomEvent(CALIBRATION_EVENT,
{bubbles: true, cancelable: true, detail: {
started: true
}});

const CAL_PROGRESS_EVENT = new CustomEvent(CALIBRATION_EVENT,
{bubbles: true, cancelable: true, detail: {
iteration: 0,
bestGuess: {}
}});


80 changes: 63 additions & 17 deletions www/js/calibrationDraw.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
let BestGuess = {...initialGuess};
let BestGuess = { ...initialGuess };
let SavedMeasurements = [];
/**
* Functions for drawing on the cal details tab
*/

function measurementsChanged() {
try {
loadInitialData();
const calTextElement = document.getElementById("caldata");
let textValue = `${calTextElement.value}`;
textValue = textValue.replace('CLBM:','');
if (textValue.indexOf('bl:')>0) {
textValue = textValue.replace("CLBM:", "");
if (textValue.indexOf("bl:") > 0) {
textValue = JSON.stringify(eval(textValue));
}
let caldata = JSON.parse(textValue);
Expand All @@ -19,29 +20,66 @@ function measurementsChanged() {
}
}

function initialDataChanged() {
initialGuess.tl.x = parseFloat(document.getElementById("tlx").value);
initialGuess.tl.y = parseFloat(document.getElementById("tly").value);
initialGuess.tr.x = parseFloat(document.getElementById("trx").value);
initialGuess.tr.y = parseFloat(document.getElementById("try").value);
initialGuess.br.x = parseFloat(document.getElementById("brx").value);
}

function loadInitialData() {
document.getElementById("tlx").value = initialGuess.tl.x;
document.getElementById("tly").value = initialGuess.tl.y;
document.getElementById("trx").value = initialGuess.tr.x;
document.getElementById("try").value = initialGuess.tr.y;
document.getElementById("brx").value = initialGuess.br.x;
}

function updateCalibrationSave(caldata) {
let id=0;
SavedMeasurements = caldata.map(m=>({...m, id: id++}));
document.querySelector('button#compute-sim-button').disabled = false;
let id = 0;
SavedMeasurements = caldata.map((m) => ({ ...m, id: id++ }));
calibrationTableUpdate();
resetButtonsDisabled(false);
// draw one...
computeLinesFitness(SavedMeasurements, BestGuess);
}

const calibrationCallback = (e) => {
if (e.detail.started) {
// todo: anything?
} else if (e.detail.progress) {
// update fields.
document.getElementById("tlx").value = e.detail.currentGuess.tl.x;
document.getElementById("tly").value = e.detail.currentGuess.tl.y;
document.getElementById("trx").value = e.detail.currentGuess.tr.x;
document.getElementById("try").value = e.detail.currentGuess.tr.y;
document.getElementById("brx").value = e.detail.currentGuess.br.x;
} else if (e.detail.complete) {
initialDataChanged();
// update initial stuff.
initialGuess = {...BestGuess};
loadInitialData();
}
console.log(e.detail);
};

function computeSim(measurements = null) {

document.getElementById("caltable").removeEventListener(CALIBRATION_EVENT, calibrationCallback);
document.getElementById("caltable").addEventListener(CALIBRATION_EVENT, calibrationCallback)

resetButtonsDisabled(true);
clearCalCanvas();
BestGuess = findMaxFitness(measurements || SavedMeasurements);
results = document.querySelector("#messages").value
findMaxFitness(measurements || SavedMeasurements);
results = document.querySelector("#messages").value;
console.log(results);
}

//Deletes everything from the canvas
function clearCalCanvas() {
const canvas = document.getElementById('CursorLayer');
const context = canvas.getContext('2d');
const canvas = document.getElementById("CursorLayer");
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
}

Expand All @@ -59,9 +97,13 @@ function changeStrokeStyle(inputValue) {
const green = [0, 128, 0]; // RGB values for green
const red = [255, 0, 0]; // RGB values for red
const range = 60 - 20; // Range of input values
const increment = (red.map((value, index) => value - green[index])).map(value => value / range); // Increment for each RGB value
const increment = red
.map((value, index) => value - green[index])
.map((value) => value / range); // Increment for each RGB value

const color = green.map((value, index) => Math.round(value + increment[index] * (inputValue - 20))); // Calculate the color based on the input value
const color = green.map((value, index) =>
Math.round(value + increment[index] * (inputValue - 20))
); // Calculate the color based on the input value

const canvas = document.getElementById("CursorLayer");
const ctx = canvas.getContext("2d");
Expand All @@ -78,7 +120,6 @@ function changeStrokeStyle(inputValue) {
* @returns {void}
*/
function drawLines(line1, line2, line3, line4, guess, measurement) {

//Compute the tensions in the upper two belts
//const { TL, TR } = calculateTensions(line1.xEnd, line1.yEnd, guess); //This assumes the ends are in the same place which they aren't at first

Expand Down Expand Up @@ -129,11 +170,16 @@ function drawLines(line1, line2, line3, line4, guess, measurement) {
gridApi.applyTransaction({
update: [{ ...measurement, line1, line2, line3, line4 }],
});
} else{
console.log('no id', measurement);
} else {
console.log("no id", measurement);
}
}

function calibrationTableUpdate() {
gridApi?.setGridOption('rowData', SavedMeasurements);
}
gridApi?.setGridOption("rowData", SavedMeasurements);
}

function stopCalculations() {
resetButtonsDisabled(false);
window.computing = false;
}
Loading

0 comments on commit 632e51e

Please sign in to comment.