-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.html
98 lines (95 loc) · 2.74 KB
/
controller.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
<!DOCTYPE html>
<html>
<head>
<style>
div.direction{
border: 1px solid blueviolet;
width: 300px;
height: 200px;
}
</style>
</head>
<body>
<div>
<textarea id="tts"></textarea>
<button id="btnPlay">Play</button>
</div>
<div id="dir" tabIndex="0" class="direction"/>
<script>
const ep = 'http://10.0.0.100:8080';
//const ep = 'http://127.0.0.1:8080';
const motorEp = ep + '/motor';
const leftM = motorEp + '/ev3-ports:outD';
const rightM = motorEp + '/ev3-ports:outA';
const driverName = 'lego-ev3-l-motor';
const initSpeed = -50;
let pressedKey = '';
const run = async (leftSpeed, rightSpeed)=> {
await fetch(leftM + '/write/duty_cycle_sp/' + leftSpeed, {
method: 'POST'
});
await fetch(rightM + '/write/duty_cycle_sp/' + rightSpeed, {
method: 'POST'
});
fetch(leftM + '/write/command/run-direct', {
method: 'POST'
});
fetch(rightM + '/write/command/run-direct', {
method: 'POST'
});
};
const stop = async () =>{
fetch(leftM + '/write/command/stop', {
method: 'POST'
});
fetch(rightM + '/write/command/stop', {
method: 'POST'
});
};
const processKey = ()=>{
switch(pressedKey){
case 'ArrowUp':
run(initSpeed, initSpeed);
break;
case 'ArrowDown':
run(-initSpeed, -initSpeed);
break;
case 'ArrowLeft':
run(Math.floor(-initSpeed/2), Math.floor(initSpeed/2));
break;
case 'ArrowRight':
run(Math.floor(initSpeed/2), Math.floor(-initSpeed/2));
break;
}
};
const onKeyDown=(key)=>{
if(pressedKey)
return;
console.log('key down:', key);
pressedKey = key.code;
processKey();
};
const onkeyUp=async ()=>{
console.log('key up');
if(pressedKey)
await stop();
pressedKey = '';
};
const init = async()=> {
console.log('init');
document.querySelector('#btnPlay').onclick = async ()=>{
await fetch(ep+'/sound/text/'+document.querySelector('#tts').value);
};
document.querySelector("#dir").onkeydown = onKeyDown;
document.querySelector("#dir").onkeyup = onkeyUp;
await fetch(leftM + '/' + driverName, {
method: 'CREATE',
});
await fetch(rightM + '/' + driverName, {
method: 'CREATE',
});
}
init();
</script>
</body>
</html>