-
Notifications
You must be signed in to change notification settings - Fork 19
/
home.html
320 lines (233 loc) · 9.76 KB
/
home.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
<!--
3jsbot code stencil
Implementation of robot kinematics, control, and decision making
in HTML5/JavaScript and threejs
@author odestcj / https://github.com/odestcj
Forgive my coding style. I am still a typedef struct kind of guy.
Need to get a handle on all of the global variables... in the future.
-->
<html>
<body>
<!-- //////////////////////////////////////////////////
///// JAVASCRIPT INCLUDES
////////////////////////////////////////////////// -->
<!-- threejs - https://github.com/mrdoob/three.js/ - for 3D rendering -->
<script src="js/three.min.js"></script>
<!-- threejs camera controls helpers -->
<script src="js/TrackballControls.js"></script>
<script src="js/OrbitControls.js"></script>
<!-- threejs keyboard input helper -->
<script src="js/THREEx.KeyboardState.js"></script>
<!-- numericjs - https://github.com/sloisel/numeric - for matrix inversion -->
<script src="js/numeric-1.2.6.js"></script>
<!-- definition of robot kinematics -->
<!-- CS148: change this script pointer to change the robot definition -->
<!--
<script src="robots/robot_urdf_example.js"></script>
<script src="robots/robot_crawler.js"></script>
-->
<script src="robots/robot_br2.js"></script>
<!-- definition of robot's world (ie, planning scene) -->
<!--
<script src="worlds/world_empty.js"></script>
<script src="worlds/world_basic.js"></script>
<script src="worlds/world_s.js"></script>
<script src="worlds/world_random.js"></script>
<script src="worlds/world_local_minima.js"></script>
-->
<!-- 3jsbot includes -->
<script src="3jsbot.js"></script>
<script src="3jsbot_threejs.js"></script>
<script src="3jsbot_userinput.js"></script>
<!-- FK/drawing assignment
<script src="3jsbot_forward_kinematics.js"></script>
<script src="3jsbot_matrix.js"></script>
-->
<!-- FK/joint control assignment
<script src="3jsbot_quaternion.js"></script>
<script src="3jsbot_controls.js"></script>
<script src="3jsbot_pd_control.js"></script>
-->
<!-- IK assignment
<script src="3jsbot_inverse_kinematics.js"></script>
-->
<!-- planning assignment
<script src="3jsbot_rrt_connect.js"></script>
<script src="3jsbot_collision.js"></script>
-->
<script>
//////////////////////////////////////////////////
///// MAIN FUNCTION CALLS
//////////////////////////////////////////////////
// 3jsbot uses init() to initialize threejs scene, user input, and robot kinematics
// CS148: do not change this function call. init() will call my_init(), where you can add your own initialization methods
init();
// 3jsbot uses animate() as the main animation loop maintained by threejs
// CS148: do not change this function call. animate() will call my_animate(), where you can add your own animation methods
animate();
// CS148: my_animate is where your robot's controls and movement are updated over time
function my_init() {
console.log(JSON.stringify(robot)); // just to see initial starting robot object
init_robot(); // initialize robot kinematics
// CS148: INITIAL GETTING FAMILIAR CODE (START HERE)
var local_spacing = 0.9; // variables declared with "var" are local
global_spacing = 0.9; // variables declared with "var" are global
my_object = {}; // objects can be created with braces
my_object.university = "Brown"; // create object property with an assignment
my_object.course_number = 148;
my_object["subject"] = "robotics";
if (typeof copied_object === 'undefined') { // if my_object does not already exist
console.log(my_object); // check it out on the console
}
// objects are copied by reference
copied_object = my_object;
copied_object.subject = "autonomous_robotics"; // what is my_object.subject on the console?
my_array = [8, 6, 7, 5, 3, 0, 9];
my_array[6] = 'ni-i-i-ine';
var i; // local variable
for (i=0;i<my_array.length;i++) {
console.log(my_array[i]);
}
textbar = document.createElement('div');
textbar.style.position = 'absolute';
//textbar.style.zIndex = 1; // if you still don't see the label, try uncommenting this
textbar.style.width = window.innerWidth-10;
textbar.style.height = 20;
textbar.style.backgroundColor = "black";
textbar.style.color = "#00ff00";
textbar.innerHTML = "Welcome to 3jsbot. I want to see some text. Too much coffee?";
textbar.style.top = 10 + 'px';
textbar.style.left = 10 + 'px';
document.body.appendChild(textbar);
// CS148: INITIAL GETTING FAMILIAR CODE (END HERE)
}
function init_robot() {
// ASSUME: robot kinematics are described separate js file (eg., "robot_urdf_example.js")
// initialize and create threejs mesh objects for robot links
init_robot_links();
// initialize robot joints and create threejs mesh objects for robot joints and form kinematic hiearchy
init_robot_joints();
// initialize inverse kinematics target location
ik_target = [[0],[0.8],[1.0],[1]];
/* uncomment for motion planning execution (motion planning)
// initialize flags for executing planner
generating_motion_plan = false;
generate_motion_plan = false;
*/
/* uncomment for collision testing with world (motion planning)
// set scene for planner
robot_set_planning_scene();
*/
}
// CS148: nothing to add here
function init_robot_links() {
for (x in robot.links) {
robot.links[x].name = x;
}
// CS148: do not remove, this is used to create threejs geometries
init_robot_links_geoms();
// initialize controls for robot base link
robot.control = {xyz: [0,0,0], rpy:[0,0,0]};
}
// CS148: add kinematic hierarchy in this function
function init_robot_joints() {
// build kinematic hierarchy by looping over each joint in the robot
// (object fields can be index through array-style indices, object[field] = property)
// and insert threejs scene graph (each joint and link are directly connect to scene root)
// NOTE: kinematic hierarchy is maintained independently by this code, not threejs
// NOTE: simpleApplyMatrix can be used to set threejs transform for a rendered object
var x,tempmat;
for (x in robot.joints) {
// give the joint its name as an id
robot.joints[x].name = x;
console.log(robot.joints[x].child); // to print the child link of joints for example
// CONSTRUCT KINEMATIC HIERARCHY
}
// CS148: do not remove, this is used to create threejs geometries
init_robot_joints_geoms();
}
//////////////////////////////////////////////////
///// ANIMATION AND INTERACTION LOOP
//////////////////////////////////////////////////
// CS148: my_animate is where your robot's controls and movement are updated over time
function my_animate() {
// CS148: INITIAL GETTING FAMILIAR CODE (START HERE)
// keyboard is threejs helper for reading keyboard state
if (keyboard.pressed("x"))
textbar.innerHTML = "moving up"; // make the pieces move up
else if (keyboard.pressed("z"))
textbar.innerHTML = "zzzzzz"; // stop jittering the pieces
else {
// make the pieces jitter, and say something interesting
}
// jsmat will be used to transform a 3D object to a specific location in the world
// we will represent matrices with index notation
// such that matrix[row][column] is the indexing
var jsmat = [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
];
var spacing = 0.9; // variables declared with "var" are local
// jsmat[0][3] corresponds to the x-coordinate of the position for the 3D object
// Object.key(object) is a rough way to get number of keys in an object
jsmat[0][3] = -Object.keys(robot.joints).length*spacing/2;
// iterate over each joint of the robot independently
for (x in robot.joints) {
// jsmat[1][3] corresponds to the y-coordinate of the position for the 3D object
jsmat[1][3] = 2+Math.random()*0.02; // Math object has lots of helpful functions
// jsmat[2][3] corresponds to the z-coordinate of the position for the 3D object
jsmat[2][3] = Math.random()*0.02;
// convert 2D JavaScript array to the threejs format
threemat = matrix_2Darray_to_threejs(jsmat);
// transform
simpleApplyMatrix(robot.joints[x].geom,threemat);
jsmat[0][3] += spacing;
}
// iterate over each link of the robot independently
jsmat[0][3] = -Object.keys(robot.joints).length*spacing/2;
for (x in robot.links) {
jsmat[1][3] = 1+Math.random()*0.02;
jsmat[2][3] = Math.random()*0.02;
threemat = matrix_2Darray_to_threejs(jsmat);
simpleApplyMatrix(robot.links[x].geom,threemat);
jsmat[0][3] += spacing;
}
// CS148: INITIAL GETTING FAMILIAR CODE (END)
// ROBOT DYNAMICS
// apply robot controls to robot configuration (assuming pure kinematics for now)
//robot_apply_controls();
// DRAW ROBOT
// forward kinematics over robot links
//robot_forward_kinematics();
// HANDLE USER CONTROLS
// handle user input
user_input();
// UPDATE AUTONOMOUS CONTROLS
// proportional-derivative control
/*
if (update_pd)
robot_pd_control();
update_pd = false;
*/
// inverse kinematics
//robot_inverse_kinematics(ik_target, "forearm_right_yaw", ik_local);
// configuration space motion planning
/*
if ((generate_motion_plan) && (!generating_motion_plan)) {
robot_rrt_planner_init();
generating_motion_plan = true;
generate_motion_plan = false;
}
if (generating_motion_plan) {
rrt_result = robot_rrt_planner_iterate();
if (rrt_result === "reached") {
generating_motion_plan = false;
}
}
*/
}
</script>
</body>
</html>