From a424482fb295cd9014740e7ee9af717f353ecaae Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Tue, 21 Mar 2017 21:11:05 -0600 Subject: [PATCH 1/4] Moved Routes to a Routes File --- BoneCentral/WebApplication/index.js | 234 ++------------------------- BoneCentral/WebApplication/routes.js | 212 ++++++++++++++++++++++++ 2 files changed, 224 insertions(+), 222 deletions(-) create mode 100644 BoneCentral/WebApplication/routes.js diff --git a/BoneCentral/WebApplication/index.js b/BoneCentral/WebApplication/index.js index 6c0632f..caa2507 100644 --- a/BoneCentral/WebApplication/index.js +++ b/BoneCentral/WebApplication/index.js @@ -1,13 +1,14 @@ -var express = require('express'); -var bodyParser = require('body-parser'); -var fileSystem = require('fs'); -var path = require("path"); -var CppInterface = require('../Brain/CppInterface'); -var VisionInterface = require("../Brain/VisionInterface"); -var WebLogger = require('./WebLogger'); -var FileLogger = require('./FileLogger'); -var app = express(); -var GoThroughGate = require("../Brain/GoThroughGate"); +var express = require('express'); +var bodyParser = require('body-parser'); +var fileSystem = require('fs'); +var path = require("path"); +var routes = require('./routes.js'); +var WebLogger = require('./WebLogger'); +var FileLogger = require('./FileLogger'); +var CppInterface = require('../Brain/CppInterface'); +var VisionInterface = require("../Brain/VisionInterface"); +var GoThroughGate = require("../Brain/GoThroughGate"); +var ThrustManager = require("../Brain/ThrustManager"); var peripheralsFactory = new CppInterface.Factory(); var ThrustManager = require("../Brain/ThrustManager"); @@ -31,218 +32,7 @@ var goThroughGate = new GoThroughGate(visionFactoy, thrustManager, webLogger); app.use('/', express.static('static')); app.use(bodyParser.json()); - -app.get("/goThroughGate", function (req, res) { - goThroughGate.execute().done(function () { - thrustController.goDirection(0, 0, 0); - }); - res.send("Going through gate"); -}); - -app.get("/terminate", function (req, res) { - goThroughGate._shouldQuit = true; - res.send("Terminated"); -}); - -//From IThruster -app.get('/initialize', function(req, res) { - res.send('initialize'); -}); - -app.post('/thrust', function(req, res) { - res.send('thrust ' + req.body.powerLevel); -}); - -app.get('/stdoutData', function(req, res) { - res.send(webLogger.pull()); -}); - -// From IThrustController -app.post('/move', function (req, res) { - var params = req.body; - thrustController.move(params.throttle); - res.send(''); -}); - -// From IThrustController -app.post('/secondaryDive', function (req, res) { - var params = req.body; - thrustController.secondaryDive(params.throttle); - res.send(''); -}); - -// From IThrustController -app.post('/dive', function (req, res) { - var params = req.body; - thrustController.dive(params.throttle); - res.send(''); -}); - -// From IThrustController -app.post('/yaw', function (req, res) { - var params = req.body; - thrustController.yaw(params.throttle); - res.send(''); -}); - -// From IThrustController -app.post('/pitch', function (req, res) { - var params = req.body; - thrustController.pitch(params.throttle); - res.send(''); -}); - -// From IThrustController -app.post('/roll', function (req, res) { - var params = req.body; - thrustController.roll(params.throttle); - res.send(''); -}); - -// From IThrustController -app.post('/goDirection', function(req, res) { - var params = req.body; - thrustController.goDirection(params.move, params.secondaryDive, params.dive); - res.send(''); -}); - -// From IThrustController -app.post('/rotate', function(req, res) { - var params = req.body; - thrustController.rotate(params.yaw, params.pitch, params.roll); - res.send(''); -}); - -// From IThrustController -app.get('/killThrust', function(req, res) { - thrustController.kill(); - res.send('killThrust'); -}); - -// From Imu -app.get('/turnOnImuSensor', function(req, res) { - powerManager.turnOnImu(); - res.send('turnOnImuSensor'); -}); - -app.get('/turnOffImuSensor', function(req, res) { - powerManager.turnOffImu(); - res.send('turnOffImuSensor'); -}); - -app.get('/getAcceleration', function(req, res) { - imuSensor.getAcceleration().done(function(accel) { - webLogger.info("Acceleration: " + JSON.stringify(accel)); - }); - res.send('ran getAcceleration'); -}); - -app.get('/getAngularAcceleration', function(req, res) { - imuSensor.getAngularAcceleration().done(function(accel) { - webLogger.info("Angular Acceleration: " + JSON.stringify(accel)); - }); - res.send('ran getAngularAcceleration'); -}); - -app.get('/getHeading', function(req, res) { - imuSensor.getHeading().done(function(heading) { - webLogger.info("Heading: " + JSON.stringify(heading)); - }); - res.send('ran getHeading'); -}); - -app.get('/getInternalTemperature', function(req, res) { - imuSensor.getInternalTemperature().done(function(temperature) { - webLogger.info("Internal Temperature: " + JSON.stringify(temperature)); - }); - res.send('getInternalTemperature'); -}); - -app.get('/getInternalPressure', function(req, res) { - imuSensor.getInternalPressure().done(function(pressure) { - webLogger.info("Internal Pressure: " + JSON.stringify(pressure)); - }); - res.send('getInternalPressure'); -}); - -app.get('/getExternalTemperature', function(req, res) { - imuSensor.getExternalTemperature().done(function(temperature) { - webLogger.info("External Temperature: " + JSON.stringify(temperature)); - }); - res.send('getExternalTemperature'); -}); - -app.get('/getExternalPressure', function(req, res) { - imuSensor.getExternalPressure().done(function(pressure) { - webLogger.info("External Pressure: " + JSON.stringify(pressure)); - }); - res.send('getExternalPressure'); -}); - -app.get('/exit', function(req, res) { - res.send('exit'); - powerManager.exit(); - process.exit(); -}); - - -// From IPowerController { -app.get('/turnOnEscs', function(req, res) { - powerManager.turnOnEscs(); - res.send('turnOnEscs'); -}); - -app.get('/turnOffEscs', function(req, res) { - powerManager.turnOffEscs(); - res.send('turnOffEscs'); -}); - - -// Headlight Control -app.get('/headlight', function(req, res) { - headLights.toggleLights(); - res.send('toggled Headlights'); -}); - -app.get('/startSearchingForPoles', function (req, res) { - gateDetector.startSearching(); - res.send('startSearchingForPoles'); -}); - -app.get('/getPoleCoordinates', function (req, res) { - gateDetector.getPoleCoordinates().done(function (poleCoords) { - webLogger.info("Pole Coordinates: " + JSON.stringify(poleCoords)); - }); - res.send('getPoleCoordinates'); -}); - -app.get('/refreshHsv', function (req, res) { - gateDetector.refreshHsv(); - res.send('refreshHsv'); -}); - -app.get('/stopSearchingForPoles', function (req, res) { - gateDetector.stopSearching(); - res.send('stopSearchingForPoles'); -}); - -// Script Run -app.post('/runScript', function(req, res) { - var scriptIndex = req.body.scriptId; - fileSystem.readdir("./TestScripts", function (err, fileNames) { - if(fileNames[scriptIndex]) { - var filePath = path.resolve("TestScripts", fileNames[scriptIndex]); - if(require.cache[filePath]) delete require.cache[filePath]; - var script = require(filePath); - script.execute(thrustController, imuSensor, webLogger, headLights, powerManager); - res.send('ran ' + fileNames[scriptIndex]); - } - else { - res.send('script not found'); - } - }) -}); - +routes(app); app.listen(80, function () { console.log('Example app listening on port 80!'); diff --git a/BoneCentral/WebApplication/routes.js b/BoneCentral/WebApplication/routes.js new file mode 100644 index 0000000..113c106 --- /dev/null +++ b/BoneCentral/WebApplication/routes.js @@ -0,0 +1,212 @@ +module.exports = function(app, msngr) { + app.get("/goThroughGate", function (req, res) { + goThroughGate.execute().done(function () { + thrustController.goDirection(0, 0, 0); + }); + res.send("Going through gate"); + }); + + app.get("/terminate", function (req, res) { + goThroughGate._shouldQuit = true; + res.send("Terminated"); + }); + + //From IThruster + app.get('/initialize', function(req, res) { + res.send('initialize'); + }); + + app.post('/thrust', function(req, res) { + res.send('thrust ' + req.body.powerLevel); + }); + + app.get('/stdoutData', function(req, res) { + res.send(webLogger.pull()); + }); + + // From IThrustController + app.post('/move', function (req, res) { + var params = req.body; + thrustController.move(params.throttle); + res.send(''); + }); + + // From IThrustController + app.post('/secondaryDive', function (req, res) { + var params = req.body; + thrustController.secondaryDive(params.throttle); + res.send(''); + }); + + // From IThrustController + app.post('/dive', function (req, res) { + var params = req.body; + thrustController.dive(params.throttle); + res.send(''); + }); + + // From IThrustController + app.post('/yaw', function (req, res) { + var params = req.body; + thrustController.yaw(params.throttle); + res.send(''); + }); + + // From IThrustController + app.post('/pitch', function (req, res) { + var params = req.body; + thrustController.pitch(params.throttle); + res.send(''); + }); + + // From IThrustController + app.post('/roll', function (req, res) { + var params = req.body; + thrustController.roll(params.throttle); + res.send(''); + }); + + // From IThrustController + app.post('/goDirection', function(req, res) { + var params = req.body; + thrustController.goDirection(params.move, params.secondaryDive, params.dive); + res.send(''); + }); + + // From IThrustController + app.post('/rotate', function(req, res) { + var params = req.body; + thrustController.rotate(params.yaw, params.pitch, params.roll); + res.send(''); + }); + + // From IThrustController + app.get('/killThrust', function(req, res) { + thrustController.kill(); + res.send('killThrust'); + }); + + // From Imu + app.get('/turnOnImuSensor', function(req, res) { + powerManager.turnOnImu(); + res.send('turnOnImuSensor'); + }); + + app.get('/turnOffImuSensor', function(req, res) { + powerManager.turnOffImu(); + res.send('turnOffImuSensor'); + }); + + app.get('/getAcceleration', function(req, res) { + imuSensor.getAcceleration().done(function(accel) { + webLogger.info("Acceleration: " + JSON.stringify(accel)); + }); + res.send('ran getAcceleration'); + }); + + app.get('/getAngularAcceleration', function(req, res) { + imuSensor.getAngularAcceleration().done(function(accel) { + webLogger.info("Angular Acceleration: " + JSON.stringify(accel)); + }); + res.send('ran getAngularAcceleration'); + }); + + app.get('/getHeading', function(req, res) { + imuSensor.getHeading().done(function(heading) { + webLogger.info("Heading: " + JSON.stringify(heading)); + }); + res.send('ran getHeading'); + }); + + app.get('/getInternalTemperature', function(req, res) { + imuSensor.getInternalTemperature().done(function(temperature) { + webLogger.info("Internal Temperature: " + JSON.stringify(temperature)); + }); + res.send('getInternalTemperature'); + }); + + app.get('/getInternalPressure', function(req, res) { + imuSensor.getInternalPressure().done(function(pressure) { + webLogger.info("Internal Pressure: " + JSON.stringify(pressure)); + }); + res.send('getInternalPressure'); + }); + + app.get('/getExternalTemperature', function(req, res) { + imuSensor.getExternalTemperature().done(function(temperature) { + webLogger.info("External Temperature: " + JSON.stringify(temperature)); + }); + res.send('getExternalTemperature'); + }); + + app.get('/getExternalPressure', function(req, res) { + imuSensor.getExternalPressure().done(function(pressure) { + webLogger.info("External Pressure: " + JSON.stringify(pressure)); + }); + res.send('getExternalPressure'); + }); + + app.get('/exit', function(req, res) { + res.send('exit'); + powerManager.exit(); + process.exit(); + }); + + + // From IPowerController { + app.get('/turnOnEscs', function(req, res) { + powerManager.turnOnEscs(); + res.send('turnOnEscs'); + }); + + app.get('/turnOffEscs', function(req, res) { + powerManager.turnOffEscs(); + res.send('turnOffEscs'); + }); + + + // Headlight Control + app.get('/headlight', function(req, res) { + headLights.toggleLights(); + res.send('toggled Headlights'); + }); + + app.get('/startSearchingForPoles', function (req, res) { + gateDetector.startSearching(); + res.send('startSearchingForPoles'); + }); + + app.get('/getPoleCoordinates', function (req, res) { + gateDetector.getPoleCoordinates().done(function (poleCoords) { + webLogger.info("Pole Coordinates: " + JSON.stringify(poleCoords)); + }); + res.send('getPoleCoordinates'); + }); + + app.get('/refreshHsv', function (req, res) { + gateDetector.refreshHsv(); + res.send('refreshHsv'); + }); + + app.get('/stopSearchingForPoles', function (req, res) { + gateDetector.stopSearching(); + res.send('stopSearchingForPoles'); + }); + + // Script Run + app.post('/runScript', function(req, res) { + var scriptIndex = req.body.scriptId; + fileSystem.readdir("./TestScripts", function (err, fileNames) { + if(fileNames[scriptIndex]) { + var filePath = path.resolve("TestScripts", fileNames[scriptIndex]); + if(require.cache[filePath]) delete require.cache[filePath]; + var script = require(filePath); + script.execute(thrustController, imuSensor, webLogger, headLights, powerManager); + res.send('ran ' + fileNames[scriptIndex]); + } + else { + res.send('script not found'); + } + }) + }); +} \ No newline at end of file From a38726fa472e683a9fe3c9d84769e824187bc2db Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Tue, 21 Mar 2017 21:11:59 -0600 Subject: [PATCH 2/4] Moved Hardware Commands to Seperate File --- BoneCentral/WebApplication/hardware.js | 169 ++++++++++++++++++++++++ BoneCentral/WebApplication/index.js | 37 +----- BoneCentral/WebApplication/messenger.js | 40 ++++++ BoneCentral/WebApplication/routes.js | 106 +++++---------- 4 files changed, 246 insertions(+), 106 deletions(-) create mode 100644 BoneCentral/WebApplication/hardware.js create mode 100644 BoneCentral/WebApplication/messenger.js diff --git a/BoneCentral/WebApplication/hardware.js b/BoneCentral/WebApplication/hardware.js new file mode 100644 index 0000000..f34457a --- /dev/null +++ b/BoneCentral/WebApplication/hardware.js @@ -0,0 +1,169 @@ +var fileSystem = require('fs'); +var path = require("path"); +var WebLogger = require('./WebLogger'); +var FileLogger = require('./FileLogger'); +var CppInterface = require('../Brain/CppInterface'); +var VisionInterface = require("../Brain/VisionInterface"); +var GoThroughGate = require("../Brain/GoThroughGate"); +var ThrustManager = require("../Brain/ThrustManager"); + +var fileLogger = new FileLogger("./test.log"); +var webLogger = new WebLogger(fileLogger); +var peripheralsFactory = new CppInterface.Factory(); +var visionFactoy = new VisionInterface.Factory(); +var thrustManager = new ThrustManager(peripheralsFactory); +var goThroughGate = new GoThroughGate(visionFactoy, thrustManager, webLogger); + +var thrustController = peripheralsFactory.createThrustController(); +var powerManager = peripheralsFactory.createPowerManager(); +var imuSensor = peripheralsFactory.createImuSensor(); +var headLights = peripheralsFactory.createHeadlights(); +var gateDetector = visionFactoy.createGateDetector(webLogger); + +peripheralsFactory.createCppLogSource(webLogger); +CppInterface.Peripherals.initialize(); + +function DMSG(x) { + console.log(x); +} + +module.exports = { + goThroughGate: function() { + goThroughGate.execute().done(function () { + thrustController.goDirection(0, 0, 0); + }); + }, + terminate: function() { + goThroughGate._shouldQuit = true; + }, + pullWebLog: function() { + return webLogger.pull(); + }, + + move: function(value) { + DMSG("Moving: " + value); + thrustController.move(value); + }, + dive: function(value) { + DMSG("Diving: " + value); + thrustController.dive(value); + }, + secondaryDive: function(value) { + DMSG("Diving2: " + value); + thrustController.secondaryDive(value); + }, + yaw: function(value) { + DMSG("Yawing: " + value); + thrustController.yaw(value); + }, + pitch: function(value) { + DMSG("Pitching: " + value); + thrustController.pitch(value); + }, + roll: function(value) { + DMSG("Rolling: " + value); + thrustController.roll(value); + }, + goDirection: function(move, dive, dive2) { + DMSG("Go Direction: " + move + ", " + dive2 + ", " + dive); + thrustController.goDirection(move, dive2, dive); + }, + rotate: function(yaw, pitch, roll) { + DMSG("Rotate: " + yaw + ", " + pitch + ", " + roll); + thrustController.rotate(yaw, pitch, roll); + }, + killThrust: function() { + DMSG("Kill Thrusters"); + thrustController.kill(); + }, + + turnOnImu: function() { + powerManager.turnOnImu(); + }, + turnOffImu: function() { + powerManager.turnOffImu(); + }, + getAcceleration: function() { + imuSensor.getAcceleration().done(function(accel) { + webLogger.info("Acceleration: " + JSON.stringify(accel)); + }); + }, + getAngularAcceleration: function() { + imuSensor.getAngularAcceleration().done(function(accel) { + webLogger.info("Angular Acceleration: " + JSON.stringify(accel)); + }); + }, + getHeading: function() { + imuSensor.getHeading().done(function(heading) { + webLogger.info("Heading: " + JSON.stringify(heading)); + }); + }, + getInternalTemperature: function() { + imuSensor.getInternalTemperature().done(function(temperature) { + webLogger.info("Internal Temperature: " + JSON.stringify(temperature)); + }); + }, + getExternalTemperature: function() { + imuSensor.getExternalTemperature().done(function(temperature) { + webLogger.info("External Temperature: " + JSON.stringify(temperature)); + }); + }, + getInternalPressure: function() { + imuSensor.getInternalPressure().done(function(pressure) { + webLogger.info("Internal Pressure: " + JSON.stringify(pressure)); + }); + }, + getExternalPressure: function() { + imuSensor.getExternalPressure().done(function(pressure) { + webLogger.info("External Pressure: " + JSON.stringify(pressure)); + }); + }, + + exit: function() { + powerManager.exit(); + process.exit(); + }, + + turnOnEscs: function() { + DMSG("Turn On Escs"); + powerManager.turnOnEscs(); + }, + turnOffEscs: function() { + DMSG("Turn Off Escs"); + powerManager.turnOffEscs(); + }, + + toggleLights: function() { + DMSG("Toggle Lights"); + headLights.toggleLights(); + }, + + startPoleSearch: function() { + gateDetector.startSearching(); + }, + stopPoleSearch: function() { + gateDetector.stopSearching(); + }, + getPoleLocation: function() { + gateDetector.getPoleCoordinates().done(function (poleCoords) { + webLogger.info("Pole Coordinates: " + JSON.stringify(poleCoords)); + }); + }, + refreshHSV: function() { + gateDetector.refreshHsv(); + }, + runScript: function(index) { + fileSystem.readdir("./TestScripts", function (err, fileNames) { + if(fileNames[index]) { + var filePath = path.resolve("TestScripts", fileNames[index]); + if(require.cache[filePath]) delete require.cache[filePath]; + var script = require(filePath); + script.execute(thrustController, imuSensor, webLogger, headLights, powerManager); + return ('ran ' + fileNames[index]); + } + else { + return ('script not found'); + } + }) + } +}; \ No newline at end of file diff --git a/BoneCentral/WebApplication/index.js b/BoneCentral/WebApplication/index.js index caa2507..717bedc 100644 --- a/BoneCentral/WebApplication/index.js +++ b/BoneCentral/WebApplication/index.js @@ -1,39 +1,14 @@ var express = require('express'); var bodyParser = require('body-parser'); -var fileSystem = require('fs'); -var path = require("path"); var routes = require('./routes.js'); -var WebLogger = require('./WebLogger'); -var FileLogger = require('./FileLogger'); -var CppInterface = require('../Brain/CppInterface'); -var VisionInterface = require("../Brain/VisionInterface"); -var GoThroughGate = require("../Brain/GoThroughGate"); -var ThrustManager = require("../Brain/ThrustManager"); - -var peripheralsFactory = new CppInterface.Factory(); -var ThrustManager = require("../Brain/ThrustManager"); -var thrustManager = new ThrustManager(peripheralsFactory); - -var thrustController = peripheralsFactory.createThrustController(); -var headLights = peripheralsFactory.createHeadlights(); -var powerManager = peripheralsFactory.createPowerManager(); -var imuSensor = peripheralsFactory.createImuSensor(); - -var fileLogger = new FileLogger("./test.log"); -var webLogger = new WebLogger(fileLogger); -peripheralsFactory.createCppLogSource(webLogger); - -CppInterface.Peripherals.initialize(); - -var visionFactoy = new VisionInterface.Factory(); -var gateDetector = visionFactoy.createGateDetector(webLogger); - -var goThroughGate = new GoThroughGate(visionFactoy, thrustManager, webLogger); +var messenger = require('./messenger.js'); +var port = 80; +var app = express(); app.use('/', express.static('static')); app.use(bodyParser.json()); -routes(app); +routes(app, messenger); -app.listen(80, function () { - console.log('Example app listening on port 80!'); +app.listen(port, function () { + console.log('Web app listening on port 80!'); }); diff --git a/BoneCentral/WebApplication/messenger.js b/BoneCentral/WebApplication/messenger.js new file mode 100644 index 0000000..6d070bd --- /dev/null +++ b/BoneCentral/WebApplication/messenger.js @@ -0,0 +1,40 @@ +var hdwr = require('./hardware.js'); + +module.exports = { + goThroughGate: hdwr.goThroughGate, + terminate: hdwr.terminate, + + exit: hdwr.exit, + pullWebLog: hdwr.pullWebLog, + + move: hdwr.move, + dive: hdwr.dive, + secondaryDive: hdwr.secondaryDive, + yaw: hdwr.yaw, + pitch: hdwr.pitch, + roll: hdwr.roll, + goDirection: hdwr.goDirection, + rotate: hdwr.rotate, + killThrust: hdwr.killThrust, + + turnOnImu: hdwr.turnOnImu, + turnOffImu: hdwr.turnOffImu, + accelerometer: hdwr.getAcceleration, + gyroscope: hdwr.getAngularAcceleration, + compass: hdwr.getHeading, + getTemperature1:hdwr.getInternalTemperature, + getTemperature2:hdwr.getExternalTemperature, + getPressure1: hdwr.getInternalPressure, + getPressure2: hdwr.getExternalPressure, + + turnOnEscs: hdwr.turnOnEscs, + turnOffEscs: hdwr.turnOffEscs, + + toggleLights: hdwr.toggleLights, + + startPoleSearch:hdwr.startPoleSearch, + stopPoleSearch: hdwr.stopPoleSearch, + getPoleLocation:hdwr.getPoleLocation, + refreshHSV: hdwr.refreshHSV, + runScript: hdwr.runScript +}; \ No newline at end of file diff --git a/BoneCentral/WebApplication/routes.js b/BoneCentral/WebApplication/routes.js index 113c106..11d6110 100644 --- a/BoneCentral/WebApplication/routes.js +++ b/BoneCentral/WebApplication/routes.js @@ -1,13 +1,11 @@ module.exports = function(app, msngr) { app.get("/goThroughGate", function (req, res) { - goThroughGate.execute().done(function () { - thrustController.goDirection(0, 0, 0); - }); + msngr.goThroughGate(); res.send("Going through gate"); }); app.get("/terminate", function (req, res) { - goThroughGate._shouldQuit = true; + msngr.terminate(); res.send("Terminated"); }); @@ -21,192 +19,150 @@ module.exports = function(app, msngr) { }); app.get('/stdoutData', function(req, res) { - res.send(webLogger.pull()); + res.send(msngr.pullWebLog()); }); // From IThrustController app.post('/move', function (req, res) { - var params = req.body; - thrustController.move(params.throttle); + msngr.move(req.body.throttle) res.send(''); }); - // From IThrustController app.post('/secondaryDive', function (req, res) { - var params = req.body; - thrustController.secondaryDive(params.throttle); + msngr.secondaryDive(req.body.throttle); res.send(''); }); - // From IThrustController app.post('/dive', function (req, res) { - var params = req.body; - thrustController.dive(params.throttle); + msngr.dive(req.body.throttle); res.send(''); }); - // From IThrustController app.post('/yaw', function (req, res) { - var params = req.body; - thrustController.yaw(params.throttle); + msngr.yaw(req.body.throttle); res.send(''); }); - // From IThrustController app.post('/pitch', function (req, res) { - var params = req.body; - thrustController.pitch(params.throttle); + msngr.pitch(req.body.throttle); res.send(''); }); - // From IThrustController app.post('/roll', function (req, res) { - var params = req.body; - thrustController.roll(params.throttle); + msngr.roll(req.body.throttle); res.send(''); }); - // From IThrustController app.post('/goDirection', function(req, res) { var params = req.body; - thrustController.goDirection(params.move, params.secondaryDive, params.dive); + msngr.goDirection(params.move, params.secondaryDive, params.dive); res.send(''); }); - // From IThrustController app.post('/rotate', function(req, res) { var params = req.body; - thrustController.rotate(params.yaw, params.pitch, params.roll); + msngr.rotate(params.yaw, params.pitch, params.roll); res.send(''); }); - // From IThrustController app.get('/killThrust', function(req, res) { - thrustController.kill(); + msngr.killThrust(); res.send('killThrust'); }); // From Imu app.get('/turnOnImuSensor', function(req, res) { - powerManager.turnOnImu(); + msngr.turnOnImu(); res.send('turnOnImuSensor'); }); app.get('/turnOffImuSensor', function(req, res) { - powerManager.turnOffImu(); + msngr.turnOffImu(); res.send('turnOffImuSensor'); }); app.get('/getAcceleration', function(req, res) { - imuSensor.getAcceleration().done(function(accel) { - webLogger.info("Acceleration: " + JSON.stringify(accel)); - }); + msngr.accelerometer(); res.send('ran getAcceleration'); }); app.get('/getAngularAcceleration', function(req, res) { - imuSensor.getAngularAcceleration().done(function(accel) { - webLogger.info("Angular Acceleration: " + JSON.stringify(accel)); - }); + msngr.gyroscope(); res.send('ran getAngularAcceleration'); }); app.get('/getHeading', function(req, res) { - imuSensor.getHeading().done(function(heading) { - webLogger.info("Heading: " + JSON.stringify(heading)); - }); + msngr.compass(); res.send('ran getHeading'); }); app.get('/getInternalTemperature', function(req, res) { - imuSensor.getInternalTemperature().done(function(temperature) { - webLogger.info("Internal Temperature: " + JSON.stringify(temperature)); - }); + msngr.getTemperature1(); res.send('getInternalTemperature'); }); app.get('/getInternalPressure', function(req, res) { - imuSensor.getInternalPressure().done(function(pressure) { - webLogger.info("Internal Pressure: " + JSON.stringify(pressure)); - }); + msngr.getPressure1(); res.send('getInternalPressure'); }); app.get('/getExternalTemperature', function(req, res) { - imuSensor.getExternalTemperature().done(function(temperature) { - webLogger.info("External Temperature: " + JSON.stringify(temperature)); - }); + msngr.getTemperature2(); res.send('getExternalTemperature'); }); app.get('/getExternalPressure', function(req, res) { - imuSensor.getExternalPressure().done(function(pressure) { - webLogger.info("External Pressure: " + JSON.stringify(pressure)); - }); + msngr.getPressure2(); res.send('getExternalPressure'); }); + // From Process app.get('/exit', function(req, res) { res.send('exit'); - powerManager.exit(); - process.exit(); + msngr.exit(); }); // From IPowerController { app.get('/turnOnEscs', function(req, res) { - powerManager.turnOnEscs(); + msngr.turnOnEscs(); res.send('turnOnEscs'); }); app.get('/turnOffEscs', function(req, res) { - powerManager.turnOffEscs(); + msngr.turnOffEscs(); res.send('turnOffEscs'); }); // Headlight Control app.get('/headlight', function(req, res) { - headLights.toggleLights(); + msngr.toggleLights(); res.send('toggled Headlights'); }); app.get('/startSearchingForPoles', function (req, res) { - gateDetector.startSearching(); + msngr.startPoleSearch(); res.send('startSearchingForPoles'); }); app.get('/getPoleCoordinates', function (req, res) { - gateDetector.getPoleCoordinates().done(function (poleCoords) { - webLogger.info("Pole Coordinates: " + JSON.stringify(poleCoords)); - }); + msngr.getPoleLocation(); res.send('getPoleCoordinates'); }); app.get('/refreshHsv', function (req, res) { - gateDetector.refreshHsv(); + msngr.refreshHSV(); res.send('refreshHsv'); }); app.get('/stopSearchingForPoles', function (req, res) { - gateDetector.stopSearching(); + msngr.stopPoleSearch(); res.send('stopSearchingForPoles'); }); // Script Run app.post('/runScript', function(req, res) { - var scriptIndex = req.body.scriptId; - fileSystem.readdir("./TestScripts", function (err, fileNames) { - if(fileNames[scriptIndex]) { - var filePath = path.resolve("TestScripts", fileNames[scriptIndex]); - if(require.cache[filePath]) delete require.cache[filePath]; - var script = require(filePath); - script.execute(thrustController, imuSensor, webLogger, headLights, powerManager); - res.send('ran ' + fileNames[scriptIndex]); - } - else { - res.send('script not found'); - } - }) + res.send(msngr.runScript(req.body.scriptId)); }); } \ No newline at end of file From 4f7f30cd0fc816dd71cb992d0a5f762ec7da59de Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Fri, 17 Mar 2017 21:36:59 +0000 Subject: [PATCH 3/4] Bundled C++ Functions Into One Module --- BoneCentral/Brain/AI/test.js | 8 +++++ BoneCentral/Brain/CppInterface/Factory.js | 32 ++++++++----------- .../Brain/CppInterface/PeripheralsProcess.js | 4 +-- BoneCentral/Brain/CppInterface/index.js | 2 +- BoneCentral/Brain/app.js | 24 ++++++++++++++ BoneCentral/Brain/config.json | 3 ++ BoneCentral/Brain/config.txt | 5 +++ BoneCentral/WebApplication/hardware.js | 26 +++++++-------- 8 files changed, 69 insertions(+), 35 deletions(-) create mode 100644 BoneCentral/Brain/AI/test.js create mode 100644 BoneCentral/Brain/app.js create mode 100644 BoneCentral/Brain/config.json create mode 100644 BoneCentral/Brain/config.txt diff --git a/BoneCentral/Brain/AI/test.js b/BoneCentral/Brain/AI/test.js new file mode 100644 index 0000000..1188b8e --- /dev/null +++ b/BoneCentral/Brain/AI/test.js @@ -0,0 +1,8 @@ +var Poseidon = require('../app.js'); + +var power = Poseidon.HardwareFactory.createPowerManager(); + +setTimeout(function() { + power.exit(); + process.exit(); +}, 10000); \ No newline at end of file diff --git a/BoneCentral/Brain/CppInterface/Factory.js b/BoneCentral/Brain/CppInterface/Factory.js index 2727448..b1d52a8 100644 --- a/BoneCentral/Brain/CppInterface/Factory.js +++ b/BoneCentral/Brain/CppInterface/Factory.js @@ -2,41 +2,37 @@ * Created by Nathan Copier on 4/28/2016. */ -var HeadLights = require("./Headlights"); -var PowerManager = require("./PowerManager"); -var ThrustController = require("./ThrustController"); -var CppLogSource = require("./CppLogSource"); -var ImuSensor = require("./ImuSensor"); -var Sockets = require('../Sockets'); -var Ports = require('../Sockets/Ports.json'); +var HeadLights = require("./Headlights"); +var PowerManager = require("./PowerManager"); +var ThrustController = require("./ThrustController"); +var ImuSensor = require("./ImuSensor"); +var CppLogSource = require("./CppLogSource"); module.exports = (function() { - - var logSocket = Sockets.createSocket(Ports.LoggerPort); - var dispatcherSocket = Sockets.createSocket(Ports.DispatcherPort); - - function Factory() {} + function Factory(dispatcherSocket, logSocket) { + this.dispatcherSocket = dispatcherSocket; + this.logSocket = logSocket; + } Factory.prototype.createCppLogSource = function (loggerOutput) { - return new CppLogSource(logSocket.Output, loggerOutput) + return new CppLogSource(this.logSocket.Output, loggerOutput) }; Factory.prototype.createHeadlights = function () { - return new HeadLights(dispatcherSocket.Input); + return new HeadLights(this.dispatcherSocket.Input); }; Factory.prototype.createImuSensor = function () { - return new ImuSensor(dispatcherSocket.Output, dispatcherSocket.Input); + return new ImuSensor(this.dispatcherSocket.Output, this.dispatcherSocket.Input); }; Factory.prototype.createPowerManager = function () { - return new PowerManager(dispatcherSocket.Input); + return new PowerManager(this.dispatcherSocket.Input); }; Factory.prototype.createThrustController = function () { - return new ThrustController(dispatcherSocket.Input); + return new ThrustController(this.dispatcherSocket.Input); }; return Factory; - })(); \ No newline at end of file diff --git a/BoneCentral/Brain/CppInterface/PeripheralsProcess.js b/BoneCentral/Brain/CppInterface/PeripheralsProcess.js index 9ad682b..7fa3934 100644 --- a/BoneCentral/Brain/CppInterface/PeripheralsProcess.js +++ b/BoneCentral/Brain/CppInterface/PeripheralsProcess.js @@ -3,8 +3,8 @@ */ var Spawner = require("child_process"); -var Path = require("path"); -var Ports = require("../Sockets/Ports.json"); +var Path = require("path"); +var Ports = require("../Sockets/Ports.json"); var initialize = function () { var args = [ diff --git a/BoneCentral/Brain/CppInterface/index.js b/BoneCentral/Brain/CppInterface/index.js index 161886b..fce039c 100644 --- a/BoneCentral/Brain/CppInterface/index.js +++ b/BoneCentral/Brain/CppInterface/index.js @@ -9,5 +9,5 @@ module.exports = { CppLogSource: require("./CppLogSource"), ImuSensor: require("./ImuSensor"), Factory: require("./Factory"), - Peripherals: require("./PeripheralsProcess") + Process: require("./PeripheralsProcess") }; \ No newline at end of file diff --git a/BoneCentral/Brain/app.js b/BoneCentral/Brain/app.js new file mode 100644 index 0000000..5fa4474 --- /dev/null +++ b/BoneCentral/Brain/app.js @@ -0,0 +1,24 @@ +var Config = require('./config.json'); +var Ports = require('./Sockets/Ports.json'); +var Sockets = require('./Sockets'); +var Peripherals = require('./CppInterface/PeripheralsProcess.js'); +var HardwareFactory = require('./CppInterface/Factory.js'); +var Vision = require('./VisionInterface'); + +var server = Sockets.createSocket(Ports.DispatcherPort); +var logServer = Sockets.createSocket(Ports.LoggerPort); +var hardwareFactory = new HardwareFactory(server, logServer); +var visionFactory = new Vision.Factory(); + +if(Config.appStartMode === "auto") { + Peripherals.initialize(); +} +else { + console.log("Entering manual mode: run './Periphers --dispatcherPort="+ + Ports.DispatcherPort+" --loggerPort="+Ports.LoggerPort+"'"); +} + +module.exports = { + HardwareFactory: hardwareFactory, + VisionFactory: visionFactory +}; \ No newline at end of file diff --git a/BoneCentral/Brain/config.json b/BoneCentral/Brain/config.json new file mode 100644 index 0000000..d3e14ec --- /dev/null +++ b/BoneCentral/Brain/config.json @@ -0,0 +1,3 @@ +{ + "appStartMode":"manual" +} \ No newline at end of file diff --git a/BoneCentral/Brain/config.txt b/BoneCentral/Brain/config.txt new file mode 100644 index 0000000..1b327e6 --- /dev/null +++ b/BoneCentral/Brain/config.txt @@ -0,0 +1,5 @@ +appStartMode: + manual - The js app is started seperately from the c++ app. (The user must + manualy start the c++ app after starting the js app.) + auto - The js app starts concurrently with the c++ app as if they were + one. diff --git a/BoneCentral/WebApplication/hardware.js b/BoneCentral/WebApplication/hardware.js index f34457a..556dacb 100644 --- a/BoneCentral/WebApplication/hardware.js +++ b/BoneCentral/WebApplication/hardware.js @@ -1,27 +1,25 @@ var fileSystem = require('fs'); var path = require("path"); +var Poseidon = require('../Brain/app.js'); + var WebLogger = require('./WebLogger'); var FileLogger = require('./FileLogger'); -var CppInterface = require('../Brain/CppInterface'); -var VisionInterface = require("../Brain/VisionInterface"); -var GoThroughGate = require("../Brain/GoThroughGate"); +// var GoThroughGate = require("../Brain/GoThroughGate"); var ThrustManager = require("../Brain/ThrustManager"); var fileLogger = new FileLogger("./test.log"); var webLogger = new WebLogger(fileLogger); -var peripheralsFactory = new CppInterface.Factory(); -var visionFactoy = new VisionInterface.Factory(); -var thrustManager = new ThrustManager(peripheralsFactory); -var goThroughGate = new GoThroughGate(visionFactoy, thrustManager, webLogger); +var thrustManager = new ThrustManager(Poseidon.HardwareFactory); +// var goThroughGate = new GoThroughGate(Poseidon.VisionFactoy, thrustManager, webLogger); -var thrustController = peripheralsFactory.createThrustController(); -var powerManager = peripheralsFactory.createPowerManager(); -var imuSensor = peripheralsFactory.createImuSensor(); -var headLights = peripheralsFactory.createHeadlights(); -var gateDetector = visionFactoy.createGateDetector(webLogger); +var thrustController = Poseidon.HardwareFactory.createThrustController(); +var powerManager = Poseidon.HardwareFactory.createPowerManager(); +var imuSensor = Poseidon.HardwareFactory.createImuSensor(); +var headLights = Poseidon.HardwareFactory.createHeadlights(); +var gateDetector = Poseidon.VisionFactory.createGateDetector(webLogger); -peripheralsFactory.createCppLogSource(webLogger); -CppInterface.Peripherals.initialize(); +// Poseidon.HardwareFactory.createCppLogSource(webLogger); +// CppInterface.Peripherals.initialize(); function DMSG(x) { console.log(x); From a5e0cb04cc8ab7aca5a226b1503deba76411c105 Mon Sep 17 00:00:00 2001 From: TekuConcept Date: Fri, 17 Mar 2017 22:02:57 +0000 Subject: [PATCH 4/4] Deleted Experimental/Broken Code Snippets --- BoneCentral/WebApplication/hardware.js | 30 -------------------- BoneCentral/WebApplication/messenger.js | 8 ++---- BoneCentral/WebApplication/routes.js | 30 -------------------- BoneCentral/WebApplication/static/index.html | 6 ---- 4 files changed, 2 insertions(+), 72 deletions(-) diff --git a/BoneCentral/WebApplication/hardware.js b/BoneCentral/WebApplication/hardware.js index 556dacb..babcbb1 100644 --- a/BoneCentral/WebApplication/hardware.js +++ b/BoneCentral/WebApplication/hardware.js @@ -4,36 +4,20 @@ var Poseidon = require('../Brain/app.js'); var WebLogger = require('./WebLogger'); var FileLogger = require('./FileLogger'); -// var GoThroughGate = require("../Brain/GoThroughGate"); -var ThrustManager = require("../Brain/ThrustManager"); var fileLogger = new FileLogger("./test.log"); var webLogger = new WebLogger(fileLogger); -var thrustManager = new ThrustManager(Poseidon.HardwareFactory); -// var goThroughGate = new GoThroughGate(Poseidon.VisionFactoy, thrustManager, webLogger); var thrustController = Poseidon.HardwareFactory.createThrustController(); var powerManager = Poseidon.HardwareFactory.createPowerManager(); var imuSensor = Poseidon.HardwareFactory.createImuSensor(); var headLights = Poseidon.HardwareFactory.createHeadlights(); -var gateDetector = Poseidon.VisionFactory.createGateDetector(webLogger); - -// Poseidon.HardwareFactory.createCppLogSource(webLogger); -// CppInterface.Peripherals.initialize(); function DMSG(x) { console.log(x); } module.exports = { - goThroughGate: function() { - goThroughGate.execute().done(function () { - thrustController.goDirection(0, 0, 0); - }); - }, - terminate: function() { - goThroughGate._shouldQuit = true; - }, pullWebLog: function() { return webLogger.pull(); }, @@ -136,20 +120,6 @@ module.exports = { headLights.toggleLights(); }, - startPoleSearch: function() { - gateDetector.startSearching(); - }, - stopPoleSearch: function() { - gateDetector.stopSearching(); - }, - getPoleLocation: function() { - gateDetector.getPoleCoordinates().done(function (poleCoords) { - webLogger.info("Pole Coordinates: " + JSON.stringify(poleCoords)); - }); - }, - refreshHSV: function() { - gateDetector.refreshHsv(); - }, runScript: function(index) { fileSystem.readdir("./TestScripts", function (err, fileNames) { if(fileNames[index]) { diff --git a/BoneCentral/WebApplication/messenger.js b/BoneCentral/WebApplication/messenger.js index 6d070bd..f9618b7 100644 --- a/BoneCentral/WebApplication/messenger.js +++ b/BoneCentral/WebApplication/messenger.js @@ -1,8 +1,8 @@ var hdwr = require('./hardware.js'); module.exports = { - goThroughGate: hdwr.goThroughGate, - terminate: hdwr.terminate, + // goThroughGate: hdwr.goThroughGate, + // terminate: hdwr.terminate, exit: hdwr.exit, pullWebLog: hdwr.pullWebLog, @@ -32,9 +32,5 @@ module.exports = { toggleLights: hdwr.toggleLights, - startPoleSearch:hdwr.startPoleSearch, - stopPoleSearch: hdwr.stopPoleSearch, - getPoleLocation:hdwr.getPoleLocation, - refreshHSV: hdwr.refreshHSV, runScript: hdwr.runScript }; \ No newline at end of file diff --git a/BoneCentral/WebApplication/routes.js b/BoneCentral/WebApplication/routes.js index 11d6110..e751edb 100644 --- a/BoneCentral/WebApplication/routes.js +++ b/BoneCentral/WebApplication/routes.js @@ -1,14 +1,4 @@ module.exports = function(app, msngr) { - app.get("/goThroughGate", function (req, res) { - msngr.goThroughGate(); - res.send("Going through gate"); - }); - - app.get("/terminate", function (req, res) { - msngr.terminate(); - res.send("Terminated"); - }); - //From IThruster app.get('/initialize', function(req, res) { res.send('initialize'); @@ -141,26 +131,6 @@ module.exports = function(app, msngr) { res.send('toggled Headlights'); }); - app.get('/startSearchingForPoles', function (req, res) { - msngr.startPoleSearch(); - res.send('startSearchingForPoles'); - }); - - app.get('/getPoleCoordinates', function (req, res) { - msngr.getPoleLocation(); - res.send('getPoleCoordinates'); - }); - - app.get('/refreshHsv', function (req, res) { - msngr.refreshHSV(); - res.send('refreshHsv'); - }); - - app.get('/stopSearchingForPoles', function (req, res) { - msngr.stopPoleSearch(); - res.send('stopSearchingForPoles'); - }); - // Script Run app.post('/runScript', function(req, res) { res.send(msngr.runScript(req.body.scriptId)); diff --git a/BoneCentral/WebApplication/static/index.html b/BoneCentral/WebApplication/static/index.html index 70a56bd..cd355dc 100644 --- a/BoneCentral/WebApplication/static/index.html +++ b/BoneCentral/WebApplication/static/index.html @@ -116,8 +116,6 @@
- - @@ -145,10 +143,6 @@ - - - -