Skip to content

Commit e6df3ae

Browse files
adding trajectory tracking options
1 parent 3af3720 commit e6df3ae

File tree

4 files changed

+122
-18
lines changed

4 files changed

+122
-18
lines changed

index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@
182182
<button id="pause">Pause</button>
183183
<button id="step">Step</button>
184184
</div>
185+
<div id="reference-trajectory-container" class="controls-container">
186+
<label for="reference-trajectory">Reference Trajectory:</label>
187+
<select id="reference-trajectory" name="reference-trajectory">
188+
</select>
189+
<template id="reference-trajectory-option-template">
190+
<span class="slider-container">
191+
<div>
192+
<span class="control-container-label"></span>
193+
<span class="control-container-label"></span>
194+
</div>
195+
<div>
196+
<input type="range" min="0" max="1" step="0.05" value="0.5">
197+
</div>
198+
</span>
199+
</template>
200+
<div id="reference-trajectory-options">
201+
</div>
202+
</div>
185203
<div class="controls-container">
186204
<span class="slider-container">
187205
<div>

index.js

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as rlt from "rltools"
55
import * as math from "mathjs"
66
import { Gamepad } from "./gamepad.js"
77
import { GamepadController } from "./gamepad_controller.js"
8+
import { Position } from "./trajectories/position.js"
9+
import { Lissajous } from "./trajectories/lissajous.js"
810
// import Controller from "./controller.js"
911

1012
// check url for "file" parameter
@@ -14,22 +16,6 @@ const file_url = file ? file : "./blob/checkpoint.h5"
1416

1517
let proxy_controller = null
1618

17-
function lissajous(t){
18-
const scale = 0.5
19-
const duration = 10
20-
const A = 1
21-
const B = 0.5
22-
const progress = t * 2 * Math.PI / duration
23-
const d_progress = 2 * Math.PI / duration
24-
const x = scale * Math.sin(A * progress)
25-
const y = scale * Math.sin(B * progress)
26-
const vx = scale * Math.cos(A * progress) * A * d_progress
27-
const vy = scale * Math.cos(B * progress) * B * d_progress
28-
return [x, y, 0, vx, vy, 0]
29-
}
30-
function default_trajectory(t){
31-
return [0, 0, 0, 0, 0, 0]
32-
}
3319

3420
class ProxyController{
3521
constructor(current_policy){
@@ -62,6 +48,7 @@ class MultiController{
6248
}
6349

6450
let model = null
51+
let trajectory = null
6552
class Policy{
6653
constructor(){
6754
this.step = 0
@@ -113,17 +100,17 @@ class Policy{
113100
if(!this.policy_states || this.policy_states.length !== states.length){
114101
this.policy_states = states.map(() => null)
115102
}
103+
this.step += 1
116104
return states.map((state, i) => {
117105
state.observe()
118106
const observation_description = document.getElementById("observations").observation
119107
let input = math.matrix([observation_description.split(".").map(x => this.get_observation(state, x)).flat()])
120-
const input_offset = default_trajectory(this.step / 100)
108+
const input_offset = trajectory.evaluate(this.step / 100)
121109
input_offset.forEach((x, i) => {
122110
input._data[0][i] = input._data[0][i] - x
123111
})
124112
const [output, new_state] = model.evaluate_step(input, this.policy_states[i])
125113
this.policy_states[i] = new_state
126-
this.step += 1
127114
return output.valueOf()[0]
128115
})
129116
}
@@ -176,6 +163,43 @@ async function load_model(checkpoint){
176163
}
177164

178165
async function main(){
166+
const trajectory_select = document.getElementById("reference-trajectory")
167+
const trajectories = {"Position": Position, "Lissajous": Lissajous}
168+
trajectory_select.innerHTML = ""
169+
for (const name in trajectories) {
170+
trajectory_select.innerHTML += `<option value="${name}">${name}</option>`
171+
}
172+
trajectory_select.addEventListener("change", (event) => {
173+
const trajectory_class = trajectories[event.target.value]
174+
trajectory = new trajectory_class()
175+
176+
const trajectory_options_container = document.getElementById("reference-trajectory-options")
177+
trajectory_options_container.innerHTML = ""
178+
const trajectory_option_template = document.getElementById("reference-trajectory-option-template")
179+
180+
for (const [key, config] of Object.entries(trajectory.parameters)) {
181+
const template = trajectory_option_template.content.cloneNode(true)
182+
183+
const labels = template.querySelectorAll(".control-container-label")
184+
labels[0].textContent = key
185+
labels[1].textContent = config.default
186+
187+
const slider = template.querySelector("input[type=range]")
188+
slider.min = config.range[0]
189+
slider.max = config.range[1]
190+
slider.step = 0.01
191+
slider.value = config.default
192+
193+
slider.addEventListener("input", () => {
194+
labels[1].textContent = slider.value
195+
trajectory.set_parameter(key, parseFloat(slider.value))
196+
})
197+
198+
trajectory_options_container.appendChild(template)
199+
}
200+
})
201+
trajectory_select.dispatchEvent(new Event("change"))
202+
179203
document.getElementById("default-checkpoint-btn").addEventListener("click", async () => {
180204
load_model(file_url)
181205
})

trajectories/lissajous.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
export class Lissajous{
3+
constructor(){
4+
this.parameters = {
5+
"period": {"range": [1, 15], "default": 10},
6+
"scale": {"range": [0, 5], "default": 0.1},
7+
}
8+
this.parameter_values = {}
9+
for(const [name, param] of Object.entries(this.parameters)){
10+
this.parameter_values[name] = param.default
11+
}
12+
}
13+
set_parameter(name, value){
14+
this.parameter_values[name] = value
15+
}
16+
evaluate(t){
17+
const scale = this.parameter_values.scale
18+
const duration = this.parameter_values.period
19+
const A = 1
20+
const B = 0.5
21+
const progress = t * 2 * Math.PI / duration
22+
const d_progress = 2 * Math.PI / duration
23+
const x = scale * Math.sin(A * progress)
24+
const y = scale * Math.sin(B * progress)
25+
const vx = scale * Math.cos(A * progress) * A * d_progress
26+
const vy = scale * Math.cos(B * progress) * B * d_progress
27+
return [x, y, 0, vx, vy, 0]
28+
}
29+
}

trajectories/position.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export class Position{
2+
constructor(){
3+
this.parameters = {
4+
"x": {"range": [-10, 10], "default": 0},
5+
"y": {"range": [-10, 10], "default": 0},
6+
"z": {"range": [-10, 10], "default": 0},
7+
}
8+
this.parameter_values = {}
9+
for(const [name, param] of Object.entries(this.parameters)){
10+
this.parameter_values[name] = param.default
11+
}
12+
}
13+
set_parameter(name, value){
14+
this.parameter_values[name] = value
15+
}
16+
evaluate(t){
17+
return [this.parameter_values.x, this.parameter_values.y, this.parameter_values.z, 0, 0, 0]
18+
}
19+
}
20+
21+
// function lissajous(t){
22+
// const scale = 0.5
23+
// const duration = 10
24+
// const A = 1
25+
// const B = 0.5
26+
// const progress = t * 2 * Math.PI / duration
27+
// const d_progress = 2 * Math.PI / duration
28+
// const x = scale * Math.sin(A * progress)
29+
// const y = scale * Math.sin(B * progress)
30+
// const vx = scale * Math.cos(A * progress) * A * d_progress
31+
// const vy = scale * Math.cos(B * progress) * B * d_progress
32+
// return [x, y, 0, vx, vy, 0]
33+
// }

0 commit comments

Comments
 (0)