-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsim-robo.js
More file actions
174 lines (168 loc) · 4.68 KB
/
sim-robo.js
File metadata and controls
174 lines (168 loc) · 4.68 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
import { getDistanceFromWall } from "./util2d.js";
export class SimRobo extends HTMLElement {
constructor() {
super();
const create = () => {
const img = new Image();
img.src = "./sim-robo.png"; // 612x540
img.width = 306 / 4;
img.height = 270 / 4;
return img;
};
this.img = create();
this.img.style.position = "absolute";
this.motorr = 0;
this.motorl = 0;
this.speed = 2;
this.speeddir = 2; // deg
this.direction = 90;
this.t = null;
this.reset();
const img2 = create();
img2.style.filter = "grayscale(100%)";
img2.width = img2.width / 2;
img2.height = img2.height / 2;
this.appendChild(img2);
// drag
this.dragging = false;
let offsetX, offsetY;
const img = this.img;
img.addEventListener('mousedown', (e) => {
this.dragging = true;
offsetX = e.clientX - img.offsetLeft;
offsetY = e.clientY - img.offsetTop;
img.style.cursor = 'grabbing';
e.preventDefault();
});
document.addEventListener('mousemove', (e) => {
if (this.dragging) {
const x = e.clientX - offsetX;
const y = e.clientY - offsetY;
img.style.left = `${x}px`;
img.style.top = `${y}px`;
img.posx = x;
img.posy = y;
this.calcDistanceFromWall();
}
});
document.addEventListener('mouseup', () => {
if (this.dragging) {
this.dragging = false;
img.style.cursor = 'move';
}
});
//
this.activeflg = false;
setInterval(() => {
if (this.activeflg) {
if (!this.parentElement) {
document.body.removeChild(this.img);
this.activeflg = false;
}
} else {
if (this.parentElement) {
this.reset();
document.body.appendChild(this.img);
this.activeflg = true;
}
}
}, 200);
}
reset() {
this.img.posx = innerWidth / 2 - this.img.width / 2;
this.img.posy = innerWidth / 2 - this.img.height / 2;
this.img.style.left = this.img.posx + "px";
this.img.style.top = this.img.posy + "px";
this.calcDistanceFromWall();
}
out(n) {
if (n & 1) {
this.motorr = -1;
} else if (n & 2) {
this.motorr = 1;
} else {
this.motorr = 0;
}
if (n & 16) {
this.motorl = 1;
} else if (n & 32) {
this.motorl = -1;
} else {
this.motorl = 0;
}
this.move();
}
move() {
const move = this.motorl != 0 || this.motorr != 0;
if (!this.t && move) {
this.t = setInterval(() => {
if (this.dragging) return;
this.direction += (this.motorl - this.motorr) * this.speeddir;
const step = (this.motorr + this.motorl) * this.speed;
const th = this.direction / 180 * Math.PI;
const dx = Math.cos(th) * step;
const dy = Math.sin(th) * step;
this.img.posx += dx;
this.img.posy += dy;
if (this.img.posx > innerWidth - this.img.width) {
this.img.posx = innerWidth - this.img.width;
} else if (this.img.posx < 0) {
this.img.posx = 0;
}
if (this.img.posy > innerHeight - this.img.height) {
this.img.posy = innerHeight - this.img.height;
} else if (this.img.posy < 0) {
this.img.posy = 0;
}
this.img.style.left = (this.img.posx + dx) + "px";
this.img.style.top = (this.img.posy + dy) + "px";
this.img.style.transform = `rotate(${this.direction - 90}deg)`;
this.img.style.transformOrigin = `center center`;
this.calcDistanceFromWall();
if (this.ex) {
this.ex.setStateIN(2, this.distancesensor);
}
}, 100);
} else if (this.t && !move) {
clearInterval(this.t);
this.t = null;
}
}
calcDistanceFromWall() {
const x = this.img.posx;
const y = this.img.posy;
const th = this.direction / 180 * Math.PI + Math.PI;
const len = getDistanceFromWall(x, y, th, 0, 0, innerWidth, innerHeight);
this.distance = len;
this.distancesensor = Math.max(Math.floor(1023 - len * 10), 0);
//console.log(len, this.direction, this.distancesensor);
}
setSegments(n) {
// pwm
let pwmmode = false;
{
const v = this.ex.getPWMValue(2);
const len = this.ex.getPWMLen(2);
if (len > 0) {
this.motorr = (v > len ? len : v) / len;
pwmmode = true;
}
}
{
const v = this.ex.getPWMValue(5);
const len = this.ex.getPWMLen(5);
if (len > 0) {
this.motorl = (v > len ? len : v) / len;
pwmmode = true;
}
}
if (!pwmmode) {
this.out(n);
}
this.move();
}
setIchigoJamCore(ex) {
this.ex = ex;
}
}
customElements.define("sim-robo", SimRobo);