From 5ee80200a984bcb64448520dc9c9c0b0026dcc43 Mon Sep 17 00:00:00 2001 From: PSVM-J Date: Sat, 6 Oct 2018 21:03:07 +0300 Subject: [PATCH 1/3] add module octoliner --- modules/@amperka/octoliner.js | 81 +++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 modules/@amperka/octoliner.js diff --git a/modules/@amperka/octoliner.js b/modules/@amperka/octoliner.js new file mode 100644 index 0000000..0aa4574 --- /dev/null +++ b/modules/@amperka/octoliner.js @@ -0,0 +1,81 @@ +var Octoliner = function(opts) { + opts = opts || {}; + this.expander = require("@amperka/gpio-expander").connect(opts); + this.expander.pwmFreq(60000); + this._sensePin = 0; + this._ledBrightnessPin = 9; + this._sensorPinMap = [4, 5, 6, 8, 7, 3, 2, 1]; + this._lineState = []; + this.setSensitivity(0.8); + this.setBrightness(1); +}; + +Octoliner.prototype.setSensitivity = function(sense) { + this.expander.analogWrite(this._sensePin, sense); +}; + +Octoliner.prototype.setBrightness = function(brightness) { + this.expander.analogWrite(this._ledBrightnessPin, brightness); +}; + +Octoliner.prototype.analogRead = function(sensor) { + sensor &= 0x07; + return this.expander.analogRead(this._sensorPinMap[sensor]); +}; + +Octoliner.prototype.digitalRead = function(sensor){ + sensor &= 0x07; + return this.expander.digitalRead(this._sensorPinMap[sensor]); +}; + +Octoliner.prototype.changeAddr = function(nAddr) { + this.expander.changeAddr(nAddr); +}; + +Octoliner.prototype.saveAddr = function(){ + this.expander.saveAddr(); +}; + +Octoliner.prototype.getBinaryLine = function(treshold) { + treshold = treshold || 0.5; + var result = 0; + for (var i = 0; i < 8; ++i) { + var value = this.analogRead(i); + if (treshold < value) + result |= 1 << i; + } + return result; +}; + +Octoliner.prototype.mapLine = function(binaryLine) { + var sum = 0; + var avg = 0; + var weight = [4, 3, 2, 1, -1, -2, -3, -4]; + if (Array.isArray(binaryLine)) { + console.log(binaryLine); + for (var i = 0; i < 8; i++) { + if (binaryLine[i]) { + sum += binaryLine[i]; + avg += binaryLine[i] * weight[i]; + console.log(sum); + } + } + if (sum != 0) { + return avg / sum / 4.0; + } + return 0; + } else { + this._lineState = []; + for (var i = 0; i < 8; i++) { + var mask = 1 << i; + if (binaryLine & mask) { + this._lineState[i] = 1; + } + } + return this.mapLine(this._lineState); + } +}; + +exports.connect = function(opts) { + return new Octoliner(opts); +}; From 176aa7d88ce121c6dedfe9fbef009911b1a70b61 Mon Sep 17 00:00:00 2001 From: PSVM-J Date: Wed, 10 Oct 2018 15:35:03 +0300 Subject: [PATCH 2/3] fix console.log, add reset func --- modules/@amperka/octoliner.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/@amperka/octoliner.js b/modules/@amperka/octoliner.js index 0aa4574..2392581 100644 --- a/modules/@amperka/octoliner.js +++ b/modules/@amperka/octoliner.js @@ -52,12 +52,10 @@ Octoliner.prototype.mapLine = function(binaryLine) { var avg = 0; var weight = [4, 3, 2, 1, -1, -2, -3, -4]; if (Array.isArray(binaryLine)) { - console.log(binaryLine); for (var i = 0; i < 8; i++) { if (binaryLine[i]) { sum += binaryLine[i]; avg += binaryLine[i] * weight[i]; - console.log(sum); } } if (sum != 0) { @@ -76,6 +74,10 @@ Octoliner.prototype.mapLine = function(binaryLine) { } }; +Octoliner.prototype.reset = function(){ + this.expander.reset(); +}; + exports.connect = function(opts) { return new Octoliner(opts); }; From c24475653cff0041c493c619b069b4482b05e0bb Mon Sep 17 00:00:00 2001 From: PSVM-J Date: Mon, 12 Nov 2018 14:21:17 +0300 Subject: [PATCH 3/3] fix BinaryLine and other --- modules/@amperka/octoliner.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/modules/@amperka/octoliner.js b/modules/@amperka/octoliner.js index 2392581..0bbe681 100644 --- a/modules/@amperka/octoliner.js +++ b/modules/@amperka/octoliner.js @@ -1,12 +1,20 @@ var Octoliner = function(opts) { opts = opts || {}; + if (opts.i2c === undefined) { + I2C1.setup({ + sda: SDA, + scl: SCL, + bitrate: 100000 + }); + opts.i2c = I2C1; + } this.expander = require("@amperka/gpio-expander").connect(opts); this.expander.pwmFreq(60000); this._sensePin = 0; this._ledBrightnessPin = 9; this._sensorPinMap = [4, 5, 6, 8, 7, 3, 2, 1]; this._lineState = []; - this.setSensitivity(0.8); + this.setSensitivity(0.91); this.setBrightness(1); }; @@ -23,7 +31,7 @@ Octoliner.prototype.analogRead = function(sensor) { return this.expander.analogRead(this._sensorPinMap[sensor]); }; -Octoliner.prototype.digitalRead = function(sensor){ +Octoliner.prototype.digitalRead = function(sensor) { sensor &= 0x07; return this.expander.digitalRead(this._sensorPinMap[sensor]); }; @@ -32,7 +40,7 @@ Octoliner.prototype.changeAddr = function(nAddr) { this.expander.changeAddr(nAddr); }; -Octoliner.prototype.saveAddr = function(){ +Octoliner.prototype.saveAddr = function() { this.expander.saveAddr(); }; @@ -47,34 +55,34 @@ Octoliner.prototype.getBinaryLine = function(treshold) { return result; }; -Octoliner.prototype.mapLine = function(binaryLine) { +Octoliner.prototype.mapLine = function(Line) { var sum = 0; var avg = 0; var weight = [4, 3, 2, 1, -1, -2, -3, -4]; - if (Array.isArray(binaryLine)) { + if (Array.isArray(Line)) { for (var i = 0; i < 8; i++) { - if (binaryLine[i]) { - sum += binaryLine[i]; - avg += binaryLine[i] * weight[i]; - } + sum += Line[i]; + avg += Line[i] * weight[i]; } if (sum != 0) { return avg / sum / 4.0; } - return 0; + return 0; } else { this._lineState = []; for (var i = 0; i < 8; i++) { var mask = 1 << i; - if (binaryLine & mask) { + if (Line & mask) { this._lineState[i] = 1; + } else { + this._lineState[i] = 0; } } return this.mapLine(this._lineState); } }; -Octoliner.prototype.reset = function(){ +Octoliner.prototype.reset = function() { this.expander.reset(); };