Compare commits
10 Commits
a68d666d27
...
swerveCode
| Author | SHA1 | Date | |
|---|---|---|---|
|
5de8efd55b
|
|||
|
eb0942d890
|
|||
|
5c4a6909eb
|
|||
|
f1d5cf518f
|
|||
|
73a386fe5a
|
|||
|
6a7f071c17
|
|||
|
052429a724
|
|||
|
9ba978512d
|
|||
|
e9a233653a
|
|||
|
ff5fb1e972
|
20
index.html
20
index.html
@ -26,21 +26,21 @@
|
||||
<legend>Translation & Rotation</legend>
|
||||
|
||||
<div class="control-group">
|
||||
<label for="vx-slider">Move Forward/Backward (m/s)</label>
|
||||
<input type="range" id="vx-slider" min="-3" max="3" step="0.1" value="0">
|
||||
<output id="vx-value">0.0</output>
|
||||
<label for="vx-slider">Strafe Left/Right (pixels/s)</label>
|
||||
<input type="range" id="vx-slider" min="-300" max="300" step="10" value="0">
|
||||
<output id="vx-value">0</output>
|
||||
</div>
|
||||
|
||||
<div class="control-group">
|
||||
<label for="vy-slider">Strafe Left/Right (m/s)</label>
|
||||
<input type="range" id="vy-slider" min="-3" max="3" step="0.1" value="0">
|
||||
<output id="vy-value">0.0</output>
|
||||
<label for="vy-slider">Move Forward/Backward (pixels/s)</label>
|
||||
<input type="range" id="vy-slider" min="-300" max="300" step="10" value="0">
|
||||
<output id="vy-value">0</output>
|
||||
</div>
|
||||
|
||||
<div class="control-group">
|
||||
<label for="omega-slider">Rotation (rad/s)</label>
|
||||
<input type="range" id="omega-slider" min="-3" max="3" step="0.1" value="0">
|
||||
<output id="omega-value">0.0</output>
|
||||
<output id="omega-value">0</output>
|
||||
</div>
|
||||
|
||||
<button id="reset-btn" type="button">Reset Controls</button>
|
||||
@ -50,9 +50,9 @@
|
||||
<legend>Performance Limits</legend>
|
||||
|
||||
<div class="control-group">
|
||||
<label for="max-speed-slider">Max Module Speed (m/s)</label>
|
||||
<input type="range" id="max-speed-slider" min="1" max="5" step="0.1" value="4">
|
||||
<output id="max-speed-value">4.0</output>
|
||||
<label for="max-speed-slider">Max Module Speed (pixels/s)</label>
|
||||
<input type="range" id="max-speed-slider" min="1" max="300" step="10" value="150">
|
||||
<output id="max-speed-value">0</output>
|
||||
</div>
|
||||
</fieldset>
|
||||
</section>
|
||||
|
||||
209
script.js
209
script.js
@ -1,3 +1,7 @@
|
||||
/*
|
||||
* BEGIN CLASS DECLARATIONS
|
||||
*/
|
||||
|
||||
// 2D vector class to make some of the math easier
|
||||
class Vec2D {
|
||||
constructor(x, y) {
|
||||
@ -45,13 +49,17 @@ class SwerveModule {
|
||||
// Swerve drive class to represent the robot as a whole
|
||||
class SwerveDrive {
|
||||
constructor(modulePositionsAndNames) {
|
||||
this.setModules(modulePositionsAndNames);
|
||||
}
|
||||
|
||||
setModules(modulePositionsAndNames) {
|
||||
// Take an array of module positions with a name and create an array of SwerveModule objects
|
||||
this.modules = modulePositionsAndNames.map(module =>
|
||||
new SwerveModule(module.x, module.y, module.name)
|
||||
);
|
||||
}
|
||||
|
||||
drive(velocityX, velocityY, turnSpeed, maxSpeed) {
|
||||
drive(velocityX, velocityY, turnSpeed, maxModuleSpeed) {
|
||||
// Take in a requested speeds and update every module
|
||||
this.modules.forEach(module =>
|
||||
module.calculateState(velocityX, velocityY, turnSpeed)
|
||||
@ -59,10 +67,13 @@ class SwerveDrive {
|
||||
|
||||
// If any speeds exceed the max speed, normalize down so we don't effect movement direction
|
||||
const maxCalculated = Math.max(...this.modules.map(m => m.speed), 0);
|
||||
if (maxCalculated > maxSpeed) {
|
||||
const scale = maxSpeed / maxCalculated;
|
||||
if (maxCalculated > maxModuleSpeed) {
|
||||
const scale = maxModuleSpeed / maxCalculated;
|
||||
this.modules.forEach(module => {
|
||||
module.speed *= scale;
|
||||
module.velocity.x *= scale;
|
||||
module.velocity.y *= scale;
|
||||
module.speed = module.velocity.magnitude();
|
||||
module.angle = module.velocity.angle();
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -71,8 +82,8 @@ class SwerveDrive {
|
||||
// Preset robot generators
|
||||
const PresetConfigs = {
|
||||
twoWheel: (size) => [
|
||||
{ x: 0, y: size / 2, name: "Left" },
|
||||
{ x: 0, y: -size / 2, name: "Right" }
|
||||
{ x: size / 2, y: 0, name: "Left" },
|
||||
{ x: -size / 2, y: 0, name: "Right" }
|
||||
],
|
||||
|
||||
threeWheel: (size) => {
|
||||
@ -95,7 +106,7 @@ const PresetConfigs = {
|
||||
},
|
||||
|
||||
fourWheelRectangle: (size) => {
|
||||
const width = size * 0.7;
|
||||
const width = size * 0.5;
|
||||
const length = size;
|
||||
return [
|
||||
{ x: length / 2, y: width / 2, name: "FL" },
|
||||
@ -134,6 +145,11 @@ const PresetConfigs = {
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* END CLASS DECLARATIONS
|
||||
* BEGIN DOM VARIABLES
|
||||
*/
|
||||
|
||||
// Get all control elements
|
||||
const vxSlider = document.getElementById('vx-slider');
|
||||
const vySlider = document.getElementById('vy-slider');
|
||||
@ -160,22 +176,77 @@ const preset4RectBtn = document.getElementById('preset-4rect');
|
||||
const preset6WheelBtn = document.getElementById('preset-6wheel');
|
||||
const preset8WheelBtn = document.getElementById('preset-8wheel');
|
||||
|
||||
// Add event listeners for drive controls
|
||||
/*
|
||||
* END DOM VARIABLES
|
||||
* BEGIN LISTENER CODE
|
||||
*/
|
||||
|
||||
|
||||
vxSlider.addEventListener('input', (e) => {
|
||||
vxOutput.textContent = parseFloat(e.target.value).toFixed(1);
|
||||
vxOutput.textContent = parseFloat(e.target.value);
|
||||
});
|
||||
vxOutput.textContent = parseFloat(vxSlider.value);
|
||||
|
||||
vySlider.addEventListener('input', (e) => {
|
||||
vyOutput.textContent = parseFloat(e.target.value).toFixed(1);
|
||||
vyOutput.textContent = parseFloat(e.target.value);
|
||||
});
|
||||
vyOutput.textContent = parseFloat(vySlider.value);
|
||||
|
||||
omegaSlider.addEventListener('input', (e) => {
|
||||
omegaOutput.textContent = parseFloat(e.target.value).toFixed(1);
|
||||
omegaOutput.textContent = parseFloat(e.target.value);
|
||||
});
|
||||
omegaOutput.textContent = parseFloat(omegaSlider.value);
|
||||
|
||||
maxSpeedSlider.addEventListener('input', (e) => {
|
||||
maxSpeedOutput.textContent = parseFloat(e.target.value).toFixed(1);
|
||||
maxSpeedOutput.textContent = parseFloat(e.target.value);
|
||||
});
|
||||
maxSpeedOutput.textContent = parseFloat(maxSpeedSlider.value);
|
||||
|
||||
resetBtn.addEventListener('click', (e) => {
|
||||
vxSlider.value = 0;
|
||||
vySlider.value = 0;
|
||||
omegaSlider.value = 0;
|
||||
|
||||
vxOutput.textContent = parseFloat(vxSlider.value);
|
||||
vyOutput.textContent = parseFloat(vySlider.value);
|
||||
omegaOutput.textContent = parseFloat(omegaSlider.value);
|
||||
});
|
||||
|
||||
// Preset button event listeners
|
||||
preset2WheelBtn.addEventListener('click', () => {
|
||||
const positions = PresetConfigs.twoWheel(robotSize);
|
||||
robot.setModules(positions);
|
||||
});
|
||||
|
||||
preset3WheelBtn.addEventListener('click', () => {
|
||||
const positions = PresetConfigs.threeWheel(robotSize);
|
||||
robot.setModules(positions);
|
||||
});
|
||||
|
||||
preset4WheelBtn.addEventListener('click', () => {
|
||||
const positions = PresetConfigs.fourWheelSquare(robotSize);
|
||||
robot.setModules(positions);
|
||||
});
|
||||
|
||||
preset4RectBtn.addEventListener('click', () => {
|
||||
const positions = PresetConfigs.fourWheelRectangle(robotSize);
|
||||
robot.setModules(positions);
|
||||
});
|
||||
|
||||
preset6WheelBtn.addEventListener('click', () => {
|
||||
const positions = PresetConfigs.sixWheel(robotSize);
|
||||
robot.setModules(positions);
|
||||
});
|
||||
|
||||
preset8WheelBtn.addEventListener('click', () => {
|
||||
const positions = PresetConfigs.eightWheel(robotSize);
|
||||
robot.setModules(positions);
|
||||
});
|
||||
|
||||
/*
|
||||
* END LISTENER CODE
|
||||
* BEGIN ANIMATION CODE
|
||||
*/
|
||||
|
||||
// Get the canvas and context as constants
|
||||
const canvas = document.getElementById('swerve-canvas');
|
||||
@ -184,21 +255,73 @@ const ctx = canvas.getContext('2d');
|
||||
// Get CSS variables for use in canvas
|
||||
const rootStyles = getComputedStyle(document.documentElement);
|
||||
|
||||
function drawGrid(ctx, sideLength, gridSquareSize) {
|
||||
function drawGrid(ctx, sideLength, gridSquareSize, xOffset, yOffset, rotation) {
|
||||
ctx.save();
|
||||
|
||||
// Apply rotation transform
|
||||
ctx.rotate(-rotation);
|
||||
|
||||
ctx.strokeStyle = rootStyles.getPropertyValue('--grid-color');
|
||||
ctx.lineWidth = 1;
|
||||
const startX = (-sideLength / 2) - xOffset;
|
||||
const endX = (sideLength / 2) - xOffset;
|
||||
const startY = (-sideLength / 2) - yOffset;
|
||||
const endY = (sideLength / 2) - yOffset;
|
||||
|
||||
for (let i = -sideLength / 2; i <= sideLength / 2; i += gridSquareSize) {
|
||||
// Draw vertical lines
|
||||
for (let i = startX; i <= endX; i += gridSquareSize) {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(i, -sideLength / 2);
|
||||
ctx.lineTo(i, sideLength / 2);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
// Draw horizontal lines
|
||||
for (let i = startY; i <= endY; i += gridSquareSize) {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(-sideLength / 2, i);
|
||||
ctx.lineTo(sideLength / 2, i);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
function drawModule(ctx, module) {
|
||||
const x = module.position.x;
|
||||
const y = module.position.y;
|
||||
const arrowLength = Math.max(module.speed / 2, 5);
|
||||
|
||||
ctx.save();
|
||||
ctx.translate(x, y);
|
||||
|
||||
ctx.fillStyle = rootStyles.getPropertyValue('--swerve-fill-color');
|
||||
ctx.beginPath();
|
||||
ctx.arc(0, 0, 10, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
|
||||
ctx.strokeStyle = rootStyles.getPropertyValue('--swerve-module-color');
|
||||
ctx.lineWidth = 4;
|
||||
ctx.stroke();
|
||||
|
||||
// Draw velocity arrow if module is moving
|
||||
if (module.speed > 0.01) {
|
||||
ctx.strokeStyle = rootStyles.getPropertyValue('--swerve-arrow-color');
|
||||
ctx.fillStyle = rootStyles.getPropertyValue('--swerve-arrow-color');
|
||||
ctx.lineWidth = 4;
|
||||
|
||||
const endX = arrowLength * Math.cos(module.angle);
|
||||
const endY = arrowLength * Math.sin(module.angle);
|
||||
|
||||
// Arrow line
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(0, 0);
|
||||
ctx.lineTo(endX, endY);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
function drawRobot(ctx, robot) {
|
||||
@ -207,7 +330,6 @@ function drawRobot(ctx, robot) {
|
||||
ctx.lineWidth = 4;
|
||||
|
||||
const modules = robot.modules.sort((a, b) => Math.atan2(a.position.y, a.position.x) - Math.atan2(b.position.y, b.position.x));
|
||||
console.log(modules);
|
||||
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(modules[0].position.x, modules[0].position.y);
|
||||
@ -217,8 +339,59 @@ function drawRobot(ctx, robot) {
|
||||
ctx.closePath();
|
||||
ctx.fill();
|
||||
ctx.stroke();
|
||||
|
||||
modules.forEach(module => drawModule(ctx, module));
|
||||
}
|
||||
|
||||
ctx.translate(canvas.width / 2, canvas.height / 2);
|
||||
drawGrid(ctx, 600, 50);
|
||||
drawRobot(ctx, new SwerveDrive(PresetConfigs.eightWheel(200)));
|
||||
|
||||
// Initialize Variables
|
||||
const robotSize = 200;
|
||||
const robot = new SwerveDrive(PresetConfigs.fourWheelSquare(robotSize));
|
||||
let xSpeed = 0;
|
||||
let ySpeed = 0;
|
||||
let turnSpeed = -1;
|
||||
let robotRotation = 0; // Track cumulative robot rotation for grid display
|
||||
|
||||
let gridSquareSize = 25;
|
||||
let xGridOffset = 0;
|
||||
let yGridOffset = 0;
|
||||
robot.drive(xSpeed, ySpeed, 0, 500);
|
||||
|
||||
function animate() {
|
||||
// Clear and set up canvas
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
ctx.save();
|
||||
ctx.translate(canvas.width / 2, canvas.height / 2);
|
||||
|
||||
// Update speeds based on sliders
|
||||
xSpeed = parseFloat(vxSlider.value);
|
||||
ySpeed = -parseFloat(vySlider.value);
|
||||
turnSpeed = parseFloat(omegaSlider.value);
|
||||
|
||||
// Animate the grid with robot movement
|
||||
let offsetSpeedDivisor = (100 - gridSquareSize <= 0 ? 1 : 100 - gridSquareSize);
|
||||
robotRotation += turnSpeed * 0.01; // Scale factor for reasonable rotation speed
|
||||
|
||||
// Convert robot velocities to world velocities for grid movement
|
||||
const cosRot = Math.cos(robotRotation);
|
||||
const sinRot = Math.sin(robotRotation);
|
||||
const worldVx = xSpeed * cosRot - ySpeed * sinRot;
|
||||
const worldVy = xSpeed * sinRot + ySpeed * cosRot;
|
||||
|
||||
// Update grid offsets based on robot movement
|
||||
xGridOffset = (xGridOffset + (worldVx / offsetSpeedDivisor)) % gridSquareSize;
|
||||
yGridOffset = (yGridOffset + (worldVy / offsetSpeedDivisor)) % gridSquareSize;
|
||||
|
||||
// Draw the robot and it's movement. Grid should be oversized so movement
|
||||
// doesn't find the edge of the grid
|
||||
drawGrid(ctx, canvas.width * 2, gridSquareSize, xGridOffset, yGridOffset, robotRotation);
|
||||
drawRobot(ctx, robot);
|
||||
|
||||
robot.drive(xSpeed, ySpeed, turnSpeed, parseFloat(maxSpeedSlider.value));
|
||||
|
||||
// Do it all over again
|
||||
ctx.restore();
|
||||
requestAnimationFrame(animate);
|
||||
}
|
||||
|
||||
animate();
|
||||
@ -23,10 +23,11 @@
|
||||
|
||||
--shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
|
||||
|
||||
--grid-color: #e4e4e733;
|
||||
--grid-color: #e4e4e78c;
|
||||
--robot-frame-color: #1e81bf;
|
||||
--robot-fill-color: #5f9ec4;
|
||||
--swerve-module-color: #7bb6db;
|
||||
--swerve-module-color: #1e81bf;
|
||||
--swerve-fill-color: #7986cb;
|
||||
--swerve-arrow-color: #c73c3c;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user