From bffabbb228a8631e57444d9164b6b3803377d4cc Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Tue, 18 Aug 2015 23:45:01 -0400
Subject: [PATCH] adding in will's gcode

---
 js/cam/assemblers/Assembler.js    | 48 ++++++++++++++++++++----
 js/cam/assemblers/Component.js    | 61 +++++++++++++++++++++++++------
 js/cam/processes/GCodeExporter.js |  2 +-
 js/plists/CamPList.js             |  7 ++--
 4 files changed, 95 insertions(+), 23 deletions(-)

diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index bf61b2d7..edca07f9 100644
--- a/js/cam/assemblers/Assembler.js
+++ b/js/cam/assemblers/Assembler.js
@@ -20,7 +20,7 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         this.numMaterials = json.numMaterials;
 
         this.customHeader = json.customHeader || function(settings){
-//            this.home(settings);
+            return "";
         };
         this.customFooter = json.customFooter || function(){};
         this.customHome = json.customHome || function(){};
@@ -189,8 +189,14 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
     //post process
 
     Assembler.prototype.postProcess = function(settings, exporter){
+
+        _.each(this.components, function(component){
+            component.postReset();
+        });
+
         var data = "";
         data += exporter.makeHeader(settings);
+        data += this._makeHeader(settings, exporter);
         data += "\n\n";
         data += exporter.addComment("begin program");
         data += "\n";
@@ -205,17 +211,22 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         return data;
     };
 
-    Assembler.prototype.makeHeader = function(settings){
-        return this.customHeader(settings);
+    Assembler.prototype._makeHeader = function(settings, exporter){
+        var data = this.customHeader(settings);
+        data += this.home(exporter, settings);
+        return data;
     };
 
     Assembler.prototype.home = function(exporter, settings){
+//        this.components.zAxis.postMoveTo(settings.rapidHeight*settings*scale);
         return exporter.goHome(settings);
     };
     
     Assembler.prototype._postProcessCells = function(settings, exporter){
         var data = "";
         var self = this;
+
+        var lastIndex = null;
     
         lattice.rasterCells(cam._getOrder(cam.get("camStrategy")), function(cell){
             if (!cell) return;
@@ -223,7 +234,7 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
             var cellIndex = cell.getAbsoluteIndex();
 
             if (!self.shouldPickUpStock){
-                data += self._postGetStock(cellIndex, cellPosition, cell.materialName, settings, exporter);
+                data += self._postGetStock(cellIndex, lastIndex, cellPosition, cell.materialName, settings, exporter);
             } else {
 //                var thisStockPosition = _.clone(stockPosition);
 //                if (multStockPositions) {
@@ -237,12 +248,14 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
             }
             data += self._postReleaseStock(cellIndex, cellPosition, cell.materialName, settings, exporter);
             data += "\n";
+            lastIndex = cellIndex.clone();
         });
 
         return data;
     };
     
     Assembler.prototype._postMoveXY = function(position, settings, exporter){
+        position = this.components.zAxis.applyAbsoluteRotation(position);
         return exporter.rapidXY(position, settings);
     };
     
@@ -256,8 +269,14 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
 //        return data;
 //    };
     
-    Assembler.prototype._postGetStock = function(index, position, material, settings, exporter){
-        return exporter.addComment("get stock " + JSON.stringify(index));
+    Assembler.prototype._postGetStock = function(index, lastIndex, position, material, settings, exporter){
+        var data = "";
+        if (lastIndex === null || (lastIndex.z-index.z)%2 != 0){
+            data += exporter.addLine("G0", ["A" + (index.z%2*0.3125).toFixed(4)], "new layer");
+            data += "\n";
+        }
+        data += exporter.addComment("get stock " + JSON.stringify(index));
+        return data;
     };
     
     Assembler.prototype._postReleaseStock = function(index, position, material, settings, exporter){
@@ -271,12 +290,21 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         }
         position.sub(stock.getPosition().multiplyScalar(settings.scale));
 
+
         position.sub(settings.originPosition);
-        data += this._postMoveXY(position, settings, exporter);
+        data += this._postMoveXY(position.clone(), settings, exporter);
 
         data += exporter.rapidZ(position.z+settings.safeHeight, settings);
         data += exporter.moveZ(position.z, settings);
+
+        //place part
         data += exporter.addComment(JSON.stringify(index));
+        if (material == "brass") data += exporter.addLine("M3");
+        else if (material == "fiberGlass") data += exporter.addLine("M4");
+        data += exporter.addLine("G4", ["P" + 0.75]);
+        data += exporter.addLine("M5");
+        data += exporter.moveZ(position.z, settings);
+
         data += exporter.moveZ(position.z+settings.safeHeight, settings);
         data += exporter.rapidZ(settings.rapidHeight, settings);
         return data;
@@ -322,6 +350,10 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
         }
         this.components.frame.rotateTo(new THREE.Vector3(0, 0, 0), speed, callback);
     };
+
+    Assembler.prototype.rotateTo = function(index, speed, settings, callback){
+        this.components.frame.rotateTo(new THREE.Vector3(0, 0, Math.PI/2), speed, callback);
+    };
     
     Assembler.prototype.releaseStock = function(index, settings){
         lattice.showCellAtIndex(index);
@@ -340,7 +372,7 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
             callback();
         }
 
-        var startingPos = this.components.zAxis.getAbsolutePosition();//get position of end effector
+        var startingPos = this.components.xAxis.getPosition().add(this.components.yAxis.getPosition().add(this.components.zAxis.getPosition()))//this.components.zAxis.getAbsolutePosition();//get position of end effector
         speed = this._normalizeSpeed(startingPos, position, new THREE.Vector3(speed, speed, speed));//todo fix this
         this.components.xAxis.moveTo(position, speed.x, sketchyCallback);
         this.components.frame.moveTo(position, speed.y, sketchyCallback);
