Skip to content

Commit 4e293af

Browse files
can fly los
1 parent 4113290 commit 4e293af

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default class Controller {
4747

4848
// note: gl-matrix uses a column-major layout
4949
const A = mat4.transpose(mat4.create(), mat4.fromValues(
50-
1, 1, 1, 1,
50+
1, 1, 1, 1,
5151
+rp[0][1], +rp[1][1], +rp[2][1], +rp[3][1], // positive y rotor displacement causes positive x/roll
5252
-rp[0][0], -rp[1][0], -rp[2][0], -rp[3][0], // positive x rotor displacement causes negative y/pitch
5353
k_m_dir[0]*k_m[0], k_m_dir[1]*k_m[1], k_m_dir[2]*k_m[2], k_m_dir[3]*k_m[3]

gamepad.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export class Gamepad{
234234
}
235235
}
236236
this.update_live_view(gamepad)
237-
if((Object.keys(this.gamepad_interface).map((key) => key in this.control_map && this.control_map[key].index !== -1)).every()){
237+
if((Object.keys(this.gamepad_interface).every((key) => key in this.control_map && this.control_map[key].index !== -1))){
238238
const output = {}
239239
for(const control in this.control_map){
240240
const details = this.control_map[control];
@@ -246,7 +246,7 @@ export class Gamepad{
246246
output[control] = value_inverted;
247247
}
248248
else{
249-
output[control](raw_value.pressed);
249+
output[control] = raw_value.pressed;
250250
}
251251
}
252252
for(const listener of this.listeners){

gamepad_controller.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export class GamepadController{
33
constructor(gamepad){
44
this.actions = null
55
this.actions_time = null
6+
this.k_omega = 0.001;
67
gamepad.addListener((output) => {
78
this.actions = output
89
this.actions_time = performance.now()
@@ -13,20 +14,31 @@ export class GamepadController{
1314
const state = JSON.parse(state_binding.get_state());
1415
const params = JSON.parse(state_binding.get_parameters());
1516

16-
const temp = vec3.create();
17-
vec3.scale(temp, omega, -this.k_omega);
18-
vec3.add(tau, tau, temp);
17+
const rp = params['dynamics']['rotor_positions'];
18+
const k_m = params['dynamics']['rotor_torque_constants'];
19+
const k_m_dir = params['dynamics']['rotor_torque_directions'].map(v => v[2]); // only considering the z direction
20+
const [a, b, c] = params['dynamics']['rotor_thrust_coefficients'][0];
1921

20-
const l = 0.028;
21-
const k_q = params['dynamics']['rotor_torque_constants'][0];
22+
const omega = vec3.fromValues(...state['angular_velocity']);
23+
24+
const omega_range = [1.5, 1.5, -1]; // yaw command is clockwise
25+
const max_thrust = params["dynamics"]["rotor_thrust_coefficients"].map(tc => tc.reduce((a, c, i) => a + c * Math.pow(params.dynamics.action_limit.max, i), 0)).reduce((a, c) => a + c, 0);
26+
const omega_des_array = this.actions !== null ? ["roll", "pitch", "yaw"].map((k, i) => this.actions[k] * omega_range[i]) : [0, 0, 0];
27+
const omega_des = vec3.fromValues(...omega_des_array);
28+
29+
const omega_error = vec3.subtract(vec3.create(), omega, omega_des);
30+
const tau = vec3.create();
31+
vec3.scale(tau, omega_error, -this.k_omega);
32+
33+
const T = this.actions !== null ? (this.actions["thrust"] + 1) / 2 * max_thrust : 0;
2234

2335
const controlInputs = vec4.fromValues(T, tau[0], tau[1], tau[2]);
2436

2537
const A = mat4.transpose(mat4.create(), mat4.fromValues(
26-
1, 1, 1, 1,
27-
-l, -l, l, l,
28-
-l, l, l, -l,
29-
-k_q, k_q, -k_q, k_q
38+
1, 1, 1, 1,
39+
+rp[0][1], +rp[1][1], +rp[2][1], +rp[3][1], // positive y rotor displacement causes positive x/roll
40+
-rp[0][0], -rp[1][0], -rp[2][0], -rp[3][0], // positive x rotor displacement causes negative y/pitch
41+
k_m_dir[0]*k_m[0], k_m_dir[1]*k_m[1], k_m_dir[2]*k_m[2], k_m_dir[3]*k_m[3]
3042
));
3143
const A_inv = mat4.invert(mat4.create(), A);
3244

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,19 @@ async function main(){
248248
type: "button"
249249
},
250250
})
251+
proxy_controller.policy = new GamepadController(gamepad)
252+
gamepad.addListener((output) => {
253+
if(output["reset"] === true){
254+
const button = document.getElementById("initial-states")
255+
button.dispatchEvent(new Event('click'));
256+
}
257+
})
251258
}
252259
});
253260
});
261+
const gamepadRadio = document.querySelector('input[name="choice"][value="gamepad"]');
262+
gamepadRadio.checked = true;
263+
gamepadRadio.dispatchEvent(new Event('change'));
254264
})
255265
document.getElementById("controller-code").addEventListener("keydown", async (e) => {
256266
if (e.key === "Enter") {
@@ -358,6 +368,7 @@ async function main(){
358368
reader.readAsText(file);
359369
}
360370
})
371+
361372
}
362373

363374
document.addEventListener("DOMContentLoaded", main)

0 commit comments

Comments
 (0)