From 45b53205866742b5f4be117c58cc2b87b3dfe240 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 14 Aug 2015 16:22:41 -0400
Subject: [PATCH] rotation between layers starting to work

---
 js/cam/assemblers/Assembler.js      | 14 +++++---
 js/cam/assemblers/Component.js      | 51 ++++++++++++++++++++++++++++-
 js/cam/processes/GCodeExporter.js   | 11 ++++---
 js/menus/templates/CamMenuView.html |  2 +-
 js/plists/CamPList.js               |  4 +--
 5 files changed, 69 insertions(+), 13 deletions(-)

diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index 78dbf6cc..3322393a 100644
--- a/js/cam/assemblers/Assembler.js
+++ b/js/cam/assemblers/Assembler.js
@@ -223,16 +223,20 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         });
     };
     
-    Assembler.prototype.pickUpStock = function(settings){
+    Assembler.prototype.pickUpStock = function(index, position, speed, settings, callback){
+        if (index.z%2 != 0) {//rotate on odd rows
+            this.components.frame.rotateTo(new THREE.Vector3(0, 0, Math.PI/2), speed, callback);
+        } else {
+            this.components.frame.rotateTo(new THREE.Vector3(0, 0, 0), speed, callback);
+        }
         _.each(this.stock, function(stock){
             stock.show();
         });
+        callback();
     };
     
-    Assembler.prototype.releaseStock = function(json, settings){
-        json = JSON.parse(json);
-        console.log(json.index);
-        lattice.showCellAtIndex(json.index);
+    Assembler.prototype.releaseStock = function(index, position, settings){
+        lattice.showCellAtIndex(index);
         _.each(this.stock, function(stock){
             stock.hide();
         });
diff --git a/js/cam/assemblers/Component.js b/js/cam/assemblers/Component.js
index 56c2902d..b8f9319a 100644
--- a/js/cam/assemblers/Component.js
+++ b/js/cam/assemblers/Component.js
@@ -73,17 +73,66 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
         return this.object3D.position.clone();
     };
 
+    Component.prototype.getRotation = function(){
+        return this.object3D.rotation.toVector3();
+    };
+
     Component.prototype.getObject3D = function(){
         return this.object3D;
     };
 
+    Component.prototype.rotateTo = function(target, speed, callback){
+        if (target === null){
+            if (callback) callback();
+            return;
+        }
+        var currentPosition = this.getRotation();
+        var increment = 0.15;//speed/1500.0*cam.get("simSpeed");
+        var incrVector = target.clone().sub(currentPosition);
+
+        if (increment == 0 || incrVector.length() == 0) {
+            if (callback) callback();
+            return;
+        }
+        increment = Math.max(increment, 0.00001);//need to put a min on the increment - otherwise this stalls out with floating pt tol
+
+        incrVector.normalize().multiplyScalar(increment);
+        this._incrementalRotation(incrVector, target, callback);
+    };
+
+    Component.prototype._incrementalRotation = function(increment, target, callback){
+        var self = this;
+        setTimeout(function(){
+            var remainingDist = (target.clone().sub(self.getRotation())).length();
+            var nextPos;
+            if (remainingDist == 0) {
+                if (callback) callback();
+                return;
+            } else if (remainingDist < increment.length()){
+                nextPos = target;//don't overshoot
+                self.object3D.rotation.x = target.x;
+                self.object3D.rotation.y = target.y;
+                self.object3D.rotation.z = target.z;
+                if (callback) callback();
+                return;
+            } else {
+                nextPos = self.getRotation().add(increment);
+            }
+
+//            console.log(target.clone().normalize());
+            console.log(increment);
+            self.object3D.rotateOnAxis(target.clone().normalize(), nextPos.z);
+            self._incrementalRotation(increment, target, callback);
+        }, 10);
+    };
+
     Component.prototype.moveTo = function(target, speed, callback){
         if (target === null){
             if (callback) callback();
             return;
         }
         var currentPosition = this.getPosition();
-        var increment = speed/25*cam.get("simSpeed");
+        var increment = speed/1500.0*cam.get("simSpeed");
         var incrVector = target.clone().sub(currentPosition);
 
         if (increment == 0 || incrVector.length() == 0) {
diff --git a/js/cam/processes/GCodeExporter.js b/js/cam/processes/GCodeExporter.js
index 9b173600..f2e0f915 100644
--- a/js/cam/processes/GCodeExporter.js
+++ b/js/cam/processes/GCodeExporter.js
@@ -97,12 +97,15 @@ define(['underscore', 'cam', 'lattice'], function(_, cam, lattice){
     };
 
     GCodeExporter.prototype.simulate = function(line, machine, settings,  callback){
-        if (line.substr(0,11) == "(get stock)"){
-            machine.pickUpStock(settings);
-            return callback();
+        if (line.substr(0,10) == "(get stock"){
+            var json = line.substr(11,line.length-12);
+            json = JSON.parse(json);
+            return machine.pickUpStock(json.index, json.position, this.animationSpeed, settings, callback);
         }
         if (line.substr(0,2) == "({"){
-            machine.releaseStock(line.substr(1,line.length-2), settings);
+            var json = line.substr(1,line.length-2);
+            json = JSON.parse(json);
+            machine.releaseStock(json.index, json.position, settings);
             return callback();
         }
         if (line[0] == "F"){//speed
diff --git a/js/menus/templates/CamMenuView.html b/js/menus/templates/CamMenuView.html
index abf7b09f..28e38d16 100644
--- a/js/menus/templates/CamMenuView.html
+++ b/js/menus/templates/CamMenuView.html
@@ -56,7 +56,7 @@ Origin (xyz):&nbsp;&nbsp;
     Stock Height: &nbsp;&nbsp;<input data-property="stockPosition" data-key="z" value="<%= stockPosition.z %>" placeholder="Z" class="form-control floatInput assembler" type="text"><br/><br/>
 <% } %>
 Approach Height  (<%= units %>): &nbsp;&nbsp;<input data-property="safeHeight" value="<%= safeHeight %>" placeholder="Z" class="form-control floatInput assembler" type="text"><br/><br/>
-Speeds (<%= units %> per second):<br/><br/>
+Speeds (<%= units %> per min):<br/><br/>
     Rapids (xy, z): &nbsp;&nbsp;<input data-property="rapidSpeeds" data-key="xy" value="<%= rapidSpeeds.xy %>" placeholder="XY" class="form-control floatInput assembler" type="text">&nbsp;
         <input data-property="rapidSpeeds" data-key="z" value="<%= rapidSpeeds.z %>" placeholder="Z" class="form-control floatInput assembler" type="text"><br/><br/>
     Feed Rate (xy, z): &nbsp;&nbsp;<input data-property="feedRate" data-key="xy" value="<%= feedRate.xy %>" placeholder="XY" class="form-control floatInput assembler" type="text">&nbsp;
diff --git a/js/plists/CamPList.js b/js/plists/CamPList.js
index 8c3c587c..eb1e6262 100644
--- a/js/plists/CamPList.js
+++ b/js/plists/CamPList.js
@@ -142,8 +142,8 @@ define(['three'], function(THREE){
                     rapidHeightRelative: true,
                     safeHeight: 0.5,
                     originPosition: new THREE.Vector3(0,0,0),
-                    rapidSpeeds:{xy: 3, z: 2},
-                    feedRate:{xy: 0.1, z: 0.1}
+                    rapidSpeeds:{xy: 240, z: 120},
+                    feedRate:{xy: 6, z: 6}
                 }
             }
         },
-- 
GitLab