Public
Edited
Apr 15, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// does not take into account sharp corners or anything yet
function computeVelocity(toolpath, rampSpeed, maxVelocity) {
let result = [];

let currentTime = 0;
let currentVelocity = 0;

for (let i = 0; i < toolpath.length - 1; i++) {
let distance = Math.sqrt(
Math.pow(toolpath[i + 1].x - toolpath[i].x, 2) +
Math.pow(toolpath[i + 1].y - toolpath[i].y, 2)
);

let timeToReach = distance / rampSpeed;

// Acceleration phase
while (currentVelocity < maxVelocity && currentTime < timeToReach) {
result.push({ time: currentTime, velocity: currentVelocity });
currentVelocity += rampSpeed * 0.01; // Small time step for smoother plot
currentTime += 0.01;
}

// Constant velocity phase
while (currentTime < timeToReach) {
result.push({ time: currentTime, velocity: maxVelocity });
currentTime += 0.01;
}
}

return result;
}
Insert cell
Insert cell
Insert cell
points = computeVelocity(starPoints, rampSpeed, maxVelocity)
Insert cell
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.line(points, {
x: "time",
y: "velocity"
})
]
})
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more