-
Notifications
You must be signed in to change notification settings - Fork 0
/
example1.ts
70 lines (62 loc) · 2.02 KB
/
example1.ts
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
import { Color, discoverMoveHub, Orientation } from "../src/moveHub";
function timeout(ms: number) {
return new Promise<void>((resolve) => setTimeout(() => resolve(), ms));
}
function readLine() {
return new Promise<Buffer>((resolve) => process.stdin.once("data", (data) => resolve(data)));
}
async function main() {
console.log("Looking for MoveHub...");
const moveHub = await discoverMoveHub();
console.log("Connected. Press return to disconnect.");
let isTurning = false;
let isUp = true;
async function turn() {
if (!isTurning && isUp) {
isTurning = true;
await moveHub.led(Color.Red);
await moveHub.motorAB.stop();
await moveHub.motorD.angledAndWait(30, 10);
await moveHub.motorD.angledAndWait(60, -10);
await moveHub.motorD.angledAndWait(30, 10);
await moveHub.motorAB.angledAndWait(620, 20, -20);
await moveHub.led(Color.Off);
await moveHub.motorAB.constant(40);
isTurning = false;
}
}
await moveHub.colorAndDistanceC.subscribe("colorAndDistance", (cd) => {
if (cd.distance < 0.2) {
turn();
}
});
await moveHub.motorAB.setSpeedMode(true);
await moveHub.motorAB.subscribe("speed", (speed) => {
if (speed === 0) {
turn();
}
});
await moveHub.subscribeTilt("simple", async (value) => {
const old = isUp;
const cur = value === Orientation.Up;
isUp = cur;
if (old && !cur) {
await moveHub.motorAB.stop();
for (let i = 0; i < 10; i++) {
await moveHub.led(Color.Green);
await timeout(100);
await moveHub.led(Color.Off);
}
} else if (!old && cur) {
await moveHub.motorAB.constant(40);
}
});
try {
await readLine();
await moveHub.disconnect();
console.log("Disconnected.");
} finally {
process.stdin.end();
}
}
main();