-
Notifications
You must be signed in to change notification settings - Fork 9
4. Download and customize sample code
Rachel Simone Weil edited this page Jan 10, 2017
·
3 revisions
- Now that the device is set up, let's get some code! Clone this repo to your local machine (
git clone https://github.com/hxlnt/feather-nodebot.gitat command line). - Then,
cd feather-nodebotand hit Enter. Then,npm installand hit Enter. This will install the Johnny-Five library as well some basic code to control your robot over WiFi. - Open index.js and replace "192.168.XX.XX" with the IP address from step 9. Replace the values for deviceID and deviceKey with the name and connection string you got in step 2 (registering your NodeBot with RoboWriter).
- To drive your bot, type
node index.jsat the command line. You should see an output similar to this one upon a successful connection: `` 1481283325844 Device(s) Firmata 1481283345878 Connected Firmata 1481283369317 Repl Initialized
`` Inside the Node.js command prompt, use the keyboard keys up, right, and left to drive the car. Press the space bar to stop the car. Press q to quit.
- Incorporate the following code in your project to see your bot's motion on the RoboWriter dashboard. Your custom code goes in the place indicated and should use the motion functions at the end of the code. (See code comments for more info.)
'use strict';
// CHANGE THESE TWO VARIABLES! //
var deviceID = 'myname'; // This is the deviceID you entered in iothub-explorer
var deviceKey = 'XXXXXXXXXXXXXXXXXXXXXX'; // This is the primary key returned by iothub-explorer
// Node modules - Don't modify
var moment = require('moment');
var EtherPortClient = require("etherport-client").EtherPortClient;
var Protocol = require('azure-iot-device-amqp').Amqp;
var Client = require('azure-iot-device').Client;
var Message = require('azure-iot-device').Message;
// Setup - Don't modify
var connectionString = 'HostName=huzzahbots.azure-devices.net;DeviceId=' + deviceID + ';SharedAccessKey=' + deviceKey + '';
var client = Client.fromConnectionString(connectionString, Protocol);
var currentaction = "offline";
// Do this code when the board is ready - Don't modify
var connectCallback = function (err) {
if (err) { console.error('Your device is not connected to the web dashboard. Could not connect: ' + err.message); }
else {
console.log('Client connected');
client.on('message', function (msg) {
currentaction = "home";
console.log('Id: ' + msg.messageId + ' Body: ' + msg.data);
client.complete(msg, printResultFor('completed'));
});
client.on('error', function (err) {
currentaction = "offline";
console.error(err.message);
});
client.on('disconnect', function () {
currentaction = "offline";
client.removeAllListeners();
client.open(connectCallback);
});
}
};
client.open(connectCallback);
function printResultFor(op) {
return function printResult(err, res) {
if (err) console.log(op + ' error: ' + err.toString());
if (res) console.log(op + ' status: ' + res.constructor.name);
};
}
function letsPlay(){
var scalar = 256; // Friction coefficient
var actioncounter = 0;
var newcommand = "home()";
var speed = 255;
function actionSender(){
var distance = 0;
Math.round(actioncounter);
if (currentaction == "fd" || currentaction == "bk") {
var a = (moment.now() - actioncounter) * 0.18 * speed / scalar;
newcommand = "" + currentaction +"(" + a + ")";
distance = a;
}
else if (currentaction == "rt" || currentaction == "lt") {
var a = (moment.now() - actioncounter) * 0.18 * speed / scalar;
newcommand = "" + currentaction +"(" + a + ")";
distance = 0;
}
else if (currentaction == "home") {
newcommand = "home()";
distance = 0;
}
else {
newcommand = "fd(0)";
distance = 0;
};
distance = distance.toString();
var data = JSON.stringify({ deviceId: deviceID, command: newcommand, distance: distance });
var message = new Message(data);
console.log('Sending message: ' + message.getData());
client.sendEvent(message, printResultFor('send'));
actioncounter = moment.now();
}
////////////////////////////////////////////////////////////////
// Write your Johnny-Five code here!
///////////////////////////////////////////////////////////////
// Use these functions for forward, stop, left, and right here for tracking on the RoboWriter.
function forward() {
currentaction = "fd";
console.log("Forward!");
}
function stop() {
currentaction = "stopped";
console.log("Stop!");
}
function left() {
currentaction = "lt";
console.log("Left!");
}
function right() {
currentaction = "rt";
console.log("Right!");
}
function exit() {
currentaction = "offline";
setTimeout(process.exit, 1000);
}