-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdrive.js
110 lines (85 loc) · 1.78 KB
/
drive.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
100
101
102
103
104
105
106
107
108
109
110
var five = require('johnny-five'),
dualshock = require('dualshock-controller');
var board, lServo, rServo, ds;
ds = dualshock({
config: 'dualShock3'
});
ds.on('error', function (data) {
console.log('ruh roh something broke');
});
board = new five.Board({
port: '/dev/tty.usbserial-AH00156H'
});
board.on("ready", function () {
rServo = new five.Servo({
pin: 10,
type: 'continuous'
});
lServo = new five.Servo({
pin: 11,
type: 'continuous'
});
lServo.stop();
rServo.stop();
var moveSpeed = 0.1;
function stop() {
lServo.stop();
rServo.stop();
}
function turn (rightOn, leftOn, timeout) {
if (rightOn) {
rServo.cw(moveSpeed);
} else {
rServo.ccw(moveSpeed);
}
if (leftOn) {
lServo.ccw(moveSpeed);
} else {
lServo.cw(moveSpeed);
}
if (timeout) {
setTimeout(stop, timeout);
}
}
function turnLeft (timeout) {
console.log('turning left!');
turn (false, true, timeout);
}
function turnRight (timeout) {
console.log('turning right!');
turn (true, false, timeout);
}
function goStraight (timeout) {
console.log('going straight!');
turn (true, true, timeout);
}
function goBack (timeout) {
console.log('back it up!');
turn (false, false, timeout);
}
ds.on('square:press', function () {
turnLeft();
});
ds.on('square:release', function () {
stop();
});
ds.on('circle:press', function () {
turnRight();
});
ds.on('circle:release', function () {
stop();
});
ds.on('triangle:press', function () {
goStraight();
});
ds.on('triangle:release', function () {
stop();
});
ds.on('x:press', function () {
goBack();
});
ds.on('x:release', function () {
stop();
});
});
ds.connect();