diff --git a/js/cam/assemblers/Component.js b/js/cam/assemblers/Component.js
index 78f28b21..d99ed4c8 100644
--- a/js/cam/assemblers/Component.js
+++ b/js/cam/assemblers/Component.js
@@ -20,6 +20,9 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
         this.motionVector = new THREE.Vector3();
         if (json.centerOfRotation) this.centerOfRotation = new THREE.Vector3(json.centerOfRotation.x, json.centerOfRotation.y, json.centerOfRotation.z);
         if (json.motionVector) this.motionVector.set(json.motionVector.x, json.motionVector.y, json.motionVector.z);
+
+
+        this.postReset();
     }
 
     //assembler setup
@@ -80,6 +83,14 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
         return this.id;
     };
 
+
+
+
+
+
+
+    //position/rotation/etc
+
     Component.prototype.getPosition = function(){
         return this.object3D.position.clone();
     };
@@ -89,6 +100,19 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
         return this.parentObject.getAbsolutePosition().add(this.parentObject.applyRotation(this.getPosition()));
     };
 
+    Component.prototype.getMotionVector = function(){
+        return this.motionVector.clone();
+    };
+
+    Component.prototype.getAbsoluteMotionVector = function(){
+        if (!this.parent) return this.getMotionVector();
+        return this.applyAbsoluteRotation(this.getMotionVector());
+    };
+
+    Component.prototype.getRotation = function(){//for rotary axes
+        return this.object3D.rotation.toVector3().clone();
+    };
+
     Component.prototype.getOrientation = function(){
         return this.object3D.quaternion.clone();
     };
@@ -116,16 +140,27 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
 
 
 
-    //simulation animation
+    //post processing
 
-    Component.prototype.getPosition = function(){
-        return this.object3D.position.clone();
+    Component.prototype.postReset = function(){
+        this._postAngle = 0;
+        this._postPosition = 0;
     };
 
-    Component.prototype.getRotation = function(){
-        return this.object3D.rotation.toVector3().clone();
+    Component.prototype.postRotateTo = function(newAngle){
+        this._postAngle = newAngle;
+    };
+
+
+    Component.prototype.postMoveTo = function(newPosition){
+        this._postPosition = newPosition;
     };
 
+
+
+
+    //simulation animation
+
     Component.prototype.getObject3D = function(){
         return this.object3D;
     };
@@ -168,14 +203,14 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
         }, 10);
     };
 
-    Component.prototype.moveTo = function(target, speed, callback){
-        var target = this._multiplyVectors(target, this.motionVector);
+    Component.prototype.moveTo = function(target, speed, callback){//all local in sim bc gcode is in local coordinate systems
+        var target = this._multiplyVectors(target, this.getMotionVector());//this.getAbsoluteMotionVector()
         if (target === null){
             if (callback) callback();
             return;
         }
 
-        var currentPosition = this.getPosition();
+        var currentPosition = this.getPosition();//local position
         var increment = speed/1500.0*cam.get("simSpeed");
         var incrVector = target.clone().sub(currentPosition);
 
@@ -190,13 +225,17 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
     };
 
     Component.prototype._multiplyVectors = function(target, motion){
-        if (target.x === null && motion.x > 0) return null;
-        if (target.y === null && motion.y > 0) return null;
-        if (target.z === null && motion.z > 0) return null;
+        if (target.x === null && motion.x > 0.001) return null;
+        if (target.y === null && motion.y > 0.001) return null;
+        if (target.z === null && motion.z > 0.001) return null;
         var target = new THREE.Vector3(target.x, target.y, target.z);
         return target.multiply(motion);
     };
 
+    Component.prototype.getThisDistanceToTarget = function(target){
+        return target.clone().multiply(this.getAbsoluteMotionVector());
+    };
+
     Component.prototype._incrementalMove = function(increment, target, callback){
         var self = this;
         setTimeout(function(){
diff --git a/js/cam/processes/GCodeExporter.js b/js/cam/processes/GCodeExporter.js
index b58d39e9..6ec8212e 100644
--- a/js/cam/processes/GCodeExporter.js
+++ b/js/cam/processes/GCodeExporter.js
@@ -19,7 +19,7 @@ define(['underscore', 'cam', 'lattice'], function(_, cam, lattice){
     ////    data += this.addLine("G49", [], "cancel tool length comp");
     //    data += this.addLine("G40", [], "cancel tool radius comp");
     ////    data += this.addLine("M09", [], "coolant off");
-        data += this.goHome(settings);
+//        data += this.goHome(settings);
         return data;
     };
 
diff --git a/js/plists/CamPList.js b/js/plists/CamPList.js
index 04723a0e..cb704fe7 100644
--- a/js/plists/CamPList.js
+++ b/js/plists/CamPList.js
@@ -126,11 +126,12 @@ define(['three'], function(THREE){
                     camStrategy: "raster",
                     placementOrder: "XYZ",//used for raster strategy entry
                     camProcess: "gcode",
-                    rapidHeight:40,
+                    rapidHeight:30,
                     rapidHeightRelative: true,
-                    safeHeight: 0.5,
+                    safeHeight: 4.5,
+                    clearHeight: 8,
                     originPosition: new THREE.Vector3(0,0,0),
-                    rapidSpeeds:{xy: 240, z: 120},
+                    rapidSpeeds:{xy: 250, z: 250},
                     feedRate:{xy: 6, z: 6}
                 }
             }
-- 
GitLab