-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.js
113 lines (100 loc) · 2.92 KB
/
robot.js
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
var connection;
(function (ext) {
console.log("robot v1");
// Cleanup function when the extension is unloaded
ext._shutdown = function() {};
ext._getStatus = function() {
return { status:2, msg:'Ready' };
};
ext.connect = function(ip) {
console.log("connect1");
console.log(ip);
console.log(connection);
if (!connection || connection.readyState > 1){
console.log("not isConnected");
connection = new WebSocket('ws://192.168.1.132:81/test', ['arduino']);
connection.onopen = function () {
console.log("connection open");
connection.isConnected=true;
console.log("readyState", connection.readyState);
//connection.send('hello');
};
connection.onclose = function () {
console.log("connection close");
connection.isConnected=false;
//connection.send('hello');
};
connection.onerror = function (error) {
console.log('WebSocket Error ', error);
};
connection.onmessage = function (e) {
console.log('Server: ', e.data);
};
} else{
console.log(" isConnected");
console.log("readyState", connection.readyState);
}
};
ext.disconnect = function() {
console.log("disconnect");
console.log(connection);
connection.close();
};
ext.leftGo = function(direction) {
//var json ='{"action":"leftGo","direction":"'+direction+'"}';
var msg = {
action: "leftGo",
direction: direction
};
connection.send(JSON.stringify(msg));
console.log("leftGo!", msg);
};
ext.leftStop = function() {
var msg = {
action: "leftStop"
};
console.log("leftStop!", msg);
connection.send(JSON.stringify(msg));
};
ext.rightGo = function(direction) {
var msg = {
action: "rightGo",
direction: direction
};
console.log("rightGo!", msg);
connection.send(JSON.stringify(msg));
};
ext.rightStop = function() {
var msg = {
action: "rightStop"
};
console.log("rightStop!", msg);
connection.send(JSON.stringify(msg));
};
ext.setSpeed = function(speed) {
var msg = {
action: "setSpeed",
speed: speed
};
console.log("setSpeed!", msg);
connection.send(JSON.stringify(msg));
};
// Block and block menu descriptions
var descriptor = {
blocks: [
[" ", "Connect", "connect"],
[" ", "Disconnect", "disconnect"],
[" ", "Left Wheel Go %m.blockPos", "leftGo", "forward"],
[" ", "Left Wheel Stop", "leftStop"],
[" ", "Right Wheel Go %m.blockPos", "rightGo", "forward"],
[" ", "Right Wheel Stop", "rightStop"],
[" ", "Set Speed %m.speed", "setSpeed", 50],
],
menus: {
blockPos: ['forward', 'reverse'],
speed: [10,20,30,40,50,60,70,80,90,100]
}
};
// Register the extension
ScratchExtensions.register('Robot', descriptor, ext);
})({});