Compare commits
4 Commits
evenlySpac
...
e593efafa4
| Author | SHA1 | Date | |
|---|---|---|---|
|
e593efafa4
|
|||
|
7d02789a39
|
|||
|
b1b67fb0bd
|
|||
|
62bb28ca9d
|
28
README.md
Normal file
28
README.md
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# MuneBase Swerve Sim
|
||||||
|
|
||||||
|
A browser-based simulator for Swerve Drive robots.
|
||||||
|
|
||||||
|
Live demo: https://swerve-vis.munebase.dev
|
||||||
|
|
||||||
|
|
||||||
|
You can host the project locally with a simple static server and open it in your browser:
|
||||||
|
|
||||||
|
- Python 3 (built-in):
|
||||||
|
|
||||||
|
python3 -m http.server 8000
|
||||||
|
|
||||||
|
Then open: http://localhost:8000
|
||||||
|
|
||||||
|
- Node.js (with npx):
|
||||||
|
|
||||||
|
npx http-server -c-1 .
|
||||||
|
|
||||||
|
Open `index.html` from the project root in your browser if your editor provides a static preview.
|
||||||
|
|
||||||
|
Notes
|
||||||
|
|
||||||
|
- Third-party scripts are in the `vendor/` directory.
|
||||||
|
|
||||||
|
License
|
||||||
|
|
||||||
|
This repository is provided as-is.
|
||||||
@ -199,7 +199,7 @@ if scale < 1:
|
|||||||
<legend>Translation & Rotation</legend>
|
<legend>Translation & Rotation</legend>
|
||||||
|
|
||||||
<div class="control-group">
|
<div class="control-group">
|
||||||
<button id="control-mode-toggle" type="button">Switch to Keyboard Controls (WASD + QE)</button>
|
<button id="control-mode-toggle" type="button">Switch to Keyboard/Joystick Controls</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="slider-controls">
|
<div id="slider-controls">
|
||||||
|
|||||||
218
script.js
218
script.js
@ -4,6 +4,81 @@
|
|||||||
|
|
||||||
import GrahamScan from "./vendor/lucio/graham-scan.mjs";
|
import GrahamScan from "./vendor/lucio/graham-scan.mjs";
|
||||||
|
|
||||||
|
class Joystick {
|
||||||
|
constructor(ctx, x, y, radius) {
|
||||||
|
this.ctx = ctx;
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.radius = radius;
|
||||||
|
this.nubX = x;
|
||||||
|
this.nubY = y;
|
||||||
|
this.active = false;
|
||||||
|
this.visible = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
draw() {
|
||||||
|
if (!this.visible)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.ctx.save();
|
||||||
|
|
||||||
|
this.ctx.beginPath();
|
||||||
|
this.ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
|
||||||
|
this.ctx.fillStyle = rootStyles.getPropertyValue('--primary-dark-blue');
|
||||||
|
this.ctx.fill();
|
||||||
|
|
||||||
|
|
||||||
|
this.ctx.beginPath();
|
||||||
|
this.ctx.arc(this.nubX, this.nubY, this.radius / 3, 0, Math.PI * 2);
|
||||||
|
this.ctx.fillStyle = rootStyles.getPropertyValue('--accent-blue');
|
||||||
|
this.ctx.fill();
|
||||||
|
|
||||||
|
this.ctx.restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
checkShouldActivate(x, y) {
|
||||||
|
if (!this.touchInRange(x, y))
|
||||||
|
return;
|
||||||
|
this.active = true;
|
||||||
|
this.processTouch(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
deactivate() {
|
||||||
|
this.active = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
touchInRange(x, y) {
|
||||||
|
const deltaX = x - this.x;
|
||||||
|
const deltaY = y - this.y;
|
||||||
|
const dist = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
|
||||||
|
return dist <= this.radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
processTouch(x, y) {
|
||||||
|
if (this.touchInRange(x, y) && this.active) {
|
||||||
|
this.nubX = x;
|
||||||
|
this.nubY = y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reset() {
|
||||||
|
this.nubX = this.x;
|
||||||
|
this.nubY = this.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
getX() {
|
||||||
|
return (this.nubX - this.x) / this.radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
getY() {
|
||||||
|
return (this.y - this.nubY) / this.radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsVisible(visible) {
|
||||||
|
this.visible = visible;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 2D vector class to make some of the math easier
|
// 2D vector class to make some of the math easier
|
||||||
class Vec2D {
|
class Vec2D {
|
||||||
constructor(x, y) {
|
constructor(x, y) {
|
||||||
@ -269,6 +344,15 @@ const PresetConfigs = {
|
|||||||
* BEGIN DOM VARIABLES
|
* BEGIN DOM VARIABLES
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Get canvas
|
||||||
|
const canvas = document.getElementById('swerve-canvas');
|
||||||
|
|
||||||
|
// Get the canvas context as constant
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
|
// Get CSS variables for use in canvas
|
||||||
|
const rootStyles = getComputedStyle(document.documentElement);
|
||||||
|
|
||||||
// Get all control elements
|
// Get all control elements
|
||||||
const vxSlider = document.getElementById('vx-slider');
|
const vxSlider = document.getElementById('vx-slider');
|
||||||
const vySlider = document.getElementById('vy-slider');
|
const vySlider = document.getElementById('vy-slider');
|
||||||
@ -379,17 +463,21 @@ controlModeToggle.addEventListener('click', () => {
|
|||||||
vxOutput.textContent = '0';
|
vxOutput.textContent = '0';
|
||||||
vyOutput.textContent = '0';
|
vyOutput.textContent = '0';
|
||||||
omegaOutput.textContent = '0';
|
omegaOutput.textContent = '0';
|
||||||
|
leftJoystick.setIsVisible(true);
|
||||||
|
rightJoystick.setIsVisible(true);
|
||||||
} else {
|
} else {
|
||||||
// Switch to slider mode
|
// Switch to slider mode
|
||||||
sliderControls.style.display = 'block';
|
sliderControls.style.display = 'block';
|
||||||
keyboardControls.style.display = 'none';
|
keyboardControls.style.display = 'none';
|
||||||
controlModeToggle.textContent = 'Switch to Keyboard Controls (WASD + QE)';
|
controlModeToggle.textContent = 'Switch to Keyboard/Joystick Controls';
|
||||||
|
|
||||||
// Reset manual input state
|
// Reset manual input state
|
||||||
Object.keys(keyState).forEach(key => keyState[key] = false);
|
Object.keys(keyState).forEach(key => keyState[key] = false);
|
||||||
manualInputVelX = 0;
|
manualInputVelX = 0;
|
||||||
manualInputVelY = 0;
|
manualInputVelY = 0;
|
||||||
manualInputOmega = 0;
|
manualInputOmega = 0;
|
||||||
|
leftJoystick.setIsVisible(false);
|
||||||
|
rightJoystick.setIsVisible(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -552,6 +640,105 @@ applyCustomBtn.addEventListener('click', () => {
|
|||||||
updateModuleDisplays(robot);
|
updateModuleDisplays(robot);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function convertTouchToCanvas(inCoords) {
|
||||||
|
const rect = canvas.getBoundingClientRect();
|
||||||
|
|
||||||
|
// Calculate the scale factor between display size and canvas internal size
|
||||||
|
const scaleX = canvas.width / rect.width;
|
||||||
|
const scaleY = canvas.height / rect.height;
|
||||||
|
|
||||||
|
// Convert client coordinates to canvas coordinates, accounting for scaling
|
||||||
|
const x = (inCoords.x - rect.left) * scaleX - canvas.width / 2;
|
||||||
|
const y = (inCoords.y - rect.top) * scaleY - canvas.height / 2;
|
||||||
|
|
||||||
|
return { x, y };
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.addEventListener('mousedown', (event) => {
|
||||||
|
const canvasCoords = convertTouchToCanvas({ x: event.clientX, y: event.clientY });
|
||||||
|
leftJoystick.checkShouldActivate(canvasCoords.x, canvasCoords.y);
|
||||||
|
rightJoystick.checkShouldActivate(canvasCoords.x, canvasCoords.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
canvas.addEventListener('mousemove', (event) => {
|
||||||
|
const canvasCoords = convertTouchToCanvas({ x: event.clientX, y: event.clientY });
|
||||||
|
leftJoystick.processTouch(canvasCoords.x, canvasCoords.y);
|
||||||
|
rightJoystick.processTouch(canvasCoords.x, canvasCoords.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
canvas.addEventListener('mouseup', (event) => {
|
||||||
|
leftJoystick.deactivate();
|
||||||
|
rightJoystick.deactivate();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Touch event listeners for mobile/tablet support
|
||||||
|
canvas.addEventListener('touchstart', (event) => {
|
||||||
|
event.preventDefault(); // Prevent scrolling and default touch behavior
|
||||||
|
|
||||||
|
for (let i = 0; i < event.touches.length; i++) {
|
||||||
|
const touch = event.touches[i];
|
||||||
|
const canvasCoords = convertTouchToCanvas({ x: touch.clientX, y: touch.clientY });
|
||||||
|
// alert(`X: ${canvasCoords.x}, Y: ${canvasCoords.y}`);
|
||||||
|
leftJoystick.checkShouldActivate(canvasCoords.x, canvasCoords.y);
|
||||||
|
rightJoystick.checkShouldActivate(canvasCoords.x, canvasCoords.y);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
canvas.addEventListener('touchmove', (event) => {
|
||||||
|
event.preventDefault(); // Prevent scrolling while using joysticks
|
||||||
|
|
||||||
|
for (let i = 0; i < event.touches.length; i++) {
|
||||||
|
const touch = event.touches[i];
|
||||||
|
const canvasCoords = convertTouchToCanvas({ x: touch.clientX, y: touch.clientY });
|
||||||
|
leftJoystick.processTouch(canvasCoords.x, canvasCoords.y);
|
||||||
|
rightJoystick.processTouch(canvasCoords.x, canvasCoords.y);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
canvas.addEventListener('touchend', (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
// If no touches remain, deactivate both joysticks
|
||||||
|
if (event.touches.length === 0) {
|
||||||
|
leftJoystick.deactivate();
|
||||||
|
rightJoystick.deactivate();
|
||||||
|
} else {
|
||||||
|
// Check if the remaining touches are still in range of the joysticks
|
||||||
|
let leftStillActive = false;
|
||||||
|
let rightStillActive = false;
|
||||||
|
|
||||||
|
for (let i = 0; i < event.touches.length; i++) {
|
||||||
|
const touch = event.touches[i];
|
||||||
|
const canvasCoords = convertTouchToCanvas({ x: touch.clientX, y: touch.clientY });
|
||||||
|
|
||||||
|
if (leftJoystick.touchInRange(canvasCoords.x, canvasCoords.y)) {
|
||||||
|
leftStillActive = true;
|
||||||
|
}
|
||||||
|
if (rightJoystick.touchInRange(canvasCoords.x, canvasCoords.y)) {
|
||||||
|
rightStillActive = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!leftStillActive) {
|
||||||
|
leftJoystick.deactivate();
|
||||||
|
leftJoystick.reset();
|
||||||
|
}
|
||||||
|
if (!rightStillActive) {
|
||||||
|
rightJoystick.deactivate();
|
||||||
|
rightJoystick.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
canvas.addEventListener('touchcancel', (event) => {
|
||||||
|
// Handle touch cancellation (e.g., when system interrupts)
|
||||||
|
leftJoystick.deactivate();
|
||||||
|
leftJoystick.reset();
|
||||||
|
rightJoystick.deactivate();
|
||||||
|
rightJoystick.reset();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* END LISTENER CODE
|
* END LISTENER CODE
|
||||||
* BEGIN DYNAMIC DOM FUNCTIONS
|
* BEGIN DYNAMIC DOM FUNCTIONS
|
||||||
@ -696,12 +883,8 @@ function updateModuleDisplays(robot) {
|
|||||||
* BEGIN ANIMATION CODE
|
* BEGIN ANIMATION CODE
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Get the canvas and context as constants
|
const leftJoystick = new Joystick(ctx, -250, 250, 100);
|
||||||
const canvas = document.getElementById('swerve-canvas');
|
const rightJoystick = new Joystick(ctx, 250, 250, 100);
|
||||||
const ctx = canvas.getContext('2d');
|
|
||||||
|
|
||||||
// Get CSS variables for use in canvas
|
|
||||||
const rootStyles = getComputedStyle(document.documentElement);
|
|
||||||
|
|
||||||
function drawGrid(ctx, sideLength, gridSquareSize, xOffset, yOffset) {
|
function drawGrid(ctx, sideLength, gridSquareSize, xOffset, yOffset) {
|
||||||
ctx.save();
|
ctx.save();
|
||||||
@ -806,8 +989,12 @@ function drawRobot(ctx, robot, heading) {
|
|||||||
ctx.restore(); // Restore to remove rotation
|
ctx.restore(); // Restore to remove rotation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Initialize Variables
|
// Initialize Variables
|
||||||
|
// Joysticks
|
||||||
|
const supportsTouch = (navigator.maxTouchPoints > 0);
|
||||||
|
|
||||||
|
|
||||||
|
// General robot
|
||||||
const robotSize = 200;
|
const robotSize = 200;
|
||||||
const defaultModulePositions = PresetConfigs.fourWheelSquare(robotSize);
|
const defaultModulePositions = PresetConfigs.fourWheelSquare(robotSize);
|
||||||
const robot = new SwerveDrive(defaultModulePositions, "4-Wheel Square");
|
const robot = new SwerveDrive(defaultModulePositions, "4-Wheel Square");
|
||||||
@ -829,9 +1016,15 @@ function animate() {
|
|||||||
|
|
||||||
// Update speeds based on control mode
|
// Update speeds based on control mode
|
||||||
if (isManualInputMode) {
|
if (isManualInputMode) {
|
||||||
xSpeed = manualInputVelX;
|
const maxSpeed = parseFloat(keyboardMaxSpeed.value);
|
||||||
ySpeed = -manualInputVelY; // Negative because canvas Y axis is inverted
|
const maxRotation = parseFloat(keyboardMaxRotation.value);
|
||||||
turnSpeed = manualInputOmega;
|
// xSpeed = manualInputVelX;
|
||||||
|
// ySpeed = -manualInputVelY; // Negative because canvas Y axis is inverted
|
||||||
|
// turnSpeed = manualInputOmega;
|
||||||
|
|
||||||
|
xSpeed = leftJoystick.getX() * maxSpeed;
|
||||||
|
ySpeed = -leftJoystick.getY() * maxSpeed;
|
||||||
|
turnSpeed = rightJoystick.getX() * maxRotation;
|
||||||
} else {
|
} else {
|
||||||
xSpeed = parseFloat(vxSlider.value);
|
xSpeed = parseFloat(vxSlider.value);
|
||||||
ySpeed = -parseFloat(vySlider.value);
|
ySpeed = -parseFloat(vySlider.value);
|
||||||
@ -870,6 +1063,9 @@ function animate() {
|
|||||||
drawGrid(ctx, canvas.width * 2, gridSquareSize, xGridOffset, yGridOffset);
|
drawGrid(ctx, canvas.width * 2, gridSquareSize, xGridOffset, yGridOffset);
|
||||||
drawRobot(ctx, robot, robot.gyroHeading);
|
drawRobot(ctx, robot, robot.gyroHeading);
|
||||||
|
|
||||||
|
leftJoystick.draw();
|
||||||
|
rightJoystick.draw();
|
||||||
|
|
||||||
// Do it all over again
|
// Do it all over again
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
requestAnimationFrame(animate);
|
requestAnimationFrame(animate);
|
||||||
|
|||||||
@ -362,3 +362,9 @@ button:hover {
|
|||||||
color: var(--text-light);
|
color: var(--text-light);
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media only screen and (max-width: 768px) {
|
||||||
|
main {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user