-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsearching.js
61 lines (54 loc) · 1.39 KB
/
searching.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
const firmata = require('firmata');
const j5 = require('johnny-five');
const SERVO_PIN = 3;
const POS_MAX = 180;
const POS_MIN = 0;
let servoPos = 90;
let stepInterval = 5; // stepsize (deg) of servo
// start the johnny-five connection
const board = new firmata.Board('/dev/ttyACM0', err => {
err && console.error(err);
});
let moving = false;
function moveServo(pos, pause = 30) {
if (moving) return;
moving = true;
board.servoWrite(SERVO_PIN, pos);
setTimeout(() => moving = false, pause);
}
function stepServo() {
servoPos += stepInterval;
moveServo(servoPos);
}
board.on('ready', function(err) {
console.log('BOARD READY');
board.servoConfig(SERVO_PIN, POS_MIN, POS_MAX);
moveServo(servoPos);
});
var pingBoard = new j5.Board({port: '/dev/ttyUSB0', repl: false});
pingBoard.on("ready", function() {
const sonar = new j5.Proximity({
controller: 'HCSR04',
pin: 'A0'
});
sonar.on('data', function() {
if (this.cm > 20 && !moving) {
console.log(`can not see anything at ${servoPos} moving along`);
if (stepInterval > 0) {
if (servoPos < POS_MAX) {
stepServo();
} else {
stepInterval = -1 * stepInterval;
stepServo();
}
} else {
if (servoPos > POS_MIN) {
stepServo();
} else {
stepInterval = -1 * stepInterval;
stepServo();
}
}
}
});
});