-
Notifications
You must be signed in to change notification settings - Fork 11
/
demo4.ts
345 lines (300 loc) · 7.77 KB
/
demo4.ts
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
import { Sim3D, SimulatorConfig, RobotSpecs, RobotBuilder } from "../index";
import { GUI } from "dat.gui";
import { RobotHandle } from "../engine/handles";
import { ISimulatorEvent } from "../engine/specs/CoreSpecs";
let gui: GUI;
let simulator: Sim3D;
let robot: RobotHandle;
const LEFT_DRIVE_CHANNEL = 0;
const RIGHT_DRIVE_CHANNEL = 1;
const COLOR_SENSOR_CHANNEL = 1;
const DISTANCE_SENSOR_CHANNEL = 0;
const setting = {
speed: 1,
};
const simConfig: SimulatorConfig = {
defaultWorld: {
xLength: 6,
zLength: 6,
perimeter: { height: 1, thickness: 0.3 },
camera: {
position: {
x: 0,
y: 2,
z: 2,
},
},
},
};
window.onload = main;
window.onresize = () => {
if (simulator) {
simulator.onresize();
}
};
const demoOptions = {
debugMode: false,
cameraMode: "position",
};
function getHexColor(color: number): string {
return color.toString(16).padStart(6, "0");
}
function main() {
const canvas = <HTMLCanvasElement>document.getElementById("demo1");
const debug_mode_default = false;
//debug_mode_default = true;
simulator = new Sim3D(canvas, simConfig);
simulator.onresize();
simulator.beginRendering();
simulator.setDebugMode(debug_mode_default);
gui = new GUI();
(window as any).gui = gui; // eslint-disable-line @typescript-eslint/no-explicit-any
const debugFolder = gui.addFolder("Debug Options");
const debugModeController = debugFolder
.add(demoOptions, "debugMode")
.setValue(debug_mode_default);
debugModeController.onChange((val) => {
simulator.setDebugMode(val);
});
const settingFolder = gui.addFolder("Settings");
settingFolder.add(setting, "speed", 0, 5, 0.1);
const robotBuilder = new RobotBuilder.Builder();
const wheel = new RobotBuilder.WheelBuilder(0.1, 0.03)
.setMountPoint(RobotSpecs.WheelMountingPoint.LEFT_FRONT)
.setMountOffset({ x: -0.0075, y: -0.05, z: 0.033 });
const motor = new RobotBuilder.MotorBuilder()
.setChannel(LEFT_DRIVE_CHANNEL)
.setMaxForce(1);
const colorSensor = new RobotBuilder.ColorSensorBuilder(COLOR_SENSOR_CHANNEL);
colorSensor.setMountFace(RobotSpecs.SensorMountingFace.BOTTOM);
const distanceSensor = new RobotBuilder.DistanceSensorBuilder(
DISTANCE_SENSOR_CHANNEL
);
distanceSensor.setMountFace(RobotSpecs.SensorMountingFace.FRONT);
distanceSensor.setMaxRange(20.0);
robotBuilder
.setId("robo")
.setDimensions({ x: 0.225, y: 0.125, z: 0.255 })
.addWheel("left-drive", wheel)
.addWheel(
"left-drive",
wheel
.copy()
.setMountPoint(RobotSpecs.WheelMountingPoint.LEFT_REAR)
.setMountOffset({ x: -0.0075, y: -0.05, z: -0.033 })
)
.addWheel(
"right-drive",
wheel
.copy()
.setMountPoint(RobotSpecs.WheelMountingPoint.RIGHT_FRONT)
.setMountOffset({ x: 0.0075, y: -0.05, z: 0.033 })
)
.addWheel(
"right-drive",
wheel
.copy()
.setMountPoint(RobotSpecs.WheelMountingPoint.RIGHT_REAR)
.setMountOffset({ x: 0.0075, y: -0.05, z: -0.033 })
)
.addMotor("left-drive", motor)
.addMotor("right-drive", motor.copy().setChannel(RIGHT_DRIVE_CHANNEL))
.addComplexSensor(colorSensor)
.addBasicSensor(distanceSensor);
const spec = robotBuilder.generateSpec();
robot = simulator.addRobot(spec);
simulator.addZone({
type: "zone",
zoneId: "A",
zoneShape: {
type: "rectangle",
xLength: 2,
zLength: 1,
},
initialPosition: {
x: 1,
y: -2,
},
});
simulator.addZone({
type: "zone",
zoneId: "B",
zoneShape: {
type: "rectangle",
xLength: 1,
zLength: 1,
},
initialPosition: {
x: -0.5,
y: -2,
},
baseColor: 0xff0000,
});
simulator.addZone({
type: "zone",
zoneId: "C",
zoneShape: {
type: "rectangle",
xLength: 3,
zLength: 0.1,
},
initialPosition: {
x: 1,
y: -2.4,
},
baseColor: 0x443355ff,
});
simulator.addZone({
type: "zone",
zoneId: "D",
zoneShape: {
type: "rectangle",
xLength: 1,
zLength: 1,
},
initialPosition: {
x: 0,
y: 2,
},
baseColor: 0x0000ff,
});
simulator.addZone({
type: "zone",
zoneId: "E",
zoneShape: {
type: "rectangle",
xLength: 1,
zLength: 1,
},
initialPosition: {
x: 0,
y: 1,
},
baseColor: 0x00ff00,
});
simulator.addZone({
type: "zone",
zoneId: "F",
zoneShape: {
type: "rectangle",
xLength: 1,
zLength: 1,
},
initialPosition: {
x: 1,
y: 1,
},
baseColor: 0xff0000,
});
simulator.addZone({
type: "zone",
zoneId: "G",
zoneShape: {
type: "rectangle",
xLength: 1,
zLength: 1,
},
initialPosition: {
x: 1,
y: 2,
},
baseColor: 0xff00ff,
});
const el = window.document.body;
el.addEventListener("keydown", keyListener);
el.addEventListener("keyup", keyListener);
const eventListEl = window.document.createElement("ol");
const clearEl = window.document.createElement("button");
el.appendChild(eventListEl);
el.appendChild(clearEl);
eventListEl.style.fontFamily = "monospace";
clearEl.textContent = "Clear";
clearEl.addEventListener("click", (e) => {
e.preventDefault();
e.stopImmediatePropagation();
eventListEl.innerHTML = "";
});
const distanceSensorDiv = window.document.createElement("div");
el.appendChild(distanceSensorDiv);
window.setInterval(() => {
distanceSensorDiv.innerText = JSON.stringify(
robot.getAnalogInput(DISTANCE_SENSOR_CHANNEL)
);
}, 500);
const colorSensorDiv = window.document.createElement("div");
el.appendChild(colorSensorDiv);
window.setInterval(() => {
// TODO: Make it display the actual color, or at least rgb value
const colorSensorValue = robot.getComplexSensorValue(
COLOR_SENSOR_CHANNEL,
"ColorSensor"
);
const color =
"color" in colorSensorValue
? getHexColor(colorSensorValue.color)
: "ffffff";
console.log(`color is ${color}`);
colorSensorDiv.style.backgroundColor = `#${color}`;
colorSensorDiv.style.width = "30px";
colorSensorDiv.style.height = "30px";
colorSensorDiv.style.borderStyle = "solid";
colorSensorDiv.style.borderColor = "#000000";
}, 500);
simulator.addListener("simulation-event", (e) => {
const event = e as ISimulatorEvent;
const eventEl = window.document.createElement("li");
eventEl.innerText = JSON.stringify(event.data);
const eventTypeEl = window.document.createElement("i");
eventTypeEl.innerText = event.type;
eventEl.prepend(eventTypeEl);
// eventListEl.appendChild(eventEl);
});
}
enum Drive {
LEFT,
RIGHT,
BOTH,
}
enum Direction {
POS,
NEG,
}
function keyListener(this: HTMLElement, e: KeyboardEvent) {
console.assert(e.type === "keydown" || e.type == "keyup");
const isDown = e.type == "keydown";
let drive: Drive = null;
let direction: Direction = null;
switch (e.code) {
case "KeyW":
drive = Drive.BOTH;
direction = Direction.POS;
break;
case "KeyA":
drive = Drive.RIGHT;
direction = Direction.POS;
break;
case "KeyS":
drive = Drive.BOTH;
direction = Direction.NEG;
break;
case "KeyD":
drive = Drive.LEFT;
direction = Direction.POS;
break;
default:
return;
}
e.preventDefault();
e.stopImmediatePropagation();
if (e.repeat) {
return;
}
let motorPower = direction === Direction.POS ? 1 : -1;
motorPower *= isDown ? setting.speed : 0;
if (drive === Drive.LEFT || drive === Drive.BOTH) {
robot.setMotorPower(LEFT_DRIVE_CHANNEL, motorPower);
}
if (drive === Drive.RIGHT || drive === Drive.BOTH) {
robot.setMotorPower(RIGHT_DRIVE_CHANNEL, motorPower);
}
}