-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add module octoliner #72
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут не binaryLine надо, а просто line, т.к. этот алгоритм работает и на дробных числах. |
||
var sum = 0; | ||
var avg = 0; | ||
var weight = [4, 3, 2, 1, -1, -2, -3, -4]; | ||
if (Array.isArray(binaryLine)) { | ||
for (var i = 0; i < 8; i++) { | ||
if (binaryLine[i]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и без проверки здесь |
||
sum += binaryLine[i]; | ||
avg += binaryLine[i] * weight[i]; | ||
} | ||
} | ||
if (sum != 0) { | ||
return avg / sum / 4.0; | ||
} | ||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. отступ |
||
} 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); | ||
} | ||
}; | ||
|
||
Octoliner.prototype.reset = function(){ | ||
this.expander.reset(); | ||
}; | ||
|
||
exports.connect = function(opts) { | ||
return new Octoliner(opts); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Хорошо бы задать дефолтный i2c или ошибку, если он не задан.