-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.js
99 lines (84 loc) · 2.82 KB
/
Bot.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
import { EventEmitter } from 'events';
export default class Bot extends EventEmitter {
constructor(ws) {
super();
this.ws = ws;
this.leftPower = 0;
this.rightPower = 0;
this.leftIR = false;
this.rightIR = false;
this.lidar = 0;
this.lidarStatus = 0;
this.powerLastSent = 0;
this.botID = null;
this.motorInterval = setInterval(() => {
const now = (new Date()).getTime();
if(now - this.powerLastSent > 200) {
this.sendPower();
}
}, 200);
ws.on('close', () => {
console.log('Bot disconnected!');
clearInterval(this.motorInterval);
this.emit('disconnect');
});
ws.on('message', (data, isBinary) => {
if(isBinary) {
if(data.length < 1) return;
const packetID = data[0];
if(packetID === 2 && data.length >= 3) {
// IR packet
//console.log('IR Data:');
//console.log(data);
this.leftIR = data[1] !== 0x00;
this.rightIR = data[2] !== 0x00;
} else if(packetID === 4 && data.length >= 3) {
// Lidar packet
this.lidar = data[1]
this.lidarStatus = data[2];
}
} else {
if(this.botID === null) {
this.botID = data.toString();
console.log(`🤖 Bot "${this.botID}" connected`);
this.emit('connect');
}
}
});
}
rescalePower(power) {
if(power > 100) {
power = 100;
} else if(power < -100) {
power = -100;
}
return Math.round((power / 100) * 128);
}
setPower(left, right) {
this.leftPower = this.rescalePower(left);
this.rightPower = this.rescalePower(right);
this.sendPower();
}
sendPower() {
this.powerLastSent = (new Date()).getTime();
const leftPositive = (this.leftPower > 0) ? 0x01 : 0x00;
const left = Math.abs(this.leftPower);
const rightPositive = (this.rightPower > 0) ? 0x01 : 0x00;
const right = Math.abs(this.rightPower);
const buffer = Buffer.alloc(7);
buffer.writeUInt8(0x01, 0);
buffer.writeUInt8(leftPositive, 1);
buffer.writeUInt16LE(left, 2);
buffer.writeUInt8(rightPositive, 4);
buffer.writeUInt16LE(right, 5);
this.ws.send(buffer);
}
sendServo(angle) {
if(angle < 0) angle = 0;
if(angle > 180) angle = 180;
const buffer = Buffer.alloc(2);
buffer.writeUInt8(0x03, 0);
buffer.writeUInt8(angle, 1);
this.ws.send(buffer);
}
}