Skip to content
Snippets Groups Projects
Assembler.js 10.4 KiB
Newer Older
Amanda Ghassaei's avatar
Amanda Ghassaei committed
/**
 * Created by aghassaei on 3/10/15.
 */

Assembler = Backbone.Model.extend({

    defaults: {
amandaghassaei's avatar
amandaghassaei committed
        camStrategy: "raster",
        placementOrder: "XYZ",//used for raster strategy entry
amandaghassaei's avatar
amandaghassaei committed
        camProcess: "gcode",
        machineName: "handOfGod",
        machine: null,
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        exporter: null,
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        dataOut: "",
        needsPostProcessing: true,
        editsMadeToProgram: false,//warn the user that they will override changes

        rapidHeight: 6,//always store relative to origin
        rapidHeightRelative: true,
amandaghassaei's avatar
amandaghassaei committed
        safeHeight: 0.5,//inches above stock or assembly, when feed rate should slow

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        origin: null,
        originPosition: new THREE.Vector3(20,0,0),//in abs coordinates
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        stock: null,
        stockPosition: new THREE.Vector3(20,0,0),//in abs coordinates
        stockPositionRelative: true,
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        stockFixed: false,//stock is fixed position from origin
        multipleStockPositions: false,
amandaghassaei's avatar
amandaghassaei committed
        stockArraySize: {x:4, y:4},
amandaghassaei's avatar
amandaghassaei committed
        stockSeparation: 2.78388,
        rapidSpeeds:{xy: 3, z: 2},//rapids at clearance height
        feedRate:{xy: 0.1, z: 0.1},//speed when heading towards assembly

        simLineNumber: 0,//used for stock simulation, reading through gcode
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        simSpeed: 4//#X times real speed
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

amandaghassaei's avatar
amandaghassaei committed
    initialize: function(options){
Amanda Ghassaei's avatar
Amanda Ghassaei committed

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        _.bindAll(this, "postProcess");

Amanda Ghassaei's avatar
Amanda Ghassaei committed
        //bind events
amandaghassaei's avatar
amandaghassaei committed
        this.listenTo(options.appState, "change:currentTab", this._tabChanged);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.listenTo(this, "change:originPosition", this._moveOrigin);
        this.listenTo(this, "change:stockPosition", this._moveStock);
        this.listenTo(this,
                "change:originPosition " +
                "change:stockPosition " +
                "change:feedRate " +
                "change:rapidSpeeds " +
                "change:camProcess " +
amandaghassaei's avatar
amandaghassaei committed
                "change:camStrategy " +
                "change:placementOrder " +
                "change:safeHeight " +
amandaghassaei's avatar
amandaghassaei committed
                "change:stockArraySize " +
                "change:stockSeparation " +
                "change:multipleStockPositions " +
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                "change:rapidHeight " +
                "change:machineName",
            this._setNeedsPostProcessing);
amandaghassaei's avatar
amandaghassaei committed
        this.listenTo(options.lattice,
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                "change:numCells " +
amandaghassaei's avatar
amandaghassaei committed
                "change:units " +
Amanda Ghassaei's avatar
Amanda Ghassaei committed
                "change:scale " +
                "change:cellType " +
                "change:connectionType",
            this._setNeedsPostProcessing);
        this.listenTo(options.lattice, "change:scale", this._setCAMScale);
        this.listenTo(globals.appState, "change:stockSimulationPlaying", this._stockSimulation);
Amanda Ghassaei's avatar
Amanda Ghassaei committed

        this.listenTo(options.lattice, "change:partType", this._updatePartType);
        this.listenTo(options.lattice, "change:cellType change:connectionType", this._updateCellType);
        this.listenTo(options.appState, "change:cellMode", this._updateCellMode);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.listenTo(this, "change:machineName", this.selectMachine);
        this._initOriginAndStock();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
    selectMachine: function(){
        var machineName = this.get("machineName");
        if (this.get("machine")) this.get("machine").destroy();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.set("machine", null);
        if (machineName == "shopbot"){
            this.set("machine", new Shopbot());
        } else if (machineName == "handOfGod"){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            this.set("machine", new God());
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        } else if (machineName == "oneBitBot"){
            this.set("machine", new OneBitBot());
        } else console.warn("selected machine not recognized");
    makeProgramEdits: function(data){
        this.set("dataOut", data, {silent:true});
        this.set("editsMadeToProgram", true, {silent: true});
    },

///////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////VISUALIZATION//////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

Amanda Ghassaei's avatar
Amanda Ghassaei committed
    isVisible: function(){
        var currentTab = globals.appState.get("currentTab");
        return (currentTab == "cam" || currentTab == "animate" || currentTab == "send");
    },

    _updateCellType: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if (this.get("machine")) this.get("machine").updateCellType();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.set("machineName", "handOfGod");//todo this should go away with dynamic allocation of this model

    _updatePartType: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if (this.get("machine")) this.get("machine").updatePartType();
    },

    _updateCellMode: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if (this.get("machine")) this.get("machine").setVisibility(this.isVisible());
        globals.three.render();
amandaghassaei's avatar
amandaghassaei committed
    _setCAMScale: function(){
        var scale = globals.lattice.get("scale");
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.get("origin").scale.set(scale/8, scale/8, scale/8);
        this.get("stock").scale.set(scale/8, scale/8, scale/8);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if (this.get("machine")) this.get("machine").setScale(scale);
amandaghassaei's avatar
amandaghassaei committed
    _tabChanged: function(){
        this._setCAMVisibility();
        if (globals.appState.get("currentTab") != "animate") this.resetSimulation();
amandaghassaei's avatar
amandaghassaei committed
    },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
    _setCAMVisibility: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        var visible = this.isVisible();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.get("origin").visible = visible;
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.get("stock").visible = visible;
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if (visible && !this.get("machine")) this.selectMachine();
        if (this.get("machine")) this.get("machine").setVisibility(visible);
        globals.three.render();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

    _initOriginAndStock: function(){//todo this is ugly
        var origin = new THREE.Mesh(new THREE.SphereGeometry(1),
            new THREE.MeshBasicMaterial({color:0xff0000}));
        globals.three.sceneAdd(origin);
        this.set("origin", origin);
        this._moveOrigin();
        //init stock mesh
        var stock = new THREE.Mesh(new THREE.SphereGeometry(1),
            new THREE.MeshBasicMaterial({color:0xff00ff}));
        globals.three.sceneAdd(stock);
        this.set("stock", stock);
        this._moveStock();
        this._setCAMScale();
        this._setCAMVisibility();
    },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
    _moveOrigin: function(){
        var position = this.get("originPosition");
        this.get("origin").position.set(position.x, position.y, position.z);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if (this.get("stockFixed")) this._updateStockPosToOrigin(position, this.previous("originPosition"));
        globals.three.render();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if (this.get("machine") && this.get("machine").setMachinePosition) this.get("machine").setMachinePosition();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
    },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
    _updateStockPosToOrigin: function(newOrigin, lastOrigin){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        console.log(newOrigin);
        console.log(lastOrigin);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        var newStockPosition = _.clone(this.get("stockPosition"));
        _.each(_.keys(newStockPosition), function(key){
            newStockPosition[key] += newOrigin[key] - lastOrigin[key];
            newStockPosition[key] = parseFloat(newStockPosition[key].toFixed(4));
        });
        this.set("stockPosition", newStockPosition);
    },

    _moveStock: function(){
        var position = this.get("stockPosition");
        this.get("stock").position.set(position.x, position.y, position.z);
        globals.three.render();
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////SIMULATION//////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

amandaghassaei's avatar
amandaghassaei committed
    resetSimulation: function(){
        this.set("simLineNumber", 0, {silent:true});
        globals.appState.set("stockSimulationPlaying", false);
        globals.three.stopAnimationLoop();
        globals.lattice.showCells();
amandaghassaei's avatar
amandaghassaei committed
    },

    _stockSimulation: function(){
        if (globals.appState.get("stockSimulationPlaying")){
            globals.three.startAnimationLoop();
            var currentLine = this.get("simLineNumber");
            if (currentLine == 0) globals.lattice.hideCells();
            var allLines = this.get("dataOut").split("\n");
            if(currentLine<allLines.length){
                var self = this;
amandaghassaei's avatar
amandaghassaei committed
                this.get("exporter").simulate(allLines[currentLine], this.get("machine"),
                    this.get("originPosition"), function(){
                    currentLine++;
                    self.set("simLineNumber", currentLine);
                    self._stockSimulation();
                });
                //finished simulation
amandaghassaei's avatar
amandaghassaei committed
                this.resetSimulation();
            globals.three.stopAnimationLoop();
            this.get("machine").pause();
        }

///////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////POST PROCESSING////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

    _setNeedsPostProcessing: function(){
        this.set("needsPostProcessing", true);
amandaghassaei's avatar
amandaghassaei committed
    },

    postProcess: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.set("needsPostProcessing", false);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        var exporter = this._getExporter();

        var data = "";
        data += exporter.makeHeader();
        data += "\n\n";
        data += exporter.addComment("begin program");
        data += "\n";
        data = this.get("machine").postProcess(data, exporter);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        data += "\n\n";
        data += exporter.addComment("end program");
        data += "\n";
        data += exporter.makeFooter();

        this.set("dataOut", data);
amandaghassaei's avatar
amandaghassaei committed
        this.set("editsMadeToProgram", false);
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.set("exporter", exporter);
        if (!globals.appState.get("stockSimulationPlaying")) this.resetSimulation();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        return {data:data, exporter:exporter};
    },

    _getExporter: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
//        var currentExporter = this.get("exporter");
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        var camProcess = this.get("camProcess");
        if (camProcess == "shopbot") {
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            return new ShopbotExporter();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        } else if (camProcess == "gcode") {
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            return new GCodeExporter();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        } else if (camProcess == "tinyG"){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
            return new TinyGExporter();
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        }
        console.warn("cam process not supported");
        return null;
    },

    _getOrder: function(strategy){
amandaghassaei's avatar
amandaghassaei committed
        if (strategy == "raster") return this.get("placementOrder");
        console.warn("strategy not recognized");
        return "";
    },

Amanda Ghassaei's avatar
Amanda Ghassaei committed
    save: function(){
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        if (this.get("needsPostProcessing")){
            var output = this.postProcess();
            output.exporter.save(output.data);
            return;
        }
Amanda Ghassaei's avatar
Amanda Ghassaei committed
        this.get("exporter").save(this.get("dataOut"));