// MACROLOOP // G96 EMULATE ATTEMP ;) // check flag, if VAR 750 = 0, save spindle speed + sleep. intVarFlag = Convert.ToInt32(exec.Getvar(750)); if ( intVarFlag != 252 ) { if ( intFlagSet == 1 ) { // reset Spindle Speed Setting to original setting & clear internal macro flag {allows for change of state reset} AS3.Setfield(dblSpindleSpeedSave, 869); AS3.Validatefield(869); intFlagSet = 0; // clear internal macro flag intStaticValues = 0; // clear static values flag } // Do nothing and return from loop } // if VAR 750 = 252 (flag set), check Z DRO and adjust spindle speed else { if ( intFlagSet != 1 ) // initial loop, save current Spindle Speed Setting & set spindle speed setting relative to X axis position { dblSpindleSpeedSave = Convert.ToDouble(AS3.Getfield(869)); // save current Spindle Speed Setting fcnSetSpindleSpeed(); intFlagSet = 1; // Set MACROLOOP flag } else { // check that Spindle Speed is same as previously set (incase a Gcode has changed it on the fly) if ( strSpindleSpeedSet != AS3.Getfield(869) ) { // Resave save current Spindle Speed Setting dblSpindleSpeed = Convert.ToDouble(AS3.Getfield(869)); exec.Setvar(dblSpindleSpeed, 753); // Recalculate according to current Z axis DRO position & Reset Spindle Speed fcnSetSpindleSpeed(); } else { fcnSetSpindleSpeed(); } } } Thread.Sleep(50); #Events static int intVarFlag; static int intFlagSet; static int intStaticValues; static double dblSpindleSpeedSave; static double dblSpindleSpeedSet; static int intSpindleSpeedSet; static double dblMaxSpindleSpeed; static double dblSurfaceSpeed; static double dblXposCurr; static double dblCirc; static double dblSpindleSpeed; static string strSpindleSpeedSet; static int intDROPosition; static double dblSpindleSpeedMin; static double dblSpindleSpeedMax; //functions void fcnSetSpindleSpeed() { dblMaxSpindleSpeed = exec.Getvar(751); // Set maximum spindle { RPM } dblMaxSpindleSpeed = Math.Min(dblMaxSpindleSpeed, dblSpindleSpeedMax); dblSurfaceSpeed = exec.Getvar(752); // Set surface speed {units per minute} dblXposCurr = exec.GetXpos(); fcnGetStaticValues(); if (dblXposCurr <= 0) { dblXposCurr = Math.Round(0.0000001, intDROPosition); dblCirc = 2 * dblXposCurr * 22 / 7; dblSpindleSpeedSet = dblSurfaceSpeed / dblCirc; dblSpindleSpeedSet = Math.Round(dblSpindleSpeedSet,1); if (dblSpindleSpeedSet < dblSpindleSpeedMin) { dblSpindleSpeedSet = dblSpindleSpeedMin; } if (dblSpindleSpeedSet > dblMaxSpindleSpeed) { dblSpindleSpeedSet = dblMaxSpindleSpeed; } } else { dblCirc = 2 * dblXposCurr * 22 / 7; dblSpindleSpeedSet = dblSurfaceSpeed / dblCirc; dblSpindleSpeedSet = Math.Round(dblSpindleSpeedSet,1); if (dblSpindleSpeedSet < 10) { dblSpindleSpeedSet = 10; } if (dblSpindleSpeedSet > dblMaxSpindleSpeed) { dblSpindleSpeedSet = dblMaxSpindleSpeed; } } AS3.Setfield(dblSpindleSpeedSet, 869); AS3.Validatefield(869); strSpindleSpeedSet = Convert.ToString(dblSpindleSpeedSet); } void fcnGetStaticValues() { if (intStaticValues != 1) { string strDROPosition = AS3.Getfield(195); string strSpindleSpeedMin = AS3.Getfield(177); string strSpindleSpeedMax = AS3.Getfield(178); intDROPosition = Convert.ToInt32(strDROPosition); dblSpindleSpeedMin = Convert.ToDouble(strSpindleSpeedMin); dblSpindleSpeedMax = Convert.ToDouble(strSpindleSpeedMax); } }