Fix communication problems
authorNick Downing <nick.downing@lifx.co>
Wed, 18 Mar 2020 20:30:19 +0000 (07:30 +1100)
committerNick Downing <nick.downing@lifx.co>
Wed, 18 Mar 2020 20:30:19 +0000 (07:30 +1100)
blockly.html.jst
js/mirobot.js

index 44e30fc..35f1a1e 100644 (file)
@@ -256,27 +256,29 @@ return async env => {
           if (blockly_stop) {
             console.log('stop in deviceMoveTo()')
             throw new Error('stopped')
+            console.log('can\'t get here')
           }
-          move_axes([500, 500, 500, 500, 500], [a, b, c, d, e])
-          while (tx_queue.length)
-            await sleep(10)
+ console.assert(!blockly_stop, '!blockly_stop')
+          await move_axes([500, 500, 500, 500, 500], [a, b, c, d, e])
         }
 
         var deviceGripper = async f => {
           if (blockly_stop) {
             console.log('stop in deviceGripper()')
             throw new Error('stopped')
+            console.log('can\'t get here')
           }
-          move_axis(5, 500, f)
-          while (tx_queue.length)
-            await sleep(10)
+ console.assert(!blockly_stop, '!blockly_stop')
+          await move_axis(5, 500, f)
         }
 
         var deviceDelay = async millisecs => {
           if (blockly_stop) {
             console.log('stop in deviceDelay()')
             throw new Error('stopped')
+            console.log('can\'t get here')
           }
+ console.assert(!blockly_stop, '!blockly_stop')
           await sleep(millisecs)
         }
 
