File size: 2,831 Bytes
bdc1ac8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Direct teleoperator for Node.js platform
 * Provides programmatic control without user interface
 */

import {
  BaseNodeTeleoperator,
  type TeleoperatorSpecificState,
} from "./base-teleoperator.js";
import type {
  DirectTeleoperatorConfig,
  MotorConfig,
} from "../types/teleoperation.js";
import type { MotorCommunicationPort } from "../utils/motor-communication.js";
import {
  readMotorPosition,
  writeMotorPosition,
} from "../utils/motor-communication.js";

/**
 * Direct teleoperator provides programmatic motor control
 * Use this when you want to control motors directly from code
 */
export class DirectTeleoperator extends BaseNodeTeleoperator {
  constructor(
    config: DirectTeleoperatorConfig,
    port: MotorCommunicationPort,
    motorConfigs: MotorConfig[]
  ) {
    super(port, motorConfigs);
  }

  async initialize(): Promise<void> {
    // Read current motor positions
    for (const config of this.motorConfigs) {
      const position = await readMotorPosition(this.port, config.id);
      if (position !== null) {
        config.currentPosition = position;
      }
    }
  }

  start(): void {
    this.isActive = true;
  }

  stop(): void {
    this.isActive = false;
  }

  getState(): TeleoperatorSpecificState {
    return {};
  }

  /**
   * Move motor to exact position (programmatic control)
   */
  async moveMotor(motorName: string, targetPosition: number): Promise<boolean> {
    if (!this.isActive) return false;

    const motorConfig = this.motorConfigs.find((m) => m.name === motorName);
    if (!motorConfig) return false;

    const clampedPosition = Math.max(
      motorConfig.minPosition,
      Math.min(motorConfig.maxPosition, targetPosition)
    );

    try {
      await writeMotorPosition(
        this.port,
        motorConfig.id,
        Math.round(clampedPosition)
      );
      motorConfig.currentPosition = clampedPosition;
      return true;
    } catch (error) {
      console.warn(`Failed to move motor ${motorName}:`, error);
      return false;
    }
  }

  /**
   * Move multiple motors simultaneously
   */
  async moveMotors(
    positions: { [motorName: string]: number }
  ): Promise<{ [motorName: string]: boolean }> {
    const results: { [motorName: string]: boolean } = {};
    
    const promises = Object.entries(positions).map(
      async ([motorName, position]) => {
        const success = await this.moveMotor(motorName, position);
        results[motorName] = success;
      }
    );

    await Promise.all(promises);
    return results;
  }

  /**
   * Get current motor positions
   */
  getCurrentPositions(): { [motorName: string]: number } {
    const positions: { [motorName: string]: number } = {};
    for (const config of this.motorConfigs) {
      positions[config.name] = config.currentPosition;
    }
    return positions;
  }
}