node-poweredup/docs/devices_tachomotor.js.html
Nathan Kellenicki 60a42dfc47
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
New build with PRs
2024-04-25 17:10:39 -07:00

245 lines
16 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: devices/tachomotor.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: devices/tachomotor.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
var __createBinding = (this &amp;&amp; this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this &amp;&amp; this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this &amp;&amp; this.__importStar) || function (mod) {
if (mod &amp;&amp; mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" &amp;&amp; Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModeMap = exports.Mode = exports.TachoMotor = void 0;
const basicmotor_1 = require("./basicmotor");
const Consts = __importStar(require("../consts"));
const utils_1 = require("../utils");
/**
* @class TachoMotor
* @extends BasicMotor
*/
class TachoMotor extends basicmotor_1.BasicMotor {
constructor(hub, portId, modeMap = {}, type = Consts.DeviceType.UNKNOWN) {
super(hub, portId, Object.assign({}, modeMap, exports.ModeMap), type);
this._brakeStyle = Consts.BrakingStyle.BRAKE;
this._maxPower = 100;
this.useAccelerationProfile = true;
this.useDecelerationProfile = true;
}
receive(message) {
const mode = this._mode;
switch (mode) {
case Mode.ROTATION:
const degrees = message.readInt32LE(this.isWeDo2SmartHub ? 2 : 4);
/**
* Emits when a rotation sensor is activated.
* @event TachoMotor#rotate
* @type {object}
* @param {number} rotation
*/
this.notify("rotate", { degrees });
break;
}
}
/**
* Set the braking style of the motor.
*
* Note: This applies to setSpeed, rotateByDegrees, and gotoAngle.
* @method TachoMotor#setBrakingStyle
* @param {number} style Either BRAKE or HOLD
*/
setBrakingStyle(style) {
this._brakeStyle = style;
}
/**
* Set the max power of the motor.
*
* Note: This applies to setSpeed, rotateByDegrees, and gotoAngle.
* @method TachoMotor#setMaxPower
* @param {number} style Either BRAKE or HOLD
*/
setMaxPower(maxPower) {
this._maxPower = maxPower;
}
/**
* Set the global acceleration time
* @method TachoMotor#setAccelerationTime
* @param {number} time How long acceleration should last (in milliseconds).
* @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
*/
setAccelerationTime(time, profile = 0x00) {
const message = Buffer.from([0x81, this.portId, 0x11, 0x05, 0x00, 0x00, profile]);
message.writeUInt16LE(time, 4);
this.send(message);
}
/**
* Set the global deceleration time
* @method TachoMotor#setDecelerationTime
* @param {number} time How long deceleration should last (in milliseconds).
* @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
*/
setDecelerationTime(time, profile = 0x00) {
const message = Buffer.from([0x81, this.portId, 0x11, 0x06, 0x00, 0x00, profile]);
message.writeUInt16LE(time, 4);
this.send(message);
}
/**
* Set the motor speed.
* @method TachoMotor#setSpeed
* @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @param {number} time How long the motor should run for (in milliseconds).
* @returns {Promise} Resolved upon successful issuance of the command.
*/
setSpeed(speed, time) {
if (!this.isVirtualPort &amp;&amp; speed instanceof Array) {
throw new Error("Only virtual ports can accept multiple speeds");
}
if (this.isWeDo2SmartHub) {
throw new Error("Motor speed is not available on the WeDo 2.0 Smart Hub");
}
this.cancelEventTimer();
return new Promise((resolve) => {
if (speed === undefined || speed === null) {
speed = 100;
}
let message;
if (time !== undefined) {
if (speed instanceof Array) {
message = Buffer.from([0x81, this.portId, 0x11, 0x0a, 0x00, 0x00, (0, utils_1.mapSpeed)(speed[0]), (0, utils_1.mapSpeed)(speed[1]), this._maxPower, this._brakeStyle, this.useProfile()]);
}
else {
message = Buffer.from([0x81, this.portId, 0x11, 0x09, 0x00, 0x00, (0, utils_1.mapSpeed)(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
}
message.writeUInt16LE(time, 4);
}
else {
if (speed instanceof Array) {
message = Buffer.from([0x81, this.portId, 0x11, 0x08, (0, utils_1.mapSpeed)(speed[0]), (0, utils_1.mapSpeed)(speed[1]), this._maxPower, this.useProfile()]);
}
else {
message = Buffer.from([0x81, this.portId, 0x11, 0x07, (0, utils_1.mapSpeed)(speed), this._maxPower, this.useProfile()]);
}
}
this.send(message);
this._finishedCallbacks.push(() => {
return resolve();
});
});
}
/**
* Rotate a motor by a given amount of degrees.
* @method TachoMotor#rotateByDegrees
* @param {number} degrees How much the motor should be rotated (in degrees).
* @param {number} [speed=100] For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100.
* @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
*/
rotateByDegrees(degrees, speed) {
if (!this.isVirtualPort &amp;&amp; speed instanceof Array) {
throw new Error("Only virtual ports can accept multiple speeds");
}
if (this.isWeDo2SmartHub) {
throw new Error("Rotation is not available on the WeDo 2.0 Smart Hub");
}
this.cancelEventTimer();
return new Promise((resolve) => {
if (speed === undefined || speed === null) {
speed = 100;
}
let message;
if (speed instanceof Array) {
message = Buffer.from([0x81, this.portId, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, (0, utils_1.mapSpeed)(speed[0]), (0, utils_1.mapSpeed)(speed[1]), this._maxPower, this._brakeStyle, this.useProfile()]);
}
else {
message = Buffer.from([0x81, this.portId, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, (0, utils_1.mapSpeed)(speed), this._maxPower, this._brakeStyle, this.useProfile()]);
}
message.writeUInt32LE(degrees, 4);
this.send(message);
this._finishedCallbacks.push(() => {
return resolve();
});
});
}
useProfile() {
let value = 0x00;
if (this.useAccelerationProfile) {
value += 0x01;
}
if (this.useDecelerationProfile) {
value += 0x02;
}
return value;
}
}
exports.TachoMotor = TachoMotor;
var Mode;
(function (Mode) {
Mode[Mode["ROTATION"] = 2] = "ROTATION";
})(Mode || (exports.Mode = Mode = {}));
exports.ModeMap = {
"rotate": Mode.ROTATION
};
//# sourceMappingURL=tachomotor.js.map</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AbsoluteMotor.html">AbsoluteMotor</a></li><li><a href="BaseHub.html">BaseHub</a></li><li><a href="BasicMotor.html">BasicMotor</a></li><li><a href="ColorDistanceSensor.html">ColorDistanceSensor</a></li><li><a href="CurrentSensor.html">CurrentSensor</a></li><li><a href="Device.html">Device</a></li><li><a href="DuploTrainBase.html">DuploTrainBase</a></li><li><a href="DuploTrainBaseColorSensor.html">DuploTrainBaseColorSensor</a></li><li><a href="DuploTrainBaseMotor.html">DuploTrainBaseMotor</a></li><li><a href="DuploTrainBaseSpeaker.html">DuploTrainBaseSpeaker</a></li><li><a href="DuploTraniBaseSpeedometer.html">DuploTraniBaseSpeedometer</a></li><li><a href="Hub.html">Hub</a></li><li><a href="HubLED.html">HubLED</a></li><li><a href="LPF2Hub.html">LPF2Hub</a></li><li><a href="Light.html">Light</a></li><li><a href="Mario.html">Mario</a></li><li><a href="MarioAccelerometer.html">MarioAccelerometer</a></li><li><a href="MarioBarcodeSensor.html">MarioBarcodeSensor</a></li><li><a href="MarioPantsSensor.html">MarioPantsSensor</a></li><li><a href="MediumLinearMotor.html">MediumLinearMotor</a></li><li><a href="MotionSensor.html">MotionSensor</a></li><li><a href="MoveHub.html">MoveHub</a></li><li><a href="MoveHubMediumLinearMotor.html">MoveHubMediumLinearMotor</a></li><li><a href="MoveHubTiltSensor.html">MoveHubTiltSensor</a></li><li><a href="PiezoBuzzer.html">PiezoBuzzer</a></li><li><a href="PoweredUP.html">PoweredUP</a></li><li><a href="RemoteControl.html">RemoteControl</a></li><li><a href="RemoteControlButton.html">RemoteControlButton</a></li><li><a href="SimpleMediumLinearMotor.html">SimpleMediumLinearMotor</a></li><li><a href="TachoMotor.html">TachoMotor</a></li><li><a href="Technic3x3ColorLightMatrix.html">Technic3x3ColorLightMatrix</a></li><li><a href="TechnicColorSensor.html">TechnicColorSensor</a></li><li><a href="TechnicDistanceSensor.html">TechnicDistanceSensor</a></li><li><a href="TechnicForceSensor.html">TechnicForceSensor</a></li><li><a href="TechnicLargeAngularMotor.html">TechnicLargeAngularMotor</a></li><li><a href="TechnicLargeLinearMotor.html">TechnicLargeLinearMotor</a></li><li><a href="TechnicMediumAngularMotor.html">TechnicMediumAngularMotor</a></li><li><a href="TechnicMediumHub.html">TechnicMediumHub</a></li><li><a href="TechnicMediumHubAccelerometerSensor.html">TechnicMediumHubAccelerometerSensor</a></li><li><a href="TechnicMediumHubGyroSensor.html">TechnicMediumHubGyroSensor</a></li><li><a href="TechnicMediumHubTiltSensor.html">TechnicMediumHubTiltSensor</a></li><li><a href="TechnicSmallAngularMotor.html">TechnicSmallAngularMotor</a></li><li><a href="TechnicXLargeLinearMotor.html">TechnicXLargeLinearMotor</a></li><li><a href="TiltSensor.html">TiltSensor</a></li><li><a href="TrainMotor.html">TrainMotor</a></li><li><a href="VoltageSensor.html">VoltageSensor</a></li><li><a href="WeDo2SmartHub.html">WeDo2SmartHub</a></li></ul><h3>Events</h3><ul><li><a href="AbsoluteMotor.html#event:absolute">absolute</a></li><li><a href="AbsoluteMotor.html#event:rotate">rotate</a></li><li><a href="ColorDistanceSensor.html#event:ambient">ambient</a></li><li><a href="ColorDistanceSensor.html#event:color">color</a></li><li><a href="ColorDistanceSensor.html#event:colorAndDistance">colorAndDistance</a></li><li><a href="ColorDistanceSensor.html#event:distance">distance</a></li><li><a href="ColorDistanceSensor.html#event:distanceCount">distanceCount</a></li><li><a href="ColorDistanceSensor.html#event:reflect">reflect</a></li><li><a href="ColorDistanceSensor.html#event:rgbIntensity">rgbIntensity</a></li><li><a href="CurrentSensor.html#event:current">current</a></li><li><a href="DuploTrainBase.html#event:button">button</a></li><li><a href="DuploTrainBaseColorSensor.html#event:color">color</a></li><li><a href="DuploTrainBaseColorSensor.html#event:intensity">intensity</a></li><li><a href="DuploTrainBaseColorSensor.html#event:reflect">reflect</a></li><li><a href="DuploTrainBaseColorSensor.html#event:rgb">rgb</a></li><li><a href="DuploTrainBaseSpeedometer.html#event:speed">speed</a></li><li><a href="Hub.html#event:attach">attach</a></li><li><a href="Hub.html#event:button">button</a></li><li><a href="Hub.html#event:detach">detach</a></li><li><a href="Hub.html#event:disconnect">disconnect</a></li><li><a href="LPF2Hub.html#event:button">button</a></li><li><a href="Mario.html#event:button">button</a></li><li><a href="MarioAccelerometer.html#event:accel">accel</a></li><li><a href="MarioAccelerometer.html#event:gest">gest</a></li><li><a href="MarioBarcodeSensor.html#event:barcode">barcode</a></li><li><a href="MarioBarcodeSensor.html#event:rgb">rgb</a></li><li><a href="MarioPantsSensor.html#event:pants">pants</a></li><li><a href="MediumLinearMotor.html#event:rotate">rotate</a></li><li><a href="MotionSensor.html#event:distance">distance</a></li><li><a href="MoveHub.html#event:button">button</a></li><li><a href="MoveHubMediumLinearMotor.html#event:rotate">rotate</a></li><li><a href="MoveHubTiltSensor.html#event:tilt">tilt</a></li><li><a href="PoweredUP.html#event:discover">discover</a></li><li><a href="RemoteControl.html#event:button">button</a></li><li><a href="RemoteControlButton.html#event:remoteButton">remoteButton</a></li><li><a href="TachoMotor.html#event:rotate">rotate</a></li><li><a href="TechnicColorSensor.html#event:ambient">ambient</a></li><li><a href="TechnicColorSensor.html#event:color">color</a></li><li><a href="TechnicColorSensor.html#event:reflect">reflect</a></li><li><a href="TechnicDistanceSensor.html#event:distance">distance</a></li><li><a href="TechnicDistanceSensor.html#event:fastDistance">fastDistance</a></li><li><a href="TechnicForceSensor.html#event:force">force</a></li><li><a href="TechnicForceSensor.html#event:tapped">tapped</a></li><li><a href="TechnicForceSensor.html#event:touch">touch</a></li><li><a href="TechnicLargeAngularMotor.html#event:absolute">absolute</a></li><li><a href="TechnicLargeAngularMotor.html#event:rotate">rotate</a></li><li><a href="TechnicLargeLinearMotor.html#event:absolute">absolute</a></li><li><a href="TechnicLargeLinearMotor.html#event:rotate">rotate</a></li><li><a href="TechnicMediumAngularMotor.html#event:absolute">absolute</a></li><li><a href="TechnicMediumAngularMotor.html#event:rotate">rotate</a></li><li><a href="TechnicMediumHub.html#event:button">button</a></li><li><a href="TechnicMediumHubAccelerometerSensor.html#event:accel">accel</a></li><li><a href="TechnicMediumHubGyroSensor.html#event:gyro">gyro</a></li><li><a href="TechnicMediumHubTiltSensor.html#event:impactCount">impactCount</a></li><li><a href="TechnicMediumHubTiltSensor.html#event:tilt">tilt</a></li><li><a href="TechnicSmallAngularMotor.html#event:absolute">absolute</a></li><li><a href="TechnicSmallAngularMotor.html#event:rotate">rotate</a></li><li><a href="TechnicXLargeLinearMotor.html#event:absolute">absolute</a></li><li><a href="TechnicXLargeLinearMotor.html#event:rotate">rotate</a></li><li><a href="TiltSensor.html#event:tilt">tilt</a></li><li><a href="VoltageSensor.html#event:voltage">voltage</a></li><li><a href="WeDo2SmartHub.html#event:button">button</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Thu Apr 25 2024 17:10:11 GMT-0700 (Pacific Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>