-
Notifications
You must be signed in to change notification settings - Fork 1
/
control.html
131 lines (128 loc) · 3.63 KB
/
control.html
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<meta name="HandheldFriendly" content="True" />
<meta name="MobileOptimized" content="320" />
<style>
* {
user-select:none;
-webkit-user-select:none;
}
span {
color:white;
font-size:5em;
font-weight: bold;
display:inline-block;
width:30%;
border:2px solid black;
background-color: green;
height:200px;
line-height:200px;
text-align:center;
}
span.stop {
background-color: red;
}
</style>
</head>
<body>
<div class='controls'>
<span class='left'>←</span> <span class='forward'>↑</span>
<span class='right'>→</span>
<br />
<!--<span class='stop'>STOP</span>-->
</div>
<pre id='log'>
</pre>
<script src="/socket.io/socket.io.js"></script>
<script src="/js/nxt.js"></script>
<script>
var logEl = document.getElementById('log');
function log(msg) {
if (logEl) {
logEl.textContent = msg + "\n" + logEl.textContent;
}
}
///////////////////////////////////////////////////////////////
function power(motor, power, angle) {
nxt.set_output_state(motor, power, nxt.MOTORON, nxt.REGULATION_MODE_MOTOR_SYNC, 0, nxt.MOTOR_RUN_STATE_RUNNING, angle);
}
var amount = 3600;
function left() {
power(nxt.MOTOR_B, 100, amount);
power(nxt.MOTOR_A, 99, amount);
}
function backward() {
console.log("Backwards");
//power(1, 100, amount);
//power(nxt.MOTOR_A, 50, 149);
power(nxt.MOTOR_A, 50, 50);
power(nxt.MOTOR_B, 99, amount);
power(nxt.MOTOR_A, 99, amount);
}
function forward() {
//power(1, 100, amount);
power(nxt.MOTOR_A, 50, 50);
power(nxt.MOTOR_B, 100, amount);
power(nxt.MOTOR_A, 100, amount);
}
function right() {
power(nxt.MOTOR_A, 100, amount);
power(nxt.MOTOR_B, 99, amount);
}
function stop() {
nxt.set_output_state(nxt.MOTOR_ALL, 0, nxt.MOTOROFF, nxt.REGULATION_MODE_IDLE, 0, nxt.MOTOR_RUN_STATE_RUNNING, 0);
//nxt.set_output_state(nxt.MOTOR_B, 0, nxt.MOTOROFF, nxt.REGULATION_MODE_IDLE, 0, nxt.MOTOR_RUN_STATE_RUNNING, 0);
}
function p(e) {
e.preventDefault();
return false;
}
document.body.onclick = p;
document.body.onmousemove = p;
document.body.ontouchmove = p;
document.body.ondblclick = p;
if (/mobile/i.test(navigator.userAgent)) {
document.getElementsByClassName('left')[0].ontouchstart = left;
document.getElementsByClassName('forward')[0].ontouchstart = forward;
document.getElementsByClassName('right')[0].ontouchstart = right;
//document.getElementsByClassName('stop')[0].ontouchstart = stop;
document.body.ontouchend = stop;
} else {
document.getElementsByClassName('left')[0].onmousedown = left;
document.getElementsByClassName('forward')[0].onmousedown = forward;
document.getElementsByClassName('right')[0].onmousedown = right;
//document.getElementsByClassName('stop')[0].onmousedown = stop;
document.body.onmouseup = stop;
}
var isDown = false;
window.onkeydown = function(e) {
if (isDown) {
return;
}
isDown = true;
console.log(e.keyCode);
if (e.keyCode == 37) {
left();
} else if (e.keyCode == 39) {
right();
} else if (e.keyCode == 38) {
forward();
} else if (e.keyCode == 40) {
backward();
}
}
window.onkeyup = function(e) {
isDown = false;
console.log("Up");
stop();
}
///////////////////////////////////////////////////////////////
log("Connecting");
socket.on('welcome', function (data) {
log('Connected');
});
</script>
</body>
</html>