Skip to content

Instantly share code, notes, and snippets.

@giseburt
Forked from ril3y/WreckingBall.js
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save giseburt/7c365b3efaa16b5814f1 to your computer and use it in GitHub Desktop.

Select an option

Save giseburt/7c365b3efaa16b5814f1 to your computer and use it in GitHub Desktop.
var VELOCITY_ACCURACY = 1
var calcPen = function(zLen){
//Pendulum Movement Equation
//T = 2pi*SQRT(L/g)
var twoPi = 6.2831926
var gravity = 9.8
var result = twoPi * Math.sqrt(zLen / gravity);
return(result.toFixed(4))
}
var _calcVelocity = function(xyLen, jerk, zLen, axis, isDoubleMove){
// V = 1 / 3 (3T j v_1 - 3D j - v_1 sqrt(j v_1) sqrt(sqrt(3) 30)) / j
// V' = 1 / 3 (-sqrt(j v_1) sqrt(sqrt(3) 30) - 1 / 2 j v_1 sqrt(sqrt(3) 30) / sqrt(j v_1) + 3T j) / j
isDoubleMove = isDoubleMove || false
var jerk = jerk*1000000;
var pendulumPeriod = calcPen(zLen/100);
var sqrt_three_thirty = Math.sqrt(Math.sqrt(3)*30);
// Guess xyLen/pendulumPeriod, which will get the loop started
var v_1 = xyLen/pendulumPeriod;
// The previous guess -- lets start it way off
var v_1_previous = v_1-100;
//console.log("v_1: "+ v_1);
while (Math.abs(v_1 - v_1_previous) > VELOCITY_ACCURACY) {
v_1_previous = v_1;
// V = (3*T*j*v_1 - 3*D*j - v_1*sqrt(j*v_1)*sqrt_three_thirty) / 3*j
// V' = (-sqrt(j*v_1)*sqrt_three_thirty - (j*v_1*sqrt_three_thirty)/(2*sqrt(j*v_1)) + 3*T*j) / 3*j
var sqrt_j_v = Math.sqrt(jerk*v_1);
// V = (3*T*j*v_1 - 3*D*j - v_1*sqrt_j_v*sqrt_three_thirty) / 3*j
// V' = (-sqrt_j_v*sqrt_three_thirty - (j*v_1*sqrt_three_thirty)/(2*sqrt_j_v) + 3*T*j) / 3*j
var F_v_1 = (3*pendulumPeriod*jerk*v_1 - 3*xyLen*jerk - v_1*sqrt_j_v*sqrt_three_thirty) / 3*jerk;
var Fd_v_1 = (-sqrt_j_v*sqrt_three_thirty - (jerk*v_1*sqrt_three_thirty)/(2*sqrt_j_v) + 3*pendulumPeriod*jerk) / 3*jerk;
v_1 = v_1 - (F_v_1/Fd_v_1);
//console.log("v_1: "+ v_1.toFixed(4) + " F_v_1: " + F_v_1.toFixed(4) + " Fd_v_1: " + Fd_v_1.toFixed(4));
if(isDoubleMove){
console.log("G1 F" + (v_1*60).toFixed(4) + " "+ axis+xyLen + " (Comment: single move)")
}
}
return (v_1*60).toFixed(0);
}
var xJerk = 2500;
var yJerk = 2500;
exports.calcXYmove = function(xlen, ylen, zlen){
// Calculate the total move length
var totalMovelen = Math.sqrt(Math.pow(xlen, 2) + Math.pow(ylen, 2))
console.log("Total Move Len: " + totalMovelen)
// Calculate the actual jerk to be used
var jerk = xJerk; // default
if (xlen == 0) {
// We'll assume we won't ever pass a 0,0 move!!
jerk = yJerk;
} else {
// See https://github.com/synthetos/g2/blob/edge/TinyG2/plan_line.cpp#L134-L211
// for an explanation of the below:
var recip_L2 = 1/(totalMovelen*totalMovelen);
// Start with the x axis
var x_axis_contribution = xlen/totalMovelen;
var y_axis_contribution = ylen/totalMovelen;
var xC = ((xlen*xlen) * recip_L2)/xJerk;
var yC = ((ylen*ylen) * recip_L2)/yJerk;
if (xC > yC) {
jerk = xJerk / Math.abs(x_axis_contribution);
} else {
jerk = yJerk / Math.abs(y_axis_contribution);
}
}
console.log("Calculated Jerk: " + jerk)
var vel = _calcVelocity(totalMovelen, jerk, zlen, "")
console.log("G1 F" + (vel) + " X" + xlen + " Y" + ylen + " (Comment: double move)")
}
exports.calcVelocity = _calcVelocity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment