-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotReal.js
49 lines (38 loc) · 948 Bytes
/
RobotReal.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
const SerialPort = require('serialport')
const SerialChannel = require('./SerialChannel')
module.exports = class RobotReal {
constructor (comPort) {
this.port = new SerialPort(comPort, {
baudRate: 115200,
})
// Ready promise
this.promiseReady = new Promise((resolve, reject) => {
this.port.on('open', () => resolve())
})
this.channel = new SerialChannel(this.port)
}
// Resolves promise once connected
ready() {
return this.promiseReady
}
async front(){
await this.channel.execute('f')
}
async left(){
await this.channel.execute('r')
}
async right(){
await this.channel.execute('l')
}
async beep(){
await this.channel.execute('b')
}
async stop(){
await this.channel.execute('s')
}
async read(){
let response = await this.channel.execute('u')
let distance = parseInt(response.split(':')[2]) || 100
return distance < 15 ? 1 : 0
}
}