-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller_example.html
More file actions
163 lines (156 loc) · 4.96 KB
/
controller_example.html
File metadata and controls
163 lines (156 loc) · 4.96 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1.0">
/*CSSの内容*/
<title>M5StickC RoverC Control</title>
<style>
html, body {
position: fixed;
overflow: hidden;
width: 100%;
height: 100%;
}
body {
display: flex;
flex-direction: column;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background-color: white;
}
h1 {
text-align: center;
margin-top: 1rem;
margin-bottom: 15px;
}
#controller {
display: flex;
flex-direction: row;
justify-content: space-between; /* ここを変更 */
margin-top: 15px;
margin-bottom: 15px; /* ここを追加 */
width: 90%; /* ここを追加 */
margin-left: auto; /* ここを追加 */
margin-right: auto; /* ここを追加 */
}
.direction-buttons, .rotation-buttons {
display: flex;
flex-direction: column;
justify-content: center;
}
.direction-buttons {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 1rem;
}
.rotation-buttons {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 1rem;
margin-left: 1rem;
}
#forward, #back, #left, #right, #left_rotation, #right_rotation, #grip, #release {
padding: 0.5rem 1rem;
border: 1px solid #333;
background-color: #fff;
cursor: pointer;
user-select: none;
}
#speed {
display: block;
margin: 1rem auto;
width: 60%;
}
.speed-value {
text-align: center;
margin-bottom: 1rem;
margin-left: 10px;
}
#slider-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-top: 15px;
margin-bottom: 20px;
}
</style>
</head>
<body>
/*画面上でのレイアウト*/
<h1>RoverC Controler</h1>
<div id="controller">
<div class="direction-buttons">
<div></div>
<button id="forward">↑</button>
<div></div>
<button id="left">←</button>
<div></div>
<button id="right">→</button>
<div></div>
<button id="back">↓</button>
<div></div>
</div>
<div class="rotation-buttons">
<button id="grip">GRIP</button>
<button id="release">RELEASE</button>
<button id="left_rotation">LEFT ROTATIOn</button>
<button id="right_rotation">RIGHT ROTATION</button>
</div>
</div>
<div id="slider-container">
<input id="speed" type="range" min="50" max="100" value="75">
<div class="speed-value">Speed: <span id="speed-value">75</span></div>
</div>
<script>
let ws = new WebSocket("ws://" + location.hostname + ":80/ws");
ws.onopen = () => {
console.log("WebSocket connected");
};
ws.onclose = () => {
console.log("WebSocket disconnected");
};
ws.onmessage = (event) => {
console.log(`Received: ${event.data}`);
};
let currentCommand = ""; // 現在のコマンド状態を保持する変数
//ここでボタンを押したときの処理を書く。ボタンにはidが振られており、押したボタンと、そのときの速度設定をjsonに格納する
let buttons = document.querySelectorAll("button");
buttons.forEach((button) => {
button.addEventListener("touchstart", (event) => {
event.preventDefault();
currentCommand = button.id; // 現在のコマンドを更新
let msg = {
command: button.id,
speed: document.getElementById("speed").value
};
ws.send(JSON.stringify(msg));
});
//ここでボタンを離した時の処理を書く。ボタンにはidが振られており、押したボタンと、そのときの速度設定をjsonに格納する
button.addEventListener("touchend", (event) => {
event.preventDefault();
currentCommand = "stop"; // 現在のコマンドを更新
let msg = {
command: "stop",
speed: document.getElementById("speed").value
};
ws.send(JSON.stringify(msg));
});
});
let speed = document.getElementById("speed");
let speedDisplay = document.getElementById("speed-display");
//ここで移動スピードを設定するスライダーの値が切り替わった時に、値をjsonに格納する処理を書く。
speed.addEventListener("input", () => {
let msg = {
command: currentCommand,
speed: speed.value
};
ws.send(JSON.stringify(msg));
document.getElementById("speed-value").textContent = speed.value;
});
</script>
</body>
</html>