Created
December 10, 2016 16:34
-
-
Save fimdomeio/74711317bf5417abafce41607dac42d1 to your computer and use it in GitHub Desktop.
Manual control axidraw to help with registration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This requires nodejs to run | |
install | |
npm install terminal-kit | |
npm install superagent | |
run | |
node axidraw-manual-control.js | |
Keys | |
left, right, up, down -> control pen position | |
+ , - -> control movement increments | |
a , z -> control pen height | |
*/ | |
var term = require( 'terminal-kit' ).terminal ; | |
var request = require('superagent'); | |
// go Home | |
request | |
.put('localhost:4242/v1/pen') | |
.send({ state: 0}) | |
.end(function(err, res){}); | |
request | |
.put('localhost:4242/v1/pen') | |
.send({ x: 0, y: 0 }) | |
.set('Accept', 'application/json') | |
.end(function(err, res){}); | |
// axidraw width: 29.9cm | |
var canvasWidth = 29.9; | |
// axidraw height 21.6 | |
var canvasHeight = 21.6; | |
var x = 0; | |
var y = 0; | |
var increment = 30; | |
var penHeight = 0; | |
// applys increments in mm | |
function applyIncrement(increment, direction) { | |
var result = 0; | |
if(direction === 'x'){ | |
result = increment * canvasWidth / 100 ; | |
} else if(direction === 'y') { | |
result = increment * canvasHeight / 100; | |
} | |
if( result < 0) result = 0; | |
if(result > 100) result = 100; | |
return result; | |
} | |
term.grabInput() ; | |
term.on( 'key' , function( name , matches , data ) { | |
term.clear() ; | |
// console.log( "'key' event:" , name ) ; | |
if(name === '0') { | |
x = 0; | |
y = 0; | |
} | |
if(name === 'RIGHT') { | |
x += applyIncrement(increment, 'x'); | |
} | |
else if(name === 'LEFT') { | |
x -= applyIncrement(increment, 'x'); | |
} | |
else if(name === 'DOWN') { | |
y += applyIncrement(increment, 'y'); | |
} | |
else if(name === 'UP') { | |
y -= applyIncrement(increment, 'y'); | |
} | |
else if(name === '+') { | |
if (increment < 1) { | |
increment += 0.1; | |
}else { | |
increment += 1; | |
} | |
term.green( 'increment: ' + increment + "% \n") ; | |
} | |
else if(name === '-') { | |
if(increment > 0) { | |
if (increment <= 1) { | |
increment -= 0.1; | |
}else { | |
increment -= 1; | |
} | |
term.green( 'increment: ' + increment + "%\n") ; | |
} | |
} | |
else if(name === 'a' || name === 'z'){ | |
if(name === 'a') { | |
if(penHeight < 1) { | |
penHeight += 0.05; | |
} | |
} | |
else if(name === 'z') { | |
if(penHeight > 0) { | |
penHeight -= 0.05; | |
} | |
} | |
request | |
.put('localhost:4242/v1/pen') | |
.send({ state: penHeight}) | |
.end(function(err, res){}); | |
term.green( 'penHeight: ' + penHeight.toFixed(2) + "\n") ; | |
} | |
request | |
.put('localhost:4242/v1/pen') | |
.send({ x: x, y: y}) | |
.end(function(err, res){ | |
term.green( 'x: ' + (x * canvasWidth / 100).toFixed(2) +'cm y: ' + (y * canvasHeight / 100).toFixed(2) +"cm\n") ; | |
}); | |
// Detect CTRL-C and exit 'manually' | |
if ( name === 'CTRL_C' || name === 'q' ) { process.exit() ; } | |
}) ; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment