Source: devices/absolutemotor.js

  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.ModeMap = exports.Mode = exports.AbsoluteMotor = void 0;
  27. const tachomotor_1 = require("./tachomotor");
  28. const Consts = __importStar(require("../consts"));
  29. const utils_1 = require("../utils");
  30. /**
  31. * @class AbsoluteMotor
  32. * @extends TachoMotor
  33. */
  34. class AbsoluteMotor extends tachomotor_1.TachoMotor {
  35. constructor(hub, portId, modeMap = {}, type = Consts.DeviceType.UNKNOWN) {
  36. super(hub, portId, Object.assign({}, modeMap, exports.ModeMap), type);
  37. }
  38. receive(message) {
  39. const mode = this._mode;
  40. switch (mode) {
  41. case Mode.ABSOLUTE:
  42. const angle = (0, utils_1.normalizeAngle)(message.readInt16LE(this.isWeDo2SmartHub ? 2 : 4));
  43. /**
  44. * Emits when a the motors absolute position is changed.
  45. * @event AbsoluteMotor#absolute
  46. * @type {object}
  47. * @param {number} absolute
  48. */
  49. this.notify("absolute", { angle });
  50. break;
  51. default:
  52. super.receive(message);
  53. break;
  54. }
  55. }
  56. /**
  57. * Rotate a motor by a given angle.
  58. * @method AbsoluteMotor#gotoAngle
  59. * @param {number} angle Absolute position the motor should go to (degrees from 0).
  60. * @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.
  61. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
  62. */
  63. gotoAngle(angle, speed = 100) {
  64. if (!this.isVirtualPort && angle instanceof Array) {
  65. throw new Error("Only virtual ports can accept multiple positions");
  66. }
  67. if (this.isWeDo2SmartHub) {
  68. throw new Error("Absolute positioning is not available on the WeDo 2.0 Smart Hub");
  69. }
  70. this.cancelEventTimer();
  71. return new Promise((resolve) => {
  72. if (speed === undefined || speed === null) {
  73. speed = 100;
  74. }
  75. let message;
  76. if (angle instanceof Array) {
  77. message = Buffer.from([0x81, this.portId, 0x11, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (0, utils_1.mapSpeed)(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
  78. message.writeInt32LE((0, utils_1.normalizeAngle)(angle[0]), 4);
  79. message.writeInt32LE((0, utils_1.normalizeAngle)(angle[1]), 8);
  80. }
  81. else {
  82. message = Buffer.from([0x81, this.portId, 0x11, 0x0d, 0x00, 0x00, 0x00, 0x00, (0, utils_1.mapSpeed)(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
  83. message.writeInt32LE((0, utils_1.normalizeAngle)(angle), 4);
  84. }
  85. this.send(message);
  86. this._finishedCallbacks.push(() => {
  87. return resolve();
  88. });
  89. });
  90. }
  91. /**
  92. * Rotate motor to real zero position.
  93. *
  94. * Real zero is marked on Technic angular motors (SPIKE Prime). It is also available on Technic linear motors (Control+) but is unmarked.
  95. * @method AbsoluteMotor#gotoRealZero
  96. * @param {number} [speed=100] Speed between 1 - 100. Note that this will always take the shortest path to zero.
  97. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
  98. */
  99. gotoRealZero(speed = 100) {
  100. return new Promise((resolve) => {
  101. const oldMode = this.mode;
  102. let calibrated = false;
  103. this.on("absolute", async ({ angle }) => {
  104. if (!calibrated) {
  105. calibrated = true;
  106. if (angle < 0) {
  107. angle = Math.abs(angle);
  108. }
  109. else {
  110. speed = -speed;
  111. }
  112. await this.rotateByDegrees(angle, speed);
  113. if (oldMode) {
  114. this.subscribe(oldMode);
  115. }
  116. return resolve();
  117. }
  118. });
  119. this.requestUpdate();
  120. });
  121. }
  122. /**
  123. * Reset zero to current position
  124. * @method AbsoluteMotor#resetZero
  125. * @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
  126. */
  127. resetZero() {
  128. return new Promise((resolve) => {
  129. const data = Buffer.from([0x81, this.portId, 0x11, 0x51, 0x02, 0x00, 0x00, 0x00, 0x00]);
  130. this.send(data);
  131. return resolve();
  132. });
  133. }
  134. }
  135. exports.AbsoluteMotor = AbsoluteMotor;
  136. var Mode;
  137. (function (Mode) {
  138. Mode[Mode["ROTATION"] = 2] = "ROTATION";
  139. Mode[Mode["ABSOLUTE"] = 3] = "ABSOLUTE";
  140. })(Mode || (exports.Mode = Mode = {}));
  141. exports.ModeMap = {
  142. "rotate": Mode.ROTATION,
  143. "absolute": Mode.ABSOLUTE
  144. };
  145. //# sourceMappingURL=absolutemotor.js.map