-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.html
More file actions
427 lines (397 loc) · 11.6 KB
/
main.html
File metadata and controls
427 lines (397 loc) · 11.6 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>循迹小车可视化编程</title>
<script src="https://unpkg.com/blockly/blockly.min.js"></script>
<script src="https://unpkg.com/blockly/blocks_compressed.js"></script>
<script src="https://unpkg.com/blockly/javascript_compressed.js"></script>
<script src="https://unpkg.com/blockly/msg/zh-hans.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.header {
background-color: #2c3e50;
color: white;
padding: 15px;
text-align: center;
}
.container {
display: flex;
height: calc(100vh - 100px);
}
.blockly-container {
flex: 1;
height: 100%;
}
.code-preview {
width: 400px;
padding: 15px;
background-color: #2c3e50;
color: white;
overflow-y: auto;
}
.controls {
padding: 15px;
background-color: #34495e;
text-align: center;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 10px 20px;
margin: 5px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #2980b9;
}
pre {
background-color: #1a252f;
padding: 10px;
border-radius: 4px;
font-size: 14px;
white-space: pre-wrap;
}
.blocklyTreeLabel {
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<div class="header">
<h1>循迹小车可视化编程</h1>
</div>
<div class="controls">
<button onclick="generateCode()">生成代码</button>
<button onclick="downloadCode()">下载Arduino代码</button>
<button onclick="resetWorkspace()">重置工作区</button>
</div>
<div class="container">
<div id="blocklyDiv" class="blockly-container"></div>
<div class="code-preview">
<h3>生成的Arduino代码:</h3>
<pre id="generatedCode"></pre>
</div>
</div>
<script>
// 初始化Blockly工作区
const workspace = Blockly.inject('blocklyDiv', {
toolbox: {
"kind": "categoryToolbox",
"contents": [
{
"kind": "category",
"name": "传感器",
"colour": 230,
"contents": [
{
"kind": "block",
"type": "sensor_read"
},
{
"kind": "block",
"type": "sensor_condition"
}
]
},
{
"kind": "category",
"name": "电机控制",
"colour": 120,
"contents": [
{
"kind": "block",
"type": "motor_control"
},
{
"kind": "block",
"type": "motor_direction"
},
{
"kind": "block",
"type": "motor_speed"
}
]
},
{
"kind": "category",
"name": "逻辑控制",
"colour": 210,
"contents": [
{
"kind": "block",
"type": "controls_if"
},
{
"kind": "block",
"type": "logic_compare"
},
{
"kind": "block",
"type": "logic_operation"
}
]
},
{
"kind": "category",
"name": "循环",
"colour": 60,
"contents": [
{
"kind": "block",
"type": "controls_whileUntil"
},
{
"kind": "block",
"type": "controls_repeat_ext"
}
]
},
{
"kind": "category",
"name": "变量",
"colour": 330,
"custom": "VARIABLE"
},
{
"kind": "category",
"name": "函数",
"colour": 290,
"custom": "PROCEDURE"
}
]
},
collapse: true,
comments: true,
disable: false,
maxBlocks: Infinity,
trashcan: true,
horizontalLayout: false,
toolboxPosition: 'start',
css: true,
media: 'https://blockly-demo.appspot.com/static/media/',
rtl: false,
scrollbars: true,
sounds: true,
oneBasedIndex: true,
grid: {
spacing: 20,
length: 1,
colour: '#ccc',
snap: true
},
zoom: {
controls: true,
wheel: true,
startScale: 1.0,
maxScale: 3,
minScale: 0.3,
scaleSpeed: 1.2
}
});
// 定义传感器读取块
Blockly.Blocks['sensor_read'] = {
init: function() {
this.appendDummyInput()
.appendField("读取传感器")
.appendField(new Blockly.FieldDropdown([
["左侧传感器", "leftSensor"],
["右侧传感器", "rightSensor"],
["碰撞传感器", "bumperSensor"]
]), "SENSOR");
this.setOutput(true, "Number");
this.setColour(230);
this.setTooltip("读取指定传感器的值");
this.setHelpUrl("");
}
};
// 定义传感器条件块
Blockly.Blocks['sensor_condition'] = {
init: function() {
this.appendDummyInput()
.appendField("传感器")
.appendField(new Blockly.FieldDropdown([
["左侧传感器", "leftSensor"],
["右侧传感器", "rightSensor"],
["碰撞传感器", "bumperSensor"]
]), "SENSOR")
.appendField("检测到")
.appendField(new Blockly.FieldDropdown([
["白线", "WHITE"],
["黑线", "BLACK"]
]), "CONDITION");
this.setOutput(true, "Boolean");
this.setColour(230);
this.setTooltip("检查传感器是否检测到白线或黑线");
this.setHelpUrl("");
}
};
// 定义电机控制块
Blockly.Blocks['motor_control'] = {
init: function() {
this.appendDummyInput()
.appendField("设置")
.appendField(new Blockly.FieldDropdown([
["左侧电机", "LEFT"],
["右侧电机", "RIGHT"],
["两个电机", "BOTH"]
]), "MOTOR")
.appendField("速度")
.appendField(new Blockly.FieldNumber(0, 0, 255), "SPEED");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(120);
this.setTooltip("设置电机的速度(0-255)");
this.setHelpUrl("");
}
};
// 定义电机方向块
Blockly.Blocks['motor_direction'] = {
init: function() {
this.appendDummyInput()
.appendField("设置")
.appendField(new Blockly.FieldDropdown([
["左侧电机", "LEFT"],
["右侧电机", "RIGHT"],
["两个电机", "BOTH"]
]), "MOTOR")
.appendField("方向为")
.appendField(new Blockly.FieldDropdown([
["前进", "FORWARD"],
["后退", "BACKWARD"]
]), "DIRECTION");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(120);
this.setTooltip("设置电机的转动方向");
this.setHelpUrl("");
}
};
// 定义电机速度块
Blockly.Blocks['motor_speed'] = {
init: function() {
this.appendDummyInput()
.appendField("设置")
.appendField(new Blockly.FieldDropdown([
["左侧电机", "LEFT"],
["右侧电机", "RIGHT"],
["两个电机", "BOTH"]
]), "MOTOR")
.appendField("速度")
.appendField(new Blockly.FieldNumber(0, 0, 255), "SPEED")
.appendField("方向")
.appendField(new Blockly.FieldDropdown([
["前进", "FORWARD"],
["后退", "BACKWARD"]
]), "DIRECTION");
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(120);
this.setTooltip("同时设置电机的速度和方向");
this.setHelpUrl("");
}
};
// 生成Arduino代码的函数
function generateCode() {
// 生成JavaScript代码
const code = Blockly.JavaScript.workspaceToCode(workspace);
// 转换为Arduino代码
const arduinoCode = convertToArduino(code);
// 显示生成的代码
document.getElementById('generatedCode').textContent = arduinoCode;
}
// 将JavaScript代码转换为Arduino代码
function convertToArduino(jsCode) {
// 这里简化处理,实际应用中需要更复杂的转换
let arduinoCode = `/*
ELEC1100 Your Lab#06 & Project Template
循迹小车程序 - 可视化编程生成
*/
// 引脚定义
const int pinL_Sensor = A5; // 左侧传感器
const int pinB_Sensor = A4; // 碰撞传感器
const int pinR_Sensor = A3; // 右侧传感器
const int pinL_PWM = 9; // 左侧电机速度
const int pinL_DIR = 10; // 左侧电机方向
const int pinR_PWM = 11; // 右侧电机速度
const int pinR_DIR = 12; // 右侧电机方向
// 传感器变量
int bumperSensor = 0;
int leftSensor = 0;
int rightSensor = 0;
void setup() {
// 设置引脚模式
pinMode(pinB_Sensor, INPUT);
pinMode(pinL_Sensor, INPUT);
pinMode(pinR_Sensor, INPUT);
pinMode(pinL_DIR, OUTPUT);
pinMode(pinR_DIR, OUTPUT);
pinMode(pinL_PWM, OUTPUT);
pinMode(pinR_PWM, OUTPUT);
// 初始化电机方向
digitalWrite(pinL_DIR, HIGH);
digitalWrite(pinR_DIR, HIGH);
// 停止电机
analogWrite(pinL_PWM, 0);
analogWrite(pinR_PWM, 0);
}
void loop() {
// 读取传感器值
bumperSensor = digitalRead(pinB_Sensor);
leftSensor = digitalRead(pinL_Sensor);
rightSensor = digitalRead(pinR_Sensor);
// 用户代码开始
`;
// 添加用户代码
const lines = jsCode.split('\n');
for (let line of lines) {
// 转换常见的JavaScript语句为Arduino语句
let arduinoLine = line
.replace(/console\.log/g, '//')
.replace(/alert/g, '//')
.replace(/var /g, '')
.replace(/let /g, '')
.replace(/const /g, '');
arduinoCode += ' ' + arduinoLine + '\n';
}
arduinoCode += ` // 用户代码结束
}`;
return arduinoCode;
}
// 下载代码
function downloadCode() {
const code = document.getElementById('generatedCode').textContent;
if (!code || code.includes('生成的Arduino代码将显示在这里')) {
alert('请先生成代码!');
return;
}
const blob = new Blob([code], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'robot_car.ino';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// 重置工作区
function resetWorkspace() {
if (confirm('确定要重置工作区吗?所有未保存的更改将丢失。')) {
workspace.clear();
document.getElementById('generatedCode').textContent = '生成的Arduino代码将显示在这里';
}
}
// 初始化显示
document.getElementById('generatedCode').textContent = '生成的Arduino代码将显示在这里';
</script>
</body>
</html>