-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (80 loc) · 2.35 KB
/
Copy pathindex.js
File metadata and controls
94 lines (80 loc) · 2.35 KB
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
import argv from 'minimist'
import keypress from 'keypress'
import RadioCar from './src/RadioCar.js'
let drive, timer
argv(process.argv.slice(2))
if (argv.h || argv.help) help()
else if (argv.v || argv.version) version()
else run()
function run () {
console.info('Connecting to device...')
drive = new RadioCar({
channel: argv.c || argv.channel,
freq: argv.f || argv.freq,
speed: argv.speed,
swaplr: argv.swaplr,
id: argv.d || argv.id,
gain: argv.gain,
sampleRate: argv['sample-rate']
})
drive.init().then(() => {
console.info('Found device!')
console.info('Use keys to drive - Run with --help for details')
keypress(process.stdin)
process.stdin.on('keypress', (ch, key) => {
if (!key) return
if (key && key.ctrl && key.name === 'c') return exit()
clearTimeout(timer)
switch (key.name) {
case 'w': drive.forward(); break
case 'q': drive.forwardLeft(); break
case 'e': drive.forwardRight(); break
case 's': drive.reverse(); break
case 'a': drive.reverseLeft(); break
case 'd': drive.reverseRight(); break
case 'z': drive.left(); break
case 'c': drive.right(); break
case 'u': return drive.turn180(stop) // 180 stops automatically
}
timer = setTimeout(stop, 150)
function stop () {
drive.stop()
}
})
process.stdin.setRawMode(true)
process.stdin.resume()
}).catch((error) => console.error(error))
}
function help () {
console.info(`
Usage:
monster-drift [options]
Options:
-h, --help Show this message
-v, --version Show version
-c, --channel=N Set transmit channel (default: 19)
--freq=Hz Set transmit frequency (overwrites --channel)
--speed=N Set default forward speed (1-3, default: 1)
--swaplr Invert left/right stearing
-d, --id HackRF device id (default: 0)
--gain HackRF TX gain (0-47 dB in 1 dB steps, default: 47)
--sample-rate Sample rate used when transmitting (default: 8e6)
Use the keyboard to drive.
Basic driving:
w: Forward
q: Forward/Right
e: Forward/Left
s: Reverse
a: Reverse/Right
d: Reverse/Left
z: Wheels Left
c: Wheels Right
Tricks:
u: Turn 180
Exit by pressing ctrl+c
P.S. A high key-repeat will improve the experience :)
`)
}
function exit () {
drive.close(() => process.exit())
}