From 464ffb3e9c6148c9c51a472933a63e133b46bca0 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 18 Sep 2015 01:13:06 -0400
Subject: [PATCH] comm is nearly there

---
 js/SerialMonitor/PositionControlPanelView.js |  2 +-
 js/SerialMonitor/fakeCam.js                  | 12 +++++
 js/SerialMonitor/main.js                     |  3 +-
 js/cam/Cam.js                                | 14 +++++-
 js/cam/assemblers/Assembler.js               |  8 +++
 js/cam/assemblers/Component.js               | 10 +++-
 js/menus/SendMenuView.js                     | 19 +++++---
 js/menus/templates/SendMenuView.html         |  2 +-
 js/models/MachineState.js                    |  9 +---
 js/models/SerialComm.js                      | 51 ++++++++++----------
 10 files changed, 86 insertions(+), 44 deletions(-)
 create mode 100644 js/SerialMonitor/fakeCam.js

diff --git a/js/SerialMonitor/PositionControlPanelView.js b/js/SerialMonitor/PositionControlPanelView.js
index dbe11fb9..c55bc85d 100644
--- a/js/SerialMonitor/PositionControlPanelView.js
+++ b/js/SerialMonitor/PositionControlPanelView.js
@@ -30,7 +30,7 @@ define(['jquery', 'underscore', 'backbone', 'text!PositionControlPanelView.html'
             $("#positionData").html(_.template(positionTemplate)(machineState.toJSON()));
         },
 
-        _askForPosition: function(e){
+        _askForPosition: function(e){//todo should be in machine state init
             if (e) e.preventDefault();
             this.model.send('{"sr":n}');
         },
diff --git a/js/SerialMonitor/fakeCam.js b/js/SerialMonitor/fakeCam.js
new file mode 100644
index 00000000..e3452110
--- /dev/null
+++ b/js/SerialMonitor/fakeCam.js
@@ -0,0 +1,12 @@
+/**
+ * Created by aghassaei on 9/18/15.
+ */
+
+
+define([], function(){
+
+    return {
+        setPosition: function(){}
+    }
+
+});
\ No newline at end of file
diff --git a/js/SerialMonitor/main.js b/js/SerialMonitor/main.js
index d1bd5e11..e97b76d1 100644
--- a/js/SerialMonitor/main.js
+++ b/js/SerialMonitor/main.js
@@ -22,7 +22,8 @@ require.config({
         plist: '../plists/PList',
         commPlist: '../plists/commPlist',
         serialComm: '../models/SerialComm',
-        machineState: '../models/MachineState'
+        machineState: '../models/MachineState',
+        cam: 'fakeCam'
 
     },
 
diff --git a/js/cam/Cam.js b/js/cam/Cam.js
index 76e0d76f..7b3298c9 100644
--- a/js/cam/Cam.js
+++ b/js/cam/Cam.js
@@ -297,7 +297,6 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
 
         simulateCurrentLine: function(callback){
             var lineNum = this.get("simLineNumber");
-            console.log(lineNum);
             three.startAnimationLoop();
             if (lineNum == 0) lattice.hideCells("cells");
             var allLines = this.get("dataOut").split("\n");
@@ -318,6 +317,19 @@ define(['underscore', 'three', 'backbone', 'appState', 'latticeCAM', 'threeModel
             }
         },
 
+        setPosition: function(position){
+            var scale = lattice.get("scale");
+
+            _.each(position, function(val, key){
+                position[key] = val/scale;
+            });
+
+            this.get("assembler").immediatelyMoveTo(position);
+
+
+            three.render();
+        },
+
 
 
 
diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index 665cef16..c19fbbe2 100644
--- a/js/cam/assemblers/Assembler.js
+++ b/js/cam/assemblers/Assembler.js
@@ -232,6 +232,14 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
     
     Assembler.prototype.pause = function(){
     };
+
+    Assembler.prototype.immediatelyMoveTo = function(data){
+        this.components.xAxis.immediatelySetPosition(new THREE.Vector3(data.x, 0, 0));
+        this.components.yAxis.immediatelySetPosition(new THREE.Vector3(0, data.y, 0));
+        this.components.zAxis.immediatelySetPosition(new THREE.Vector3(0, 0, data.z));
+        if (data.a != 0) this.components.substrate.immediatelySetAngle(-Math.PI/2);
+        else this.components.substrate.immediatelySetAngle(data.a);
+    };
     
     Assembler.prototype.moveTo = function(position, speed, settings, callback){
 
diff --git a/js/cam/assemblers/Component.js b/js/cam/assemblers/Component.js
index e67e7b0f..2064ffea 100644
--- a/js/cam/assemblers/Component.js
+++ b/js/cam/assemblers/Component.js
@@ -171,7 +171,15 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
 
 
 
-    //simulation animation
+    //simulation
+
+    Component.prototype.immediatelySetAngle = function(angle){
+        this.secondWrapper.rotation.z = angle;
+    };
+
+    Component.prototype.immediatelySetPosition = function(nextPos){
+        this.object3D.position.set(nextPos.x, nextPos.y, nextPos.z);
+    };
 
     Component.prototype.getObject3D = function(){
         return this.object3D;
diff --git a/js/menus/SendMenuView.js b/js/menus/SendMenuView.js
index ca6fe42b..61a9f6ea 100644
--- a/js/menus/SendMenuView.js
+++ b/js/menus/SendMenuView.js
@@ -23,8 +23,11 @@ define(['jquery', 'underscore', 'menuParent', 'serialComm', 'commPlist', 'text!s
             this.listenTo(this.model, "change:stockSimulationPlaying", this.render);
             this.listenTo(serialComm, "change:isStreaming", this.render);
             this.listenTo(cam, "change:simLineNumber", this._lineNumChanged);
+        },
 
-            this.nextLine = cam.get("simLineNumber");
+        _getPropertyOwner: function($target){
+            if ($target.hasClass("cam")) return cam;
+            return null;
         },
 
         _startStream: function(e){
@@ -44,13 +47,13 @@ define(['jquery', 'underscore', 'menuParent', 'serialComm', 'commPlist', 'text!s
 
         _decrementLineNum: function(e){
             e.preventDefault();
-            var nextLine = this.nextLine - 1;
+            var nextLine = cam.get("simLineNumber") - 1;
             this._setNextLine(nextLine);
         },
 
         _incrementLineNum: function(e){
             e.preventDefault();
-            var nextLine = this.nextLine + 1;
+            var nextLine = cam.get("simLineNumber") + 1;
             this._setNextLine(nextLine);
         },
 
@@ -58,8 +61,12 @@ define(['jquery', 'underscore', 'menuParent', 'serialComm', 'commPlist', 'text!s
             var length = cam.get("dataOut").split("\n").length;
             if (nextLine < 0) nextLine = length-1;
             if (nextLine > length-1) nextLine = 0;
-            this.nextLine = nextLine;
-            $("#nextLine").val(this.nextLine);
+            cam.set("simLineNumber", nextLine);
+        },
+
+        _showNextLine: function(nextLine){
+            console.log(nextLine);
+            $("#nextLine").val(nextLine);
             this._drawGcodeHighlighter(nextLine);
         },
 
@@ -70,7 +77,7 @@ define(['jquery', 'underscore', 'menuParent', 'serialComm', 'commPlist', 'text!s
 
         _lineNumChanged: function(){
             var lineNum = cam.get("simLineNumber");
-            this._setNextLine(lineNum);
+            this._showNextLine(lineNum);
         },
 
         _drawGcodeHighlighter: function(lineNum){
diff --git a/js/menus/templates/SendMenuView.html b/js/menus/templates/SendMenuView.html
index 66fbb174..e3243e88 100644
--- a/js/menus/templates/SendMenuView.html
+++ b/js/menus/templates/SendMenuView.html
@@ -10,7 +10,7 @@
         <% } %>
         <br/>
         <a href="#" id="openSerialMonitor" class="btn btn-block btn-lg btn-default">Open Serial Monitor</a><br/>
-        Next Line: &nbsp;&nbsp;<input id="nextLine" value=<%= nextLine %> placeholder="##" class="intInput form-control unresponsiveInput" type="text">
+        Next Line: &nbsp;&nbsp;<input id="nextLine" property="simLineNumber" value="<%= nextLine %>" placeholder="##" class="intInput form-control cam" type="text">
         &nbsp;&nbsp;<a href="#" id="previousLineButton" class="btn btn-lg btn-default"><span class="fui-triangle-up"></span></a>
         &nbsp;&nbsp;<a href="#" id="nextLineButton" class="btn btn-lg btn-default"><span class="fui-triangle-down"></span></a><br/><br/>
         <div id="gcodeEditor"><%= dataOut %></div><br/>
diff --git a/js/models/MachineState.js b/js/models/MachineState.js
index a6923937..5bfa06f9 100644
--- a/js/models/MachineState.js
+++ b/js/models/MachineState.js
@@ -29,15 +29,8 @@ define(['underscore', 'backbone'], function(_, Backbone){
             }
         },
 
-        setFooterStatus: function(data){
-            if (data[1] == 0){//ok status
-                this.set("status", 3);
-                this._triggerNextCommand();
-            } else this.set("status", 10);
-        },
-
         isReadyStatus: function(status){
-            if (status == undefined) status = this.get("status");
+            if (status === undefined) status = this.get("status");
             return status == 1 || status == 3 || status == 4;
         },
 
diff --git a/js/models/SerialComm.js b/js/models/SerialComm.js
index ba0ff1f7..5c6b3a09 100644
--- a/js/models/SerialComm.js
+++ b/js/models/SerialComm.js
@@ -2,8 +2,8 @@
  * Created by aghassaei on 6/17/15.
  */
 
-define(['underscore', 'backbone', 'socketio', 'machineState'],
-    function(_, Backbone, io, machineState){
+define(['underscore', 'backbone', 'socketio', 'machineState', 'cam'],
+    function(_, Backbone, io, machineState, cam){
 
     var SerialComm = Backbone.Model.extend({
 
@@ -21,6 +21,7 @@ define(['underscore', 'backbone', 'socketio', 'machineState'],
 
         initialize: function(){
             this.machineState = machineState;
+            this.listenTo(machineState, "change", this._updateVirtualMachine);
             this.attemptToConnectToNode();
         },
 
@@ -61,31 +62,33 @@ define(['underscore', 'backbone', 'socketio', 'machineState'],
             this.socket.emit("stopStream");
         },
 
+        _updateVirtualMachine: function(){
+            cam.setPosition(this.getMachineState().toJSON());
+        },
+
         sendGCode: function(){
+            if (!this.get("isStreaming")) return;
+            if (!cam.initialize) return;//don't do this from serial monitor window
             var self = this;
             var machineState = this.getMachineState();
-            require(['cam'], function(cam){
-                if (machineState && machineState.isReadyStatus()){
-                    var lineNum = cam.get("simLineNumber");
-                    var allLines = cam.get("dataOut").split("\n");
-                    if (lineNum >= 0 && lineNum < allLines.length) {
-                        var line = allLines[lineNum];
-                        self.listenToOnce(machineState, "readyForNextCommand", function(){
-                            lineNum ++;
-                            cam.set("simLineNumber", lineNum);
-                            self.sendGCode();
-                        });
-                        self.send('{"gc":"' + line + '"}');
-                        cam.simulateCurrentLine();
-                    } else if (lineNum == allLines.length){
-                        cam.simulateCurrentLine();
-                        self.pauseStream();
-                    } else {
-                        console.warn("invalid line number " + lineNum);
-                    }
-
+            if (machineState && machineState.isReadyStatus()){
+                var lineNum = cam.get("simLineNumber");
+                var allLines = cam.get("dataOut").split("\n");
+                if (lineNum >= 0 && lineNum < allLines.length) {
+                    var line = allLines[lineNum];
+                    self.listenToOnce(machineState, "readyForNextCommand", function(){
+                        lineNum ++;
+                        cam.set("simLineNumber", lineNum);
+                        self.sendGCode();
+                    });
+                    self.send('{"gc":"' + line + '"}');
+                } else if (lineNum == allLines.length){
+                    self.pauseStream();
+                } else {
+                    console.warn("invalid line number " + lineNum);
                 }
-            });
+
+            } else console.warn("machine not ready");
         },
 
         refreshMachineState: function(){//when updating connection, create a new instance of machine state
@@ -135,8 +138,6 @@ define(['underscore', 'backbone', 'socketio', 'machineState'],
                 } else if (json.sr){
                     serialComm.getMachineState().setPosition(json.sr);
                 }
-                if (json.f) serialComm.getMachineState().setFooterStatus(json.f);
-                else if (json.r.f) serialComm.getMachineState().setFooterStatus(json.r.f);
             } catch(err) {}
         });
 
-- 
GitLab