1 | var log = null; |
---|
2 | var container = null; |
---|
3 | var connection = null; |
---|
4 | var map = null; |
---|
5 | |
---|
6 | /// Create a circle model. |
---|
7 | function circleModel(radius) { |
---|
8 | var svg = document.createElementNS(webmap.svgns, "g"); |
---|
9 | var circle = document.createElementNS(webmap.svgns, "circle"); |
---|
10 | var line = document.createElementNS(webmap.svgns, "line"); |
---|
11 | svg.appendChild(circle); |
---|
12 | svg.appendChild(line); |
---|
13 | |
---|
14 | circle.setAttribute("cx", 0); |
---|
15 | circle.setAttribute("cy", 0); |
---|
16 | circle.setAttribute("r", radius); |
---|
17 | |
---|
18 | line.setAttribute("x1", 0); |
---|
19 | line.setAttribute("y1", 0); |
---|
20 | line.setAttribute("x2", radius); |
---|
21 | line.setAttribute("y2", 0); |
---|
22 | |
---|
23 | return svg; |
---|
24 | } |
---|
25 | |
---|
26 | /// Example robot class for Stage robots. |
---|
27 | function StageRobot(name, index, connecion) { |
---|
28 | |
---|
29 | // Create SVG content. |
---|
30 | var svg = circleModel(0.2); |
---|
31 | |
---|
32 | // Call parent constructor. |
---|
33 | webmap.Robot.call(this, name, svg); |
---|
34 | |
---|
35 | // Add CSS classes. |
---|
36 | this.svg.classList.add("stage"); |
---|
37 | this.svg.classList.add("stage_" + index); |
---|
38 | |
---|
39 | // Set up odometry, navigation and laser scanner. |
---|
40 | var topic_base = "/robot_" + index; |
---|
41 | this.setOdometryTopic(connection, topic_base + "/odom"); |
---|
42 | this.setTwistTopic(connection, topic_base + "/cmd_vel"); |
---|
43 | this.addModule("LaserScanner", connection, topic_base + "/base_scan"); |
---|
44 | } |
---|
45 | |
---|
46 | webmap.extend(webmap.Robot, StageRobot); |
---|
47 | |
---|
48 | function init() { |
---|
49 | log = document.getElementById("log"); |
---|
50 | container = document.getElementById("webmap_container"); |
---|
51 | |
---|
52 | // Create the map. |
---|
53 | map = new webmap.Map(container, 700, 500); |
---|
54 | map.addModule("MouseViewControl", 1, 1.5); |
---|
55 | map.addModule("ShowSelected", document.getElementById("selected")); |
---|
56 | map.addModule("Teleop", 1, 1.5); |
---|
57 | map.addImage("background.png", -29.35, -27, 58.7, 54.0); |
---|
58 | map.scale(500 / 58.7, map.canvas_width * 0.5, map.canvas_height * 0.5); |
---|
59 | |
---|
60 | // Create the rosbridge connection. |
---|
61 | connection = new ros.Bridge("ws://localhost:9090"); |
---|
62 | connection.onClose = function (e) { |
---|
63 | log.textContent = "Connection closed.\n" + log.textContent; |
---|
64 | }; |
---|
65 | connection.onError = function (e) { |
---|
66 | log.textContent = "Error: " + e + ".\n" + log.textContent; |
---|
67 | }; |
---|
68 | connection.onOpen = function (e) { |
---|
69 | log.textContent = "Connection established.\n" + log.textContent; |
---|
70 | // Add two Stage robots. |
---|
71 | map.addRobot(new StageRobot("Robot 0", 0, connection)); |
---|
72 | map.addRobot(new StageRobot("Robot 1", 1, connection)); |
---|
73 | }; |
---|
74 | } |
---|
75 | |
---|
76 | document.addEventListener("DOMContentLoaded", init, false); |
---|