forked from rwaldron/johnny-five
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonar.js
175 lines (136 loc) · 3.74 KB
/
sonar.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var Board = require("./board"),
events = require("events"),
util = require("util"),
within = require("./mixins/within");
var priv = new Map();
var Devices;
/**
* Sonar
* @constructor
*
* @param {Object} opts Options: pin (analog)
*/
function Sonar(opts) {
if (!(this instanceof Sonar)) {
return new Sonar(opts);
}
Board.Component.call(
this, opts = Board.Options(opts)
);
var device, state;
// Sonar instance properties
this.freq = opts.freq || 100;
this.value = null;
state = {
last: 0,
median: 0,
samples: []
};
priv.set(this, state);
if (typeof opts.device === "string") {
device = Devices[opts.device];
} else {
device = opts.device;
}
if (typeof device === "undefined") {
device = Devices.DEFAULT;
}
device.initialize.call(this, opts);
if (!device.descriptor.inches) {
device.descriptor.inches = {
get: function() {
return +(this.cm * 0.39).toFixed(2);
}
};
}
device.descriptor.in = device.descriptor.inches;
Object.defineProperties(this, device.descriptor);
// Throttle
setInterval(function() {
// Nothing read since previous interval
if (state.samples.length === 0) {
return;
}
state.median = state.samples.sort()[Math.floor(state.samples.length / 2)];
this.value = state.median;
this.emit("data", state.median);
// If the state.median value for this interval is not the same as the
// state.median value in the last interval, fire a "change" event.
//
if (state.last && state.median &&
(state.median.toFixed(1) !== state.last.toFixed(1))) {
this.emit("change", state.median);
}
// Store this media value for comparison
// in next interval
state.last = state.median;
// Reset state.samples;
state.samples.length = 0;
}.bind(this), this.freq);
}
util.inherits(Sonar, events.EventEmitter);
Object.assign(Sonar.prototype, within);
Devices = {
SRF10: {
initialize: function(opts) {
var samples = priv.get(this).samples;
var address = 0x70;
var delay = 65;
// Set up I2C data connection
this.io.i2cConfig(opts);
// Startup parameter
this.io.i2cWrite(address, [0x01, 16]);
this.io.i2cWrite(address, [0x02, 255]);
this.io.setMaxListeners(100);
function read() {
this.io.i2cWrite(address, [0x02]);
this.io.i2cReadOnce(address, 2, function(data) {
samples.push((data[0] << 8) | data[1]);
}.bind(this));
prime.call(this);
}
function prime() {
// 0x52 result in us (microseconds)
this.io.i2cWrite(address, [0x00, 0x52]);
setTimeout(read.bind(this), delay);
}
prime.call(this);
},
descriptor: {
cm: {
get: function() {
var median = priv.get(this).median;
return +((((median / 2) * 343.2) / 10) / 1000).toFixed(1);
}
}
}
},
DEFAULT: {
initialize: function() {
var samples = priv.get(this).samples;
// Set the pin to ANALOG mode
this.mode = this.io.MODES.ANALOG;
this.io.pinMode(this.pin, this.mode);
this.io.analogRead(this.pin, function(data) {
samples.push(data);
}.bind(this));
},
descriptor: {
cm: {
get: function() {
var median = priv.get(this).median;
return +((median / 2) * 2.54).toFixed(1);
}
}
}
}
};
Devices.SRF02 = Devices.SRF08 = Devices.SRF10;
module.exports = Sonar;
// Reference
//
// http://www.maxbotix.com/tutorials.htm#Code_example_for_the_BasicX_BX24p
// http://www.electrojoystick.com/tutorial/?page_id=285
// Tutorials
//
// http://www.sensorpedia.com/blog/how-to-interface-an-ultrasonic-rangefinder-with-sensorpedia-via-twitter-guide-2/