-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.html
204 lines (189 loc) · 4.76 KB
/
robot.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://static.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
<script type="text/javascript" src="http://static.robotwebtools.org/roslibjs/current/roslib.min.js"></script>
<script type="text/javascript" type="text/javascript">
// Connecting to ROS
// -----------------
var ros = new ROSLIB.Ros({
url : 'ws://192.168.0.107:9090' // Robot_IP:ROSBridge_default_port
});
ros.on('connection', function() {
console.log('Connected to websocket server.');
});
ros.on('error', function(error) {
console.log('Error connecting to websocket server: ', error);
});
ros.on('close', function() {
console.log('Connection to websocket server closed.');
});
// Publishing a Topic
// ------------------
var cmdVel = new ROSLIB.Topic({
ros : ros,
name : '/cmd_vel',
messageType : 'geometry_msgs/Twist'
});
var takeoff = new ROSLIB.Topic({
ros : ros,
name : '/ardrone/takeoff',
messageType : 'std_msgs/Empty'
});
var land = new ROSLIB.Topic({
ros : ros,
name : '/ardrone/land',
messageType : 'std_msgs/Empty'
});
var imageTopic = new ROSLIB.Topic({
ros : ros,
name : '/ardrone/bottom/image_raw/compressed',
messageType : 'sensor_msgs/CompressedImage'
});
imageTopic.subscribe(function(message)
{
var imagedata = "data:image/jpg;base64," + message.data;
document.getElementById('xy_image').setAttribute('src', imagedata);
imageTopic.unsubscribe();
});
var move_takeoff = new ROSLIB.Message({});
var move_land = new ROSLIB.Message({});
var move = new ROSLIB.Message({
linear : {
x : 0,
y : 0,
z : 0
},
angular : {
x : 0,
y : 0,
z : 0
}
});
var f = b = l = r = u = d = 0; // latches for Forward, Backward, Left, Right, Up & Down
function message_change(choice){
move.linear.x = move.linear.y = move.linear.z = move.angular.x = move.angular.y = move.angular.z = 0;
switch(choice) {
case 1: // move forward
if(this.f == 0){
move.linear.x=1;
this.f=1;
break;
}
else{
this.f = 0;
move.linear.x = move.linear.y = move.linear.z = move.angular.x = move.angular.y = move.angular.z = 0;
}
break;
case 2: // move forward
if(this.b == 0){
move.linear.x=-1;
this.b=1;
}
else{
this.b = 0;
move.linear.x = move.linear.y = move.linear.z = move.angular.x = move.angular.y = move.angular.z = 0;
}
break;
case 3: // turn left
if(this.l == 0){
move.linear.x=1;
move.angular.z=1;
this.l=1;
}
else{
this.l = 0;
move.linear.x = move.linear.y = move.linear.z = move.angular.x = move.angular.y = move.angular.z = 0;
}
break;
case 4: // turn right
if(this.r == 0){
move.linear.x=1;
move.angular.z=-1;
this.r=1;
}
else{
this.r = 0;
move.linear.x = move.linear.y = move.linear.z = move.angular.x = move.angular.y = move.angular.z = 0;
}
break;
case 5: // move up
if(this.u == 0){
move.linear.z=1;
this.u=1;
}
else{
this.u = 0;
move.linear.x = move.linear.y = move.linear.z = move.angular.x = move.angular.y = move.angular.z = 0;
}
break;
case 6: // move down
if(this.d == 0){
move.linear.z=-1;
this.d=1;
}
else{
this.d = 0;
move.linear.x = move.linear.y = move.linear.z = move.angular.x = move.angular.y = move.angular.z = 0;
}
break;
case 7: // stop and reset all latches
var f = b = l = r = u = d = 0;
break;
default: // no use, default case is just idling here
break;
}
cmdVel.publish(move);
}
</script>
<style type="text/css">
#maincontainer
{
top:0px;
padding-top:0;
margin:auto; position:relative;
width: 200px;
height: 10px;
}
</style>
</head>
<body>
<div id="controller">
<button onclick=takeoff.publish(move_takeoff)>
Takeoff
</button>
<button onclick=land.publish(move_land)>
Land
</button>
<br>
<div style="text-align:center;width:480px;">
<button onclick=message_change(1)>
Move Forward
</button>
<br>
<button onclick=message_change(3) style="margin:30px;">
Turn Left
</button>
<button onclick=message_change(4) style="margin:30px;">
Turn Right
</button>
<br>
<button onclick=message_change(2)>
Move Backward
</button>
</div>
<br>
<button onclick=message_change(5)>
Move Up
</button>
<button onclick=message_change(6)>
Move Down
</button>
<br>
<button onclick=message_change(7)>
Stop
</button>
</div>
</body>
</html>