Skip to content

Commit d8bf672

Browse files
adding reference traj view
1 parent 9229d01 commit d8bf672

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class ProxyController {
2828
reset() {
2929
this.policy.reset()
3030
}
31+
get_reference(states){
32+
return this.policy.get_reference(states)
33+
}
3134
}
3235
class MultiController {
3336
constructor(Controller) {
@@ -46,11 +49,15 @@ class MultiController {
4649
}
4750
this.controllers.forEach(controller => controller.reset())
4851
}
52+
get_reference(states){
53+
console.assert(this.controllers.length == states.length)
54+
return this.controllers.map((c) => c.get_reference())
55+
}
4956
}
5057

5158
let model = null
5259
let trajectory = null
53-
class Policy {
60+
class Policy{
5461
constructor() {
5562
this.step = 0
5663
this.policy_states = null
@@ -106,7 +113,7 @@ class Policy {
106113
state.observe()
107114
const observation_description = document.getElementById("observations").observation
108115
let input = math.matrix([observation_description.split(".").map(x => this.get_observation(state, x)).flat()])
109-
const input_offset = trajectory.evaluate(this.step / 100)
116+
const input_offset = this._get_reference()
110117
input_offset.forEach((x, i) => {
111118
input._data[0][i] = input._data[0][i] - x
112119
})
@@ -119,6 +126,13 @@ class Policy {
119126
this.step = 0
120127
this.policy_states = null
121128
}
129+
_get_reference(){
130+
return trajectory.evaluate(this.step / 100)
131+
}
132+
get_reference(states){
133+
const ref = this._get_reference()
134+
return states.map(() => ref)
135+
}
122136
}
123137

124138

l2f.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// import * as ui from "./ui.js"
22
import createModule from "l2f-interface";
3+
import * as THREE from "three"
34
// const DEBUG = true
45
const DEBUG = false
56

@@ -82,6 +83,7 @@ export class L2F{
8283
this.control()
8384
});
8485
this.dt = null
86+
this.references = null
8587
}
8688
async change_num_quadrotors(num){
8789
const diff = num - this.states.length
@@ -113,6 +115,21 @@ export class L2F{
113115
}
114116
simulate_step(){
115117
const actions = this.policy.evaluate_step(this.states)
118+
const references = this.policy.get_reference(this.states)
119+
if(this.references === null){
120+
// create three.js reference ball
121+
this.references_ui = references.map((reference, i) => {
122+
const geometry = new THREE.SphereGeometry(Math.cbrt(this.parameters[i].dynamics.mass) / 20, 32, 32);
123+
const material = new THREE.MeshStandardMaterial({ color: 0xff4444 });
124+
const ball = new THREE.Mesh(geometry, material);
125+
const reference_ui_objects = this.ui_state.simulator.add(ball)
126+
return ball
127+
})
128+
}
129+
this.references = references
130+
this.references.forEach((reference, i) => {
131+
this.references_ui[i].position.set(reference[0], reference[1], reference[2])
132+
})
116133
console.assert(actions.length === this.states.length, "Action dimension mismatch")
117134
this.states.forEach((state, i) => {
118135
const action = actions[i]
@@ -176,6 +193,7 @@ export class L2F{
176193
if(this.render_states && this.render_actions){
177194
this.ui.render_multi(this.ui_state, this.parameters, this.render_states, this.render_actions)
178195
}
196+
this.ui_state
179197
requestAnimationFrame(() => this.render());
180198
if(this.DEBUG){
181199
this.stats.end()

0 commit comments

Comments
 (0)