All files / src/physics motor-controller.ts

79.33% Statements 119/150
81.63% Branches 40/49
90.9% Functions 10/11
79.33% Lines 119/150

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                        1x 1746x 1745x 1745x 12x                         1746x 1x 1x           1x 1x 1x 1x       1x 2034x 2034x 2034x 2034x 2034x 2034x   2034x 2034x   2034x 2034x 2034x         2034x 6x 6x   2034x             2034x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x 1707x           2034x 32x   32x 32x 31x 31x 32x 1x 1x 1x 1x 1x   32x 32x 32x   32x 32x 32x 32x 32x 32x 32x 32x 32x         2034x 6x 6x 6x   6x             6x 6x 6x 6x 6x 6x 6x 6x 6x   2034x 1119x 1119x 1119x 1119x 1119x 1119x 1119x 1119x 1119x   2034x 534x 534x         2034x 32722x   10906x 32722x 1183x 1183x   32722x 25420x       25420x 25420x 25420x 25420x 25420x 25420x   7302x 7302x     7302x 7302x   32722x 1330x 1330x 29x 29x 32722x 301x 301x     301x   7302x 32722x 2034x  
export type MotorPort = 'A' | 'B' | 'C' | 'D' | 'E' | 'F';
export type StopAction = 'COAST' | 'BRAKE' | 'HOLD';
 
export interface MotorTarget {
  type: 'CONTINUOUS' | 'DEGREES' | 'TIME';
  targetSpeedDegPerSec: number;
  remainingDegrees?: number;
  remainingSeconds?: number;
  stopAction: StopAction;
  onComplete?: () => void;
}
 
export function resolveMotorSpeedInput(speedPercent: any, defaultSpeed: number = 50): number {
  if (typeof speedPercent === 'number' && Number.isFinite(speedPercent)) {
    return Math.max(-100, Math.min(100, speedPercent));
  }
  if (typeof speedPercent === 'string') {
    const s = speedPercent.trim().toLowerCase();
    if (s === 'counterclockwise' || s === 'ccw' || s === 'anticlockwise' || s === 'reverse' || s === 'backward') {
      return -Math.abs(defaultSpeed);
    }
    if (s === 'clockwise' || s === 'cw' || s === 'forward') {
      return Math.abs(defaultSpeed);
    }
    const parsed = parseFloat(s);
    if (Number.isFinite(parsed)) {
      return Math.max(-100, Math.min(100, parsed));
    }
  }
  if (typeof speedPercent === 'object' && speedPercent !== null) {
    let sign = 1;
    if (speedPercent.direction) {
      const d = String(speedPercent.direction).toLowerCase();
      if (d === 'counterclockwise' || d === 'ccw' || d === 'anticlockwise' || d === 'reverse' || d === 'backward') {
        sign = -1;
      }
    }
    const spd = speedPercent.speed !== undefined ? Number(speedPercent.speed) : defaultSpeed;
    const val = Number.isFinite(spd) ? spd : defaultSpeed;
    return Math.max(-100, Math.min(100, Math.abs(val) * sign));
  }
  return defaultSpeed;
}
 
export class VirtualMotor {
  public port: MotorPort;
  public degrees: number = 0; // Accumulated encoder angle
  public velocityDegPerSec: number = 0; // Current velocity
  public targetSpeedDegPerSec: number = 0;
  public maxSpeedDegPerSec: number = 1000; // ~166 RPM
  public maxTorqueNm: number = 0.25;
 
  private activeTarget: MotorTarget | null = null;
  private stopAction: StopAction = 'BRAKE';
 
  constructor(port: MotorPort) {
    this.port = port;
  }
 
  /**
   * Alias getter/setter for targetVelocityDegPerSec used by test suites and telemetry
   */
  public get targetVelocityDegPerSec(): number {
    return this.targetSpeedDegPerSec;
  }
 
  public set targetVelocityDegPerSec(v: number) {
    this.targetSpeedDegPerSec = v;
  }
 
  /**
   * Set continuous speed (-100% to 100% or absolute deg/s)
   */
  public start(speedPercent: any = 50): void {
    const clamped = resolveMotorSpeedInput(speedPercent, 50);
    const degPerSec = (clamped / 100) * this.maxSpeedDegPerSec;
    this.targetSpeedDegPerSec = degPerSec;
    this.activeTarget = {
      type: 'CONTINUOUS',
      targetSpeedDegPerSec: degPerSec,
      stopAction: 'BRAKE',
    };
  }
 