@@ -285,11 +287,9 @@ return async env => {
           () => {
             ;(
               async () => {
-                if (device !== undefined && !device_busy) {
+                if (!device_busy) {
                   device_busy = true;
-                  move_axes([500, 500, 500, 500, 500, 500], [0, 0, 0, 0, 0, 0]);
-                  while (tx_queue.length)
-                    await sleep(10)
+                  await move_axes([500, 500, 500, 500, 500, 500], [0, 0, 0, 0, 0, 0]);
                   device_busy = false;
                 }
               }
@@ -305,7 +305,7 @@ return async env => {
               console.log('stopping blockly');
               blockly_stop = true;
             }
-            else if (device !== undefined && !device_busy) {
+            else if (!device_busy && device !== undefined) {
               console.log('starting blockly');
               blockly_stop = false;
               blockly_active = true;
index 90025a9..dd356e3 100644 (file)
@@ -10,7 +10,7 @@ let rx_task = async device => {
   rx_active = true
   try {
     while (rx_cont) {
-      transfer_result = await device.transferIn(2, 1)
+      transfer_result = await device.transferIn(2, 256)
       //console.log('transfer_result', transfer_result)
       if (transfer_result.status === 'ok')
         for (let i = 0; i < transfer_result.data.byteLength; ++i) {
@@ -19,7 +19,7 @@ let rx_task = async device => {
             break
           }
           rx_buf += String.fromCharCode(
-            transfer_result.data.getUint8(0)
+            transfer_result.data.getUint8(i)
           )
         }
     }
@@ -38,84 +38,70 @@ let rx_stop = async () => {
   console.log('rx_stop done')
 }
 
-let tx_text = async (device, text) => {
-  console.log('tx_text', text)
+let tx_wait = async (device, text, timeout) => {
+  console.log('tx_wait', text)
+
+  if (rx_buf.length) {
+    console.log('unexpected rx_buf', rx_buf)
+    rx_buf = ''
+  }
+
   data = new Uint8Array(text.length);
-  for (var i = 0; i < text.length; ++i)
+  for (let i = 0; i < text.length; ++i)
     data[i] = text.charCodeAt(i);
   await device.transferOut(2, data.buffer)
-}
-let tx_wait = async (device, text, timeout) => {
-  await tx_text(device, text)
-  for (let i = 0; i < timeout; ++i) {
+
+  for (let i = 0; timeout === undefined || i < timeout; ++i) {
     if (rx_buf.slice(0, 1) === '>') {
       rx_buf = rx_buf.slice(1)
+      console.log('tx_wait returns true')
       return true
     }
     await sleep(1)
   }
+  console.log('tx_wait returns false')
   return false
 }
-let tx_active = false
-let tx_cont = false
-let tx_queue = []
-let tx_task = async device => {
-  console.log('tx_task')
-  tx_active = true
-  try {
-    while (tx_cont) {
-      if (tx_queue.length)
-        tx_wait(device, tx_queue.shift(), 60000)
-      await sleep(1)
-    }
-  }
-  catch (err) {
-    console.log('tx_task err', err.message)
-  }
-  console.log('tx_task done')
-  tx_active = false
-}
-let tx_stop = async () => {
-  console.log('tx_stop')
-  tx_cont = false
-  while (tx_active)
-    await sleep(10)
-  console.log('tx_stop done')
-}
 
 let device, device_busy = false
 let axes = [
   {
+    dirty: true,
     min_degrees: -90,
     min_position: 14578,
     max_degrees: 90,
     max_position: 35733
   },
   {
+    dirty: true,
     min_degrees: -50,
     min_position: 31031.88888888889,
     max_degrees: 75,
     max_position: 16340.91666666667
   },
   {
+    dirty: true,
     min_degrees: 0,
     min_position: 25155.5,
     max_degrees: 90,
     max_position: 14578
   },
   {
+    dirty: true,
     min_degrees: -45,
     min_position: 30444.25,
     max_degrees: 45,
     max_position: 19866.75
   },
   {
+    dirty: true,
     min_degrees: -45,
     min_position: 30444.25,
     max_degrees: 45,
     max_position: 19866.75
   },
   {
+    dirty: true,
     min_degrees: 0,
     min_position: 25155.5,
     max_degrees: 60,
@@ -123,7 +109,7 @@ let axes = [
   }
 ]
 let setToolboxTimeout;
-let move_axis = (axis, speed, value) => {
+let move_axis = async (axis, speed, value) => {
   if (value < axes[axis].min_degrees)
     value = axes[axis].min_degrees
   else if (value > axes[axis].max_degrees)
@@ -138,13 +124,14 @@ let move_axis = (axis, speed, value) => {
       (axes[axis].max_position - axes[axis].min_position) /
       (axes[axis].max_degrees - axes[axis].min_degrees))
   if (device !== undefined)
-    tx_queue.push(
+    await tx_wait(
+      device,
       `${String.fromCharCode(97 + axis)}${speed}${String.fromCharCode(65 + axis)}${axes[axis].position}\n`
     )
   if (setToolboxTimeout !== undefined)
     setToolboxTimeout()
 }
-let move_axes = (speeds, values) => {
+let move_axes = async (speeds, values) => {
   text = ''
   for (let i = 0; i < speeds.length; ++i)
     text += `${String.fromCharCode(97 + i)}${speeds[i]}`
@@ -165,11 +152,28 @@ let move_axes = (speeds, values) => {
     text += `${String.fromCharCode(65 + i)}${axes[i].position}`
   }
   if (device !== undefined)
-    tx_queue.push(text + '\n')
+    await tx_wait(device, text + '\n')
   if (setToolboxTimeout !== undefined)
     setToolboxTimeout()
 }
 
+let dirty_task = async device => {
+  console.log('dirty_task')
+  while (true) {
+    for (let i = 0; i < 6; ++i)
+      if (axes[i].dirty) {
+        try {
+          await move_axis(i, 5000, axes[i].slider.value)
+          axes[i].dirty = false
+        }
+        catch (err) {
+          console.log('dirty_task err', err.message)
+        }
+      }
+    await sleep(1)
+  }
+}
+
 document.addEventListener(
   'DOMContentLoaded',
   () => {
@@ -186,18 +190,11 @@ document.addEventListener(
     axes[5].slider = document.getElementById("slider-f")
     axes[5].value = document.getElementById("value-f")
 
+    dirty_task(); // ignore returned Promise
     for (let i = 0; i < axes.length; ++i) {
-      let value = axes[i].slider.value
-      axes[i].value.innerHTML = value
-      axes[i].position =
-        Math.round(axes[i].min_position +
-          (value - axes[i].min_degrees) *
-          (axes[i].max_position - axes[i].min_position) /
-          (axes[i].max_degrees - axes[i].min_degrees))
-
       let axis = i
       axes[i].slider.oninput = function() {
-        move_axis(axis, 32000, this.value)
+        axes[axis].dirty = true
       }
     }
 
@@ -224,15 +221,10 @@ document.addEventListener(
                     rx_buf = ''
                     rx_task(device)
       
-                    console.assert(!tx_active, '!tx_active')
-                    tx_cont = true
-                    tx_queue = []
-                    tx_task(device)
-
                     await sleep(100)
                     rx_buf = ''
       
-                    if (tx_wait(device, '\n', 100)) {
+                    if (await tx_wait(device, '\n', 100)) {
                       console.log('prompt detected')
                       document.getElementById('connection-status').innerHTML = 'Connected'
                       document.getElementById('connect-disconnect').innerHTML = 'Disconnect'
@@ -242,7 +234,6 @@ document.addEventListener(
                       console.log('no prompt detected')
                       device.close()
                       await rx_stop()
-                      await tx_stop()
                       device = undefined
                     }
                   }
@@ -250,7 +241,6 @@ document.addEventListener(
                     console.log('exception', err.message)
                     device.close()
                     await rx_stop()
-                    await tx_stop()
                     device = undefined
                   }
                 }
@@ -263,7 +253,6 @@ document.addEventListener(
                 // disconnect request
                 device.close()
                 await rx_stop()
-                await tx_stop()
                 device = undefined
                 document.getElementById('connection-status').innerHTML = 'Disconnected'
                 document.getElementById('connect-disconnect').innerHTML = 'Connect'