js-electrical-engineering-equations
Version:
This is an ES6/ES2015 library of Electrical Engineering Equations. It works with Typescript, es6, and es5.
196 lines (155 loc) • 4.98 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _utilFunctions = require("./util-functions");
var _BatteryStorage = require("./objects/BatteryStorage");
var _BatteryStorage2 = _interopRequireDefault(_BatteryStorage);
var _BatteryRuntime = require("./objects/BatteryRuntime");
var _BatteryRuntime2 = _interopRequireDefault(_BatteryRuntime);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*
* Facts
* 1 amp over 1 second is 1 coulombs
* 1 amp over 60 seconds is 60 coulombs
* 1 amp over 3600 seconds is 3600 coulombs
*
* Amp hours are amps times hours so (divide by amps to get hours) and (divide by hours to get amps)
*
* */
/**
* Represents a Battery.
*
* Either set the aH or coulombs of the battery for capacity before calculating runtime.
* @example
* let battery = Battery();
* battery.setBatteryCapacityInAmpHours(1);
* battery.setCurrentDraw(1);
* battery.calculateRuntime();
* const runtimeInMin = battery.getRunTimeInMin();
*
*/
var Battery = function () {
/**
* Takes no parameters, and initializes a fresh battery object.
* @return {Battery}
*/
function Battery() {
_classCallCheck(this, Battery);
/**
* @type {BatteryStorage}
*/
this.battery = new _BatteryStorage2.default();
/**
* @type {BatteryRuntime}
*/
this.runtime = new _BatteryRuntime2.default();
/**
* @type {number}
*/
this.efficiency = .8;
/**
* @type {number}
*/
this.precision = 4;
/**
* @type {number}
*/
this.currentDraw = 0;
}
/**
* @param {number} amps - The current draw in amps.
*/
_createClass(Battery, [{
key: "setCurrentDraw",
value: function setCurrentDraw(amps) {
this.currentDraw = amps;
}
/**
* @param {number} ampHours - The battery capacity in amp hours.
* @return {undefined}
*/
}, {
key: "setBatteryCapacityInAmpHours",
value: function setBatteryCapacityInAmpHours(ampHours) {
this.battery.ampHours = ampHours;
this.battery.coulombs = ampHours * 3600;
}
/**
* @return {number}
*/
}, {
key: "getBatteryCapacityInAmpHours",
value: function getBatteryCapacityInAmpHours() {
return (0, _utilFunctions.getFloat)(this.battery.ampHours, this.precision);
}
/**
* @param {number} coulombs - The battery capacity in coulombs.
* @return {undefined}
*/
}, {
key: "setBatteryCapacityInCoulombs",
value: function setBatteryCapacityInCoulombs(coulombs) {
this.battery.coulombs = coulombs;
this.battery.ampHours = coulombs / 3600;
}
/**
* @return {number}
*/
}, {
key: "getBatteryCapacityInCoulombs",
value: function getBatteryCapacityInCoulombs() {
return (0, _utilFunctions.getFloat)(this.battery.coulombs, this.precision);
}
/**
* @return {BatteryRuntime}
*/
}, {
key: "calculateRuntime",
value: function calculateRuntime() {
this.runtime.totalHours = this.battery.ampHours / (this.currentDraw / this.efficiency);
this.runtime.totalMin = this.runtime.totalHours * 60;
this.runtime.totalSeconds = this.runtime.totalMin * 60;
return this.getRunTime();
}
/**
* @return {BatteryRuntime}
*/
}, {
key: "getRunTime",
value: function getRunTime() {
return this.runtime;
}
/**
* @return {number}
*/
}, {
key: "getRunTimeInMin",
value: function getRunTimeInMin() {
return (0, _utilFunctions.getFloat)(this.runtime.totalMin, this.precision);
}
/**
* @return {number}
*/
}, {
key: "getRunTimeInHours",
value: function getRunTimeInHours() {
return (0, _utilFunctions.getFloat)(this.runtime.totalHours, this.precision);
}
/**
* @param {number} integer - The floating precision.
* @return {undefined}
*/
}, {
key: "setPrecision",
value: function setPrecision() {
var integer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 4;
this.precision = integer;
}
}]);
return Battery;
}();
exports.default = Battery;
module.exports = exports["default"];