Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved algorithm and user input based initial guess for calibration #22

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified index.html.gz
Binary file not shown.
157 changes: 79 additions & 78 deletions www/js/calculatesCalibrationStuff.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//This is the inital guess for how big the machine is. These numbers are wrong intensionally
const initialWidth = 3048 + 12
const initialHeight = 2200 - 14

var tlX = 0;
BarbourSmith marked this conversation as resolved.
Show resolved Hide resolved
var tlY = 2000;
var trX = 3000;
var trY = 2000;
var brX = 3000;

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

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

let result

/**------------------------------------Intro------------------------------------
Expand Down Expand Up @@ -495,79 +495,80 @@ function findMaxFitness(measurements) {
let stagnantCounter = 0;
let totalCounter = 0;
var bestGuess = JSON.parse(JSON.stringify(initialGuess));

var messagesBox = document.querySelector('#messages');

function loop() {
//Clear the canvass
clearCanvas();

currentGuess = computeLinesFitness(measurements, currentGuess);

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

if(totalCounter % 100 == 0) {
messagesBox.value += '\nFitness: ' + 1/bestGuess.fitness.toFixed(7) + ' in ' + totalCounter;
messagesBox.scrollTop
messagesBox.scrollTop = messagesBox.scrollHeight;
}

if (stagnantCounter < 100 && totalCounter < 200000) {
requestAnimationFrame(loop);
} else {

if(1/bestGuess.fitness < 0.5){
messagesBox.value += '\nWARNING FITNESS TOO LOW. DO NOT USE THESE CALIBRATION VALUES!';
console.log("Initial Guess: " + JSON.stringify(initialGuess));
BarbourSmith marked this conversation as resolved.
Show resolved Hide resolved

function iterate() {
if (stagnantCounter < 300 && totalCounter < 200000) {
//Clear the canvass
clearCanvas();

currentGuess = computeLinesFitness(measurements, currentGuess);

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

if(totalCounter % 100 == 0){
document.getElementById('messages').value += "Fitness: " + (1/bestGuess.fitness).toFixed(7) + " in " + totalCounter + "\n";
document.getElementById('messages').value += "Guess: " + JSON.stringify(bestGuess) + "\n";
document.getElementById('messages').scrollTop = document.getElementById('messages').scrollHeight;
}

// Schedule the next iteration
setTimeout(iterate, 0);
}

messagesBox.value += '\nCalibration complete \nCalibration values:';
messagesBox.value += '\nFitness: ' + 1/bestGuess.fitness.toFixed(7);
messagesBox.value += '\nMaslow_tlX: ' + bestGuess.tl.x.toFixed(1);
messagesBox.value += '\nMaslow_tlY: ' + bestGuess.tl.y.toFixed(1);
messagesBox.value += '\nMaslow_trX: ' + bestGuess.tr.x.toFixed(1);
messagesBox.value += '\nMaslow_trY: ' + bestGuess.tr.y.toFixed(1);
messagesBox.value += '\nMaslow_blX: ' + bestGuess.bl.x.toFixed(1);
messagesBox.value += '\nMaslow_blY: ' + bestGuess.bl.y.toFixed(1);
messagesBox.value += '\nMaslow_brX: ' + bestGuess.br.x.toFixed(1);
messagesBox.value += '\nMaslow_brY: ' + bestGuess.br.y.toFixed(1);
messagesBox.scrollTop
messagesBox.scrollTop = messagesBox.scrollHeight;

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.scrollTop = messagesBox.scrollHeight;

//This restarts the esp32 to prevent you from trying to move the machine after calibration
setTimeout(function() {
sendCommand('$System/Control=RESTART');
}, 2000);
else{
var messagesBox = document.querySelector('#messages');

if(1/bestGuess.fitness < 0.5){
messagesBox.value += '\nWARNING FITNESS TOO LOW. DO NOT USE THESE CALIBRATION VALUES!';
}

messagesBox.value += '\nCalibration complete \nCalibration values:';
messagesBox.value += '\nFitness: ' + 1/bestGuess.fitness.toFixed(7);
messagesBox.value += '\nMaslow_tlX: ' + bestGuess.tl.x.toFixed(1);
messagesBox.value += '\nMaslow_tlY: ' + bestGuess.tl.y.toFixed(1);
messagesBox.value += '\nMaslow_trX: ' + bestGuess.tr.x.toFixed(1);
messagesBox.value += '\nMaslow_trY: ' + bestGuess.tr.y.toFixed(1);
messagesBox.value += '\nMaslow_blX: ' + bestGuess.bl.x.toFixed(1);
messagesBox.value += '\nMaslow_blY: ' + bestGuess.bl.y.toFixed(1);
messagesBox.value += '\nMaslow_brX: ' + bestGuess.br.x.toFixed(1);
messagesBox.value += '\nMaslow_brY: ' + bestGuess.br.y.toFixed(1);
messagesBox.scrollTop
messagesBox.scrollTop = messagesBox.scrollHeight;

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.scrollTop = messagesBox.scrollHeight;

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


}
}

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


Expand Down
43 changes: 43 additions & 0 deletions www/js/tablet.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,30 @@ function tabletShowMessage(msg, collecting) {
if (msg.startsWith('$/Maslow_trX=')) {
document.getElementById('machineWidth').value = msg.substring(13, msg.length)
loadedValues['machineWidth'] = msg.substring(13, msg.length)
trX = parseFloat(msg.substring(13, msg.length))
reloadInitialGuess()
return;
}
if (msg.startsWith('$/Maslow_trY=')) {
document.getElementById('machineHeight').value = msg.substring(13, msg.length)
loadedValues['machineHeight'] = msg.substring(13, msg.length)
trY = parseFloat(msg.substring(13, msg.length))
reloadInitialGuess()
return;
}
if (msg.startsWith('$/Maslow_tlX=')) {
tlX = parseFloat(msg.substring(13, msg.length))
reloadInitialGuess()
return;
}
if (msg.startsWith('$/Maslow_tlY=')) {
tlY = parseFloat(msg.substring(13, msg.length))
reloadInitialGuess()
return;
}
if (msg.startsWith('$/Maslow_brX=')) {
brX = parseFloat(msg.substring(13, msg.length))
reloadInitialGuess()
return;
}

Expand Down Expand Up @@ -1205,6 +1224,28 @@ function loadConfigValues(){
SendPrinterCommand('$/Maslow_trY');
}

//Load all of the corner values
function loadCornerValues(){
SendPrinterCommand('$/Maslow_tlX')
SendPrinterCommand('$/Maslow_tlY')
SendPrinterCommand('$/Maslow_trX')
SendPrinterCommand('$/Maslow_trY')
SendPrinterCommand('$/Maslow_brX')
}

//Repopulate initial guess values
function reloadInitialGuess(){
console.log('Reloading initial guess')
initialGuess = {
tl: { x: 0, y: tlY },
tr: { x: trX, y: trY },
bl: { x: 0, y: 0 },
br: { x: brX, y: 0 },
fitness: 100000000,
}
console.log(JSON.stringify(initialGuess))
}

//Save the configuration values
function saveConfigValues(){
let gridWidth = document.getElementById('gridWidth').value
Expand Down Expand Up @@ -1260,6 +1301,8 @@ const onCalibrationButtonsClick = async (command, msg) => {
text = text + '\n' + "Index.html Version: " + versionNumber
msgWindow.textContent = text
msgWindow.scrollTop = msgWindow.scrollHeight
console.log("tlX: " + tlX + " tlY: " + tlY + " trX: " + trX + " trY: " + trY + " brX: " + brX)
console.log(JSON.stringify(initialGuess));
}
}

Expand Down
2 changes: 1 addition & 1 deletion www/sub/tablettab.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div class="maslow-grid-item maslow-grid-item-btn" style = "background-color: #9d88c0;" onclick="sendMove('X-')"><canvas id = "lBtn" style="width: 100%; height: 100%"></div>
<div class="maslow-grid-item maslow-grid-item-btn" style = "background-color: #f2f0e4;" onclick="moveHome()"><canvas id = "hBtn" style="width: 100%; height: 100%"></div>
<div class="maslow-grid-item maslow-grid-item-btn" style = "background-color: #9d88c0;" onclick="sendMove('X+')"><canvas id = "rBtn" style="width: 100%; height: 100%"></div>
<div class="maslow-grid-item maslow-grid-item-btn" style = "background-color: #f2f0e4; font-size: 1vw;" onclick="openModal('calibration-popup');" id = "calibrationBTN" font-size: 1vw;>Setup</div>
<div class="maslow-grid-item maslow-grid-item-btn" style = "background-color: #f2f0e4; font-size: 1vw;" onclick="loadCornerValues(); openModal('calibration-popup');" id = "calibrationBTN" font-size: 1vw;>Setup</div>
<div class="maslow-grid-item maslow-grid-item-btn" style = "background-color: #b69fcb;" onclick="sendMove('X-Y-')"><canvas id = "blBtn" style="width: 100%; height: 100%"></canvas></div>
<div class="maslow-grid-item maslow-grid-item-btn" style = "background-color: #9d88c0;" onclick="sendMove('Y-')"><canvas id = "dnBtn" style="width: 100%; height: 100%"></div>
<div class="maslow-grid-item maslow-grid-item-btn" style = "background-color: #b69fcb;" onclick="sendMove('X+Y-')"><canvas id = "brBtn" style="width: 100%; height: 100%"></canvas></div>
Expand Down