  /**
   * Run for specified degrees with callback on finish.
   * Preserves speed direction: negative speed produces reverse rotation.
   */
  public runForDegrees(degrees: number, speedPercent: any = 50, onComplete?: () => void): void {
    const clampedSpeed = resolveMotorSpeedInput(speedPercent, 50);
    // Check for near-zero or non-finite degrees / speed to prevent hanging
    if (
      !Number.isFinite(degrees) ||
      Math.abs(degrees) < 1e-4 ||
      Math.abs(clampedSpeed) < 1e-4
    ) {
      this.targetSpeedDegPerSec = 0;
      this.activeTarget = null;
      if (onComplete) onComplete();
      return;
    }
 
    const totalDir = (degrees >= 0 ? 1 : -1) * (clampedSpeed >= 0 ? 1 : -1);
    const absDeg = Math.abs(degrees);
    const speed = Math.abs((clampedSpeed / 100) * this.maxSpeedDegPerSec) * totalDir;
 
    this.targetSpeedDegPerSec = speed;
    this.activeTarget = {
      type: 'DEGREES',
      targetSpeedDegPerSec: speed,
      remainingDegrees: absDeg,
      stopAction: 'BRAKE',
      onComplete,
    };
  }
 
  /**
   * Run for duration in seconds
   */
  public runForTime(seconds: number, speedPercent: any = 50, onComplete?: () => void): void {
    const clampedSpeed = resolveMotorSpeedInput(speedPercent, 50);
    const speed = (clampedSpeed / 100) * this.maxSpeedDegPerSec;
    const safeSeconds = Math.max(0, isNaN(seconds) || !Number.isFinite(seconds) ? 0 : seconds);
 
    if (safeSeconds <= 0 || Math.abs(clampedSpeed) < 1e-4) {
      this.targetSpeedDegPerSec = 0;
      this.activeTarget = null;
      if (onComplete) onComplete();
      return;
    }
 
    this.targetSpeedDegPerSec = speed;
    this.activeTarget = {
      type: 'TIME',
      targetSpeedDegPerSec: speed,
      remainingSeconds: safeSeconds,
      stopAction: 'BRAKE',
      onComplete,
    };
  }
 
  public stop(action: StopAction = 'BRAKE'): void {
    this.stopAction = action;
    const cb = this.activeTarget?.onComplete;
    this.activeTarget = null;
    this.targetSpeedDegPerSec = 0;
    if (action !== 'COAST') {
      this.velocityDegPerSec = 0;
    }
    if (cb) cb();
  }
 
  public resetDegrees(): void {
    this.degrees = 0;
  }
 
  /**
   * Physics step update (dt in seconds)
   */
  public step(dt: number, actualAngularVelocityRadPerSec?: number): number {
    if (actualAngularVelocityRadPerSec !== undefined) {
      // Convert rad/s to deg/s
      this.velocityDegPerSec = (actualAngularVelocityRadPerSec * 180) / Math.PI;
    } else if (this.activeTarget) {
      this.velocityDegPerSec = this.activeTarget.targetSpeedDegPerSec;
    }
 
    if (!this.activeTarget) {
      if (this.stopAction === 'COAST') {
        this.targetSpeedDegPerSec = 0;
        this.velocityDegPerSec *= 0.95;
        if (Math.abs(this.velocityDegPerSec) < 0.1) this.velocityDegPerSec = 0;
      } else {
        this.targetSpeedDegPerSec = 0;
        this.velocityDegPerSec = 0;
      }
      return 0;
    }
 
    const { type, targetSpeedDegPerSec } = this.activeTarget;
    this.targetSpeedDegPerSec = targetSpeedDegPerSec;
 
    // Simulate movement
    const stepDeltaDeg = Math.abs(targetSpeedDegPerSec * dt);
    this.degrees += targetSpeedDegPerSec * dt;
 
    if (type === 'DEGREES' && this.activeTarget.remainingDegrees !== undefined) {
      this.activeTarget.remainingDegrees -= stepDeltaDeg;
      if (this.activeTarget.remainingDegrees <= 0) {
        this.stop(this.activeTarget.stopAction);
      }
    } else if (type === 'TIME' && this.activeTarget.remainingSeconds !== undefined) {
      this.activeTarget.remainingSeconds -= dt;
      if (this.activeTarget.remainingSeconds <= 0) {
        this.stop(this.activeTarget.stopAction);
      }
    }
 
    return this.targetSpeedDegPerSec;
  }
}