-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
298 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#! /usr/local/bin/node | ||
|
||
var prompt = require("prompt") | ||
, fs = require("fs") | ||
, eol = require('os').EOL | ||
, ArgumentParser = require('argparse').ArgumentParser | ||
, robot = require('./lib/server/robot_http_client').client("127.0.0.1","4242"); | ||
|
||
var args = {}, | ||
newCalibrationData = {}; | ||
|
||
function CalibrationManager(argv) { | ||
args = argv; | ||
prompt.message = ''; | ||
prompt.delimiter = ''; | ||
prompt.start(); | ||
} | ||
exports.CalibrationManager = CalibrationManager; | ||
|
||
var getCommandLineArgs = function() { | ||
var parser = new ArgumentParser({ | ||
version: '0.0.1', | ||
addHelp:true, | ||
description: 'Tapster Calibration Script' | ||
}); | ||
|
||
parser.addArgument( | ||
[ '-o', '--output' ], { | ||
defaultValue: "calibration.json" | ||
, help: 'file to save calibration data to' | ||
} | ||
); | ||
|
||
return parser.parseArgs(); | ||
}; | ||
|
||
CalibrationManager.prototype.calibrate = function() { | ||
robot.calibrationData(function (calibrationData) { | ||
console.log("Receiving existing calibration data."); | ||
newCalibrationData = calibrationData; | ||
console.log(newCalibrationData); | ||
var schema = { | ||
description: 'Please remove the arms from the robot and press any key to continue...', | ||
type: 'string' | ||
}; | ||
prompt.get(schema, function () { | ||
calibrateServos(function () { | ||
console.log("New Calibration Data Generated."); | ||
console.log(newCalibrationData); | ||
robot.setCalibrationData(newCalibrationData, function () { | ||
console.log("Robot is now calibrated!"); | ||
fs.writeFile(args.output, JSON.stringify(newCalibrationData, null, 2), function(err) { | ||
if(err) { | ||
console.log('Calibration data could not be saved: ' + err); | ||
} else { | ||
console.log('Calibration data saved to "' + args.output +'"'); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
|
||
var calibrateServos = function(cb) { | ||
var calibrateServoMinAndMax = function(armIndex, cb) { | ||
return robot.reset(function () { | ||
return calibrateServo(armIndex, true, function () { | ||
return calibrateServo(armIndex, false, cb); | ||
}); | ||
}); | ||
}; | ||
return calibrateServoMinAndMax(0, function() { | ||
return calibrateServoMinAndMax(1, function() { | ||
return calibrateServoMinAndMax(2, function() { | ||
return robot.reset(cb); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
var calibrateServo = function(armIndex, isMin, cb) { | ||
robot.angles(function (angles) { | ||
var description = 'Enter an adjustment for arm #' + (armIndex +1) + ', enter 0 when the arm is ' + | ||
(isMin ? 'parallel to the roof.' : 'perpendicular to the roof'); | ||
var schema = { | ||
name: "delta", | ||
description: description, | ||
type: 'number' | ||
}; | ||
|
||
return prompt.get(schema, function (err, result) { | ||
if (result.delta < 0.05 && result.delta > -0.05) { | ||
newCalibrationData["servo" + (armIndex+1)][(isMin ? "min" : "max" ) + "imumAngle"] = angles[armIndex]; | ||
return cb(); | ||
} else { | ||
console.log("Old Angles: " + angles); | ||
angles[armIndex] = angles[armIndex] + result.delta; | ||
console.log("New Angles: " + angles); | ||
robot.setAngles(angles[0], angles[1], angles[2], function() { | ||
return calibrateServo(armIndex, isMin, cb); | ||
}); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
if(require.main === module) { | ||
new CalibrationManager(getCommandLineArgs()).calibrate(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var fs = require("fs"); | ||
|
||
// default calibration | ||
module.exports.defaultData = { | ||
restPoint : { | ||
x : 0, | ||
y : 0, | ||
z : -120 | ||
}, | ||
servo1 : { | ||
minimumAngle : 20, | ||
maximumAngle : 90 | ||
}, | ||
servo2 : { | ||
minimumAngle : 20, | ||
maximumAngle : 90 | ||
}, | ||
servo3 : { | ||
minimumAngle : 20, | ||
maximumAngle : 90 | ||
} | ||
}; | ||
|
||
module.exports.getDataFromFilePath = function(filePath) { | ||
if (fs.existsSync(filePath)) { | ||
return JSON.parse(fs.readFileSync(filePath, "utf8")); | ||
} else { | ||
return null; | ||
} | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
var http = require('http'); | ||
|
||
exports.client = function(address, port) { | ||
var get = function(path, cb) { | ||
return http.get({ host: address, port: port, path: path }, function(res){ | ||
res.setEncoding('utf8'); | ||
return res.on('data', function(chunk) { | ||
var result = JSON.parse(chunk); | ||
return cb(result.data); | ||
}); | ||
}).on("error", function(err){ | ||
console.log("Got error: " + err.message); | ||
return cb(null, err); | ||
}); | ||
}; | ||
var post = function(path, bodyData, cb) { | ||
var req = http.request({ host: address, port: port, path: path, method: 'POST'}, function(res) { | ||
res.setEncoding('utf8'); | ||
return res.on('data', function (chunk) { | ||
var result = JSON.parse(chunk); | ||
return cb(result.data); | ||
}); | ||
}).on('error', function(err) { | ||
console.log("Got error: " + err.message); | ||
return cb(null, err); | ||
}); | ||
req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); | ||
req.write(bodyData); | ||
req.write('\n'); | ||
req.end(); | ||
}; | ||
return { | ||
address : address, | ||
port : port, | ||
url : function(uri) { | ||
return 'http://' + address + ":" + port + uri; | ||
}, | ||
angles : function(cb) { | ||
return get('/angles', cb); | ||
}, | ||
setAngles : function(theta1, theta2, theta3, cb) { | ||
var postData = "theta1=" + theta1 + "&theta2=" +theta2 + "&theta3=" + theta3; | ||
return post('/setAngles', postData, cb); | ||
}, | ||
position : function(cb) { | ||
return get('/position', cb); | ||
}, | ||
setPosition : function(x, y, z, cb) { | ||
var postData = "x=" + x + "&y=" + y + "&z=" + z; | ||
return post('/setPosition', postData, cb); | ||
}, | ||
reset : function(cb) { | ||
return post('/reset', '', cb); | ||
}, | ||
calibrationData : function(cb) { | ||
return get('/calibrationData', cb); | ||
}, | ||
setCalibrationData : function(newData, cb) { | ||
var postData = "newData=" + JSON.stringify(newData); | ||
return post('/setCalibrationData', postData, cb); | ||
} | ||
/* | ||
positionForCoordinates : function(x,y) { | ||
var xmlHttp = new XMLHttpRequest(); | ||
xmlHttp.open( "GET", this.url('/positionForCoordinates/x/' + x + "/y/" + y), false ); | ||
xmlHttp.send( null ); | ||
return eval(xmlHttp.responseText); | ||
}, | ||
coordinatesForPosition : function(x,y) { | ||
var xmlHttp = new XMLHttpRequest(); | ||
xmlHttp.open( "GET", this.url('/coordinatesForPosition/x/' + x + "/y/" + y), false ); | ||
xmlHttp.send( null ); | ||
return eval(xmlHttp.responseText); | ||
}, | ||
*/ | ||
/* | ||
tap : function(x,y) { | ||
var xmlHttp = new XMLHttpRequest(); | ||
xmlHttp.open( "GET", this.url('/tap/x/' + x + "/y/" + y), false ); | ||
xmlHttp.send( null ); | ||
return xmlHttp.responseText; | ||
}*/ | ||
}; | ||
}; |
Oops, something went wrong.