UNPKG

sabertooth-usb

Version:

API for controlling USB-enabled Sabertooth motor drivers running in Packet Serial mode

65 lines 3.27 kB
"use strict"; /** * Prints telemetry from each connected Sabertooth device. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); let sabertoothDevices = null; // Get all available sabertooth devices. (0, index_1.listSabertoothDevices)().then(devices => { if (devices.length === 0) { console.error("No Sabertooth devices found."); } else { console.log(`Found ${devices.length} Sabertooth devices.`); sabertoothDevices = devices.map(device => new index_1.SabertoothUSB(device.path, { // All options at default values. baudRate: 38400, timeout: 1000, maxGetAttemptCount: 3, address: 128, debug: true })); } }); const printStats = () => __awaiter(void 0, void 0, void 0, function* () { const startTime = Date.now(); if (sabertoothDevices !== null) { for (const sabertooth of sabertoothDevices) { console.log(`Sabertooth @ ${sabertooth.path}:`); // Check that the connection is open and working. if (sabertooth.isConnected()) { try { console.log(` Battery voltage: ${yield sabertooth.getBatteryVoltage()}V`); for (const channel of [1, 2]) { console.log(` Motor driver channel ${channel}`); console.log(` Temp: ${yield sabertooth.getMotorDriverOutputTemperature(channel)}°C`); // Note: the current sensor is extremely noisy and can vary as much as several amps. console.log(` Current: ${yield sabertooth.getMotorCurrent(channel)}A`); // Motor rates are in range [-1, 1], full-reverse to full-forward. console.log(` Rate: ${Math.round((yield sabertooth.getMotorDriverOutputRate(channel)) * 100)}%`); } } catch (e) { console.error(e); } } else { console.log(` Waiting for connection, last error: ${sabertooth === null || sabertooth === void 0 ? void 0 : sabertooth.getLastError()}`); } } console.log(`Got stats in ${((Date.now() - startTime) / 1000).toFixed(1)}s\n`); } else { console.log(`Searching for devices.`); } // Print stats at most once every second (allow for all requests to take longer than a second). setTimeout(printStats, 1000 - (Date.now() - startTime)); }); printStats(); //# sourceMappingURL=get.js.map