-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathping.js
More file actions
71 lines (57 loc) · 1.88 KB
/
Copy pathping.js
File metadata and controls
71 lines (57 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var events = require('events'),
util = require('util');
/*
* Main Ping constructor
* Process options
* Tell the board to set it up
*/
var Ping = function (options) {
if (!options || !options.board) throw new Error('Must supply required options to Ping');
this.board = options.board;
this.pin = this.board.normalizePin(options.pin || 9);
// Data response is in microseconds
this.microseconds = 0;
this.inches = 0;
this.centimeters = 0;
var types = {
read: true
};
// Loop and trigger fire-read sequences
setInterval(function () {
this.fire();
}.bind(this), 50);
this.board.on('data', function (message) {
var m = message.slice(0, -1).split('::'),
err = null,
pin, type, data;
if (!m.length) {
return;
}
pin = m[0];
type = m[1];
data = m.length === 3 ? m[2] : null;
if (pin === this.pin && types[type]) {
// See: http://arduino.cc/en/Tutorial/Ping
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
this.inches = data / 74 / 2;
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
this.centimeters = data / 29 / 2;
this.emit(type, err, data);
}
}.bind(this));
};
util.inherits(Ping, events.EventEmitter);
Ping.prototype.command = function () {
var msg = '97' + this.pin + ([].slice.call(arguments).join(''));
this.board.write(msg);
};
Ping.prototype.fire = function () {
this.command('01');
};
module.exports = Ping;