Skip to content

Commit 4f4b934

Browse files
gamepad integration into l2f decent
1 parent b705f17 commit 4f4b934

File tree

6 files changed

+86
-29
lines changed

6 files changed

+86
-29
lines changed

gamepad.css

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#gamepad-controls {
2+
display: flex;
3+
}
4+
15
#gamepad-status {
26
padding: 8px;
37
margin-bottom: 15px;
@@ -42,8 +46,14 @@
4246
border-radius: 50%;
4347
background-color: #ccc;
4448
}
49+
#gamepad-button-container-container {
50+
display: flex;
51+
flex-direction: row;
52+
justify-content: center;
53+
align-items: center;
54+
gap: 10px;
55+
}
4556
#gamepad-button-container {
46-
margin-top: 20px;
4757
display: flex;
4858
flex-wrap: wrap;
4959
justify-content: center;
@@ -56,6 +66,10 @@
5666
background-color: #007bff;
5767
color: white;
5868
cursor: pointer;
69+
align-self: flex-start;
70+
flex-grow: 0;
71+
flex-shrink: 1;
72+
flex-basis: auto;
5973
}
6074
.gamepad-button:hover {
6175
background-color: #0069d9;

gamepad.html

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<template id="gamepad-axis-template">
1111
<div class="gamepad-controls-row">
1212
<div class="gamepad-controls-name gamepad-controls-cell"></div>
13-
<div class="gamepad-slider-container gamepad-controls-cell"><input type="range" class="gamepad-slider" min="-1" max="1" value="0" step="0.01" disabled></div>
13+
<div class="gamepad-slider-container gamepad-controls-cell"><input type="range" class="gamepad-slider" min="-1" max="1" value="0" step="0.01"></div>
1414
<div class="gamepad-value-display gamepad-controls-cell">0.00</div>
1515
</div>
1616
</template>
@@ -22,14 +22,16 @@
2222
<div></div>
2323
</div>
2424
</template>
25-
<template id="gamepad-template">
25+
<div id="gamepad-container">
2626
<div id="gamepad-status" class="gamepad-disconnected">No gamepad connected</div>
27-
<div id="gamepad-state">
28-
29-
</div>
30-
<div id="gamepad-button-container">
27+
<div id="gamepad-controls">
28+
<div id="gamepad-state"></div>
29+
<div id="gamepad-button-container-container">
30+
<div id="gamepad-button-container"></div>
31+
</div>
3132
</div>
32-
</template>
33+
</div>
34+
</div>
3335
<h1>Quadrotor Gamepad Controller</h1>
3436
<script type="module">
3537
import { Gamepad } from "./gamepad.js";

gamepad.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ class Mapper{
8383
}
8484
}
8585
export class Gamepad{
86-
constructor(parent, gamepad_interface){
87-
const template = document.getElementById('gamepad-template');
88-
const clone = template.content.cloneNode(true);
89-
parent.appendChild(clone);
90-
this.element = parent.lastElementChild;
86+
constructor(element, gamepad_interface){
87+
// const template = document.getElementById('gamepad-template');
88+
// const clone = template.content.cloneNode(true);
89+
// parent.appendChild(clone);
90+
this.element = element; //parent.lastElementChild;
9191
this.gamepad_interface = gamepad_interface;
9292
this.mapper = null;
9393
this.control_map = {}
@@ -117,7 +117,9 @@ export class Gamepad{
117117
document.querySelectorAll('.gamepad-mapping-button').forEach((btn) => { btn.disabled = true; });
118118
this.render_live_view()
119119
};
120-
document.getElementById("gamepad-button-container").appendChild(reset_button);
120+
const button_container = document.getElementById("gamepad-button-container")
121+
button_container.innerHTML = '';
122+
button_container.appendChild(reset_button);
121123

122124

123125
for (const channel in gamepad_interface){
@@ -148,7 +150,7 @@ export class Gamepad{
148150
});
149151
}
150152
};
151-
document.getElementById("gamepad-button-container").appendChild(button);
153+
button_container.appendChild(button);
152154
}
153155
this.poll()
154156
}

index.css

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ canvas{
180180
font-family: monospace;
181181
font-size: 80%;
182182
/*color: #e63946;*/
183-
max-width: 700px;
183+
/* max-width: 700px; */
184184
word-break: break-all;
185185
margin-right: 12px;
186186
}
@@ -194,7 +194,7 @@ canvas{
194194
#observations{
195195
font-family: monospace;
196196
font-size: 80%;
197-
width: 60em;
197+
width: 100%;
198198
text-overflow: ellipsis;
199199
overflow: hidden;
200200
white-space: nowrap;
@@ -216,11 +216,9 @@ canvas{
216216
display: flex;
217217
flex-direction: column;
218218
margin-block-end: 0em;
219+
white-space: nowrap;
219220
}
220221

221-
#controller-container{
222-
display: none;
223-
}
224222

