Browse Source

add WebSocket remote robot example

moononournation 7 years ago
parent
commit
b0e6b48ad7
2 changed files with 140 additions and 0 deletions
  1. 83 0
      bin/ws-robot.html
  2. 57 0
      bin/ws-robot.lua

+ 83 - 0
bin/ws-robot.html

@@ -0,0 +1,83 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8">
+    <title>WebSocket Remote Robot</title>
+    <style>
+      canvas {
+        border: 1px solid #000000;
+      }
+    </style>
+  </head>
+  <body>
+    <h1>WebSocket Remote Robot</h1>
+    <p>Touch the canvas to control the robot.</p>
+    <canvas id="canvas" width="400" height="400"></canvas>
+    <script language="javascript" type="text/javascript">
+      var wsUri = "ws://" + location.hostname + "/ws-robot.lc";
+      var websocket;
+      var canvas, context;
+
+      function init() {
+        websocket = new WebSocket(wsUri);
+        websocket.onopen = function(evt) { };
+        websocket.onclose = function(evt) { };
+        websocket.onmessage = function(evt) { };
+        websocket.onerror = function(evt) { };
+
+        canvas = document.getElementById('canvas');
+        context = canvas.getContext("2d");
+
+        // Add mouse event listeners to canvas element
+        canvas.addEventListener("mousedown", press, false);
+        canvas.addEventListener("mousemove", drag, false);
+        canvas.addEventListener("mouseup", release);
+        canvas.addEventListener("mouseout", cancel, false);
+
+        // Add touch event listeners to canvas element
+        canvas.addEventListener("touchstart", press, false);
+        canvas.addEventListener("touchmove", drag, false);
+        canvas.addEventListener("touchend", release, false);
+        canvas.addEventListener("touchcancel", cancel, false);
+      }
+
+      function sendMessage(message) {
+        websocket.send(message);
+      }
+
+      function press(e) {
+        var mouseX = (e.changedTouches ? e.changedTouches[0].pageX : e.pageX) - this.offsetLeft;
+        var mouseY = (e.changedTouches ? e.changedTouches[0].pageY : e.pageY) - this.offsetTop;
+        if (mouseX < 150) {
+          sendMessage('L');
+        } else if (mouseX > 250) {
+          sendMessage('R');
+        } else {
+          sendMessage('F');
+        }
+      }
+
+      function drag(e) {
+		var mouseX = (e.changedTouches ? e.changedTouches[0].pageX : e.pageX) - this.offsetLeft;
+		var mouseY = (e.changedTouches ? e.changedTouches[0].pageY : e.pageY) - this.offsetTop;
+        if (mouseX < 150) {
+          sendMessage('L');
+        } else if (mouseX > 250) {
+          sendMessage('R');
+        } else {
+          sendMessage('F');
+        }
+      }
+
+      function release(e) {
+        sendMessage('S');
+      }
+
+      function cancel(e) {
+        sendMessage('S');
+      }
+
+      window.addEventListener("load", init, false);
+    </script>
+  </body>
+</html>

+ 57 - 0
bin/ws-robot.lua

@@ -0,0 +1,57 @@
+return function (socket)
+  --1=GPIO5, 2=GPIO4, 3=GPIO0, 4=GPIO2, 5=GPIO14, 6=GPIO12,
+  --7=GPIO13, 8=GPIO15, 9=GPIO3, 10=GPIO1, 11=GPIO9, 12=GPIO10
+  local leftpwm, leftduty, leftpin1, leftpin2 = 1, 818, 2, 4 --GPIO5, 80%, GPIO4, GPIO2
+  local rightpwm,rightduty, rightpin1, rightpin2 = 5, 717, 6, 7 --GPIO14, 70%, GPIO12, GPIO13
+  -- timer id(0-6), interval in ms
+  local tmrId, tmrMs = 4, 800
+
+  -- init motors
+  pwm.setup(leftpwm, 500, leftduty)
+  pwm.start(leftpwm)
+  gpio.mode(leftpin1, gpio.OUTPUT)
+  gpio.mode(leftpin2, gpio.OUTPUT)
+  pwm.setup(rightpwm, 500, rightduty)
+  pwm.start(rightpwm)
+  gpio.mode(rightpin1, gpio.OUTPUT)
+  gpio.mode(rightpin2, gpio.OUTPUT)
+
+  function socket.onmessage(payload, opcode)
+    curStep = payload:sub(1, 1)
+    if (curStep == 'F') then
+      gpio.write(leftpin1, gpio.LOW)
+      gpio.write(leftpin2, gpio.HIGH)
+      gpio.write(rightpin1, gpio.LOW)
+      gpio.write(rightpin2, gpio.HIGH)
+    elseif (curStep == 'B') then
+      gpio.write(leftpin1, gpio.HIGH)
+      gpio.write(leftpin2, gpio.LOW)
+      gpio.write(rightpin1, gpio.HIGH)
+      gpio.write(rightpin2, gpio.LOW)
+    elseif (curStep == 'L') then
+      gpio.write(leftpin1, gpio.LOW)
+      gpio.write(leftpin2, gpio.HIGH)
+      gpio.write(rightpin1, gpio.HIGH)
+      gpio.write(rightpin2, gpio.LOW)
+    elseif (curStep == 'R') then
+      gpio.write(leftpin1, gpio.HIGH)
+      gpio.write(leftpin2, gpio.LOW)
+      gpio.write(rightpin1, gpio.LOW)
+      gpio.write(rightpin2, gpio.HIGH)
+    elseif (curStep == 'S') then
+      gpio.write(leftpin1, gpio.LOW)
+      gpio.write(leftpin2, gpio.LOW)
+      gpio.write(rightpin1, gpio.LOW)
+      gpio.write(rightpin2, gpio.LOW)
+    end
+  end
+
+  function socket.onclose()
+    pwm.stop(leftpwm)
+    gpio.write(leftpin1, gpio.LOW)
+    gpio.write(leftpin2, gpio.LOW)
+    pwm.stop(rightpwm)
+    gpio.write(rightpin1, gpio.LOW)
+    gpio.write(rightpin2, gpio.LOW)
+  end
+end