-
Notifications
You must be signed in to change notification settings - Fork 16
/
scripts_pose.js
408 lines (324 loc) · 11.3 KB
/
scripts_pose.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
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
// ============================= INTRODUCTION
// Name : Leaflet mapping for ROS
// Author : Zhang Handuo
// Mail : hzhang032@e.ntu.edu.sg
// Date : November 2018
// Link : https://github.com/zhanghanduo/ROS_leaflet_gps
// 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 = '/gps_pose'; // '/kitti/oxts/gps/fix';
var CONFIG_default_pose_topic_name = '/sslam/robot/pose';
// The number of cycles between every marker position reload
var CONFIG_cycles_number = 10;
// If the input camera pose frame coordinate system is z_up or z_forward
var z_up = true;
// 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';
//===> Global variables
var map;
var selectionMode;
var carIcon = L.icon({
iconUrl: 'assets/car1.png',
iconSize: [22, 22],
iconAnchor: [10, 20],
popupAnchor: [0, 0],
});
var markerPosition = L.marker([0,0]);
var markerPosition2 = L.marker([0,0], {icon: carIcon});
// ============================= FUNCTIONS
var rotateVector = function(vec, ang)
{
// ang = -ang * (Math.PI/180);
var cos = Math.cos(ang);
var sin = Math.sin(ang);
return new Array(Math.round(10000*(vec[0] * cos - vec[1] * sin))/10000, Math.round(10000*(vec[0] * sin + vec[1] * cos))/10000);
};
// // Rotate an object around an axis in object space
// function rotateAroundObjectAxis( object, axis, radians ) {
// var rotationMatrix = new THREE.Matrix4();
// rotationMatrix.makeRotationAxis( axis.normalize(), radians );
// object.matrix.multiplySelf( rotationMatrix ); // post-multiply
// object.rotation.setRotationFromMatrix( object.matrix );
// }
// // Rotate an object around an axis in world space (the axis passes through the object's position)
// function rotateAroundWorldAxis( object, axis, radians ) {
// var rotationMatrix = new THREE.Matrix4();
// rotationMatrix.makeRotationAxis( axis.normalize(), radians );
// rotationMatrix.multiplySelf( object.matrix ); // pre-multiply
// object.matrix = rotationMatrix;
// object.rotation.setRotationFromMatrix( object.matrix );
// }
// ===> 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 = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
// Set attrib (always !)
var attrib = 'Map data © OpenStreetMap contributors';
//===> Map loading
map = L.map('map');
var osm = L.tileLayer(tileUrl, {
minZoom: 11,
maxZoom: 19,
attribution: attrib
});
osm.addTo(map);
L.control.scale ({maxWidth:240, metric:true, imperial:false, position: 'bottomleft'}).addTo (map);
let polylineMeasure = L.control.polylineMeasure ({
position:'topleft', unit:'metres', showBearings:false,
clearMeasurementsOnStop: false, showClearControl: true})
polylineMeasure.addTo (map);
L.easyButton('glyphicon glyphicon-refresh', function(btn, map){
window.location.reload();
}).addTo(map);
return map;
}
// ============================= SCRIPT
var bounds;
var zoomLevel = 17;
var loadedMap = false;
var loadedMap2 = false;
var i = 0;
var listenerGPS;
var listenerPose;
//===> ROS connexion
var ros = new ROSLIB.Ros({
url : 'ws://'+ CONFIG_ROS_server_URI +':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("Connexion 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 map
mapInit();
//===> Set the GPS listener
// => Create param with initial value
var paramTopicNameValue = CONFIG_default_gps_topic_name;
var paramTopicNamePose = CONFIG_default_pose_topic_name;
var paramNbCyclesValue = CONFIG_cycles_number;
// => Init the ROS param
var paramTopicName = new ROSLIB.Param({ros : ros, name : '/panel/gps_topic'});
var paramTopicPose_Name = new ROSLIB.Param({ros : ros, name : '/panel/pose_topic'});
var paramNbCycles = new ROSLIB.Param({ros : ros, name : '/panel/nb_cycles'});
// var z_up = new ROSLIB.Param({ros : ros, name : 'z_up'});
// console.log("z_up?: ", z_up);
var path_ = [];
// var firstloc;
var polyline_;
var polyline2_;
var quaternion_;
var rotation;
var translation_;
var quaternion0;
var translation0;
// => 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,
// messageType : 'sensor_msgs/NavSatFix'
messageType : 'geometry_msgs/PoseWithCovarianceStamped'
});
// Set the callback function when a message from /gps is received
var i = 0;
var utm0;
listenerGPS.subscribe(function(message) {
// We have to wait for the GPS before showing the map, because we don't know where we are
var x_ = message.pose.pose.position.x;
var y_ = message.pose.pose.position.y;
quaternion_ = new THREE.Quaternion( message.pose.pose.orientation.x,
message.pose.pose.orientation.y,
message.pose.pose.orientation.z,
message.pose.pose.orientation.w );
rotation = new THREE.Euler().setFromQuaternion( quaternion_, 'XYZ' );
// rot = rotation.z - Math.PI/4;
// console.log("rotation: ", rot);
translation_ = new THREE.Vector3( x_, y_, message.pose.pose.position.z );
if(loadedMap == false)
{
swal.close();
utm0 = L.utm(x_, y_, 48, 'N', false);
var ll0 = utm0.latLng();
if (ll0){
map.setView(ll0, zoomLevel);
}
// Add the marker on the map
markerPosition.addTo(map);
markerPosition2.addTo(map);
// firstloc = [x_, y_];
translation0 = translation_;
quaternion0 = quaternion_;
path_.push(ll0);
polyline_ = L.polyline(path_, {color: 'red'}, {weight: 1}).addTo(map);
// Set the flag to true, so we don't have to load the map again
loadedMap = true;
}
if(i % paramNbCyclesValue == 0)
{
utm0 = L.utm(x_, y_, 48, 'N', false);
ll0 = utm0.latLng();
if (ll0){
markerPosition.setLatLng(ll0);
polyline_.addLatLng(ll0);
}
// console.log("path: ", JSON.stringify(path_));
// If the marker has went out of the map, we move the map
// bounds = map.getBounds();
// if(!bounds.contains(ll0))
// map.setView(ll0, zoomLevel);
// console.log("Update position");
}
i++;
});
});
});
paramTopicPose_Name.get(function(value) {
// If the param isn't created yet, we keep the default value
if(value != null)
paramTopicNamePose = value;
else
paramTopicPose_Name.set(paramTopicNamePose);
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);
listenerPose = new ROSLIB.Topic({
ros : ros,
name : paramTopicNamePose,
messageType : 'geometry_msgs/PoseWithCovarianceStamped'
});
var i2 = 0;
var utm;
listenerPose.subscribe(function(message2) {
// 0 Declaration of variables
scale = new THREE.Vector3 (1, 1, 1);
var cam2gps0 = new THREE.Matrix4();
var quaternion_c = new THREE.Quaternion( message2.pose.pose.orientation.x,
message2.pose.pose.orientation.y,
message2.pose.pose.orientation.z,
message2.pose.pose.orientation.w );
var raw_vis = new THREE.Vector3( message2.pose.pose.position.x, message2.pose.pose.position.y, message2.pose.pose.position.z);
var imu02enu = new THREE.Matrix4();
if(z_up == true){
cam2gps0.compose(raw_vis, quaternion_c, scale);
// R(imu02enu) * R(imuk2imu0) = R(imuk2enu)
// imu02enu.multiply(cam2gps0);
cam2gps0.decompose(raw_vis, quaternion_c, scale);
var x2_ = raw_vis.x + translation0.x;
var y2_ = raw_vis.y + translation0.y;
}
else {
imu02enu.compose(translation0, quaternion0, scale);
var quaternion_cam2imu = new THREE.Quaternion( 0.5, -0.5, 0.5, -0.5 );
var quaternion_imu2cam = new THREE.Quaternion( 0.5, -0.5, 0.5, 0.5 );
var cam2imu = new THREE.Matrix4();
cam2imu.makeRotationFromQuaternion(quaternion_cam2imu);
// 1 R(camk2cam0) * R(imuk2camk) = R(imuk2cam0)
quaternion_c.multiply(quaternion_imu2cam).normalize();
cam2gps0.compose(raw_vis, quaternion_c, scale);
// 2 R(imu02enu) * R(cam02imu0) * R(imuk2cam0) = R(imuk2enu)
imu02enu.multiply(cam2imu);
imu02enu.multiply(cam2gps0);
imu02enu.decompose(raw_vis, quaternion_c, scale);
var x2_ = raw_vis.x;
var y2_ = raw_vis.y;
}
if(loadedMap2 == false)
{
polyline2_ = L.polyline(path_, {color: 'blue'}).addTo(map);
loadedMap2 = true;
}
if(i2 % paramNbCyclesValue == 0)
{
utm = L.utm(x2_, y2_, 48, 'N', false);
var ll = utm.latLng();
if (ll){
markerPosition2.setLatLng(ll);
polyline2_.addLatLng(ll);
}
// console.log("Update position");
}
i2 ++;
});
});
});