225223
#policy-container{
226224
display: grid;
@@ -232,6 +230,12 @@ canvas{
232230
padding-top: 2px;
233231
padding-bottom: 3px;
234232
}
233+
#controller-container{
234+
display: none;
235+
}
236+
#gamepad-container{
237+
display: none;
238+
}
235239
.policy-container-item{
236240
display: flex;
237241
align-items: center;

index.html

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@
9494
<input type="radio" name="choice" value="controller">
9595
Controller
9696
</label>
97+
<label>
98+
<input type="radio" name="choice" value="gamepad">
99+
Gamepad
100+
</label>
97101
</form>
98102
</div>
99103
<div id="policy-container" class="controller-container">
@@ -113,11 +117,12 @@
113117
<div id="controller-container" class="controller-container">
114118
<textarea id="controller-code" rows=10 value="(x, u) => x.map((v, i) => v + u[i] * 0.1)"></textarea>
115119
</div>
116-
<div id="gamepad-container">
120+
<div id="gamepad-container" class="controller-container">
121+
<link rel="stylesheet" href="./gamepad.css">
117122
<template id="gamepad-axis-template">
118123
<div class="gamepad-controls-row">
119124
<div class="gamepad-controls-name gamepad-controls-cell"></div>
120-
<div class="gamepad-slider-container gamepad-controls-cell"><input type="range" class="gamepad-slider" min="-1" max="1" value="0" step="0.01" disabled></div>
125+
<div class="gamepad-slider-container gamepad-controls-cell"><input type="range" class="gamepad-slider" min="-1" max="1" value="0" step="0.01"></div>
121126
<div class="gamepad-value-display gamepad-controls-cell">0.00</div>
122127
</div>
123128
</template>
@@ -129,13 +134,13 @@
129134
<div></div>
130135
</div>
131136
</template>
132-
<template id="gamepad-template">
133-
<div id="gamepad-status" class="gamepad-disconnected">No gamepad connected</div>
134-
<div id="gamepad-state">
135-
</div>
136-
<div id="gamepad-button-container">
137+
<div id="gamepad-status" class="gamepad-disconnected">No gamepad connected</div>
138+
<div id="gamepad-controls">
139+
<div id="gamepad-state"></div>
140+
<div id="gamepad-button-container-container">
141+
<div id="gamepad-button-container"></div>
137142
</div>
138-
</template>
143+
</div>
139144
</div>
140145

141146
</div>

index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SimControls } from "./sim_controls.js";
33
import { ParameterManager } from "./parameter_manager.js";
44
import * as rlt from "rltools"
55
import * as math from "mathjs"
6+
import { Gamepad } from "./gamepad.js"
67

78
// import Controller from "./controller.js"
89

@@ -210,14 +211,43 @@ async function main(){
210211
if(event.target.value === "policy"){
211212
document.getElementById("policy-container").style.display = "block"
212213
document.getElementById("controller-container").style.display = "none"
214+
document.getElementById("gamepad-container").style.display = "none"
213215
proxy_controller.policy = new Policy(model)
214216
}
215-
else{
217+
else if(event.target.value === "controller"){
216218
document.getElementById("policy-container").style.display = "none"
217219
document.getElementById("controller-container").style.display = "block"
220+
document.getElementById("gamepad-container").style.display = "none"
218221
const event = new KeyboardEvent("keydown", {key: "Enter"});
219222
document.getElementById("controller-code").dispatchEvent(event);
220223
}
224+
else if(event.target.value === "gamepad"){
225+
document.getElementById("policy-container").style.display = "none"
226+
document.getElementById("controller-container").style.display = "none"
227+
document.getElementById("gamepad-container").style.display = "block"
228+
const parent = document.getElementById("gamepad-container")
229+
const gamepad = new Gamepad(parent, {
230+
"thrust": {
231+
type: "axis",
232+
positive_direction: "Up"
233+
},
234+
"roll": {
235+
type: "axis",
236+
positive_direction: "Right"
237+
},
238+
"pitch": {
239+
type: "axis",
240+
positive_direction: "Forward"
241+
},
242+
"yaw": {
243+
type: "axis",
244+
positive_direction: "Clockwise"
245+
},
246+
"reset": {
247+
type: "button"
248+
},
249+
})
250+
}
221251
});
222252
});
223253
})

0 commit comments

Comments
 (0)