# Robot Web GUI Guide ###### tags: `apriltag localization` [TOC] ## Recommended Tutorials :::warning A simple web interface for ROS can be fully practiced after the following tutorials. The rest of the content will be based on these tutorials and give instruction on how to construct this specific webpage for apriltag localization along with what can be improved and be done. ::: 1. [The Construct - Introduction to ROSBridge Server](https://www.theconstructsim.com/developing-web-interfaces-for-ros-robots-1-introduction-to-rosbridge-server/) 2. [The Construct - Designing Frontend Webpage](https://www.theconstructsim.com/developing-web-interfaces-for-ros-robots-2-adding-styles-to-the-ros-control-web-page/) 3. [The Construct - Streaming Video (Images) on Webpage](https://www.theconstructsim.com/developing-web-interfaces-for-ros-robots-4-streaming-robots-camera-on-the-web-page/) ## Overall System :::success The system's foundation is rosbridge_suite package. It provides a simple method to setup a server on the robot and perform communication through a predefined protocol with a web client under a local network. ::: ![](https://i.imgur.com/tKpqnRR.png) * Client: <div style="text-align: justify"> &emsp; A web interface written in HTML and JavaScript on which user can request data from the server and send command to the server via JSON format text file. In order to send and request ROS compatible message, an opensource library, roslibjs, is provided along with rosbridge_suite </div> Ref: [rosbridge_suite](http://wiki.ros.org/rosbridge_suite) * Server: <div style="text-align: justify"> &emsp; A server in our case is the UPboard on the UAV which will run ROS and localization application. It can process the received and sent data from JSON to ROS message and vice versa via rosbridge_library. In order to send and receive at the same time without waiting the client to request, a sebsocket server is considered instead of TCP/UDP. </div> * Client & Server Communication: <div style="text-align: justify"> &emsp; Briefly speaking, the communication can be thought of as a consistent action of writing, sending, and reading a JSON formatted text file between client and server. On the client end, we can assign a topic and message to send and receive through predefined classes in roslibjs. These information will be written to a JSON file following the protocol and be sent to server end. Then the server will process the JSON file to ROS message through rosbridge_library. </div> ## Client(Frontend Webpage) ### Web server * SimpleHTTPServer + Single Page Web + Usage: ```console= python -m SimpleHTTPServer ``` ### Web Design #### HTML Basic Tutorials ++**Head(Libraries):**++ roslibjs, mjpegcanvas, bootstrap, vue.js ```htmlembedded= <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Control Panel</title> <script type="text/javascript" src="http://static.robotwebtools.org/roslibjs/current/roslib.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/eventemitter2@5.0.1/lib/eventemitter2.min.js"></script> <script type="text/javascript" src="https://static.robotwebtools.org/mjpegcanvasjs/current/mjpegcanvas.min.js"></script> </head> ``` ++**Body:**++ 1. Navigation bar > <div style="text-align: justify">User can redirect to different pages with navigation bar. The link of the button can be set with href in attribute tag. Note that with SimpleHTTPServer, only a single page can be created. If multiple pages needed to be setup, you can use FLASK to setup multiple web ip addresses.</div> ```htmlembedded= <div> <nav class="navbar navbar-expand-lg navbar-dark bg-secondary text-white"> <a class="navbar-brand" href="#">NCRL</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li class="nav-item active"> <a class="nav-link" href="http://127.0.0.1:5000/">Home<span class="sr-only">(current)</span></a> </div> </nav> </div> ``` 2. Block with background > jumbotron class can create a block with background, bg-(color) and text-(color) can change background and text color respectively. As for the supported color, you can check bootstrap4 tutorials on this [website](https://www.w3schools.com/bootstrap4/default.asp). ```htmlembedded= <div style="padding-top:55px; margin-bottom:20px; padding-bottom:35px;" class="jumbotron jumbotron-fluid bg-info text-white"> <h1 style="padding-bottom:20px;" class="text-center display-3"> NCRL Apriltag Localization </h1> <p class="text-center display-8">This is a web control interface for NCRL Apriltag Localization Project.</p> </div> ``` 3. Grid Layout > Layout can be created by setting rows and columns. The total number of columns in a single row is 12. ```htmlembedded= <div class="col-2 jumbotron" style="padding-bottom:2rem; padding-top:1rem; padding-right:1rem; padding-left:1rem; margin-right:1rem;"> <h3>Connection status</h3> <p class="text-muted" v-if="!connected && !failure">Not Connected</p> <p class="text-danger" v-if="failure">Error</p> <p class="text-success" v-if="connected">Connected</p> <label for="uav_server">UAV_server: </label><br/> <input id="uav_server" type="text" v-model="ws_address"/><br/> <button class="btn btn-danger" style="margin-top:10px; margin-bottom:10px;" @click="disconnect" v-if="connected">Disconnect</button> <button class="btn btn-success" style="margin-top:10px; margin-bottom:10px;" @click="connect" v-if="!connected">Connect</button> </div> ``` 4. Common attribute for Vue.js + v-if="[variable name]": > If the corresponding variable in javascriptis true, then the html line will be available. Otherwise, it'll be commented. + @click="[function name]": > When the button is pressed, then the function in method in javascript will be triggered. + v-model="[variable name]": > When a value is given to an input tag, the value will also be assigned to the corresponding variable in javascript. #### Web Functionality #### JAVASCRIPT Basic Tutorials 1. Load main.js in GUI.html's body ```htmlembedded= <script type="text/javascript" src="main.js"></script> ``` 2. Creat a vue object in main.js ```javascript= var app = new Vue({ el:'#[id]', data: { test_var: 'test', }, methods: { test: function(){ console.log(this.test_var) } }, updated(){ }, }) ``` > <div style="text-align: justify">El correspond to the id of the division you want the vue object to link to. In our case, it is linked to the 'control_gui' div. Data is the section where you can declare class variables. In methods, you can define functions that can be triggered from the html file. In updated, the lines will be executed once the html(webpage) has any modification. For instance, to implement the function of enabling a button when the input is given, the underlying code should be written in updated. There are other built-in for Vue.js to use beside el, data, methods and updated that you can find in tutorials. </div> * methods * updated 3. Streaming video ```javascript= showCamera: function(){ console.log('set camera method') this.cameraViewer = new MJPEGCANVAS.Viewer({ divID: 'mjpeg', host: '0.0.0.0', width: 640, height: 480, topic: '/iris1/camera_forward/color/image_raw', port: 8080, }) ``` 4. Publishing Topic ```javascript= // Create a control signal publisher: take off 1, return 2, stop 0 this.control_pub = new ROSLIB.Topic({ ros: this.ros, name: '/control_phase', messageType: 'std_msgs/Int8' }); this.control_signal = new ROSLIB.Message({ data: 0 }) this.control_pub.publish(this.control_signal) ``` 6. Subscribing Topic ```javascript= this.location_sub = new ROSLIB.Topic({ ros: this.ros, name: '/location', messageType: 'std_msgs/Int8' }) this.location_sub.subscribe(function(message){ document.getElementById("uav_location").innerHTML = message.data; }) ``` ==Function in subscribe cannot modify class variable in Vue??== ## Server (UAV) 1. Install rosbridge server ```console= sudo apt-get install ros-melodic-rosbridge-server ``` 2. Install web video server ```console= sudo apt-get install ros-melodic-web-video-server ``` 3. Configure launch file ```xml= <launch> <include file="$(find rosbridge_server)/launch/rosbridge_websocket.launch"> <arg name="port" value="9090"/> </include> <node name="web_video_server1" pkg="web_video_server" type="web_video_server"> <param name="port" type="int" value="8080" /> <param name="address" type="string" value="0.0.0.0" /> <param name="server_threads" type="int" value="1" /> <param name="ros_threads" type="string" value="2" /> <param name="width" type="int" value="1080" /> <param name="height" type="int" value="720" /> <param name="quality" type="int" value="90" /> </node> </launch> ``` ## Topic List ### Publisher - [X] /control_phase * messageType: std_msgs/Int8 `take off: 1, return: 2, stop: 0` - [X] /flight_destination * messageType: std_msgs/Int8 `Tag ID` - [X] /flight_height * messageType: std_msgs/Int8 ### Subscriber - [X] /id - [X] /status - [X] /location - [X] /flight_height ## Implementation Problem 1. Cannot access http server from other device * Note that permission in FireWall might affect the accessability * Use ifconfig to check the device's public ip, that is running the http server. Make sure that the address on other address is the server's device's public ip ## Demo ![](https://i.imgur.com/6XEPBXB.png) [Github](https://github.com/Andreew9504089/AprilTag_Localization/tree/master/src/single_page_gui) ### How to Use * Launch 1. roslaunch robot_gui_bridge websocket.launch on terminal 1 2. cd single_page_gui & python -m SimpleHTTPServer on terminal 2 3. check ip using ifconfig (inet), check websocket_port on terminal 1 and http_port on terminal 2 4. on other device, go to ip:http_port 5. choose GUI.html * Connection 1. In web gui, enter uav port as websocket_port and uav ip as ip https://drive.google.com/file/d/1k9Bsl7ri-q1ngy1nkoERPu9QAObmdXtr/view?usp=sharing ![](https://i.imgur.com/X7xfeIz.jpg =200x) * Control Panel ![](https://i.imgur.com/3nfAd6e.png) 1. Unlock &emsp; After connecting to uav's websocket server and launching web_control, unlock the control panel. + status: 'Standby' `Take off will be enabled` + status: 'Hovering' `Return & Land will be enabled` + status: 'Flying' `Kill will be enabled (not implemented yet)` 2. Take off `Land will be enabled` 3. Land `Take off will be enabled` 4. Cruise Height `Not implemented yet, NCRL controller doesn't support height command` 5. Turning direction `Not implemented yet` 6. Hovering Time `Not implemented yet` 7. Select Destination `Land & Return will be enabled` `Start will be enabled once the input destination is typed` 8. Indication light `Green: not in the middle of mission` `Red: In the middle of a mission (Not suggested to send command(control panel is not protected))` ### Feature of Adding OpenStreetMap using Leaflet #### [Tutorial](https://www.letswrite.tw/leaflet-osm-basic/) #### Demo ![](https://i.imgur.com/7oajqYI.png)