-
Notifications
You must be signed in to change notification settings - Fork 16
/
onemap_scripts.js
340 lines (287 loc) · 8.88 KB
/
onemap_scripts.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
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
// ============================= INTRODUCTION
// Name : Autonomous-Car-GPS-Guiding
// Author : Sylvain ARNOUTS
// Mail : [email protected]
// Date : From May to August 2016
// Link : https://github.com/sylvainar/Autonomous-Car-GPS-Guiding
// This code has been written in order to create a nice interface to interact
// with the autonomous electric car of the UPV ai2 laboratory. It displays a map,
// set a marker on the car's position using gps publishing topic, and allows a user
// to start the routing by tapping the destination on the touchscreen.
// License : Apache 2.0
// ============================= CONFIGURATION
// You can set here some parameters for the launch.
// In the lab we're using two different GPS, so further in the
// program, we're going to read rosparam to know what topic
// we should listen to and what is the number of cycles
// between each refreshing.
// The name of the GPS publisher name by default
var CONFIG_default_gps_topic_name = '/kitti/oxts/gps/fix';
// The number of cycles between every marker position reload
var CONFIG_cycles_number = 20;
// We can download the map online on OSM server, but
// it won't work if the car isn't connected to the internet.
// If you downloaded tiles and put it in the car, then you can
// access them in local, or else, connect to server.
// Set this config to "local" or "server".
var CONFIG_tile_source = 'server';
// If you use local tiles, set here the path to it
var CONFIG_tile_local_path = 'UPV/{z}/{x}/{y}.png';
// Network address to ROS server (it can be localhost or an IP)
var CONFIG_ROS_server_URI = 'localhost';
// ============================= FUNCTIONS
// ===> mapInit() : init the map
function mapInit() {
//===> Var init
// Fetch tiles
if(CONFIG_tile_source == 'local')
var tileUrl = CONFIG_tile_local_path;
if(CONFIG_tile_source == 'server')
var tileUrl = 'https://maps-{s}.onemap.sg/v3/Default/{z}/{x}/{y}.png';
//===> Map loading
var center = L.bounds([ 49.0179, 8.443], [ 49.0169, 8.429]).getCenter();
map = L.map('map');
// map.setMaxBounds([[1.56073, 104.1147], [1.16, 103.502]]);
var osm = L.tileLayer(tileUrl, {
detectRetina: true,
maxZoom: 19,
minZoom: 11
});
osm.addTo(map);
L.easyButton('glyphicon-road', function(btn, map){
swal({
title: "Where do you want to go ?",
text: "After closing this popup, click on the place you want to go.",
type: "info",
confirmButtonText: "Got it!",
showCancelButton: true,
closeOnConfirm: true,
showLoaderOnConfirm: true,
allowOutsideClick: false,
},
function(isConfirm){
if (isConfirm) selectionMode = true;
else selectionMode = false;
});
}).addTo(map);
L.easyButton('glyphicon glyphicon-cog', function(btn, map){
// TODO : add the possibility to modify params on the run
}).addTo(map);
L.easyButton('glyphicon glyphicon-refresh', function(btn, map){
window.location.reload();
}).addTo(map);
markerFinish.addTo(map).setOpacity(0)
return map;
}
// ============================= SCRIPT
//===> Global variables
var map;
var selectionMode;
var bounds;
var currentPosition = {latitude : 0, longitude : 0};
var startPoint;
var endPoint;
var markerPosition = L.marker([0,0]);
var markerFinish = L.marker([0,0]);
var zoomLevel = 17;
var routeControl;
var loadedMap = false;
var i = 0;
var listenerGPS;
//===> ROS connection
var ros = new ROSLIB.Ros({
// url : 'ws://'+ CONFIG_ROS_server_URI +':9090'
url : 'ws://localhost:9090'
});
swal({
title: "Connecting to ROS...",
showConfirmButton: false,
closeOnConfirm: false,
showLoaderOnConfirm: true,
allowOutsideClick: false,
allowEscapeKey: false
});
ros.on('connection', function() {
console.log('Connected to websocket server.');
swal({
title: "Waiting...",
text: "The navigation module can't work without the GPS. Launch the GPS and the module will start automatically.",
type: "info",
confirmButtonText: "Reload",
closeOnConfirm: false,
allowOutsideClick: false,
allowEscapeKey: false
},
function(){
window.location.reload();
});
});
ros.on('error', function(error) {
console.log('Error connecting to websocket server: ', error);
swal({
title: "Error connecting the ROS server",
text: "Unable to reach ROS server. Is rosbridge launched ?",
type: "error",
confirmButtonText: "Retry",
closeOnConfirm: false,
allowOutsideClick: false,
allowEscapeKey: false
},
function(){
window.location.reload();
});
});
ros.on('close', function() {
console.log("Connection closed.");
swal({
title: "Error connecting the ROS server",
text: "Unable to reach ROS server. Is rosbridge launched ?",
type: "error",
confirmButtonText: "Retry",
closeOnConfirm: false,
allowOutsideClick: false,
allowEscapeKey: false
},
function(){
window.location.reload();
});
});
//===> Init the routing parameters
var paramStartLat = new ROSLIB.Param({
ros : ros,
name : '/routing_machine/start/latitude'
});
var paramStartLon = new ROSLIB.Param({
ros : ros,
name : '/routing_machine/start/longitude'
});
var paramEndLat = new ROSLIB.Param({
ros : ros,
name : '/routing_machine/destination/latitude'
});
var paramEndLon = new ROSLIB.Param({
ros : ros,
name : '/routing_machine/destination/longitude'
});
var paramEndGoTo = new ROSLIB.Param({
ros : ros,
name : '/routing_machine/destination/goTo'
});
paramStartLat.set(0);
paramStartLon.set(0);
paramEndLat.set(0);
paramEndLon.set(0);
paramEndGoTo.set(false);
//===> Init the map and the click listener
mapInit();
map.on('click', function(e) {
//When a click on the map is detected
if(selectionMode == true)
{
selectionMode = false;
//First, get the coordinates of the point clicked
var lat = e.latlng.lat;
var lon = e.latlng.lng;
//Place a marker
markerFinish.setLatLng([lat,lon]);
markerFinish.setOpacity(1);
setTimeout(function() {
swal({
title: "Is this correct ?",
text: "Confirm the position to start the navigation.",
type: "info",
confirmButtonText: "Yes, let's go !",
showCancelButton: true,
closeOnConfirm: true,
allowOutsideClick: false,
},
function(isConfirm){
if (isConfirm)
{
//Logging stuff in the console
console.log('Routing Start !');
console.log('Start set to : '+ currentPosition.latitude + ' ' + currentPosition.longitude);
console.log('Destination set to : '+lat + ' ' + lon);
//Set all the parameters to the destination
paramStartLat.set(currentPosition.latitude);
paramStartLon.set(currentPosition.longitude);
paramEndLat.set(lat);
paramEndLon.set(lon);
paramEndGoTo.set(true);// goTo is set to true, that means that their is a new destination to consider.
}
else
{
markerFinish.setOpacity(0);
}
})}, 1000);
}
});
//===> Set the GPS listener
// => Create param with initial value
var paramTopicNameValue = CONFIG_default_gps_topic_name;
var paramNbCyclesValue = CONFIG_cycles_number;
// => Init the ROS param
var paramTopicName = new ROSLIB.Param({ros : ros, name : '/panel/gps_topic'});
var paramNbCycles = new ROSLIB.Param({ros : ros, name : '/panel/nb_cycles'});
listenerGPS = new ROSLIB.Topic({
ros : ros,
// name : paramTopicNameValue,
name : '/kitti/oxts/gps/fix',
messageType : 'sensor_msgs/NavSatFix'
});
// Set the callback function when a message from /gps is received
var i = 0;
// => Set the value
paramTopicName.get(function(value) {
// If the param isn't created yet, we keep the default value
if(value != null)
paramTopicNameValue = value;
else
paramTopicName.set(paramTopicNameValue);
paramNbCycles.get(function(value) {
// If the param isn't created yet, we keep the default value
if(value != null)
paramNbCyclesValue = value;
else
paramNbCycles.set(paramNbCyclesValue);
// Set the listener information
listenerGPS = new ROSLIB.Topic({
ros : ros,
// name : paramTopicNameValue,
name : '/kitti/oxts/gps/fix',
messageType : 'sensor_msgs/NavSatFix'
});
// Set the callback function when a message from /gps is received
var i = 0;
listenerGPS.subscribe(function(message) {
console.log('Subsribed gps info.');
// We have to wait for the GPS before showing the map, because we don't know where we are
var lat = message.latitude;
var lon = message.longitude;
if(loadedMap == false)
{
swal.close();
// Center the map on the car's position
map.setView([lat, lon], zoomLevel);
// Add the marker on the map
markerPosition.addTo(map);
// Set the flag to true, so we don't have to load the map again
loadedMap = true;
}
if(i % paramNbCyclesValue == 0)
{
// Refresh the global variable with the position
currentPosition.latitude = lat;
currentPosition.longitude = lon;
// Refresh the position of the marker on the map
markerPosition.setLatLng([lat, lon]);
// If the marker has went out of the map, we move the map
bounds = map.getBounds();
if(!bounds.contains([lat, lon]))
map.setView([lat, lon], zoomLevel);
console.log("Update position");
}
i++;
});
});
});