From f833736dd6675ade030006872c4be93cf9ce1281 Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Tue, 21 Mar 2017 21:24:57 -0600 Subject: [PATCH 1/4] Added Hardware Shutdown Promise --- BoneCentral/Brain/CppInterface/Factory.js | 2 +- BoneCentral/Brain/CppInterface/PowerManager.js | 4 +++- BoneCentral/Brain/Sockets/index.js | 15 ++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/BoneCentral/Brain/CppInterface/Factory.js b/BoneCentral/Brain/CppInterface/Factory.js index 2727448..f1a36d2 100644 --- a/BoneCentral/Brain/CppInterface/Factory.js +++ b/BoneCentral/Brain/CppInterface/Factory.js @@ -30,7 +30,7 @@ module.exports = (function() { }; Factory.prototype.createPowerManager = function () { - return new PowerManager(dispatcherSocket.Input); + return new PowerManager(this.dispatcherSocket.Input, this.dispatcherSocket.OnExit); }; Factory.prototype.createThrustController = function () { diff --git a/BoneCentral/Brain/CppInterface/PowerManager.js b/BoneCentral/Brain/CppInterface/PowerManager.js index d10e7e6..e34f494 100644 --- a/BoneCentral/Brain/CppInterface/PowerManager.js +++ b/BoneCentral/Brain/CppInterface/PowerManager.js @@ -1,7 +1,8 @@ module.exports = (function(){ - function PowerManager(cmdOut) { + function PowerManager(cmdOut, onExit) { this._cmdOut = cmdOut; + this._onExit = onExit; } PowerManager.prototype.turnOnEscs = function(){ @@ -22,6 +23,7 @@ module.exports = (function(){ PowerManager.prototype.exit = function () { this._cmdOut.write("exit\n"); + return this._onExit; }; return PowerManager; diff --git a/BoneCentral/Brain/Sockets/index.js b/BoneCentral/Brain/Sockets/index.js index 4882c3a..d50c89b 100644 --- a/BoneCentral/Brain/Sockets/index.js +++ b/BoneCentral/Brain/Sockets/index.js @@ -3,15 +3,24 @@ */ var Streams = require('stream'); -var Net = require('net'); +var Net = require('net'); +var $ = require('../Utilities').Promises module.exports.createSocket = function (port) { - var input = new Streams.PassThrough(); + var input = new Streams.PassThrough(); var output = new Streams.PassThrough(); + var onExit = $.Deferred(); Net.createServer(function (socket) { _handleClient(socket, input, output); + socket.on("close", function() { + onExit.resolve(); + }); }).listen(port); - return {Input: input, Output: output}; + return { + Input: input, + Output: output, + OnExit: onExit.promise() + }; }; var _handleClient = function (socket, input, output) { From 008e8c9ebc95bba5f38b9b398be6df5c2e1fbeca Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Mon, 20 Mar 2017 21:59:59 +0000 Subject: [PATCH 2/4] Added Hardware Connect Promise --- BoneCentral/Brain/CppInterface/Factory.js | 2 +- BoneCentral/Brain/CppInterface/PowerManager.js | 10 +++++++--- BoneCentral/Brain/Sockets/index.js | 15 +++++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/BoneCentral/Brain/CppInterface/Factory.js b/BoneCentral/Brain/CppInterface/Factory.js index f1a36d2..f07ccf2 100644 --- a/BoneCentral/Brain/CppInterface/Factory.js +++ b/BoneCentral/Brain/CppInterface/Factory.js @@ -30,7 +30,7 @@ module.exports = (function() { }; Factory.prototype.createPowerManager = function () { - return new PowerManager(this.dispatcherSocket.Input, this.dispatcherSocket.OnExit); + return new PowerManager(this.dispatcherSocket.Input, this.dispatcherSocket.Events); }; Factory.prototype.createThrustController = function () { diff --git a/BoneCentral/Brain/CppInterface/PowerManager.js b/BoneCentral/Brain/CppInterface/PowerManager.js index e34f494..fd9d71b 100644 --- a/BoneCentral/Brain/CppInterface/PowerManager.js +++ b/BoneCentral/Brain/CppInterface/PowerManager.js @@ -1,8 +1,8 @@ module.exports = (function(){ - function PowerManager(cmdOut, onExit) { + function PowerManager(cmdOut, events) { this._cmdOut = cmdOut; - this._onExit = onExit; + this._events = events; } PowerManager.prototype.turnOnEscs = function(){ @@ -20,10 +20,14 @@ module.exports = (function(){ PowerManager.prototype.turnOffImu = function () { this._cmdOut.write("turnOffImuSensor\n"); }; + + PowerManager.prototype.connect = function() { + return this._events.OnConnect; + } PowerManager.prototype.exit = function () { this._cmdOut.write("exit\n"); - return this._onExit; + return this._events.OnExit; }; return PowerManager; diff --git a/BoneCentral/Brain/Sockets/index.js b/BoneCentral/Brain/Sockets/index.js index d50c89b..a2da922 100644 --- a/BoneCentral/Brain/Sockets/index.js +++ b/BoneCentral/Brain/Sockets/index.js @@ -7,19 +7,26 @@ var Net = require('net'); var $ = require('../Utilities').Promises module.exports.createSocket = function (port) { - var input = new Streams.PassThrough(); - var output = new Streams.PassThrough(); - var onExit = $.Deferred(); + var input = new Streams.PassThrough(); + var output = new Streams.PassThrough(); + var onExit = $.Deferred(); + var onConnect = $.Deferred(); + Net.createServer(function (socket) { _handleClient(socket, input, output); socket.on("close", function() { onExit.resolve(); }); + onConnect.resolve(); }).listen(port); + return { Input: input, Output: output, - OnExit: onExit.promise() + Events: { + OnConnect: onConnect.promise(), + OnExit: onExit.promise() + } }; }; From 6e052a8accdbee8feb2d561fb33f739587d7c9de Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Mon, 20 Mar 2017 22:01:37 +0000 Subject: [PATCH 3/4] Added AI Script That Will Align the Machine to an Angle --- BoneCentral/Brain/AI/AlignToAngle.js | 87 ++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 BoneCentral/Brain/AI/AlignToAngle.js diff --git a/BoneCentral/Brain/AI/AlignToAngle.js b/BoneCentral/Brain/AI/AlignToAngle.js new file mode 100644 index 0000000..ac7b60a --- /dev/null +++ b/BoneCentral/Brain/AI/AlignToAngle.js @@ -0,0 +1,87 @@ +var $ = require('../Utilities').Promises +var wait = require('../Utilities').Wait; +var vector = require('../Utilities/vector.js'); +var ctools = require('../Utilities/compassTools.js'); +var PID = require('../Utilities/pid.js'); +var Poseidon = require('../app.js'); + +var upInterval, + time = 1.0, + pid_sp = 0; +var pid = new PID(); +var imu = Poseidon.HardwareFactory.createImuSensor(); +var power = Poseidon.HardwareFactory.createPowerManager(); +var engine = Poseidon.HardwareFactory.createThrustController(); +var start = $.Deferred(); +var finish = $.Deferred(); +const DELTA = 150; +const TRANSFORM = 180.0; + +pid.setDelta(DELTA); +pid.setBounds(-TRANSFORM, TRANSFORM); +pid.calibrate(0.1, 0.5, 0.01); + + + +start +.then(function() { + console.log("- System Online -"); + power.turnOnEscs(); + power.turnOnImu(); +}) +.then(function() { + upInterval = setInterval(function(){ + update(); + killOutOfTime(); + }, DELTA); +}); + +finish +.then(function() { + console.log("- Shutting Down -"); + power.turnOffImu(); + power.turnOffEscs(); + power.exit() + .then(process.exit); +}) + +power.connect() +.then(function(){ + console.log("Ready? GO!"); + start.resolve(); +}); + + + +function update() { + var waitHeading = $.Deferred(); + var waitAccel = $.Deferred(); + + imu.getHeading().done(function(data) { + waitHeading.resolve(data); + }); + imu.getAcceleration().done(function(data){ + waitAccel.resolve(data); + }); + + $.WhenAll(waitHeading, waitAccel) + .done(function(cdata,adata) { + // console.log(cdata); + // console.log(adata); + // var compass = vector.v3(cData.X,cData.Y,cData.Z); + // var accel = vector.v3(aData.X,aData.Y,aData.Z); + + // var pid_pv = ctools.degrees(compass, accel); + // var adj = pid.update(pid_sp, pid_pv) / TRANSFORM; + // console.log("Adjust: " + (adj*100).toFixed(2) + "%"); + // engine.yaw(adj); + }); +} + +function killOutOfTime() { + time = time - (DELTA/1000.0); + if(time <= 0) { + clearInterval(upInterval); + finish.resolve(); + } +} \ No newline at end of file From b09899cc0d2ea2d4b5cea8a72dde50255398151b Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Wed, 22 Mar 2017 17:20:49 -0600 Subject: [PATCH 4/4] Commented Code --- BoneCentral/Brain/AI/AlignToAngle.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/BoneCentral/Brain/AI/AlignToAngle.js b/BoneCentral/Brain/AI/AlignToAngle.js index ac7b60a..eb30e8e 100644 --- a/BoneCentral/Brain/AI/AlignToAngle.js +++ b/BoneCentral/Brain/AI/AlignToAngle.js @@ -66,15 +66,15 @@ function update() { $.WhenAll(waitHeading, waitAccel) .done(function(cdata,adata) { - // console.log(cdata); - // console.log(adata); - // var compass = vector.v3(cData.X,cData.Y,cData.Z); - // var accel = vector.v3(aData.X,aData.Y,aData.Z); + console.log(cdata); + console.log(adata); + var compass = vector.v3(cData.X,cData.Y,cData.Z); + var accel = vector.v3(aData.X,aData.Y,aData.Z); - // var pid_pv = ctools.degrees(compass, accel); - // var adj = pid.update(pid_sp, pid_pv) / TRANSFORM; - // console.log("Adjust: " + (adj*100).toFixed(2) + "%"); - // engine.yaw(adj); + var pid_pv = ctools.degrees(compass, accel); + var adj = pid.update(pid_sp, pid_pv) / TRANSFORM; + console.log("Adjust: " + (adj*100).toFixed(2) + "%"); + engine.yaw(adj); }); } @@ -84,4 +84,4 @@ function killOutOfTime() { clearInterval(upInterval); finish.resolve(); } -} \ No newline at end of file +}