From 15571dff669fa113ad280a68d086f64718a33259 Mon Sep 17 00:00:00 2001
From: Jake <jake.read@cba.mit.edu>
Date: Wed, 14 Nov 2018 12:03:06 -0500
Subject: [PATCH] ongoing

---
 lib/atkroute.js                   |  1 +
 lib/atkunit.js                    |  9 +++++++++
 lib/packets.js                    |  2 +-
 main.js                           |  5 +++--
 modules/hardware/atkseriallink.js |  1 +
 modules/hardware/atkstepper.js    | 26 ++++++++++++++++++++++----
 views.js                          |  2 +-
 7 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/lib/atkroute.js b/lib/atkroute.js
index 52a0f1a..02c2ba4 100644
--- a/lib/atkroute.js
+++ b/lib/atkroute.js
@@ -25,6 +25,7 @@ function ATKRoute(route, calls) {
     atkroute.onMessage = function(msg){
         // one key at a time, for now
         var key = msg[0].toString()
+        // *could* slice the key out at this point, but nah 
         if(this.calls[key] != null){
             this.calls[key](msg)
         }
diff --git a/lib/atkunit.js b/lib/atkunit.js
index ed784f8..72c02ad 100644
--- a/lib/atkunit.js
+++ b/lib/atkunit.js
@@ -22,6 +22,8 @@ function Hardware(){
 	hardware.state = State()
 	var state = hardware.state 
 
+	state.reset = Button('reset hardware', onReset)
+
 	state.test = Button('test network', onNetworkTest)
 	state.message = 'click above to test network'
 	state.route = '0,0' // default  
@@ -30,6 +32,13 @@ function Hardware(){
 		hardware.route.route = state.route 
 	})
 
+	function onReset(){
+		var rstpck = new Array()
+		rstpck.push(128)
+		state.message = 'reset command issued'
+		hardware.route.send(rstpck)
+	}
+
 	function onNetworkTest(){
 		var tstpck = new Array()
 		tstpck.push(127)
diff --git a/lib/packets.js b/lib/packets.js
index ba33c2c..ad63359 100644
--- a/lib/packets.js
+++ b/lib/packets.js
@@ -13,7 +13,7 @@ function unPack32(arr){
 		var unPacked = arr[0] << 24 | arr[1] << 16 | arr[2] << 8 | arr[3] 
 		return unPacked
 	} else {
-		console.log("ERR: arr > 4 at unPack32")
+		console.log("ERR: arr > 4 at unPack32", arr)
 	}
 	
 }
diff --git a/main.js b/main.js
index 40cf93b..8570552 100644
--- a/main.js
+++ b/main.js
@@ -29,11 +29,12 @@ const Programs = require('./programs.js')
 
 var program = Programs.new('new program')
 
+var step = Programs.loadModuleFromSource(program, './modules/hardware/atkstepper.js')
+
 // UI 
 const View = require('./views.js')
 View.startHttp() 
 View.startWs() 
 
 Programs.assignSocket(View.uiSocket)
-View.assignProgram(program)
-
+View.assignProgram(program)
\ No newline at end of file
diff --git a/modules/hardware/atkseriallink.js b/modules/hardware/atkseriallink.js
index ef40a5d..fcbd8b7 100644
--- a/modules/hardware/atkseriallink.js
+++ b/modules/hardware/atkseriallink.js
@@ -191,6 +191,7 @@ function ATKSerialLink() {
             if (returnRoute.toString() === atkSerialLink.routes[key].route.toString()) {
                 // strip header and return message 
                 var msg = pckt.slice(pckt.indexOf(255) + 1)
+                // this slices down to the keys ... doesn't take the keys away 
                 match = true 
                 atkSerialLink.routes[key].onMessage(msg)
             }
diff --git a/modules/hardware/atkstepper.js b/modules/hardware/atkstepper.js
index b7318bf..1a9fdf7 100644
--- a/modules/hardware/atkstepper.js
+++ b/modules/hardware/atkstepper.js
@@ -20,17 +20,30 @@ function Stepper() {
     stepper.description.alt = 'software representation of stepper motor'
 
     stepper.inputs = {
-        move: Input('move instruction', onNewInstruction),
+        trapezoid: Input('move instruction', onNewInstruction),
+        accel: Input('number', onAccelCommand),
         rmtrig: Input('event', onRawMove)
     }
 
     stepper.outputs = {
         ack: Output('move acknowledgement'),
+        position: Output('number')
     }
 
-    // alias to state (from Hardware() beginnings)
+    // ptr to state (from Hardware() beginnings)
     var state = stepper.state
 
+    // for acceleration moves, in steps/s/s 
+    state.rate = 2000
+
+    state.onChange('rate', () => {
+        if(state.rate > 2000){
+            state.rate = 2000 
+        } else if (state.rate < 100){
+            state.rate = 100 
+        }
+    })
+
     state.axis = 'X'
     state.spu = 200 // steps per unit 
     state.rawMove = -10
@@ -55,6 +68,10 @@ function Stepper() {
         sendToHardware(testMove)
     }
 
+    function onAccelCommand(num){
+        // 1 or 0 for start or stop accelerating 
+    }
+
     function onNewInstruction(move) {
         // console.log('move to stepper', stepper.state.axis, move)
         // pick out axis (check if it's a wait move)
@@ -200,15 +217,16 @@ function Stepper() {
     stepper.route.subscribe(131, onHardwareStepsComplete)
 
     function onHardwareStepsComplete(pckt) {
-        var stepsMade = PCKT.unPack32(pckt)
+        var stepsMade = PCKT.unPack32(pckt.slice(1))
         state.position += stepsMade
         var unitsMade = stepsMade / state.spu
         var ack = {
             axis: state.axis,
             increment: unitsMade
         }
-        console.log("STEPPER ACK MOVE", state.axis)
+        console.log("STEPPER ACK MOVE", state.axis, stepsMade)
         stepper.outputs.ack.emit(ack)
+        stepper.outputs.position.emit(state.position)
     }
 
     stepper.route.subscribe(132, onHardwareWaitComplete)
diff --git a/views.js b/views.js
index 9b419bf..ff9d5af 100644
--- a/views.js
+++ b/views.js
@@ -34,7 +34,7 @@ function startHttp() {
 
     // through this window
     http.listen(8080, () => {
-        console.log('listening on 8080 for static files')
+        console.log('RNDMC is listening on localhost:8080')
     })
 }
 
-- 
GitLab