-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
232 lines (215 loc) · 7.25 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Foward Kinematics</title>
<script type="text/javascript" src="jquery.js"></script>
<style>
.container {
border:2px solid gray;
width: 270px;
padding: 15px;
margin:5px;
height: 110px;
font-family: sans-serif;
}
span {
padding-left: 15px;
}
input[type="text"] {
padding-left:10px;
}
canvas {
float: left;
border: 2px solid gray;
margin: 5px;
padding: 10px;
}
.info{
float:left;
padding:10px;
padding-top: 15px;
}
.circle{
border-radius: 50%;
width:10px;
height:10px;
float:left;
}
#theta1{
background-color: red;
}
#theta2{
background-color: blue;
}
.point{
margin-bottom: 10px;
float:left;
}
.pt{
top: -4px;
position:relative;
}
.title{
padding-bottom: 10px;
}
</style>
</head>
<body>
<div class="info">
<div class="container"><div class="title">Origin to Elbow:</div>
Theta 1: <input class="slider" id="0" type="range" name="points" value="0" min="0" max="360" step=".5"><span id="slidenumber0">0°</span>
<p>Link 1: <input type="text" id="0" style="text-align:left;" placeholder="70"></p>
<div class="point">
<div class="circle" id="theta1" style="clear:both"></div><span class="pt" id="elbowPt">:</span>
</div>
</div>
<div class="container"><div class="title">Elbow to End Effector:</div>
Theta 2: <input class="slider" type="range" id="1" name="points" value="0" min="0" max="360" step=".5"><span id="slidenumber1">0°</span>
<p>Link 2: <input type="text" id="1" style="text-align:left;" placeholder="70">
</p>
<div class="point">
<div class="circle" id="theta2" style="clear:both"></div><span class="pt" id="endPt">:</span>
</div>
</div>
</div>
<canvas id="canvas" style="z-index: 1; "height="300" width="300"></canvas>
</body>
<script>
$(function() {
$(document).ready(function(){
$("[type=range]").change(function(){
var newval=$(this).val() + "°";
$("#slidenumber" + $(this).attr("id")).html($(this).val()+ "°");
system.thetas[$(this).attr("id")] = $(this).val()*Math.PI/180;
getElbow(); getEndEffector(); drawPointsLines();
})
$("[type=range]").mousemove( function(e){
$("#slidenumber" + $(this).attr("id")).html($(this).val() + "°");
system.thetas[$(this).attr("id")] = $(this).val()*Math.PI/180;
getElbow(); getEndEffector(); drawPointsLines();
});
});
$( "input[type='text']" ).change(function() {
var newValue = $(this).val();
if (newValue > 70) {newValue = 70; $(this).val("70");}
else if (newValue < 5) {newValue = 5; $(this).val("5");}
system.l[$(this).attr("id")] = newValue
getElbow(); getEndEffector(); drawPointsLines();
});
var newval=$(this).val() + "°";
$("#slidenumber" + $(this).attr("id")).html($(this).val()+ "°");
system.thetas[$(this).attr("id")] = $(this).val()*Math.PI/180;
getElbow(); getEndEffector(); drawPointsLines();
});
// start with default values
var system = new createKinematics(0,0,70,70);
var rotation1 = getRotationMatrix(system.thetas[0]);
var rotation2 = getRotationMatrix(system.thetas[1]);
var translation1 = getTranslationMatrix(system.l[0]);
var translation2 = getTranslationMatrix(system.l[1]);
// step1 = rotation1 x translation1
// step1 x [0,0,1]
var elbowPt = getElbow()[1];
var endEffector = getEndEffector();
system.points = [elbowPt,endEffector];
drawPointsLines();
function drawPointsLines(){
var c = document.getElementById("canvas");
var height = c.height;
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
ctx.beginPath();
ctx.moveTo(c.width/2,0);
ctx.lineTo(c.width/2,c.height);
ctx.strokeStyle="gray";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0,c.height/2);
ctx.lineTo(c.width,c.height/2);
ctx.strokeStyle="gray";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(150,height-150);
ctx.lineTo(system.points[0][0]+150,height-system.points[0][1]-150);
ctx.lineTo(system.points[1][0]+150,height-system.points[1][1]-150);
ctx.strokeStyle="black";
ctx.stroke();
ctx.beginPath();
ctx.fillStyle = "black";
ctx.arc(150, height-150, 3, 0, 2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = "red";
ctx.arc(system.points[0][0]+150, height-system.points[0][1]-150, 3, 0, 2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.arc(system.points[1][0]+150, height-system.points[1][1]-150, 3, 0, 2 * Math.PI);
ctx.fillStyle = "blue";
ctx.fill();
updatePoints()
}
function updatePoints(){
$("#elbowPt").html(': ('+system.points[0][0].toPrecision(3)+','+system.points[0][1].toPrecision(3)+')');
$("#endPt").html(': ('+system.points[1][0].toPrecision(3)+','+system.points[1][1].toPrecision(3)+')');
}
// object that holds all the math for calculation rotation and transition matrix and matrix multiplication
// starts with default values of 0, 0, 100,and 100
function createKinematics(theta1,theta2,l1,l2){
this.links = 2;
this.thetas = [theta1,theta2];
this.l = [l1,l2];
this.points = [0,0];
}
function getElbow(){
rotation1 = getRotationMatrix(system.thetas[0]);
translation1 = getTranslationMatrix(system.l[0]);
var step1 = threeByThreeMult(rotation1,translation1);
var elbowPt = threeByOneMult(step1,[0,0,1]);
system.points[0]=elbowPt;
return [step1,elbowPt];
}
function getEndEffector(){
rotation2 = getRotationMatrix(system.thetas[1]);
translation2 = getTranslationMatrix(system.l[1]);
var elbowPt = getElbow()[1];
// step = threeByThreeMult(step,rotation2);
step = threeByThreeMult(rotation2,translation2);
var endEffector = threeByOneMult(step,[0,0,1]);
system.points[1]=[endEffector[0] + elbowPt[0],endEffector[1] + elbowPt[1],1];
return endEffector;
}
// the rotational matrix
function getRotationMatrix(theta){
var rotation = [[Math.cos(theta),-1*Math.sin(theta),0],
[Math.sin(theta),Math.cos(theta),0],
[0,0,1]];
return rotation;
}
// the translation matrix
function getTranslationMatrix(l){
var translation = [[1,0,l],
[0,1,0],
[0,0,1]]
return translation;
}
// return 3x3 matrix multiplication
function threeByThreeMult(m1,m2){
var result = [[],[],[]];
for (var i = 0; i<=2; i++){
for (var j = 0; j<=2; j++){
result[i][j] = m1[i][0]*m2[0][j] + m1[i][1]*m2[1][j] + m1[i][2]*m2[2][j];
}
}
return result;
}
// return 3x3 * 3x1 matrix multiplication
function threeByOneMult(m1,m2){
var result = [];
for (var i = 0; i<=2; i++){
result[i] = m1[i][0]*m2[0] + m1[i][1]*m2[1] + m1[i][2]*m2[2];
}
return result;
}
</script>
</html>