-
Notifications
You must be signed in to change notification settings - Fork 6
/
2DOF1.html
566 lines (466 loc) · 17.9 KB
/
2DOF1.html
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
<!-- Can Update with the equations as used in Acceleration Analysis File -->
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>SOLVE - Mechanics of Machines Lab - Slider Crank Mechanism</title>
<!-- DND-Required css and js files -->
<link rel="stylesheet" href="sins/2DOF.css" />
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js'></script>
<script type="text/javascript" src= "sins/2DOF.js"></script>
<script type="text/javascript" >
//Script specific to simulation
//program variables
//controls section
var simstatus=1;
var rotstatus=1;
//simstate();
//comments section
var commenttext="Some Text";
var commentloc=0;
//computing section
var trans= new point(100,285);
var o= new point(0,0,"");
var a= new point(0,0,"");
var b= new point(0,0,"");
var prev= new point(0,0,"");
//var d= new point(0,0,"D");
var phase=40;
var theta2 = 40; // all angles to be defined either in degrees only or radians only throughout the program and convert as and when required
var phi=0; // All angles in Degrees. (mention the specification in the script like here)
var omega2=1; // angular velocity in rad/s
//var omega3=0, omega4=0;
var r=0,l=0;
var m1=0,m2=0;
var ampa=0,ampb=0;
var imp=0,k=0;
//var mult=1;
var flaggrashof=true;
//graphics section
var canvas;
var ctx;
//timing section
var simTimeId = setInterval("",'1000');
var pauseTime = setInterval("",'1000');
var time=0;
//point tracing section
var ptx = [];
var pty = [];
//click status of legend and quick reference
var legendCS = false;
var quickrefCS = false;
//temporary or dummy variables
var temp=0;
var offset=0;
/*
// for trials during development
function trythis()
{ alert();}
*/
//change simulation specific css content. e.g. padding on top of variable to adjust appearance in variables window
function editcss()
{
$('.variable').css('padding-top','40px');
//$('#legend').css("width",document.getElementById('legendimg').width+"px");
//$('#quickref').css("height",document.getElementById('quickrefimg').height+"px");
}
//start of simulation here; starts the timer with increments of 0.1 seconds
function startsim()
{
simTimeId=setInterval("time=time+0.1; varupdate(); ",'100');
}
// switches state of simulation between 0:Playing & 1:Paused
function simstate()
{
var imgfilename=document.getElementById('playpausebutton').src;
imgfilename = imgfilename.substring(imgfilename.lastIndexOf('/') + 1, imgfilename.lastIndexOf('.'));
if (imgfilename=="bluepausedull")
{
document.getElementById('playpausebutton').src="sins/blueplaydull.png";
clearInterval(simTimeId);
simstatus=1;
$('#theta2spinner').spinner("value",theta2); //to set simulation parameters on pause
pauseTime=setInterval("varupdate();",'100');
}
if (imgfilename=="blueplaydull")
{
time=0;
clearInterval(pauseTime);
document.getElementById('playpausebutton').src="sins/bluepausedull.png";
simTimeId=setInterval("time=time+0.1; varupdate(); ",'100');
simstatus=0;
}
}
// switches state of rotation between 1:CounterClockWise & -1:Clockwise
function rotstate()
{
var imgfilename=document.getElementById('rotationbutton').src;
imgfilename = imgfilename.substring(imgfilename.lastIndexOf('/') + 1, imgfilename.lastIndexOf('.'));
if (imgfilename=="bluecwdull")
{
document.getElementById('rotationbutton').src="sins/blueccwdull.png";
rotstatus=-1;
}
if (imgfilename=="blueccwdull")
{
document.getElementById('rotationbutton').src="sins/bluecwdull.png";
rotstatus=1;
}
}
/*
//Displaying Equations for Quick Reference
function showEquations()
{
if(quickrefCS)
{
$('#quickreficon').css('border', 'double');
$('#quickref').css('width', '0px');
$('#quickref').css('left', '600px');
$('#quickref').css('border', '0px');
quickrefCS=false;
}
else
{
$('#quickreficon').css('border', 'inset red');
$('#quickref').css('width', document.getElementById('quickrefimg').width+"px");
$('#quickref').css("left", 599-document.getElementById('quickrefimg').width+"px");
$('#quickref').css('border', 'solid 1px');
quickrefCS=true;
}
}
//Displaying Legend
function showLegend()
{
if(legendCS)
{
$('#legendicon').css('border', 'double');
$('#legend').css('height', '0px');
$('#legend').css('border', '0px');
legendCS=false;
}
else
{
$('#legendicon').css('border', 'inset red');
$('#legend').css("height", document.getElementById('legendimg').height+"px");
$('#legend').css('border', 'solid 1px');
legendCS=true;
}
}
*/
//Initialise system parameters here
function varinit()
{
varchange();
//Variable r1 slider and number input types
$('#r1slider').slider("value", 80);
$('#r1spinner').spinner("value", 80);
//Variable r2 slider and number input types
$('#r2slider').slider("value", 40);
$('#r2spinner').spinner("value", 40);
//Variable r3 slider and number input types
$('#r3slider').slider("value", 100);
$('#r3spinner').spinner("value", 100);
//Variable r4 slider and number input types
$('#r4slider').slider("value", 60);
$('#r4spinner').spinner("value", 60);
/*//Variable theta2 slider and number input types
$('#theta2slider').slider("value", 40);
$('#theta2spinner').spinner("value", 40);
//Variable omega2 slider and number input types
$('#omega2slider').slider("value", 1);
$('#omega2spinner').spinner("value", 1);*/
}
// Initialise and Monitor variable containing user inputs of system parameters.
//change #id and repeat block for new variable. Make sure new <div> with appropriate #id is included in the markup
function varchange()
{
//Variable r1 slider and number input types
$('#r1slider').slider({ max : 100, min : 0, step : 20 }); // slider initialisation : jQuery widget
$('#r1spinner').spinner({ max : 100, min : 0, step : 20 }); // number initialisation : jQuery widget
// monitoring change in value and connecting slider and number
// setting trace point coordinate arrays to empty on change of link length
$( "#r1slider" ).on( "slide", function( e, ui ) { $('#r1spinner').spinner("value",ui.value); ptx=[]; pty=[]; } );
$( "#r1spinner" ).on( "spin", function( e, ui ) { $('#r1slider').slider("value",ui.value); ptx=[]; pty=[]; } );
$( "#r1spinner" ).on( "change", function() { varchange() } );
//Variable r2 slider and number input types
$('#r2slider').slider({ max : 3, min : 1.5, step : 0.2 }); // slider initialisation : jQuery widget
$('#r2spinner').spinner({ max : 3, min : 1.5, step : 0.2 }); // number initialisation : jQuery widget
// monitoring change in value and connecting slider and number
// setting trace point coordinate arrays to empty on change of link length
$( "#r2slider" ).on( "slide", function( e, ui ) { $('#r2spinner').spinner("value",ui.value); ptx=[]; pty=[]; r2change(); } );
$( "#r2spinner" ).on( "spin", function( e, ui ) { $('#r2slider').slider("value",ui.value); ptx=[]; pty=[]; } );
$( "#r2spinner" ).on( "change", function() { varchange() } );
//Variable r3 slider and number input types
$('#r3slider').slider({ max : 3, min : 1.5, step : 0.2 }); // slider initialisation : jQuery widget
$('#r3spinner').spinner({ max : 3, min : 1.5, step : 0.2 }); // number initialisation : jQuery widget
// monitoring change in value and connecting slider and number
// setting trace point coordinate arrays to empty on change of link length
$( "#r3slider" ).on( "slide", function( e, ui ) { $('#r3spinner').spinner("value",ui.value); ptx=[]; pty=[]; } );
$( "#r3spinner" ).on( "spin", function( e, ui ) { $('#r3slider').slider("value",ui.value); ptx=[]; pty=[]; } );
$( "#r3spinner" ).on( "change", function() { varchange() } );
//Variable r4 slider and number input types
$('#r4slider').slider({ max : 20, min : 1, step : 1 }); // slider initialisation : jQuery widget
$('#r4spinner').spinner({ max : 20, min : 1, step : 1 }); // number initialisation : jQuery widget
// monitoring change in value and connecting slider and number
// setting trace point coordinate arrays to empty on change of link length
$( "#r4slider" ).on( "slide", function( e, ui ) { $('#r4spinner').spinner("value",ui.value); ptx=[]; pty=[]; } );
$( "#r4spinner" ).on( "spin", function( e, ui ) { $('#r4slider').slider("value",ui.value); ptx=[]; pty=[]; } );
$( "#r4spinner" ).on( "change", function() { varchange() } );
/*//Variable theta2 slider and number input types
$('#theta2slider').slider({ max : 360, min : 0, step : 2 }); // slider initialisation : jQuery widget
$('#theta2spinner').spinner({ max : 360, min : 0, step : 2 }); // number initialisation : jQuery widget
// monitoring change in value and connecting slider and number
// setting trace point coordinate arrays to empty on change of link length
$( "#theta2slider" ).on( "slide", function( e, ui ) { $('#theta2spinner').spinner("value",ui.value); ptx=[]; pty=[]; } );
$( "#theta2spinner" ).on( "spin", function( e, ui ) { $('#theta2slider').slider("value",ui.value); ptx=[]; pty=[]; } );
$( "#theta2spinner" ).on( "change", function() { varchange() } );
//Variable omega2 slider and number input types
$('#omega2slider').slider({ max : 1.8, min : 0.2, step : 0.2 }); // slider initialisation : jQuery widget
$('#omega2spinner').spinner({ max : 1.8, min : 0.2, step : 0.2 }); // number initialisation : jQuery widget
// monitoring change in value and connecting slider and number
// setting trace point coordinate arrays to empty on change of link length
$( "#omega2slider" ).on( "slide", function( e, ui ) { $('#omega2spinner').spinner("value",ui.value); ptx=[]; pty=[]; } );
$( "#omega2spinner" ).on( "spin", function( e, ui ) { $('#omega2slider').slider("value",ui.value); ptx=[]; pty=[]; } );
$( "#omega2spinner" ).on( "change", function() { varchange() } );*/
varupdate();
}
//Computing of various system parameters
function varupdate()
{
$('#r2slider').slider("value", $('#r2spinner').spinner('value')); //updating slider location with change in spinner(debug)
$('#r3slider').slider("value", $('#r3spinner').spinner('value'));
$('#r4slider').slider("value", $('#r4spinner').spinner('value'));
$('#r1slider').slider("value", $('#r1spinner').spinner('value'));
m1=$('#r2spinner').spinner("value");
m2=$('#r3spinner').spinner("value");
k=$('#r4spinner').spinner("value");
imp=$('#r1spinner').spinner("value");
var omeg=0;
var vimp=0;
var linmot=0;
var mult = 1;
$('#omega2set').hide();
/*$('#r3slider').slider({max: 6*$('#r2slider').slider('value')});
$('#r3slider').slider({min: 2.5*$('#r2slider').slider('value')});
$('#r3spinner').spinner({max: 6*$('#r2slider').slider('value')});
$('#r3spinner').spinner({min:2.5*$('#r2slider').slider('value')});*/
if(!simstatus)//Play
{
/*
$('#omega2slider').slider("enable");
$('#omega2spinner').spinner("enable");
*/
//$('#r4slider').slider("disable");
//$('#r4spinner').spinner("disable");
//omega2=$('#omega2spinner').spinner("value");
printcomment("",2);
omeg = Math.sqrt((k*(m1+m2))/(m1*m2));
vimp = imp/m1;
phase=phase + (rotstatus*0.1*deg(omeg));
phase=phase%360;
if(phase<0)phase+=360;
linmot = (time*1*vimp*(m1/(m1+m2)));
}
if(simstatus)//Paused
{
//$('#r4slider').slider("enable");
//$('#r4spinner').spinner("enable");
/*$('#omega2slider').slider("disable");
$('#omega2spinner').spinner("disable"); */
//theta2=$('#theta2spinner').spinner("value");
omeg = Math.sqrt((k*(m1+m2))/(m1*m2));
vimp = imp/m1;
phase=phase+(rotstatus*0.1*deg(omeg));
phase=phase%360;
if(phase<0)phase+=360;
phase = 40;
//printcomment("Crank at "+theta2+"° <br>Position of slider from Crank Center = "+roundd(b.xcoord-o.xcoord,2)+"cm",2);
}
ampa = vimp*Math.sqrt(m2*m2*m2*m1/(k*(Math.pow(m2+m1,3))));
ampb = ampa*(m1/m2);
o.xcoord=0;
o.ycoord=0;
a.xcoord=o.xcoord + 0 + linmot - ampa*Math.cos(rad(phase));
a.ycoord=o.ycoord+20;
b.xcoord=o.xcoord + 115 + linmot + ampb*Math.cos(rad(phase));
b.ycoord=o.ycoord+20;
if(b.xcoord > 430)
{
a.xcoord = a.xcoord - 300;
a.ycoord = o.ycoord + 20;
b.xcoord = b.xcoord - 300;
b.ycoord = o.ycoord + 20;
time = 0;
}
draw();
}
//Simulation graphics
function draw()
{
canvas = document.getElementById("simscreen");
ctx = canvas.getContext("2d");
ctx.clearRect(0,0,550,400); //clears the complete canvas#simscreen everytime
pointtrans(o,trans);
pointtrans(a,trans);
pointtrans(b,trans);
//Sliding Base
ctx.beginPath();
ctx.lineWidth=10;
ctx.strokeStyle= "#666666";
ctx.moveTo(25,o.ycoord);
ctx.lineTo(530,o.ycoord);
ctx.stroke();
ctx.closePath();
drawrem(ctx);
drawrect(a,50,30,05,ctx,"#CC9933","#CC9933",1);
drawrect(b,50,30,05,ctx,"#CC9933","#CC9933",1);
var ceil_to_floor = 300;
var floor_width = 10;
var floor_length = 100;
var amt = 8;//number of zigzags
var inc = Math.ceil((b.xcoord - a.xcoord )/ amt);
var spring_width = 6;//width of the spring
var sp_x = a.xcoord + 25;
var sp_y = a.ycoord;
//var inc = Math.ceil((o.ycoord + ceil_to_floor + floor_width - a.ycoord )/ amt);
for (; sp_x < b.xcoord -inc - 19 ; sp_x += inc)
{
ctx.strokeStyle = 'blue';
ctx.beginPath();
ctx.lineWidth=1;
ctx.moveTo(sp_x, sp_y-spring_width);
ctx.lineTo(sp_x+inc/2, sp_y);
ctx.lineTo(sp_x+inc, sp_y-spring_width);
ctx.stroke();
ctx.closePath();
}
}
function drawrem(context)
{
// positioning dimension display
if(theta2%360<=180)
offset=-45;
else
offset=20;
// Position Analysis Title
context.save();
context.lineWidth=1;
context.font="35px 'Comic Sans MS'";
context.fillStyle="#000000";
context.fillText("Impulse response", 133,75);
context.restore();
}
// prints comments passed as 'commenttext' in location specified by 'commentloc' in the comments box;
// 0 : Single comment box, 1 : Left comment box, 2 : Right comment box
function printcomment(commenttext,commentloc)
{
if(commentloc==0)
{
document.getElementById('commentboxright').style.visibility='hidden';
document.getElementById('commentboxleft').style.width='570px';
document.getElementById('commentboxleft').innerHTML = commenttext;
}
else if(commentloc==1)
{
document.getElementById('commentboxright').style.visibility='visible';
document.getElementById('commentboxleft').style.width='285px';
document.getElementById('commentboxleft').innerHTML = commenttext;
}
else if(commentloc==2)
{
document.getElementById('commentboxright').style.visibility='visible';
document.getElementById('commentboxleft').style.width='285px';
document.getElementById('commentboxright').innerHTML = commenttext;
}
else
{
document.getElementById('commentboxright').style.visibility='hidden';
document.getElementById('commentboxleft').style.width='570px';
document.getElementById('commentboxleft').innerHTML = "<center>please report this issue to adityaraman@gmail.com</center>";
// ignore use of deprecated tag <center> . Code is executed only if printcomment function receives inappropriate commentloc value
}
}
</script>
</head>
<body onload="editcss(); startsim(); varinit(); simstate();simstate();">
<!-- DND-Verifying JavaScript is enabled in browser -->
<noscript>
The simulation requires JavaScript to be enabled.
</noscript>
<!--
Author : Sai Aditya Raman
send an email to adityaraman09@gmail.com for any communication
-->
<!-- Defining Container for Simulation -->
<div id="container">
<!-- Replace TITLE with simulation title to be displayed next to logo-->
<div id="simtitle">Impulse response of a 2DOF system</div>
<!-- Region for simulation display -->
<canvas id="simscreen" height="400px" width="550px"></canvas>
<div id="titleincanvas"></div>
<div id="datatable1" class="datatable"></div>
<div id="datatable2" class="datatable"></div>
<!-- Equation Reference and Legend for Graphs etc
<div id="quickreficon" title="Equations" onclick="showEquations();"></div>
<div id="quickref"><img id="quickrefimg" src="sins/tick.png"></img></div>
<div id="legendicon" title="Legend" onclick="showLegend();"></div>
<div id="legend"><img id="legendimg" src="sins/tick.png"></img></div>
-->
<!-- Region for comment / question prompts -->
<div id="commentboxleft"></div>
<div id="commentboxright"></div>
<!-- DND-Title of region for variables display -->
<div id="vartitle">VARIABLES</div>
<!-- Region for Input parameters of simulation -->
<div id="variables">
<!-- System variable input from user through slider and spinner jQuery widgets
repeat block with new variable name and new id for widgets-->
<div class="variable">
<div class="varname"><p title="Mass 1">m1 (kg)</p>
<input id="r2spinner"></input></div>
<div id="r2slider"></div>
</div>
<div class="variable">
<div class="varname"><p title="Mass 2">m2 (kg)</p>
<input id="r3spinner"></input></div>
<div id="r3slider"></div>
</div>
<div id = "theta2set" class="variable">
<div class="varname"><p title=''>k (N/m) </p>
<input id="r4spinner"></input></div>
<div id="r4slider"></div>
</div>
<div id = "theta2set" class="variable">
<div class="varname"><p title='Impulse'>Impulse (N-s) </p>
<input id="r1spinner"></input></div>
<div id="r1slider"></div>
</div>
<div id = "omega2set" class="variable">
<div class="varname"><p title='Input Angular Velocity'>ω (rad/s) </p>
<input id="omega2spinner"></input></div>
<div id="omega2slider"></div>
</div>
</div>
<!-- DND-Title of region for controls display -->
<div id="ctrltitle">CONTROLS</div>
<!-- Region for User controls of simulation -->
<div id="controls">
<img id="playpausebutton" onclick="simstate()" src="sins/bluepausedull.png" />
<img id="rotationbutton" onclick="rotstate()" src="sins/bluecwdull.png" />
<!--
<div class="usercheck">
<!-- Point tracing
<input type="checkbox" id="trace" ></input> <p>Trace Mid Point</p>
<!-- Reset Parameters
<input type="button" id="resetparams" value="Reset" onclick="varinit();"></input> -->
</div>
<!-- DND-Copyright display-->
<div id="copyright" title="Developer : Supreeth Dev"> Copyright © SOLVE The Virtual Lab @ NITK Surathkal </div>
</div><!-- Container div close -->
<!-- For trials during development
write in trythis() function the outputs that you want to check.
<input type='button' onclick='trythis()' value="try me"></input>
-->
</body>
</html>