UNPKG

dynamixel

Version:

Node.js library for controlling DYNAMIXEL servo motors via U2D2 interface with Protocol 2.0 support

581 lines (580 loc) 17.1 kB
/** * @typedef {Object} DynamixelControllerOptions * @property {'usb'|'serial'|'webserial'|'auto'} [connectionType='auto'] - Connection type to use * @property {boolean} [deferConnection=false] - Whether to defer connection creation until connectToDevice is called * @property {number} [timeout=5000] - Connection timeout in milliseconds * @property {boolean} [debug=false] - Enable debug logging * @property {number} [baudRate=1000000] - Serial communication baud rate * @property {string} [portPath] - Specific port path for serial connections * @property {number} [highWaterMark=65536] - SerialPort buffer size in bytes (default 64KB) */ /** * Main DYNAMIXEL Controller * Manages connection and communication with DYNAMIXEL devices * Supports both USB and Serial connection methods */ export class DynamixelController extends EventEmitter<[never]> { /** * Get device model name from model number * @param {number} modelNumber - Model number from device * @returns {string} - Model name or 'Unknown' */ static getModelName(modelNumber: number): string; /** * @typedef {Object} SerialPortInfo * @property {string} path - Port path * @property {string} [manufacturer] - Manufacturer name * @property {string} [vendorId] - Vendor ID * @property {string} [productId] - Product ID * @property {string} [serialNumber] - Serial number * @property {boolean} isU2D2 - Whether this is a U2D2 device */ /** * @typedef {Object} USBDeviceInfo * @property {number} vendorId - USB vendor ID * @property {number} productId - USB product ID * @property {number} busNumber - USB bus number * @property {number} deviceAddress - Device address on bus * @property {boolean} isU2D2 - Whether this is a U2D2 device */ /** * @typedef {Object} CommunicationDevice * @property {string} type - Device type ('serial' | 'usb') * @property {string} name - Device display name * @property {string} [path] - Port path (for serial devices) * @property {number} [vendorId] - Vendor ID (for USB devices) * @property {number} [productId] - Product ID (for USB devices) * @property {boolean} isU2D2 - Whether this is a U2D2 device * @property {boolean} [recommended] - Whether this device is recommended */ /** * @typedef {Object} CommunicationDevices * @property {USBDeviceInfo[]} usb - Array of USB devices * @property {SerialPortInfo[]} serial - Array of serial devices * @property {boolean} webserial - Whether Web Serial API is available */ /** * @typedef {Object} SystemInfo * @property {string} platform - Operating system platform * @property {string} arch - System architecture * @property {string} nodeVersion - Node.js version * @property {boolean} usbAvailable - Whether USB module is available * @property {string} usbVersion - USB module version info */ /** * @typedef {Object} USBDiagnostics * @property {boolean} usbModuleAvailable - Whether USB module is available * @property {USBDeviceInfo[]} u2d2Devices - Found U2D2 devices * @property {USBDeviceInfo[]} allDevices - All USB devices * @property {string[]} errors - Array of error messages * @property {number} totalDevices - Total number of USB devices * @property {SystemInfo} systemInfo - System information */ /** * @typedef {Object} DeviceInfo * @property {number} id - Device ID * @property {number} modelNumber - Device model number * @property {string} modelName - Device model name * @property {number} firmwareVersion - Firmware version */ /** * Discover available communication devices (USB and Serial) * @returns {Promise<CommunicationDevices>} - Object containing USB and serial devices */ static discoverCommunicationDevices(): Promise<{ /** * - Array of USB devices */ usb: { /** * - USB vendor ID */ vendorId: number; /** * - USB product ID */ productId: number; /** * - USB bus number */ busNumber: number; /** * - Device address on bus */ deviceAddress: number; /** * - Whether this is a U2D2 device */ isU2D2: boolean; }[]; /** * - Array of serial devices */ serial: { /** * - Port path */ path: string; /** * - Manufacturer name */ manufacturer?: string | undefined; /** * - Vendor ID */ vendorId?: string | undefined; /** * - Product ID */ productId?: string | undefined; /** * - Serial number */ serialNumber?: string | undefined; /** * - Whether this is a U2D2 device */ isU2D2: boolean; }[]; /** * - Whether Web Serial API is available */ webserial: boolean; }>; /** * Discover U2D2 devices specifically * @returns {Promise<CommunicationDevice[]>} - Array of U2D2-compatible devices */ static discoverU2D2Devices(): Promise<{ /** * - Device type ('serial' | 'usb') */ type: string; /** * - Device display name */ name: string; /** * - Port path (for serial devices) */ path?: string | undefined; /** * - Vendor ID (for USB devices) */ vendorId?: number | undefined; /** * - Product ID (for USB devices) */ productId?: number | undefined; /** * - Whether this is a U2D2 device */ isU2D2: boolean; /** * - Whether this device is recommended */ recommended?: boolean | undefined; }[]>; /** * List available USB devices (for debugging) * @returns {USBDeviceInfo[]} - Array of USB device information */ static listUSBDevices(): { /** * - USB vendor ID */ vendorId: number; /** * - USB product ID */ productId: number; /** * - USB bus number */ busNumber: number; /** * - Device address on bus */ deviceAddress: number; /** * - Whether this is a U2D2 device */ isU2D2: boolean; }[]; /** * List available serial ports * @returns {Promise<SerialPortInfo[]>} - Array of serial port information */ static listSerialPorts(): Promise<{ /** * - Port path */ path: string; /** * - Manufacturer name */ manufacturer?: string | undefined; /** * - Vendor ID */ vendorId?: string | undefined; /** * - Product ID */ productId?: string | undefined; /** * - Serial number */ serialNumber?: string | undefined; /** * - Whether this is a U2D2 device */ isU2D2: boolean; }[]>; /** * Get system information and troubleshooting recommendations * @returns {SystemInfo} - System information and recommendations */ static getSystemInfo(): { /** * - Operating system platform */ platform: string; /** * - System architecture */ arch: string; /** * - Node.js version */ nodeVersion: string; /** * - Whether USB module is available */ usbAvailable: boolean; /** * - USB module version info */ usbVersion: string; }; /** * Perform comprehensive USB diagnostics * @returns {USBDiagnostics} - Diagnostic results */ static performUSBDiagnostics(): { /** * - Whether USB module is available */ usbModuleAvailable: boolean; /** * - Found U2D2 devices */ u2d2Devices: { /** * - USB vendor ID */ vendorId: number; /** * - USB product ID */ productId: number; /** * - USB bus number */ busNumber: number; /** * - Device address on bus */ deviceAddress: number; /** * - Whether this is a U2D2 device */ isU2D2: boolean; }[]; /** * - All USB devices */ allDevices: { /** * - USB vendor ID */ vendorId: number; /** * - USB product ID */ productId: number; /** * - USB bus number */ busNumber: number; /** * - Device address on bus */ deviceAddress: number; /** * - Whether this is a U2D2 device */ isU2D2: boolean; }[]; /** * - Array of error messages */ errors: string[]; /** * - Total number of USB devices */ totalDevices: number; /** * - System information */ systemInfo: { /** * - Operating system platform */ platform: string; /** * - System architecture */ arch: string; /** * - Node.js version */ nodeVersion: string; /** * - Whether USB module is available */ usbAvailable: boolean; /** * - USB module version info */ usbVersion: string; }; }; /** * @param {DynamixelControllerOptions} [options={}] - Controller options */ constructor(options?: DynamixelControllerOptions); connectionType: "usb" | "serial" | "webserial" | "auto"; connection: any; devices: Map<any, any>; isConnected: boolean; deferConnection: boolean; /** * Create the appropriate connection based on type * @param {Object} options - Connection options */ createConnection(options: Object): void; /** * Detect the best connection type for the current environment * @param {Object} options - Connection options * @returns {Object} - Connection instance */ detectBestConnection(options: Object): Object; /** * Set up connection event forwarding */ setupConnectionEvents(): void; /** * Connect to a specific device * @param {Object} deviceInfo - Device information from discovery * @param {Object} options - Connection options * @returns {Promise<boolean>} - Connection success */ connectToDevice(deviceInfo: Object, options?: Object): Promise<boolean>; /** * Connect to U2D2 device with auto-fallback * @param {string} portPath - Optional specific port path for serial connection * @returns {Promise<boolean>} - Connection success */ connect(portPath?: string): Promise<boolean>; /** * Get the detected connection type from the current connection * @returns {string} - Connection type */ getDetectedConnectionType(): string; /** * Disconnect from U2D2 device */ disconnect(): Promise<void>; /** * Ping a specific DYNAMIXEL device * @param {number} id - DYNAMIXEL ID * @param {number} timeout - Timeout in milliseconds * @returns {Promise<Object>} - Device information */ ping(id: number, timeout?: number): Promise<Object>; /** * Discover all DYNAMIXEL devices on the bus * @param {Object} options - Discovery options * @returns {Promise<DeviceInfo[]>} - Array of discovered devices */ discoverDevices(options?: Object): Promise<{ /** * - Device ID */ id: number; /** * - Device model number */ modelNumber: number; /** * - Device model name */ modelName: string; /** * - Firmware version */ firmwareVersion: number; }[]>; /** * Get a specific DYNAMIXEL device by ID * @param {number} id - DYNAMIXEL ID * @returns {DynamixelDevice|null} - Device instance or null if not found */ getDevice(id: number): DynamixelDevice | null; /** * Get all discovered devices * @returns {Array<DynamixelDevice>} - Array of device instances */ getAllDevices(): Array<DynamixelDevice>; /** * Get device count * @returns {number} - Number of discovered devices */ getDeviceCount(): number; /** * Add a device manually (if you know its ID and info) * @param {number} id - DYNAMIXEL ID * @param {Object} deviceInfo - Device information * @returns {DynamixelDevice} - Created device instance */ addDevice(id: number, deviceInfo?: Object): DynamixelDevice; /** * Set the baud rate for communication * @param {number} baudRate - Baud rate (e.g., 57600, 115200, 1000000) */ setBaudRate(baudRate: number): void; /** * Get the current baud rate * @returns {number} - Current baud rate */ getBaudRate(): number; /** * Remove a device from the controller * @param {number} id - DYNAMIXEL ID * @returns {boolean} - Success status */ removeDevice(id: number): boolean; /** * Check if controller is connected * @returns {boolean} - Connection status */ getConnectionStatus(): boolean; /** * Get connection instance (for advanced usage) * @returns {U2D2Connection} - Connection instance */ getConnection(): U2D2Connection; /** * Broadcast ping to all devices * @param {number} timeout - Timeout in milliseconds * @returns {Promise<DeviceInfo[]>} - Array of responses */ broadcastPing(timeout?: number): Promise<{ /** * - Device ID */ id: number; /** * - Device model number */ modelNumber: number; /** * - Device model name */ modelName: string; /** * - Firmware version */ firmwareVersion: number; }[]>; /** * Quick device discovery with progress reporting * @param {Function} onProgress - Progress callback (current, total, id) * @returns {Promise<DeviceInfo[]>} - Array of discovered devices */ quickDiscovery(onProgress?: Function): Promise<{ /** * - Device ID */ id: number; /** * - Device model number */ modelNumber: number; /** * - Device model name */ modelName: string; /** * - Firmware version */ firmwareVersion: number; }[]>; /** * Full device discovery (all possible IDs) * @param {Function} onProgress - Progress callback (current, total, id) * @returns {Promise<DeviceInfo[]>} - Array of discovered devices */ fullDiscovery(onProgress?: Function): Promise<{ /** * - Device ID */ id: number; /** * - Device model number */ modelNumber: number; /** * - Device model name */ modelName: string; /** * - Firmware version */ firmwareVersion: number; }[]>; } export type DynamixelControllerOptions = { /** * - Connection type to use */ connectionType?: "usb" | "serial" | "webserial" | "auto" | undefined; /** * - Whether to defer connection creation until connectToDevice is called */ deferConnection?: boolean | undefined; /** * - Connection timeout in milliseconds */ timeout?: number | undefined; /** * - Enable debug logging */ debug?: boolean | undefined; /** * - Serial communication baud rate */ baudRate?: number | undefined; /** * - Specific port path for serial connections */ portPath?: string | undefined; /** * - SerialPort buffer size in bytes (default 64KB) */ highWaterMark?: number | undefined; }; import { EventEmitter } from 'events'; import { DynamixelDevice } from './dynamixel/index.js'; import { U2D2Connection } from './transport/index.js';