From 56973ba3f6c85a130c9f2a0e80eb14bca0803c1f Mon Sep 17 00:00:00 2001
From: Jake <jake.read@cba.mit.edu>
Date: Sat, 9 Feb 2019 19:47:09 -0500
Subject: [PATCH] sww demo is go

---
 client/client.js               |   2 +-
 littlerascal.js                |  32 +++++++----
 modules/hardware/atkstepper.js |   2 +-
 modules/motion/neilparse.js    |  13 +++--
 modules/motion/simplanner.js   | 100 ++++-----------------------------
 modules/motion/usermove.js     |  78 +++++++++++++++++++++++++
 views.js                       |   8 +--
 7 files changed, 123 insertions(+), 112 deletions(-)
 create mode 100644 modules/motion/usermove.js

diff --git a/client/client.js b/client/client.js
index e06abe4..f41c7e8 100644
--- a/client/client.js
+++ b/client/client.js
@@ -60,7 +60,7 @@ window.onload = function() {
 
     nav = document.getElementById('nav')
 
-    const socket = new WebSocket('ws://localhost:8081')
+    const socket = new WebSocket('ws://localhost:8082')
 
     socket.onopen = function(evt) {
         // pass to global ref 
diff --git a/littlerascal.js b/littlerascal.js
index 769634c..95431ae 100644
--- a/littlerascal.js
+++ b/littlerascal.js
@@ -6,20 +6,19 @@ const Programs = require('./programs.js')
 var program = Programs.new('new program')
 
 /* ok
-- new plotter 
-- ack four 
-- go to pos block 
-- neil:mm adjust 
+- video
+ - cut: red
+ - mountain: green
+ - valley: blue 
 - git push
-- add y axis network wait ? 
 */
 
 // das link 
 var link = Programs.loadModuleFromSource(program, './modules/hardware/atkseriallink.js')
 link.startUp()
-link.state.log = true
+link.state.log = false
 
-let steps = 10
+let steps = 80
 
 // x motor
 var xm = Programs.loadModuleFromSource(program, './modules/hardware/atkstepper.js')
@@ -49,9 +48,11 @@ zm.state.spu = -steps
 zm.state.axis = 'Z'
 
 var planner = Programs.loadModuleFromSource(program, './modules/motion/simplanner.js')
-planner.state.minSpeed = 1
-planner.state.accel = 300
-planner.netWindow = 1
+planner.state.minSpeed = 0.1
+planner.state.accel = 20
+
+var outButton = Programs.loadModuleFromSource(program, './modules/motion/usermove.js')
+outButton.state.moveSpeed = 200
 
 /*
 
@@ -90,9 +91,16 @@ planner.outputs.moves.attach(xm.inputs.trapezoid)
 planner.outputs.moves.attach(ylm.inputs.trapezoid)
 planner.outputs.moves.attach(ylr.inputs.trapezoid)
 planner.outputs.moves.attach(zm.inputs.trapezoid)
+
 xm.outputs.ack.attach(planner.inputs.acks)
+xm.outputs.ack.attach(planner.inputs.positions)
+
 ylm.outputs.ack.attach(planner.inputs.acks)
+ylm.outputs.ack.attach(planner.inputs.positions)
+ylr.outputs.ack.attach(planner.inputs.acks)
+
 zm.outputs.ack.attach(planner.inputs.acks)
+zm.outputs.ack.attach(planner.inputs.positions)
 
 let moveToPlanner = {
 	position: {
@@ -109,6 +117,7 @@ link.onOpen = function(){
 }
 
 // ws input
+// fusion runs on socket 1234 ... 
 
 var sockit = Programs.loadModuleFromSource(program, './modules/pipes/websocket.js')
 var np = Programs.loadModuleFromSource(program, './modules/motion/neilparse.js')
@@ -117,6 +126,8 @@ sockit.outputs.data.attach(np.inputs.array)
 np.outputs.move.attach(planner.inputs.instruction)
 planner.outputs.moveComplete.attach(np.inputs.shift)
 
+outButton.outputs.move.attach(planner.inputs.instruction)
+
 // UI HOOKUP
 
 Programs.setView(program, {
@@ -131,6 +142,7 @@ let motorspace = 400
 
 Programs.setUI(sockit, top, top)
 Programs.setUI(np, 600, top)
+Programs.setUI(outButton, top, 300)
 
 Programs.setUI(link, 1800, top)
 Programs.setUI(xm, motorbar, top)
diff --git a/modules/hardware/atkstepper.js b/modules/hardware/atkstepper.js
index 0963618..6546575 100644
--- a/modules/hardware/atkstepper.js
+++ b/modules/hardware/atkstepper.js
@@ -191,7 +191,7 @@ function Stepper() {
             console.log(dMove)
         }
 
-        if(true) console.log('------------------- DMOVE', state.axis, dMove)
+        if(true) console.log('------------------- DMOVE', state.axis, dMove.steps)
 
         var packet = new Array()
         // step blocks are #131
diff --git a/modules/motion/neilparse.js b/modules/motion/neilparse.js
index 0e31574..7bbb405 100644
--- a/modules/motion/neilparse.js
+++ b/modules/motion/neilparse.js
@@ -25,10 +25,11 @@ function NeilParser() {
     // or they don't get getter / settered when we do
     neilParser.state = State()
     var state = neilParser.state
-    state.lift = 50
-    state.moveSpeed = 200
-    state.plungeSpeed = 50
-    state.jogSpeed = 600
+    state.unitsPerMM = 8
+    state.lift = 5
+    state.moveSpeed = 100
+    state.plungeSpeed = 20
+    state.jogSpeed = 200
     state.listLength = 0
 
     neilParser.inputs = {
@@ -78,8 +79,8 @@ function NeilParser() {
                 if (Array.isArray(flat[j])) {
                     let move = {
                         position: {
-                            X: flat[j][0],
-                            Y: flat[j][1]
+                            X: flat[j][0] / state.unitsPerMM,
+                            Y: flat[j][1] / state.unitsPerMM
                         }
                     }
                     if (upState) {
diff --git a/modules/motion/simplanner.js b/modules/motion/simplanner.js
index 05cb5f8..8a70348 100644
--- a/modules/motion/simplanner.js
+++ b/modules/motion/simplanner.js
@@ -30,36 +30,24 @@ function Planner() {
     var state = planner.state // reference pass attempt?
 
     state.axisIDs = 'X,Y,Z'
-    state.onUiChange('axisIDs', axisIDUpdate)
 
     state.accel = 200 // units/s/s
     state.minSpeed = 1 // units/s
 
     state.position = [0, 0, 0]
 
-    // should be grn / red button ... 
     state.isRunning = 1
-    state.onUiChange('isRunning', netStateRefresh)
 
     state.netWindow = 1
-    state.netState = [0, 0, 0]
+    state.netState = 0
 
     planner.ui = UI()
     var ui = planner.ui
 
-    ui.addElement('startStopButton', 'ui/uiButton.js')
-    ui.startStopButton.subscribe('onload', function(msg) {
-        ui.startStopButton.send({
-            calls: 'setText',
-            argument: 'start / stop planner'
-        })
-    })
-    ui.startStopButton.subscribe('onclick', onStartStop)
-
     planner.inputs = {
         instruction: Input('move instruction', onNewInstruction),
         acks: Input('move acknowledgement', onAck),
-        run: Input('boolean', onRunInstruction)
+        positions: Input('new positions', onPositions)
     }
 
     planner.outputs = {
@@ -67,23 +55,16 @@ function Planner() {
         moveComplete: Output('number')
     }
 
-    // we'll use one of these to assert / do things 
-    // after the module is loaded, and state is copied etc
-    // i.e. one thing we can do is assert a starting value 
-    planner.init = function() {
-        state.isRunning = 0
-        state.netState = [0, 0, 0]
-    }
-
     /*
     ------------------------------------------------------
     UPDATING / SETUP
     ------------------------------------------------------
     */
 
-    function onAck(msg) {
+    function onPositions(msg){
         //console.log("Planner onAck:", msg)
         // update position, net states, run netCheck
+        // positions ... 
         var axes = state.axisIDs.split(',')
         var match = axes.indexOf(msg.axis)
         if (match !== -1) {
@@ -91,82 +72,23 @@ function Planner() {
             var newPos = state.position.slice(0)
             newPos[match] += msg.increment
             state.position = newPos
-            var newNetState = state.netState.slice(0)
-            newNetState[match] -= 1
-            state.netState = newNetState
-            if (verbose) console.log('NEW NET STATE', newNetState)
         } else {
             console.log('ERR in PLANNER: missed axis on ack from stepper')
         }
-        netStateRefresh()
     }
 
-    function netStateRefresh() {
-        // check if received all four, 
-        // send a packet 
-        var ns = state.netState
-        var i = 0
-        // check equality
-        while (ns[i] == ns[i + 1]) {
-            i++
-            // console.log('EQUALITY LOOP')
-            if (i > ns.length - 1) {
-                break
-            }
-        }
-        if (i == ns.length - 1) {
+    function onAck(msg) {
+        // just count to four 
+        state.netState ++
+        if(state.netState >= 4){
             console.log('Planner confirms Move Complete')
+            state.netState = 0
             planner.outputs.moveComplete.emit(1)
         }
-    }
-
-    function onRunInstruction(boolean) {
-        if (boolean) {
-            if (state.isRunning) {
-                // already running, do nothing 
-            } else {
-                state.isRunning = 1
-                netStateRefresh()
-            }
-        } else {
-            if (state.isRunning) {
-                state.isRunning = 0
-            } else {
-                // oy 
-            }
-        }
-    }
-
-    function axisIDUpdate() {
-        var axes = state.axisIDs.split(',')
-        var position = new Array(axes.length)
-        position.fill(0)
-        var packetState = new Array(axes.length)
-        for (i in axes) {
-            position.push(0)
-            packetState.push(0)
-            // could do 
-            //planner.outputs[axes[i]] = Output('move instruction')
-        }
-        state.position = position
-        state.packetState = packetState
-        console.log(planner)
-    }
-
-    function onStartStop() {
-        if (state.isRunning) {
-            state.isRunning = 0
-        } else {
-            state.isRunning = 1
-            netStateRefresh()
-        }
+        
     }
 
     function sendMoveToNetwork(move) {
-        for (i in state.netState) {
-            state.netState[i]++
-        }
-        state.netState = state.netState
         planner.outputs.moves.emit(move)
     }
 
@@ -203,8 +125,6 @@ function Planner() {
             console.log('------------------ !ACHTUNG! -------------------')
             console.log('------------------ !ACHTUNG! -------------------')
             console.log('planner throwing zero length vector')
-            zlcounter++
-            console.log(zlcounter)
             // this means we need another one from the queue
             planner.outputs.moveComplete.emit(1)
         } else {
diff --git a/modules/motion/usermove.js b/modules/motion/usermove.js
new file mode 100644
index 0000000..e640f07
--- /dev/null
+++ b/modules/motion/usermove.js
@@ -0,0 +1,78 @@
+// boilerplate atkapi header
+const JSUnit = require('../../src/jsunit.js')
+
+let Input = JSUnit.Input
+let Output = JSUnit.Output
+let State = JSUnit.State
+
+// interface elements
+const JSUI = require('../../src/jsui.js')
+let UI = JSUI.UI
+
+// unit to convert neil's mods-output arrays into human readable move objects 
+// probably do flow control here also ?
+
+function UserMove() {
+
+    var userMove = {
+        description: {
+            name: 'userMove gen',
+            alt: 'line of userMove -> points'
+        }
+    }
+
+    // one caveat here is that we can't dynamically add objects to this, 
+    // or they don't get getter / settered when we do
+    userMove.state = State()
+    var state = userMove.state
+    state.moveTo = '100,100,10'
+    state.moveSpeed = 100
+
+    userMove.inputs = {
+        go: Input('event', (evt) => {
+            sendMove()
+        })
+    }
+
+    userMove.outputs = {
+        move: Output('move')
+    }
+
+    userMove.ui = UI()
+
+    var ui = userMove.ui
+    ui.addElement('goButton', 'ui/uiButton.js')
+    ui.goButton.subscribe('onload', function(msg) {
+        ui.goButton.send({
+            calls: 'setText',
+            argument: 'click to send move'
+        })
+    })
+    ui.goButton.subscribe('onclick', (evt) => {
+        sendMove()
+    })
+
+    function sendMove() {
+        var axes = state.moveTo.split(',')
+        if (axes.length == 3) {
+            let move = {
+                position: {
+                    X: parseFloat(axes[0]),
+                    Y: parseFloat(axes[1]),
+                    Z: parseFloat(axes[2]),
+                },
+                speed: state.moveSpeed
+            }
+            console.log('User Move is', move)
+            userMove.outputs.move.emit(move)
+        } else {
+            console.log("len for moves must be three")
+        }
+
+    }
+
+    return userMove
+}
+
+// export the module 
+module.exports = UserMove
\ No newline at end of file
diff --git a/views.js b/views.js
index fb211bf..4dd0474 100644
--- a/views.js
+++ b/views.js
@@ -44,22 +44,22 @@ function startHttp() {
     })
 
     // through this window
-    http.listen(8080, () => {
-        console.log('RNDMC is listening on localhost:8080')
+    http.listen(8081, () => {
+        console.log('RNDMC is listening on localhost:8081')
     })
 }
 
 
 function startWs() {
     // and listening for requests here
-    const wss = new WebSocket.Server({ port: 8081 })
+    const wss = new WebSocket.Server({ port: 8082 })
 
     wss.on('connection', (ws) => {
         sckt = ws
         // say hello
         socketSend('console', 'hello client')
         // send current config as list of all modules
-        console.log('socket open on 8081')
+        console.log('socket open on 8082')
         ws.on('message', (evt) => {
             socketRecv(evt)
         })
-- 
GitLab