Skip to content

Instantly share code, notes, and snippets.

@ajfisher ajfisher/__Notes.md
Created Jul 7, 2016

Embed
What would you like to do?
PoC for ProximityArray

To try it out make sure you use the latest backpack from ajfisher/nodebots-hcsr04 using i2c_multiping branch

// You can test it with this.
var five = require("johnny-five");
var board = new five.Board({ port: process.argv[2] });
board.on("ready", function() {
var prox = new five.ProximityArray({
freq: 3000,
noSensors: 4,
controller: "HCSR04I2CBACKPACK",
});
/**var prox = new five.Proximity({
freq: 3000,
controller: "HCSR04I2CBACKPACK",
});**/
prox.on("data", function() {
console.log("Proximity: ");
console.log(" cm : ", this.cm);
console.log("-----------------");
});
prox.sensors[1].on("change", function() {
console.log("Distance changed on sensor 1");
});
});
/// Add the following controller to proximity.js:
HCSR04I2CBACKPACKARRAY: {
initialize: {
value: function(opts, datahandler) {
// set this so we can call it later from the proximity array
this.datahandler = datahandler;
}
},
toCm: {
value: function(raw) {
return toFixed(raw / 29.1 / 2, 3);
}
},
data: {
set: function(data) {
this.datahandler(data);
},
},
},
// Add this to lib and make a reference to it in johnny-five.js as ProximityArray
var Board = require("./board"),
events = require("events"),
util = require("util"),
__ = require("./fn"),
Proximity = require("./proximity");
var priv = new Map();
// Private methods
function initialize(opts) {
var state = priv.get(this);
if (!opts.noSensors || opts.noSensors === 0) {
throw new Error("noSensors must be defined");
}
state.proximityStates = [];
// create the number of proximity sensors needed
for (var i=0; i < opts.noSensors; i++) {
var proximity = {
sensor: new Proximity({
controller: 'HCSR04I2CBACKPACKARRAY',
id: i,
}),
};
state.proximityStates.push(proximity);
}
// now set up the array to grab data.
var BYTES_TO_READ = 0x0C;
// set up IO connection:
this.io.i2cConfig(opts);
var read = function() {
this.io.i2cReadOnce(opts.address, BYTES_TO_READ, function(data) {
for (var i = 0; i < opts.noSensors; i++) {
// each pair of bytes is one sensor reading
// so grab them out and push them to the sensor.
state.proximityStates[i].sensor.data = (data[i*2] << 8) + data[i*2+1];
}
setTimeout(read, opts.freq);
}.bind(this));
}.bind(this);
read();
}
// Constructor
function ProximityArray(opts) {
if (!(this instanceof ProximityArray)) {
return new ProximityArray(opts);
}
this.opts = Board.Options(opts);
Board.Component.call(
this, this.opts, {
requestPin: false
}
);
// Read event throttling
var freq = opts.freq || 25;
var noSensors = opts.noSensors || 0;
var address = opts.address || 0x27;
// Make private data entry
var state = {
};
this.opts.freq = freq;
this.opts.noSensors = noSensors;
this.opts.address = address;
priv.set(this, state);
initialize.call(this, this.opts);
Object.defineProperties(this, {
sensors: {
get: function() {
return state.proximityStates.map(function(proximity) {
return proximity.sensor;
});
}
},
cm: {
get: function() {
return state.proximityStates.map(function(proximity) {
return proximity.sensor.cm;
});
}
},
in: {
get: function() {
return state.proximityStates.map(function(proximity) {
return proximity.sensor.in;
});
}
},
});
setInterval(function() {
var data = {
cm: this.cm,
centimeters: this.cm,
in: this.in,
inches: this.in
};
this.emit("data", data);
}.bind(this), freq);
}
util.inherits(ProximityArray, events.EventEmitter);
// Public methods
module.exports = ProximityArray;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.