]> defiant.homedns.org Git - ros_wild_thumper.git/blob - www/assets/javascripts/application.js
eed4a38f4a565d1d3a121920a3bd1d5254751ef5
[ros_wild_thumper.git] / www / assets / javascripts / application.js
1 function init() {
2         var ros = new ROSLIB.Ros();
3         ros.connect('ws://wildthumper:9090');
4         var isDragging = false;
5
6         ros.on('connection', function() {
7                 information.alerts.push({message: "Connected to websocket server.", success: true});
8         });
9
10         ros.on('error', function(error) {
11                 information.alerts.push({message: "Error connecting to websocket server.", danger: true});
12         });
13
14         ros.on('close', function() {
15                 information.alerts.push({message: "Connection to websocket server closed.", info: true});
16         });
17
18         //tfClient.subscribe('base_link', function(tf) {
19         //      var now = new Date();
20         //      $("#pose").text(now.toLocaleTimeString() + ": (" + tf.translation.x + ", " + tf.translation.y + ")");
21
22         //      robotMarker.x = tf.translation.x;
23         //      robotMarker.y = -tf.translation.y;
24         //      robotMarker.rotation = viewer2D.scene.rosQuaternionToGlobalTheta(tf.rotation);
25         //});
26         var poseTopic = new ROSLIB.Topic({ros: ros, name: '/robot_pose', messageType: 'geometry_msgs/Pose'});
27         var sensorTopic = new ROSLIB.Topic({ros: ros, name: '/sensors', messageType: 'wild_thumper/Sensor'});
28         var batteryTopic = new ROSLIB.Topic({ros: ros, name: '/battery', messageType: 'sensor_msgs/BatteryState'});
29         var ledStripeTopic = new ROSLIB.Topic({ros: ros, name: '/led_stripe', messageType: 'wild_thumper/LedStripe'});
30         var cmdVelTopic = new ROSLIB.Topic({ros: ros, name: '/teleop/cmd_vel', messageType: 'geometry_msgs/Twist'});
31
32         poseTopic.subscribe(function(message) {
33                 var now = new Date();
34                 $("#pose").text(now.toLocaleTimeString() + ": (" + message.position.x + ", " + message.position.y + ")");
35         });
36
37         sensorTopic.subscribe(function(message) {
38                 sensors.light = message.light;
39                 sensors.temp = message.temp.toFixed(1);
40                 sensors.humidity = message.humidity;
41                 sensors.pressure = message.pressure.toFixed(1);
42                 sensors.co = message.co;
43         });
44
45         batteryTopic.subscribe(function(message) {
46                 power.voltage = message.voltage.toFixed(1);
47                 power.current = message.current.toFixed(1);
48         });
49
50         viewer2D = new ROS2D.Viewer({
51                 divID: 'map',
52                 width: 640,
53                 height: 480,
54                 background: "#efefef"
55         });
56         $('#map')
57         .bind('mousewheel', function(e) {
58                 if (e.originalEvent.wheelDelta/120 > 0) {
59                         viewer2D.scaleToDimensions(10, 10)
60                 } else {
61                         viewer2D.scaleToDimensions(5, 5)
62                 }
63         })
64         .mousedown(function() {
65                 isDragging = true;
66                 mousePreX = undefined;
67                 mousePreY = undefined;
68         })
69         .mouseup(function() {
70                 isDragging = false;
71         })
72         .mousemove(function(event) {
73                 if (isDragging) {
74                         if (mousePreX != undefined && mousePreY != undefined) {
75                                 var diffX = event.pageX - mousePreX;
76                                 var diffY = event.pageY - mousePreY;
77                                 console.log("Moving viewer2D by " + diffX + ", " + diffY);
78                                 viewer2D.shift(diffX, diffY);
79                         }
80                         mousePreX = event.pageX;
81                         mousePreY = event.pageY;
82                 }
83         });
84
85         // Setup the nav client.
86         NAV2D.OccupancyGridClientNav({
87                 ros : ros,
88                 rootObject : viewer2D.scene,
89                 viewer : viewer2D,
90                 serverName : '/move_base'
91         });
92
93         // Initialize the teleop.
94         teleop = new KEYBOARDTELEOP.Teleop({
95                 ros : ros,
96                 topic : '/teleop/cmd_vel'
97         });
98
99         // Create a UI slider using JQuery UI.
100         $('#speed-slider').slider({
101                 range : 'min',
102                 min : 0.10,
103                 max : 1.0,
104                 step : 0.05,
105                 value : 0.5,
106                 slide : function(event, ui) {
107                         // Change the speed label.
108                         speed_label.speed = ui.value;
109                         // Scale the speed.
110                         teleop.scale = (ui.value * 2);
111                 }
112         });
113
114         // Set the initial speed.
115         teleop.scale = ($('#speed-slider').slider('value') * 2);
116
117         $('.led_color').minicolors({
118                 control: 'wheel',
119                 format: 'rgb',
120                 defaultValue: '#000000',
121                 change: function(value) {
122                         var rgb = $(this).minicolors('rgbObject');
123                         var nums = jQuery.parseJSON($(this).prop("name"));
124                         var msg = new ROSLIB.Message({
125                                 leds: []
126                         });
127                         jQuery.each(nums, function(i, num) {
128                                 msg["leds"].push({
129                                         num: num,
130                                         red: parseInt(rgb.r*127/255),
131                                         green: parseInt(rgb.g*127/255),
132                                         blue: parseInt(rgb.b*127/255)
133                                 })
134                         });
135                         ledStripeTopic.publish(msg);
136                 }
137         });
138
139         function setSpeed(trans, rot) {
140                 var msg = new ROSLIB.Message({
141                         linear: {
142                                 x : trans,
143                                 y : 0,
144                                 z : 0
145                         },
146                         angular: {
147                                 x : 0,
148                                 y : 0,
149                                 z : rot
150                         },
151                 });
152                 cmdVelTopic.publish(msg);
153         }
154
155         $('.cmd_vel_circle')
156         .bind('mousedown touchstart', function(e) {
157                 isDragging = true;
158         })
159         .bind('mouseup touchend mouseleave', function(e) {
160                 isDragging = false;
161                 setSpeed(0, 0);
162         })
163         .bind('mousemove touchmove', function(e) {
164                 if (isDragging) {
165                         // absolute click position
166                         var X,Y;
167                         if (e.originalEvent.touches) {
168                                 X = e.originalEvent.touches[0].pageX;
169                                 Y = e.originalEvent.touches[0].pageY;
170                         } else {
171                                 X = e.pageX;
172                                 Y = e.pageY;
173                         }
174                         // relative click position
175                         var Xrel = X - this.offsetLeft - $(this).width()/2; 
176                         var Yrel = Y - this.offsetTop - $(this).height()/2; 
177                         // scale to -1..+1
178                         var trans = -Yrel / ($(this).height()/2);
179                         var rot = -Xrel / ($(this).width()/2);
180                         setSpeed(trans, rot*3);
181                 }
182         });
183
184         information = new Vue({
185                 el: '#information',
186                 data: {
187                         alerts: [],
188                 },
189                 methods: {
190                         classObject: function(id) {
191                                 return {
192                                         "alert-success": this.alerts[id].success,
193                                         "alert-danger": this.alerts[id].danger,
194                                         "alert-info": this.alerts[id].info
195                                 }
196                         }
197                 }
198         })
199
200         sensors = new Vue({
201                 el: '#sensors',
202                 data: {light: '', temp: '', humidity: '', pressure: '', co: ''}
203         })
204
205         power = new Vue({
206                 el: '#power',
207                 data: {
208                         voltage: '',
209                         current: '',
210                 }
211         })
212
213         speed_label = new Vue({
214                 el: '#speed-label',
215                 data: {
216                         speed: $('#speed-slider').slider('value'),
217                 }
218         })
219 }