Skip to content

Instantly share code, notes, and snippets.

@shduff
Created October 22, 2018 21:20
Show Gist options
  • Save shduff/6bc0252c142378d5be3d8bfe0da275b1 to your computer and use it in GitHub Desktop.
Save shduff/6bc0252c142378d5be3d8bfe0da275b1 to your computer and use it in GitHub Desktop.
Plinkquito turn calculations when going to a point
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script>
// This is the x and y of the next point you're moving to
var nextPoint_x = 33;
var nextPoint_y = 72;
// Measure the x and y point of the Plinkquito chassis when totally unspooled
var startingPoint_x = 0;
var startingPoint_y = 0;
// Measure the x and y positions of your motors
var leftMotor_x = -200;
var leftMotor_y = 150;
var rightMotor_x = 200;
var rightMotor_y = 150;
// Measure the string width and the motor diameter
var stringWidth = .032;
var motorDiameter = 2.5;
var motorCircumference = Math.PI*motorDiameter;
// Set these percentages to test different proportions of error for spiraling and doubling
var percentSpiraling = .5;
var percentDoubling = .5;
// General purpose function to calculate the distance between two points with coordinates (x1, y1) and (x2, y2)
function calculateDistance (x1, y1, x2, y2) {
var deltaX = x2 - x1;
var deltaY = y2 - y1;
var distance = Math.sqrt(Math.pow(deltaX, 2)+Math.pow(deltaY, 2));
return distance;
}
// Calculate initial, unspooled length from right motor
var initRightLength = calculateDistance(rightMotor_x, rightMotor_y, startingPoint_x, startingPoint_y);
// Calculate initial, unspooled length from left motor
var initLeftLength = calculateDistance(leftMotor_x, leftMotor_y, startingPoint_x, startingPoint_y);
// General purpose funtion to calculate turns needed to get to get to a certain string length
function calculateTurns (initLength, currLength) {
var spooledLength = initLength - currLength;
var numDoublingTurns = (Math.sqrt(Math.PI*Math.pow(motorDiameter, 2) + 2*Math.PI*motorDiameter*stringWidth + 4*spooledLength*stringWidth + Math.PI*Math.pow(stringWidth, 2)) - Math.sqrt(Math.PI)*motorDiameter - Math.sqrt(Math.PI)*stringWidth)/(2*Math.sqrt(Math.PI)*stringWidth);
var numSpiralingTurns = spooledLength / Math.sqrt(Math.pow((Math.PI*motorDiameter), 2) + Math.pow(stringWidth, 2));
var turns = percentDoubling*numDoublingTurns + percentSpiraling*numSpiralingTurns;
console.log("Turns is "+turns);
return turns;
}
// Calculate left motor turns to get to next point
calculateTurns (initLeftLength, calculateDistance(leftMotor_x, leftMotor_y, nextPoint_x, nextPoint_y));
// Calculate right motor turns to get to next point
calculateTurns (initRightLength, calculateDistance(rightMotor_x, rightMotor_y, nextPoint_x, nextPoint_y));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment