Spaces:
Running
Running
File size: 18,996 Bytes
18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
import type { RobotState } from "$lib/types/robot";
import type {
MasterDriver,
SlaveDriver,
DriverJointState,
ConnectionStatus,
RobotCommand,
CommandSequence,
UnsubscribeFn
} from "$lib/types/robotDriver";
import type IUrdfJoint from "@/components/3d/robot/URDF/interfaces/IUrdfJoint";
import { getRobotPollingConfig } from "$lib/configs/performanceConfig";
/**
* Enhanced joint state that combines URDF data with driver state
*/
export interface ManagedJointState {
// URDF properties
name: string;
urdfJoint: IUrdfJoint;
servoId?: number;
// Current state
virtualValue: number; // What the UI shows/controls
realValue?: number; // What the physical robot reports
commandedValue: number; // What was last commanded by master/manual
isActive: boolean; // Whether this joint is being controlled
// Calibration state
calibrationOffset: number; // Offset to apply: realValue = rawValue + offset
restPosition: number; // Expected position for this joint in rest mode
// Sync state
lastVirtualUpdate: Date;
lastRealUpdate?: Date;
lastCommandUpdate?: Date;
syncError?: string;
}
/**
* Calibration state for robot
*/
export interface CalibrationState {
isCalibrated: boolean;
calibrationTime?: Date;
offsets: { [jointName: string]: number };
}
/**
* Control source information
*/
export interface ControlState {
hasActiveMaster: boolean;
masterName?: string;
manualControlEnabled: boolean;
lastCommandSource: "master" | "manual" | "none";
commandQueue: RobotCommand[];
}
/**
* Robot instance that manages URDF visualization and master-slave connections
*/
export class Robot {
readonly id: string;
readonly robotState: RobotState;
// Joint management
private _joints = $state<ManagedJointState[]>([]);
// Master driver (command source)
private _master = $state<MasterDriver | undefined>(undefined);
private _masterStatus = $state<ConnectionStatus>({ isConnected: false });
// Slave drivers (execution targets)
private _slaves = $state<SlaveDriver[]>([]);
private _slaveStatuses = $state<Map<string, ConnectionStatus>>(new Map());
// Control state
private _controlState = $state<ControlState>({
hasActiveMaster: false,
manualControlEnabled: true,
lastCommandSource: "none",
commandQueue: []
});
// Calibration management
private _calibrationState = $state<CalibrationState>({
isCalibrated: false,
offsets: {}
});
// Sync control - use performance config
private syncIntervalId?: number;
private readonly syncInterval = getRobotPollingConfig().ROBOT_SYNC_INTERVAL_MS;
// Event subscriptions
private unsubscribeCallbacks: UnsubscribeFn[] = [];
// Reactive getters
get joints() {
return this._joints;
}
get master() {
return this._master;
}
get masterStatus() {
return this._masterStatus;
}
get slaves() {
return this._slaves;
}
get slaveStatuses() {
return this._slaveStatuses;
}
get controlState() {
return this._controlState;
}
get calibrationState() {
return this._calibrationState;
}
get isCalibrated() {
return this._calibrationState.isCalibrated;
}
get activeJoints() {
return this._joints.filter((joint) => joint.isActive);
}
get connectedSlaves() {
return this._slaves.filter((slave) => this._slaveStatuses.get(slave.id)?.isConnected);
}
get manualControlEnabled() {
return this._controlState.manualControlEnabled && !this._controlState.hasActiveMaster;
}
constructor(id: string, robotState: RobotState) {
this.id = id;
this.robotState = robotState;
this.initializeJoints();
}
/**
* Initialize joint states from URDF
*/
private initializeJoints(): void {
const servoMap = this.robotState.urdfConfig.jointNameIdMap || {};
const restPositions = this.robotState.urdfConfig.restPosition || {};
this._joints = this.robotState.robot.joints
.filter((urdfJoint) => urdfJoint.type === "revolute" || urdfJoint.type === "continuous")
.map((urdfJoint) => {
const jointName = urdfJoint.name || "";
const restPosition = restPositions[jointName] || 0;
// Initialize URDF joint rotation to rest position
if (urdfJoint.type === "revolute") {
const radians = (restPosition * Math.PI) / 180;
const axis = urdfJoint.axis_xyz || [0, 0, 1];
urdfJoint.rotation = [radians * axis[0], radians * axis[1], radians * axis[2]];
}
return {
name: jointName,
urdfJoint,
servoId: servoMap[jointName],
virtualValue: restPosition,
commandedValue: restPosition,
realValue: undefined,
isActive: !!servoMap[jointName],
lastVirtualUpdate: new Date(),
lastRealUpdate: undefined,
lastCommandUpdate: undefined,
syncError: undefined,
calibrationOffset: 0,
restPosition
};
});
console.log(`Initialized ${this._joints.length} joints for robot ${this.id}`);
}
// ============= MASTER MANAGEMENT =============
/**
* Connect a master driver (command source)
*/
async setMaster(master: MasterDriver): Promise<void> {
// Disconnect existing master
if (this._master) {
await this.removeMaster();
}
this._master = master;
this._masterStatus = master.status;
// Subscribe to master events
const statusUnsub = master.onStatusChange((status) => {
this._masterStatus = status;
this._controlState.hasActiveMaster = status.isConnected;
this._controlState.manualControlEnabled = !status.isConnected;
});
const commandUnsub = master.onCommand((commands) => {
this.handleMasterCommands(commands);
});
const sequenceUnsub = master.onSequence((sequence) => {
this.handleMasterSequence(sequence);
});
this.unsubscribeCallbacks.push(statusUnsub, commandUnsub, sequenceUnsub);
// Update control state
this._controlState.hasActiveMaster = master.status.isConnected;
this._controlState.manualControlEnabled = !master.status.isConnected;
this._controlState.masterName = master.name;
// Start the master (for masters that need to poll/generate commands)
if (master.status.isConnected) {
await master.start();
}
console.log(`Master ${master.name} connected to robot ${this.id}`);
}
/**
* Remove the current master
*/
async removeMaster(): Promise<void> {
if (!this._master) return;
if (this._master.status.isConnected) {
// Stop the master first, then disconnect
try {
await this._master.stop();
} catch (error) {
console.warn(`Error stopping master ${this._master.name}:`, error);
}
await this._master.disconnect();
}
this._master = undefined;
this._masterStatus = { isConnected: false };
this._controlState.hasActiveMaster = false;
this._controlState.manualControlEnabled = true;
this._controlState.masterName = undefined;
console.log(`Master removed from robot ${this.id}. Manual control restored.`);
}
// ============= SLAVE MANAGEMENT =============
/**
* Add a slave driver (execution target)
*/
async addSlave(slave: SlaveDriver): Promise<void> {
// Check if slave already exists
if (this._slaves.find((s) => s.id === slave.id)) {
throw new Error(`Slave ${slave.id} already connected to robot ${this.id}`);
}
// Connect the slave
await slave.connect();
this._slaves.push(slave);
this._slaveStatuses.set(slave.id, slave.status);
// Subscribe to slave events
const statusUnsub = slave.onStatusChange((status) => {
this._slaveStatuses.set(slave.id, status);
});
const stateUnsub = slave.onStateUpdate((driverStates) => {
this.updateFromSlave(slave.id, driverStates);
});
this.unsubscribeCallbacks.push(statusUnsub, stateUnsub);
// Move slave to rest position for safety
await this.moveSlaveToRestPosition(slave);
console.log(`Slave ${slave.name} (${slave.id}) connected to robot ${this.id}`);
}
/**
* Remove a slave driver
*/
async removeSlave(slaveId: string): Promise<void> {
const slaveIndex = this._slaves.findIndex((s) => s.id === slaveId);
if (slaveIndex === -1) return;
const slave = this._slaves[slaveIndex];
// Move to rest position before disconnection
if (this._slaveStatuses.get(slaveId)?.isConnected) {
try {
await this.moveSlaveToRestPosition(slave);
await new Promise((resolve) => setTimeout(resolve, 500));
} catch (error) {
console.warn(`Failed to move slave ${slaveId} to rest position:`, error);
}
}
// Disconnect slave
if (slave.status.isConnected) {
await slave.disconnect();
}
// Remove from arrays
this._slaves.splice(slaveIndex, 1);
this._slaveStatuses.delete(slaveId);
console.log(`Slave ${slaveId} removed from robot ${this.id}`);
}
// ============= COMMAND HANDLING =============
/**
* Handle commands from master
*/
private async handleMasterCommands(commands: RobotCommand[]): Promise<void> {
console.log(`Received ${commands.length} commands from master for robot ${this.id}`);
// Update control state
this._controlState.lastCommandSource = "master";
this._controlState.commandQueue = [...commands];
// Execute on all connected slaves
const executePromises = this.connectedSlaves.map((slave) =>
slave
.executeCommands(commands)
.catch((error) => console.error(`Slave ${slave.id} failed to execute commands:`, error))
);
await Promise.allSettled(executePromises);
// Update virtual state from commands
this.updateVirtualFromCommands(commands);
}
/**
* Handle command sequence from master
*/
private async handleMasterSequence(sequence: CommandSequence): Promise<void> {
console.log(`Received sequence "${sequence.name}" from master for robot ${this.id}`);
// Process sequence commands one by one
for (const command of sequence.commands) {
await this.handleMasterCommands([command]);
// Wait for command duration if specified
if (command.duration) {
await new Promise((resolve) => setTimeout(resolve, command.duration));
}
}
}
/**
* Manual control - update joint value (when no master active)
*/
async updateJointValue(jointName: string, value: number): Promise<void> {
if (!this.manualControlEnabled) {
console.warn(`Manual control disabled - master is active for robot ${this.id}`);
return;
}
const joint = this._joints.find((j) => j.name === jointName);
if (!joint) {
console.warn(`Joint ${jointName} not found in robot ${this.id}`);
return;
}
// Apply limits
const clampedValue = this.clampJointValue(joint, value);
// Update virtual state
joint.virtualValue = clampedValue;
joint.commandedValue = clampedValue;
joint.lastVirtualUpdate = new Date();
joint.lastCommandUpdate = new Date();
// Update URDF visualization
this.updateUrdfJointRotation(joint, clampedValue);
// Create command for slaves
const command: RobotCommand = {
timestamp: Date.now(),
joints: [{ name: jointName, value: clampedValue }]
};
// Send to all connected slaves
const executePromises = this.connectedSlaves.map((slave) =>
slave
.executeCommand(command)
.catch((error) =>
console.error(`Slave ${slave.id} failed to execute manual command:`, error)
)
);
await Promise.allSettled(executePromises);
this._controlState.lastCommandSource = "manual";
}
/**
* Manual control - update multiple joints
*/
async updateJointValues(updates: { jointName: string; value: number }[]): Promise<void> {
if (!this.manualControlEnabled) {
console.warn(`Manual control disabled - master is active for robot ${this.id}`);
return;
}
// Update virtual states
const commandJoints: { name: string; value: number }[] = [];
for (const { jointName, value } of updates) {
const joint = this._joints.find((j) => j.name === jointName);
if (!joint) continue;
const clampedValue = this.clampJointValue(joint, value);
joint.virtualValue = clampedValue;
joint.commandedValue = clampedValue;
joint.lastVirtualUpdate = new Date();
joint.lastCommandUpdate = new Date();
// Update URDF visualization
this.updateUrdfJointRotation(joint, clampedValue);
commandJoints.push({ name: jointName, value: clampedValue });
}
if (commandJoints.length === 0) return;
// Create batch command for slaves
const command: RobotCommand = {
timestamp: Date.now(),
joints: commandJoints
};
// Send to all connected slaves
const executePromises = this.connectedSlaves.map((slave) =>
slave
.executeCommand(command)
.catch((error) =>
console.error(`Slave ${slave.id} failed to execute manual batch command:`, error)
)
);
await Promise.allSettled(executePromises);
this._controlState.lastCommandSource = "manual";
}
// ============= STATE MANAGEMENT =============
/**
* Update virtual state from master commands
*/
private updateVirtualFromCommands(commands: RobotCommand[]): void {
for (const command of commands) {
for (const jointCommand of command.joints) {
const joint = this._joints.find((j) => j.name === jointCommand.name);
if (joint) {
joint.virtualValue = jointCommand.value;
joint.commandedValue = jointCommand.value;
joint.lastCommandUpdate = new Date();
// Update URDF visualization
this.updateUrdfJointRotation(joint, jointCommand.value);
}
}
}
}
/**
* Update joint states from slave feedback
*/
private updateFromSlave(slaveId: string, driverStates: DriverJointState[]): void {
for (const driverState of driverStates) {
const joint = this._joints.find((j) => j.name === driverState.name);
if (joint) {
// Apply calibration offset to raw real value
if (driverState.realValue !== undefined) {
joint.realValue = driverState.realValue + joint.calibrationOffset;
} else {
joint.realValue = undefined;
}
joint.lastRealUpdate = new Date();
}
}
}
/**
* Update URDF joint rotation for 3D visualization
*/
private updateUrdfJointRotation(joint: ManagedJointState, degrees: number): void {
const urdfJoint = joint.urdfJoint;
if (!urdfJoint) return;
if (urdfJoint.type === "revolute") {
const radians = (degrees * Math.PI) / 180;
const axis = urdfJoint.axis_xyz || [0, 0, 1];
urdfJoint.rotation = [radians * axis[0], radians * axis[1], radians * axis[2]];
}
}
/**
* Apply joint limits to a value
*/
private clampJointValue(joint: ManagedJointState, value: number): number {
const limit = joint.urdfJoint.limit;
if (!limit) return value;
if (joint.urdfJoint.type === "revolute") {
if (limit.lower !== undefined && limit.upper !== undefined) {
const lowerDeg = (limit.lower * 180) / Math.PI;
const upperDeg = (limit.upper * 180) / Math.PI;
return Math.max(lowerDeg, Math.min(upperDeg, value));
}
} else if (joint.urdfJoint.type === "continuous") {
if (limit.velocity !== undefined) {
return Math.max(-limit.velocity, Math.min(limit.velocity, value));
}
}
return value;
}
// ============= MOVEMENT FUNCTIONS =============
/**
* Move robot to rest position smoothly
*/
async moveToRestPosition(durationMs: number = 3000): Promise<void> {
console.log(`Moving robot ${this.id} to rest position over ${durationMs}ms`);
const movements: Array<{
joint: ManagedJointState;
startValue: number;
targetValue: number;
totalDelta: number;
}> = [];
for (const joint of this._joints) {
if (joint.isActive) {
const startValue = joint.virtualValue;
const targetValue = joint.restPosition;
const totalDelta = targetValue - startValue;
movements.push({
joint,
startValue,
targetValue,
totalDelta
});
}
}
if (movements.length === 0) {
console.log("No active joints to move");
return;
}
const startTime = Date.now();
const updateInterval = 50;
return new Promise((resolve) => {
const animationStep = () => {
const elapsed = Date.now() - startTime;
const progress = Math.min(elapsed / durationMs, 1.0);
const easedProgress =
progress < 0.5
? 4 * progress * progress * progress
: 1 - Math.pow(-2 * progress + 2, 3) / 2;
const updates: Array<{ jointName: string; value: number }> = [];
for (const movement of movements) {
const currentValue = movement.startValue + movement.totalDelta * easedProgress;
updates.push({
jointName: movement.joint.name,
value: currentValue
});
}
this.updateJointValues(updates);
if (progress < 1.0) {
setTimeout(animationStep, updateInterval);
} else {
console.log(`Robot ${this.id} reached rest position`);
resolve();
}
};
animationStep();
});
}
/**
* Move a specific slave to rest position
*/
private async moveSlaveToRestPosition(slave: SlaveDriver): Promise<void> {
const restCommand: RobotCommand = {
timestamp: Date.now(),
joints: this._joints.map((joint) => ({
name: joint.name,
value: joint.restPosition
}))
};
await slave.executeCommand(restCommand);
}
// ============= CALIBRATION =============
/**
* Calibrate robot using first connected slave
*/
async calibrateRobot(): Promise<void> {
const connectedSlaves = this.connectedSlaves;
if (connectedSlaves.length === 0) {
throw new Error("Cannot calibrate: no slaves connected");
}
const primarySlave = connectedSlaves[0];
console.log(`Calibrating robot ${this.id} using slave ${primarySlave.id}...`);
try {
const driverStates = await primarySlave.readJointStates();
const offsets: { [jointName: string]: number } = {};
for (const driverState of driverStates) {
const joint = this._joints.find((j) => j.name === driverState.name);
if (joint && driverState.realValue !== undefined) {
const offset = joint.restPosition - driverState.realValue;
offsets[joint.name] = offset;
joint.calibrationOffset = offset;
console.log(
`Joint ${joint.name}: rest=${joint.restPosition}° real=${driverState.realValue}° offset=${offset.toFixed(1)}°`
);
}
}
this._calibrationState = {
isCalibrated: true,
calibrationTime: new Date(),
offsets
};
console.log(`Robot ${this.id} calibrated successfully`);
} catch (error) {
console.error(`Calibration failed for robot ${this.id}:`, error);
throw error;
}
}
/**
* Clear calibration
*/
clearCalibration(): void {
this._calibrationState = {
isCalibrated: false,
offsets: {}
};
this._joints.forEach((joint) => {
joint.calibrationOffset = 0;
});
console.log(`Cleared calibration for robot ${this.id}`);
}
// ============= LIFECYCLE =============
/**
* Get joint by name
*/
getJoint(name: string): ManagedJointState | undefined {
return this._joints.find((j) => j.name === name);
}
/**
* Clean up resources
*/
async destroy(): Promise<void> {
// Clean up subscriptions
this.unsubscribeCallbacks.forEach((unsub) => unsub());
this.unsubscribeCallbacks = [];
// Remove master
await this.removeMaster();
// Remove all slaves
const slaveIds = this._slaves.map((s) => s.id);
for (const slaveId of slaveIds) {
await this.removeSlave(slaveId);
}
console.log(`Robot ${this.id} destroyed`);
}
}
|