-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulator.html
More file actions
744 lines (649 loc) · 36.6 KB
/
simulator.html
File metadata and controls
744 lines (649 loc) · 36.6 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
<!DOCTYPE html>
<meta charset="UTF-8">
<!--
Simulate various actuated shapes with a simple motor model and a CSL implementation.
Stefan Bethge, 2012-2014
Neurorobotics Research Laboratory, Berlin
-->
<html>
<head>
<title>Simni Playground</title>
<script src="libs/jquery.min.js"></script>
<script src="libs/functional.min.js"></script>
<script src="libs/Stats.min.js"></script>
<script src="libs/jquery.hotkeys.min.js"></script>
<script src="libs/jsxgraphcore.js"></script>
<script src="libs/d3.v2.min.js"></script>
<script src="libs/Box2dWeb-2.1.a.3.min.js"></script>
<script src="libs/Box2dWeb-joint-torque-patch.js"></script>
<script src="libs/b2Separator.js"></script>
<!--
<script src="libs/arbor.js"></script>
-->
<script src="libs/arbor-start-stop-fixed/src/etc.js"></script>
<script src="libs/arbor-start-stop-fixed/src/kernel.js"></script>
<script src="libs/arbor-start-stop-fixed/src/graphics/colors.js"></script>
<script src="libs/arbor-start-stop-fixed/src/graphics/primitives.js"></script>
<script src="libs/arbor-start-stop-fixed/src/graphics/graphics.js"></script>
<script src="libs/arbor-start-stop-fixed/src/tween/easing.js"></script>
<script src="libs/arbor-start-stop-fixed/src/tween/tween.js"></script>
<script src="libs/arbor-start-stop-fixed/src/physics/atoms.js"></script>
<script src="libs/arbor-start-stop-fixed/src/physics/barnes-hut.js"></script>
<script src="libs/arbor-start-stop-fixed/src/physics/physics.js"></script>
<script src="libs/arbor-start-stop-fixed/src/physics/system.js"></script>
<script src="libs/arbor-start-stop-fixed/src/dev.js"></script>
<script src="libs/Color.js"></script>
<script src="graph-renderer-svg.js"></script>
<script src="data/semni.data.js"></script>
<script src="data/semni.manifold.js"></script>
<script src="libs/three.min.js"></script>
<script src="libs/three.OrbitControls.js"></script>
<script src="physics.js"></script>
<script src="abc.js"></script>
<script src="libs/FileSaver.js"></script>
<script src="logging.js"></script>
<script src="ui.js"></script>
<script src="manifolds.js"></script>
<link rel="stylesheet" type="text/css" href="libs/jsxgraph.css" />
<link rel="stylesheet" type="text/css" href="simulator.css" />
</head>
<body>
<div id="top-panel">
<div id="simulation"><canvas width="750" height="330"></canvas></div>
<div id="view_controls">
Zoom:
<button onclick="physics.debugDraw.SetDrawScale(physics.debugDraw.GetDrawScale() + 20)">+</button>
<br/>
<button onclick="physics.debugDraw.SetDrawScale(physics.debugDraw.GetDrawScale() - 20)">-</button>
<br/>
Flags:
<button onclick="physics.debugDraw.m_drawFlags ^= b2DebugDraw.e_jointBit">Angle</button><br/>
<button onclick="physics.debugDraw.m_drawFlags ^= b2DebugDraw.e_centerOfMassBit">COM</button>
<br/>
Objects:
<button id="add_box" onClick="physics.createBox()">Box</button>
<button id="add_circle" onClick="physics.createCircle()">Circle</button><br/>
Semni:
<button id="posture_0" onclick="ui.set_posture(0,0,0, false, false)">Zero</button>
<button id="get_posture" onclick="ui.getSemniTransformAsFile()">Save</button>
<button id="set_posture" onclick="$('#set_posture_file').click()">Load</button>
<input type="file" id="set_posture_file" style="display:none" onchange="ui.setSemniTransformAsFile(this.files)">
<br/>
TrajLog:
<button id="start_log" class="toggle-button" onclick="logging.toggleRecorder()">Start</button>
<button id="get_log" onclick="logging.getLogfile()">Get</button>
</div>
<div id="right_side">
<div id="simni_title">
<span id="title_text">
<span id="title_title">Simni</span>
<a href="documentation/documentation.pdf" target="_blank">Documentation</a><br/>
</span>
</div>
<div id="phase_space_plot"><input type="checkbox" id="phase_space_enabled">phase space φ<sub>hip</sub>/φ<sub>knee</sub><button id="clear_phase">clear</button></div>
<div id="motor_torque_plot"><input type="checkbox" id="motor_torque_enabled">motor torque hip (green) and knee (blue)</div>
<div id="state_to_mode_mapping" style="display:none;"><input type="checkbox" id="phase_space_2_enabled">phase space U/φ'<br/></div>
</div>
</div>
<div id="bottom-panel">
<div id="controls" class="box">
<h1>Simulation controls</h1>
<select id="pendulum_style">
<option value="1">Single Pendulum</option>
<option value="2">Double Pendulum</option>
<option value="3" selected="selected">Semni</option>
</select>
<input type="checkbox" id="animate_realtime" value="no" onchange="ui.set_realtime(this.checked)"/>
<label for="animate_realtime">realtime (~60 fps)</label>
<br/>
<script type="text/coffeescript">
$ ->
#create physic bodies depending on selection
$("#pendulum_style").on 'change', ->
if physics.lower_joint and physics.lower_joint.csl_active
$("#toggle_csl").click()
#first remove old joints and bodies
removeJoint = (joint) ->
if joint
physics.world.DestroyJoint joint
joint = null
removeBody = (body) ->
if body
physics.world.DestroyBody body
body = null
removeBody physics.body3
removeJoint physics.upper_joint
removeBody physics.body2
removeJoint physics.lower_joint
removeBody physics.body
#create new objects for selected pendulum kind
style = $(this).val()
if style is "1"
physics.pend_style = 1
physics.createPendulum()
#get initial gi/gf from current mode setting
#ui.map_mode physics.lower_joint
gi = 0.5
$("#gi_param_lower").val(gi)
physics.lower_joint.gi = gi
gf = 1.0005
$("#gf_param_lower").val(gf)
physics.lower_joint.gf = gf
gb = 0
$("#gb_param_lower").val(gb)
physics.lower_joint.gb = gb
$("#semni_posture_controls").fadeOut "fast", ->
$("#map_state_to_mode_panel").fadeIn("fast")
$("#state_to_mode_presets").fadeIn("fast")
$("#state_to_mode_mapping").fadeIn("fast")
else if style is "2"
physics.pend_style = 2
physics.createDoublePendulum()
ui.map_mode physics.lower_joint
ui.map_mode physics.upper_joint
else if style is "3"
physics.pend_style = 3
physics.createSemni()
$("#state_to_mode_presets").fadeOut "fast"
$("#state_to_mode_mapping").fadeOut "fast"
$("#mode_param_upper").fadeOut "fast"
$("#csl_mode_name_upper").fadeOut "fast"
$("#mode_param_lower").fadeOut "fast"
$("#csl_mode_name_lower").fadeOut "fast"
$("#map_state_to_mode_panel").fadeOut "fast", ->
$("#semni_posture_controls").fadeIn "fast"
$("#pendulum_style").trigger('change')
#set initial csl params from abc modes for semni
if physics.pend_style is 3
$('#csl_mode_hip').trigger("change")
$('#csl_mode_knee').trigger("change")
#set initial friction from slider
$("#friction_param").trigger("change")
</script>
<button id="toggle_pause" class="toggle-button" onClick="ui.set_pause_explore();">pause/run simulation [p]</button>
<button id="next_step" onClick="physics.step = true; physics.update();">next step
(when paused) [n]</button><br/>
<button id="toggle_csl" class="toggle-button">toggle CSL [c]</button>
<label for="csl_mode_hip">Hip</label>:
<select id="csl_mode_hip" onchange="ui.set_csl_mode_upper($('#csl_mode_hip').val())">
<option value="r+" checked="checked">r+</option>
<option value="r-">r-</option>
<option value="c">c</option>
<option value="s+">s+</option>
<option value="s-">s-</option>
</select>
<label for="csl_mode_knee">Knee</label>:
<select id="csl_mode_knee" onchange="ui.set_csl_mode_lower($('#csl_mode_knee').val())">
<option value="r+" checked="checked">r+</option>
<option value="r-">r-</option>
<option value="c">c</option>
<option value="s+">s+</option>
<option value="s-">s-</option>
</select><br/>
<button id="toggle_bounce" class="toggle-button">toggle bounce controller [b]</button>
<script type="text/coffeescript">
$ ->
$("#toggle_csl").on 'click', ->
if physics.pend_style is 3
physics.toggleCSL physics.upper_joint
physics.toggleCSL physics.lower_joint
else
physics.toggleCSL physics.lower_joint
if physics.upper_joint
physics.toggleCSL physics.upper_joint
$("#toggle_bounce").on 'click', ->
if physics.pend_style is 3
physics.toggleBounce physics.upper_joint
physics.toggleBounce physics.lower_joint
</script>
<br/>
<div style="display: none;">
<label for="friction_param">friction</label> <input id="friction_param" type="range" min="0" max="0.2" value="0.01" step="0.001" onchange="ui.set_friction(parseFloat(this.value))" />
<span id="friction_val">0.01</span><br/>
<!-- friction of 0.04 is more realistic... -->
</div>
<label for="gi_param_upper">g<sub>i</sub></label> <input id="gi_param_upper" type="text" value="30" style="width:60px;"/>
<label for="gf_param_upper">g<sub>f</sub></label> <input id="gf_param_upper" type="text" value="0.1" style="width:60px;"/>
<label for="gb_param_upper">g<sub>b</sub></label> <input id="gb_param_upper" type="text" value="0" style="width:60px;"/>
<label for="gain_param_upper">gain</label> <input id="gain_param_upper" type="text" value="1" style="width:60px;"/>
<button id="set_csl_params_upper">set</button> <br/>
<script>
$(document).ready(function(){
$("#set_csl_params_upper").on("click", function(){
physics.upper_joint.gi = parseFloat($("#gi_param_upper").val())
physics.upper_joint.gf = parseFloat($("#gf_param_upper").val())
physics.upper_joint.gb = parseFloat($("#gb_param_upper").val())
physics.upper_joint.gain = parseFloat($("#gain_param_upper").val())
})
})
</script>
<label for="mode_param_upper">csl mode hip</label> <input id="mode_param_upper" type="range" min="-2" max="3" value="1.2" step="0.005" onchange="if(physics.upper_joint){ui.map_mode(physics.upper_joint)}" />
<span id="mode_val_upper"></span> <span id="csl_mode_name_upper">contraction</span><br/>
<hr/>
<label for="gi_param_lower">g<sub>i</sub></label> <input id="gi_param_lower" type="text" value="30" style="width:60px;"/>
<label for="gf_param_lower">g<sub>f</sub></label> <input id="gf_param_lower" type="text" value="0.1" style="width:60px;"/>
<label for="gb_param_lower">g<sub>b</sub></label> <input id="gb_param_lower" type="text" value="0" style="width:60px;"/>
<label for="gain_param_lower">gain</label> <input id="gain_param_lower" type="text" value="1" style="width:60px;"/>
<button id="set_csl_params_lower">set</button> <br/>
<script>
$(document).ready(function(){
$("#set_csl_params_lower").on("click", function(){
physics.lower_joint.gi = parseFloat($("#gi_param_lower").val())
physics.lower_joint.gf = parseFloat($("#gf_param_lower").val())
physics.lower_joint.gb = parseFloat($("#gb_param_lower").val())
physics.lower_joint.gain = parseFloat($("#gain_param_lower").val())
})
})
</script>
<label for="mode_param_lower">csl mode knee</label> <input id="mode_param_lower" type="range" min="-2" max="3" value="1.01" step="0.005" onchange="ui.map_mode(physics.lower_joint)" />
<span id="mode_val_lower"></span> <span id="csl_mode_name_lower">contraction</span><br/>
<br/>
</div>
<div id="map_state_to_mode_panel" style="display:none;" class="box">
<label for="map_state_to_mode">map state to mode [m]</label><input type="checkbox" id="map_state_to_mode"><br/>
mode = w<sub>0</sub> · U + w<sub>1</sub> · φ' + w<sub>2</sub><br/>
<div class="vertical-range-box">
<input type="checkbox" id="w0_abs"><br>
<label for="w0">w<sub>0</sub></label><span id="w0_val">=197</span><br/>
<input type="range" step="1" id="w0" min="-5000" max="5000" class="vertical-range" value="197">
</div>
<div class="vertical-range-box">
<input type="checkbox" id="w1_abs"><br>
<label for="w1">w<sub>1</sub></label><span id="w1_val">=39.5</span><br/>
<input type="range" step="1" id="w1" min="-5000" max="5000" class="vertical-range" value="39.5">
</div>
<div class="vertical-range-box">
<- abs <br/>
<label for="w2">w<sub>2</sub></label><span id="w2_val">=0.2</span><br/>
<input type="range" step="0.01" id="w2" min="-2" max="2" class="vertical-range" value="0.2">
</div>
<script>
$(document).ready(function(){
$("#map_state_to_mode_panel input[type=range]").each(function(index){
$("<button>").insertAfter($(this)).attr({"id": this.id+"_increase"}).html("+").click(function(){
var range = $(this).parent().find("input[type=range]")
range.attr("value", parseInt(range.attr("value")) + 1);
range.trigger("change")
draw_state_to_mode_mapping()
})
$("<br/>").insertAfter($(this))
$("<button>").insertAfter($(this)).attr({"id": this.id+"_decrease"}).html("-").click(function(){
var range = $(this).parent().find("input[type=range]")
range.attr("value", parseInt(range.attr("value")) - 1);
range.trigger("change")
draw_state_to_mode_mapping()
})
$("<br/>").insertAfter($(this))
})
})
</script>
</div>
<script>
$("#map_state_to_mode_panel input[type=range]").on('mouseup', function(){
draw_state_to_mode_mapping()
})
</script>
<div id="state_to_mode_presets" style="display:none;" class="box">
<br/>
<button id="preset_1" onclick="ui.set_preset(114,52,0.25,false,false)">Preset 1</button>left down-c<br/>
<button id="preset_2" onclick="ui.set_preset(-811,-162,-1.08,false,false)">Preset 2</button>s right, c up,</br>f=0.018<br/>
<button id="preset_3" onclick="ui.set_preset(450,0,1.001,true,true)">Preset 3</button>self-inhibit c,<br/>f=0.022<br/>
<button id="preset_4" onclick="ui.set_preset(81,-3676,-0.5,false,false)">Preset 4</button>r right, s left
</div>
<div id="semni_posture_controls" class="box">
<h1>ABC learning</h1>
<button id="toggle_explore" class="toggle-button">toggle explore [e]</button>
<script type="text/coffeescript">
$("#toggle_explore").on 'click', ->
if physics.pend_style is 3
physics.abc.toggleExplore()
</script>
<br/>
<select id="csl_heuristic" onchange="physics.abc.set_heuristic($('#csl_heuristic').val())">
<option value="manual">manual</option>
<option value="random">random</option>
<option value="unseen" selected="selected">unseen</option>
</select>
<span id="unseen_options">
<label for="csl_heuristic">heuristic</label><br/>
<input type="checkbox" id="heuristic_keep_dir" value="no"
onchange="physics.abc.set_heuristic_keep_dir(this.checked)"/>
<label for="heuristic_keep_dir">Keep direction if possible (or always try to change)
</label></br>
<input type="checkbox" id="heuristic_keep_joint" value="yes" checked
onchange="physics.abc.set_heuristic_keep_joint(this.checked)"/>
<label for="heuristic_keep_joint">Keep joint if possible (or always try to change)</label></br>
</span>
<br/>
<!--
<button id="find_circles" onclick="physics.abc.posture_graph.findElementaryCircles(); console.log(physics.abc.posture_graph.circles);">Find circles</button>
<button id="walk_best_circle" class="toggle-button" onclick="physics.abc.posture_graph.walkCircle()">Walk best circle</button>
<br/>-->
<button id="save_graph_svg" onclick="physics.ui.getPostureGraphAsFile()">Save graph as SVG</button><br/>
<button id="save_graph_json" onclick="physics.abc.posture_graph.saveGaphToFile()">Save graph as JSON</button><br/>
<button id="set_graph_json" onclick="$('#set_graph_json_file').click()">Load graph from JSON</button><br/>
<input type="file" id="set_graph_json_file" style="display:none" onchange="physics.abc.posture_graph.loadGraphFromFile(this.files)">
<button id="set_graph_semni" onclick="$('#set_graph_semni_file').click()">Load graph from Semni</button><br/>
<input type="file" id="set_graph_semni_file" style="display:none" onchange="physics.abc.posture_graph.loadGraphFromSemniFile(this.files)">
</div>
<div id="graph_controls" class="box">
<h1>Graph properties</h1>
<input id="graph_repulsion" type="range" min="100" max="2000" value="600" step="1"
onchange="physics.abc.graph.parameters({'repulsion': parseInt(this.value)})" />
<label for="graph_repulsion">Repulsion</label></br>
<input id="graph_stiffness" type="range" min="10" max="200" value="150" step="1"
onchange="physics.abc.graph.parameters({'stiffness': parseInt(this.value)})" />
<label for="graph_stiffness">Stiffness</label><br/>
<input type="checkbox" id="graph_animated" value="yes" checked
onchange="ui.set_graph_animated(this.checked)"/>
<label for="graph_animated">Animate graph (slow) [a]</label></br>
<input type="checkbox" id="graph_pause_drawing" value="yes" checked
onchange="ui.set_pause_drawing(this.checked)"/>
<label for="graph_pause_drawing">Pause graph layouting between new poses</label></br>
<input type="checkbox" id="graph_pause_layouting" value="no"
onchange="ui.set_pause_layouting(this.checked)"/>
<label for="graph_pause_layouting">Pause graph layouting</label></br>
<input type="checkbox" id="graph_activation" value="no"
onchange="ui.set_activation(this.checked)"/>
<label for="graph_activation">Show node activation</label></br>
<input type="checkbox" id="graph_color_activation" value="no"
onchange="ui.set_color_activation(this.checked)"/>
<label for="graph_color_activation">Show node activation colors (manifold colors
otherwise)</label> </br>
<input type="checkbox" id="graph_color_visits" value="no"
onchange="ui.set_color_visits(this.checked)"/>
<label for="graph_color_visits">Show node visit count colors</label></br>
<input type="checkbox" id="graph_draw_semnis" value="yes" checked
onchange="ui.set_draw_semni(this.checked)"/>
<label for="graph_draw_semnis">Show semni postures</label></br>
<input type="checkbox" id="graph_draw_edge_labels" value="no"
onchange="ui.set_draw_edge_labels(this.checked)"/>
<label for="graph_draw_edge_labels">Show transition labels</label></br>
<input type="checkbox" id="graph_save_periodically" value="no"
onchange="ui.set_save_periodically(this.checked)"/>
<label for="graph_save_periodically">Save graph with each new posture</label></br>
<hr/>
<input type="checkbox" id="render_manifold" value="yes" checked
onchange="ui.set_render_manifold(this.checked)"/>
<label for="render_manifold">Update manifold view [m]</label></br>
</div>
<div id="manifold_view" class="box">
<canvas id="webglCanvas" width=300 height=256></canvas>
<!-- <canvas id="webglCanvas" width=800 height=600></canvas> -->
</div>
<!--<div id="semni_svg" style="width: 300px; height: 300px; float: left;"></div>-->
<div id="posture-graph">
<div id="viewport_svg"></div>
</div>
</div>
<script>
//use d3 for state_to_mode_mapping
var margin = {top: 0, right: 0, bottom: 20, left: 30},
width = 300,
height = 300;
var scalex = d3.scale.linear()
.domain([-0.02, 0.02])
.range([0, width])
var scaley = d3.scale.linear()
.domain([-0.02, 0.02])
.range([height, 0])
var svg = d3.select("#state_to_mode_mapping").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (height-margin.bottom) + ")")
.call(d3.svg.axis().scale(scalex).orient("bottom"))
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (margin.left) + ",0)")
.call(d3.svg.axis().scale(scaley).orient("left"))
svg.insert("g").attr("class", "cell")
//draw the phase space with plain old canvas
var context
function calc_mode_class(mode){
if (mode < 0)
return "support"
else if (mode > 0 && mode < 1)
return "release"
else if (mode == 1)
return "hold"
else if (mode > 1)
return "contraction"
else
return ""
}
function calc_mode_to_color(mode){
if (mode < 0)
//support
return {'red':0, 'green':physics.clip(-mode*50+10,255) , 'blue':0}
else if (mode >= 0 && mode < 0.995)
//release
return {'red':0, 'green':0, 'blue':physics.clip(mode*100+80,255)}
else if (mode < 1.005 && mode > 0.995)
//hold
return {'red':255, 'green':255, 'blue':255}
else if (mode > 1.005)
//contraction
return {'red':physics.clip(mode*50+20, 255), 'green':0, 'blue':0}
else
return ""
}
function draw_state_to_mode_mapping() {
//recalc mapping
//imgData = context.createImageData(300,300)
context.clearRect(0,0,300,300)
var i, j, mode = 0
for (i = -0.02; i < 0.02; i=i+0.0005) {
for (j = -0.02; j < 0.02; j=j+0.0005) {
mode = physics.calcMode(j,i)
px = scalex(j)//j*3750+150
py = scaley(i)//i*3750+150
/*index = (px + py * width) * 4;
imgData[index+0]=255
imgData[index+1]=0
imgData[index+2]=0
imgData[index+3]=255*/
c = calc_mode_to_color(mode)
context.fillStyle = "rgb("+c.red.toFixed()+","+c.green.toFixed()+","+c.blue.toFixed()+")"
context.fillRect(px,py, 2,2)
}
}
//context.putImageData(imgData, 300,300)
}
//use jsxgraph for phase spaces
var phase_space_enabled=false,motor_torque_enabled=false,phase_space_2_enabled=false,brd,brd2,g,g2,g3,g4,xdata=[],ydata=[],zdata=[{'U':0, 'phidot':0}],i,xdata2=[],ydata2=[],ydata3=[]
//show every nth-frame
var update_skip=update_skip_2=update_skip_initial=14
JXG.Options.showCopyright = false
JXG.Options.zoom.wheel = true
JXG.Options.axis.withLabel = false
brd = JXG.JSXGraph.initBoard('phase_space_plot', {boundingbox:[-1.2,2,1.2,-2], axis:true})
$("#clear_phase").click(function(){
lastx = xdata.pop()
lasty = ydata.pop()
xdata.map(function(v,i,a){a[i]=lastx})
ydata.map(function(v,i,a){a[i]=lasty})
})
$("#phase_space_enabled").on('click', function(){
phase_space_enabled = !phase_space_enabled
})
$("#motor_torque_enabled").on('click', function(){
motor_torque_enabled= !motor_torque_enabled
})
$("#phase_space_2_enabled").on('click', function(){
phase_space_2_enabled = !phase_space_2_enabled
})
var line = d3.svg.line()
.x(function(d, i) { return scalex(d.U); })
.y(function(d, i) { return scaley(d.phidot); });
var path = svg.append("g")
.append("path")
.data([zdata], function(d) { if (d) return d.U })
.attr("class", "line")
.attr("d", line);
function draw_phase_space(){
if(phase_space_enabled){
if (xdata.length==1000) {
xdata.shift()
ydata.shift()
}
if (physics.pend_style == 3) {
xdata.push(physics.lower_joint.GetJointAngle())
ydata.push(physics.upper_joint.GetJointAngle())
} else if (physics.pend_style == 1) {
xdata.push(physics.lower_joint.GetJointAngle())
ydata.push(0)
//brd.setBoundingBox([-2,2,2,-2])
}
if (!g) { // If the curve does not exist yet, create it.
g = brd.create('curve', [xdata,ydata], {strokeWidth:1, strokeColor:'red'});
}
//don't draw every frame
if (update_skip == 0) {
brd.update()
}
}
if(phase_space_2_enabled) {
if (zdata.length==200) {
zdata.shift()
}
angle_speed = physics.lower_joint.angle_diff
if (!angle_speed)
angle_speed=0
if (physics.upper_joint.motor_torque)
Uval = physics.upper_joint.motor_torque
else
Uval = physics.lower_joint.motor_torque
zdata.push({'U':Uval, 'phidot': angle_speed })
//don't draw every frame
if (update_skip == 0) {
path
.attr("d", line)
.attr("transform", null)
}
}
if (update_skip == 0) {
update_skip = update_skip_initial
} else {
update_skip--
}
}
for(i=0; i<1; i=i+0.001){
xdata2.push(i)
}
brd2 = JXG.JSXGraph.initBoard('motor_torque_plot', {boundingbox:[0,2,1.1,-2], axis:true})
function draw_motor_torque(){
if(motor_torque_enabled){
if (ydata2.length==1000) {
ydata2.shift()
ydata3.shift()
}
if (physics.pend_style == 3 || physics.pend_style == 2) {
//semni or double pendulum
if (physics.upper_joint.motor_torque)
//ydata2.push(physics.upper_joint.motor_control)
ydata2.push(physics.upper_joint.motor_torque)
else
ydata2.push(0)
if (physics.lower_joint.motor_torque)
//ydata3.push(physics.upper_joint.I_t)
ydata3.push(physics.lower_joint.motor_torque)
else
ydata3.push(0)
} else {
//single pendulum
if (physics.lower_joint.motor_torque)
ydata2.push(physics.lower_joint.motor_torque)
else
ydata2.push(0)
}
if (!g3) { // If the curve does not exist yet, create it.
g3 = brd2.create('curve', [xdata2,ydata2], {strokeWidth:1, strokeColor:'green'});
}
if (!g4) { // If the curve does not exist yet, create it.
g4 = brd2.create('curve', [xdata2,ydata3], {strokeWidth:1, strokeColor:'blue'});
}
//don't draw every frame
if (update_skip_2 == 0) {
brd2.update();
update_skip_2 = update_skip_initial
} else {
update_skip_2--
}
}
}
//draw state to mode mapping and state
context = $("<canvas>").appendTo("#state_to_mode_mapping").attr("width", 300).attr("height", 300)[0].getContext('2d')
/*
//state space plot
angle_speed = physics.lower_joint.angle_speed
if (!angle_speed)
angle_speed=0
zdata.push({'U':physics.body.motor_torque, 'phidot': angle_speed })
*/
</script>
<script>
$(document).ready(function(){
//key bindings
$('.toggle-button').on('click', function(){
if($(this).css('color') == 'rgb(255, 0, 0)')
$(this).css('color', 'black')
else
$(this).css('color', 'red')
})
//bind enter key in text fields to button click
function handle_enter(event){
if(event.keyCode == 13){
$(this).nextAll('button').first().click()
}
}
$("#gi_param_lower").keyup(handle_enter)
$("#gf_param_lower").keyup(handle_enter)
$("#gb_param_lower").keyup(handle_enter)
$("#gain_param_lower").keyup(handle_enter)
$("#gi_param_upper").keyup(handle_enter)
$("#gf_param_upper").keyup(handle_enter)
$("#gb_param_upper").keyup(handle_enter)
$("#gain_param_upper").keyup(handle_enter)
//keyboard shortcuts
$(document).bind('keydown', 'c', function (evt){
$('#toggle_csl').click(); return false;
})
$(document).bind('keydown', 'b', function (evt){
$('#toggle_bounce').click(); return false;
})
$(document).bind('keydown', 'e', function (evt){
$('#toggle_explore').click(); return false;
})
$(document).bind('keydown', 'p', function (evt){
$('#toggle_pause').click(); return false;
})
$(document).bind('keydown', 'n', function (evt){
$('#next_step').click(); return false;
})
$(document).bind('keydown', 'm', function (evt){
checkbox = $('#map_state_to_mode')
checkbox.click();
checkbox.attr('checked', !checkbox.is(':checked'));
return false;
})
$(document).bind('keydown', 'a', function (evt){
$('#graph_animated').click(); return false;
});
$(document).bind('keydown', 'm', function (evt){
$('#render_manifold').click(); return false;
});
$(document).bind('keydown', 'd', function (evt){
if (physics.abc.graph.renderer.previous_hover) {
n = physics.abc.graph.renderer.previous_hover.data.number
physics.abc.posture_graph.deletePosture(n);
}
return false;
});
})
</script>
</body>
<script type="text/javascript" src="libs/coffee-script.js"> </script>
</html>