UNPKG

@robotical/sensors-dashboard

Version:

A dashboard tool for depicting Marty data by Robotical

168 lines (167 loc) 8.38 kB
"use strict"; require("core-js/modules/es.symbol.description.js"); require("core-js/modules/esnext.iterator.constructor.js"); require("core-js/modules/esnext.iterator.filter.js"); require("core-js/modules/esnext.iterator.for-each.js"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.MockCogInterface = void 0; require("core-js/modules/es.number.to-fixed.js"); var _CogInterface = _interopRequireDefault(require("../CogInterface")); var _Addon = _interopRequireDefault(require("../../models/addons/Addon")); var _AddonSub = _interopRequireDefault(require("../../models/addons/AddonSub")); var _addonNames = require("../../utils/types/addon-names"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } const UPDATE_INTERVAL_MS = 750; const clamp = (value, min, max) => Math.min(Math.max(value, min), max); const mutateNumber = (value, min, max, maxDelta) => { const delta = (Math.random() * 2 - 1) * maxDelta; const next = value + delta; return Number(clamp(next, min, max).toFixed(2)); }; const pickWithChance = (current, options, changeChance) => { if (Math.random() > changeChance) return current; return options[Math.floor(Math.random() * options.length)]; }; const createInitialLight = () => ({ amb0: 120, ir0: 140, ir1: 135, ir2: 20 }); const createInitialAccel = () => ({ ax: 0.05, ay: -0.08, az: 0.97 }); const createInitialGyro = () => ({ gx: -0.5, gy: 0.4, gz: 0.1 }); const createInitialPower = () => ({ battV: 7.4, usb: "disconnected" }); const createInitialCogState = () => ({ tilt: "none", movementType: "none", rotation: "none", buttonClick: "none", objectSense: "none", lightSense: "mid", steering: "none" }); const buildCogAddonList = (light, accel, gyro, cogState) => { const lightAddon = new _Addon.default(_addonNames.COG_LIGHT_NAME, [new _AddonSub.default(_addonNames.COG_LIGHT_AMB, light.amb0), new _AddonSub.default(_addonNames.COG_IR_LEFT, light.ir0), new _AddonSub.default(_addonNames.COG_IR_RIGHT, light.ir1), new _AddonSub.default(_addonNames.COG_IR_BUTTON, light.ir2)]); const accelAddon = new _Addon.default(_addonNames.ACCELEROMETER_NAME, [new _AddonSub.default(_addonNames.ACCELEROMETER_NAME_X, accel.ax), new _AddonSub.default(_addonNames.ACCELEROMETER_NAME_Y, accel.ay), new _AddonSub.default(_addonNames.ACCELEROMETER_NAME_Z, accel.az)]); const gyroAddon = new _Addon.default(_addonNames.GYROSCOPE_NAME, [new _AddonSub.default(_addonNames.GYROSCOPE_NAME_X, gyro.gx), new _AddonSub.default(_addonNames.GYROSCOPE_NAME_Y, gyro.gy), new _AddonSub.default(_addonNames.GYROSCOPE_NAME_Z, gyro.gz)]); const stateAddon = new _Addon.default(_addonNames.COG_STATE_NAME, [new _AddonSub.default(_addonNames.COG_TILT_NAME, cogState.tilt), new _AddonSub.default(_addonNames.COG_MOVEMENT_TYPE_NAME, cogState.movementType), new _AddonSub.default(_addonNames.COG_ROTATION_NAME, cogState.rotation), new _AddonSub.default(_addonNames.COG_BUTTON_CLICK_NAME, cogState.buttonClick === "none" ? "release" : cogState.buttonClick), new _AddonSub.default(_addonNames.COG_OBJECT_SENSE_NAME, cogState.objectSense), new _AddonSub.default(_addonNames.COG_LIGHT_SENSE_NAME, cogState.lightSense)]); return [lightAddon, accelAddon, gyroAddon, stateAddon]; }; class MockCogInterface extends _CogInterface.default { constructor() { super({}, { autoSubscribe: false }); _defineProperty(this, "updateTimer", null); _defineProperty(this, "currentLight", createInitialLight()); _defineProperty(this, "currentAccel", createInitialAccel()); _defineProperty(this, "currentGyro", createInitialGyro()); _defineProperty(this, "currentPower", createInitialPower()); _defineProperty(this, "currentCogState", createInitialCogState()); _defineProperty(this, "latestAddonList", buildCogAddonList(this.currentLight, this.currentAccel, this.currentGyro, this.currentCogState)); } start() { this.dispatchCurrentState(); this.updateTimer = window.setInterval(() => { this.tick(); }, UPDATE_INTERVAL_MS); } getAvailableAddons() { return this.latestAddonList; } unsubscribeFromPublishedData() { if (this.updateTimer !== null) { clearInterval(this.updateTimer); this.updateTimer = null; } } dispatchCurrentState() { this.latestAddonList = buildCogAddonList(this.currentLight, this.currentAccel, this.currentGyro, this.currentCogState); this.setLight(_objectSpread({}, this.currentLight)); this.setAccel(_objectSpread({}, this.currentAccel)); this.setGyro(_objectSpread({}, this.currentGyro)); this.setCogPublishedState(_objectSpread({}, this.currentCogState)); } tick() { this.currentLight = this.mutateLight(this.currentLight); this.currentAccel = this.mutateAccel(this.currentAccel); this.currentGyro = this.mutateGyro(this.currentGyro); this.currentPower = this.mutatePower(this.currentPower); this.currentCogState = this.mutateCogState(this.currentCogState); this.dispatchCurrentState(); } mutateLight(light) { return { amb0: mutateNumber(light.amb0, 20, 250, 12), ir0: mutateNumber(light.ir0, 10, 380, 18), ir1: mutateNumber(light.ir1, 10, 380, 18), ir2: mutateNumber(light.ir2, 0, 180, 12) }; } mutateAccel(accel) { return { ax: mutateNumber(accel.ax, -1.1, 1.1, 0.07), ay: mutateNumber(accel.ay, -1.1, 1.1, 0.07), az: mutateNumber(accel.az, 0.6, 1.1, 0.05) }; } mutateGyro(gyro) { return { gx: mutateNumber(gyro.gx, -2.5, 2.5, 0.2), gy: mutateNumber(gyro.gy, -2.5, 2.5, 0.2), gz: mutateNumber(gyro.gz, -2.5, 2.5, 0.3) }; } mutatePower(power) { return { battV: mutateNumber(power.battV, 6.8, 8, 0.05), usb: power.usb }; } mutateCogState(prev) { const tiltOptions = ["none", "forward", "backward", "left", "right"]; const movementOptions = ["none", "shake", "move"]; const rotationOptions = ["none", "clockwise", "counterClockwise"]; const objectSenseOptions = ["none", "left", "right"]; const lightSenseOptions = ["none", "low", "mid", "high"]; let buttonClick = prev.buttonClick; if (prev.buttonClick === "none" && Math.random() < 0.08) { buttonClick = "click"; } else if (prev.buttonClick === "click") { buttonClick = "release"; } else if (prev.buttonClick === "release") { buttonClick = Math.random() < 0.3 ? "none" : "release"; } else if (Math.random() < 0.05) { buttonClick = "none"; } return { tilt: pickWithChance(prev.tilt, tiltOptions, 0.12), movementType: pickWithChance(prev.movementType, movementOptions, 0.1), rotation: pickWithChance(prev.rotation, rotationOptions, 0.1), buttonClick, objectSense: pickWithChance(prev.objectSense, objectSenseOptions, 0.08), lightSense: pickWithChance(prev.lightSense, lightSenseOptions, 0.1), steering: prev.steering }; } } exports.MockCogInterface = MockCogInterface; var _default = exports.default = MockCogInterface;