|
| 1 | +#! /usr/local/bin/node |
| 2 | +var application_root = __dirname |
| 3 | + , parser = require("./parser") |
| 4 | + , Hapi = require("hapi") |
| 5 | + , path = require("path") |
| 6 | + , five = require("johnny-five") |
| 7 | + , calibration = require("./calibration") |
| 8 | + , Robot = require("./robot").Robot |
| 9 | + , Dancer = require("./dancer").Dancer; |
| 10 | + |
| 11 | +args = parser.parseArgs(); |
| 12 | +calibration.loadDataFromFilePath(args.calibration); |
| 13 | + |
| 14 | +var board = new five.Board({ debug: false}); |
| 15 | +board.on("ready", function() { |
| 16 | + var servo1 = five.Servo({ |
| 17 | + pin: 9, |
| 18 | + range: [0,90] |
| 19 | + }); |
| 20 | + |
| 21 | + var servo2 = five.Servo({ |
| 22 | + pin: 10, |
| 23 | + range: [0,90] |
| 24 | + }); |
| 25 | + |
| 26 | + var servo3 = five.Servo({ |
| 27 | + pin: 11, |
| 28 | + range: [0,90] |
| 29 | + }); |
| 30 | + |
| 31 | + servo1.on("error", function() { |
| 32 | + console.log(arguments); |
| 33 | + }); |
| 34 | + servo2.on("error", function() { |
| 35 | + console.log(arguments); |
| 36 | + }); |
| 37 | + servo3.on("error", function() { |
| 38 | + console.log(arguments); |
| 39 | + }); |
| 40 | + |
| 41 | + // Initialize Objects |
| 42 | + var robot = new Robot(servo1,servo2,servo3,calibration.data); |
| 43 | + var dancer = new Dancer(robot); |
| 44 | + |
| 45 | + // Move to starting point |
| 46 | + robot.setPosition(calibration.data.restPoint.x, calibration.data.restPoint.y, calibration.data.restPoint.z); |
| 47 | + |
| 48 | + // create a server with a host and port |
| 49 | + var server = new Hapi.Server(); |
| 50 | + server.connection({ |
| 51 | + host: args.address, |
| 52 | + port: args.port |
| 53 | + }); |
| 54 | + |
| 55 | + server.route({ |
| 56 | + method: 'GET', |
| 57 | + path:'/status', |
| 58 | + handler: function (request, reply) { |
| 59 | + console.log("GET " + request.path + ": "); |
| 60 | + reply('\"OK\"'); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + server.route({ |
| 65 | + method: 'POST', |
| 66 | + path:'/reset', |
| 67 | + handler: function (request, reply) { |
| 68 | + console.log("GET " + request.path + ": "); |
| 69 | + robot.reset(); |
| 70 | + reply(robot.getAngles()); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + server.route({ |
| 75 | + method: 'POST', |
| 76 | + path:'/dance', |
| 77 | + handler: function (request, reply) { |
| 78 | + console.log("GET " + request.path + ": "); |
| 79 | + dancer.startDancing(); |
| 80 | + reply('\"Dancing!\"'); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + server.route({ |
| 85 | + method: 'POST', |
| 86 | + path:'/stopDancing', |
| 87 | + handler: function (request, reply) { |
| 88 | + console.log("GET " + request.path + ": "); |
| 89 | + dancer.stopDancing(); |
| 90 | + reply('\"No more dancing.\"'); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + server.route({ |
| 95 | + method: 'POST', |
| 96 | + path:'/setAngles', |
| 97 | + handler: function (request, reply) { |
| 98 | + console.log("POST " + request.path + ": "); |
| 99 | + var theta1 = parseFloat(request.payload.theta1); |
| 100 | + var theta2 = parseFloat(request.payload.theta2); |
| 101 | + var theta3 = parseFloat(request.payload.theta3); |
| 102 | + robot.setAngles(theta1, theta2, theta3); |
| 103 | + return reply("\"OK\""); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + server.route({ |
| 108 | + method: 'POST', |
| 109 | + path:'/setPosition', |
| 110 | + handler: function (request, reply) { |
| 111 | + console.log("POST " + request.path + ": "); |
| 112 | + var x = parseFloat(request.payload.x); |
| 113 | + var y = parseFloat(request.payload.y); |
| 114 | + var z = parseFloat(request.payload.z); |
| 115 | + robot.setPosition(x, y, z); |
| 116 | + return reply("\"OK\""); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + server.route({ |
| 121 | + method: 'GET', |
| 122 | + path:'/angles', |
| 123 | + handler: function (request, reply) { |
| 124 | + console.log("GET " + request.path + ": "); |
| 125 | + return reply(robot.getAngles()); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + server.route({ |
| 130 | + method: 'GET', |
| 131 | + path:'/position', |
| 132 | + handler: function (request, reply) { |
| 133 | + console.log("GET " + request.path + ": "); |
| 134 | + return reply(robot.getPosition()); |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + server.route({ |
| 139 | + method: 'GET', |
| 140 | + path:'/anglesForPosition/x/{x}/y/{y}/z/{z}', |
| 141 | + handler: function (request, reply) { |
| 142 | + console.log("GET " + req.path + ": "); |
| 143 | + var x = parseFloat(request.params.x); |
| 144 | + var y = parseFloat(request.params.y); |
| 145 | + var z = parseFloat(request.params.z); |
| 146 | + return reply(robot.getAnglesForPosition(x,y,z)); |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + server.start(); |
| 151 | + console.log("Robot listening on port " + args.port); |
| 152 | + |
| 153 | +}); |
0 commit comments