コード例 #1
0
ファイル: GCodeInterpreter.cpp プロジェクト: tedenda/KMotionX
int CGCodeInterpreter::SetCSS(int mode)  // set CSS mode
{
	char s[64];


	if (mode==CANON_SPINDLE_CSS)
	{
		double x_res=CoordMotion->Kinematics->m_MotionParams.CountsPerInchX;
		float x_factor=0.0;
		if (x_res != 0.0)
			x_factor = (float)(1.0 / x_res);

		float xoffset = (float)(UserUnitsToInches(_setup.axis_offset_x+_setup.origin_offset_x)*x_res);

		float fspeed = (float)(p_setup->speed * CoordMotion->GetSpindleRateOverride());

		// convert feet per minute or meters per minute -> inches/sec

		if (p_setup->length_units == CANON_UNITS_MM)
			fspeed /= 60.0f * 0.0254f;
		else
			fspeed *= 12.0f/60.0f;

		float max_rpm=1e9;
		if (p_setup->block1.d_number!=-1) max_rpm = (float)p_setup->block1.d_number;

		sprintf(s, "SetPersistHex %d %x",PC_COMM_CSS_X_OFFSET, *(int *)&xoffset);
		if (CoordMotion->KMotionDLL->WriteLine(s)) {CoordMotion->SetAbort(); return 1;}

		sprintf(s, "SetPersistHex %d %x",PC_COMM_CSS_X_FACTOR, *(int *)&x_factor);
		if (CoordMotion->KMotionDLL->WriteLine(s)) {CoordMotion->SetAbort(); return 1;}

		sprintf(s, "SetPersistHex %d %x",PC_COMM_CSS_S, *(int *)&fspeed);
		if (CoordMotion->KMotionDLL->WriteLine(s)) {CoordMotion->SetAbort(); return 1;}

		sprintf(s, "SetPersistHex %d %x",PC_COMM_CSS_MAX_RPM, *(int *)&max_rpm);
		if (CoordMotion->KMotionDLL->WriteLine(s)) {CoordMotion->SetAbort(); return 1;}
	}

	sprintf(s, "SetPersistHex %d %x",PC_COMM_CSS_MODE, mode);
	if (CoordMotion->KMotionDLL->WriteLine(s)) {CoordMotion->SetAbort(); return 1;}

	return 0;
}
コード例 #2
0
int CGCodeInterpreter::DoReverseSearch(const char * InFile, int CurrentLine)
{
	int GCodeReads,status;
	char trash[INTERP_TEXT_SIZE];
	char * read_ok;
	char s[MAX_LINE];

	if (CurrentLine==0) return 0;  // should always be ok to set the first line

	CoordMotion->m_Stopping = STOPPED_NONE;

// always read the tool file now (some users set it externally)
//	if (m_restart || m_ReadToolFile)
	{
		if (ToolFile[0]!=0)
		{
			status = read_tool_file(ToolFile, &_setup);
			if (status != RS274NGC_OK)	return rs274ErrorExit(status);
		}
		m_ReadToolFile = false;
	}
	

	if (!CoordMotion->m_Simulate)
	{
		int result;

		// find out which axis is which
		// force refresh and save results

		CoordMotion->m_DefineCS_valid = false;

		if  (CoordMotion->m_PreviouslyStopped)
			result = CoordMotion->ReadCurAbsPosition(&CoordMotion->current_x,&CoordMotion->current_y,&CoordMotion->current_z,
													&CoordMotion->current_a,&CoordMotion->current_b,&CoordMotion->current_c,true);
		else
			result = ReadAndSyncCurPositions(&_setup.current_x,&_setup.current_y,&_setup.current_z,&_setup.AA_current,&_setup.BB_current,&_setup.CC_current);

		if (result == 1)
		{
			if (CoordMotion->m_AxisDisabled)
				strcpy(ErrorOutput,"Unable to read defined coordinate system axis positions - Axis Disabled ");
			else
				strcpy(ErrorOutput,"Unable to read defined coordinate system axis positions ");
		}

		if (result != 0) return 1005;
	}

	status = rs274ngc_open(InFile); 
	if (status != RS274NGC_OK)	return rs274ErrorExit(status);

	if (_setup.percent_flag == ON)
		GCodeReads = 1;
	else
		GCodeReads = 0;


	// read the entire file into this array of lines
	// up until the line we wish to start
#ifdef _KMOTIONX
	//char *LineArray = new char[CurrentLine+1][INTERP_TEXT_SIZE+1];
	//this works on linux due to a C++ extension
	char LineArray[CurrentLine+1][INTERP_TEXT_SIZE+1];
#define CLEAN_ARRAY do{ } while( false )
#else
	CString *LineArray = new CString[CurrentLine+1];
	if (!LineArray) return 1;
#define CLEAN_ARRAY do{ delete [] LineArray; } while( false )
#endif

	for( ; GCodeReads<=CurrentLine ; GCodeReads++)
	{
		read_ok = fgets(trash, INTERP_TEXT_SIZE,_setup.file_pointer);
		if (!read_ok) 
		{
			rs274ngc_close();
			CLEAN_ARRAY;
			AfxMessageBox("Error while reading GCode file ");
			return 1;
		}
#ifdef _KMOTIONX
		strcpy(LineArray[GCodeReads],trash);
#else
		LineArray[GCodeReads] = trash;
#endif
	}

	rs274ngc_close();

	bool Done=false;

	bool FoundG=false;
	bool FoundF=false;
	bool FoundUnits=false;
	bool FoundX=CoordMotion->x_axis < 0;
	bool FoundY=CoordMotion->y_axis < 0;
	bool FoundZ=CoordMotion->z_axis < 0;
	bool FoundA=CoordMotion->a_axis < 0;
	bool FoundB=CoordMotion->b_axis < 0;
	bool FoundC=CoordMotion->c_axis < 0;

	bool FoundX0,FoundY0,FoundZ0,FoundA0,FoundB0,FoundC0,FoundG0,FoundF0,FoundUnits0;


	int G=-1;
	int Units=-1;
	double x=0,y=0,z=0,a=0,b=0,c=0,f=0;
	block *BL=&p_setup->block1;
	block block0;
	bool FirstLoop=true;

	if (--GCodeReads<0) {CLEAN_ARRAY; return 1;}

	status = rs274ngc_read(LineArray[GCodeReads]);
	if (status == RS274NGC_ENDFILE)
	{
		rs274ngc_close();
		CLEAN_ARRAY;
		return RS274NGC_ENDFILE;
	}

	// save what is on the line the User wants to switch to

	block0=p_setup->block1;

	// we need to determine what everthing would be 
	// after the previous line was executed

	while (!FoundG || (!FoundF && (G==-1 || G==G_1 || G==G_2 || G==G_3)) 
		    || !FoundX || !FoundY || !FoundZ || !FoundA || !FoundB || !FoundC
			|| !FoundUnits)
	{
		if (--GCodeReads<0) break;

		status = rs274ngc_read(LineArray[GCodeReads]);
		if (status == RS274NGC_ENDFILE)
		{
			rs274ngc_close();
			CLEAN_ARRAY;
			return RS274NGC_ENDFILE;
		}

		if (!FoundG)
		{
			if (BL->motion_to_be == G_0 ||
				BL->motion_to_be == G_1 ||
				BL->motion_to_be == G_2 ||
				BL->motion_to_be == G_3 ||
				BL->motion_to_be == G_4) FoundG=true;

			if (FoundG) G=BL->motion_to_be;
		}

		if (!FoundUnits)
		{
			if (BL->g_modes[6] == G_20 ||
				BL->g_modes[6] == G_21) FoundUnits=true;

			if (FoundUnits) Units=BL->g_modes[6];
		}

		if (!FoundF && BL->f_number >= 0.0) {FoundF=true; f=BL->f_number;}
		if (!FoundX && BL->x_flag) {FoundX=true; x=BL->x_number;}
		if (!FoundY && BL->y_flag) {FoundY=true; y=BL->y_number;}
		if (!FoundZ && BL->z_flag) {FoundZ=true; z=BL->z_number;}
		if (!FoundA && BL->a_flag) {FoundA=true; a=BL->a_number;}
		if (!FoundB && BL->b_flag) {FoundB=true; b=BL->b_number;}
		if (!FoundC && BL->c_flag) {FoundC=true; c=BL->c_number;}

		if (FirstLoop)
		{
			FirstLoop=false;
			FoundX0=FoundX;
			FoundY0=FoundY;
			FoundZ0=FoundZ;
			FoundA0=FoundA;
			FoundB0=FoundB;
			FoundC0=FoundC;
			FoundG0=FoundG;
			FoundF0=FoundF;
			FoundUnits0=FoundUnits;
		}
	}

	CLEAN_ARRAY;

	if ((block0.g_modes[6]!=G_20 || block0.g_modes[6]!=G_21) && FoundUnits)  // if units not specified on current line and different 
	{
		if (Units==G_20 && p_setup->length_units!=CANON_UNITS_INCHES)
		{
			if (AfxMessageBox("Backward scan found G20 Inches Mode.  Switch to Inches?" ,MB_YESNO)==IDYES)
			{
				p_setup->length_units=CANON_UNITS_INCHES;
			}
		}
		if (Units==G_21 && p_setup->length_units!=CANON_UNITS_MM)
		{
			if (AfxMessageBox("Backward scan found G21 MM Mode.  Switch to MM?" ,MB_YESNO)==IDYES)
			{
				p_setup->length_units=CANON_UNITS_MM;
			}
		}
	}

	// if jumping to a G0 assume User knows what is intended and it is ok to rapid to that location 
	if (block0.motion_to_be==G_0) return 1;


	if (GCodeReads<0)
	{
		strcpy(s,"Unable to determine starting conditions for this line.\r\r");
		if (!FoundX) strcat(s,"X? ");
		if (!FoundY) strcat(s,"Y? ");
		if (!FoundZ) strcat(s,"Z? ");
		if (!FoundA) strcat(s,"A? ");
		if (!FoundB) strcat(s,"B? ");
		if (!FoundC) strcat(s,"C? ");
		if (!FoundF && (G==-1 || G==G_1 || G==G_2 || G==G_3))strcat(s,"F? ");
		if (!FoundUnits) strcat(s,"G20/21? ");
		
		AfxMessageBox(s);
		return 1;
	}

	if (block0.motion_to_be==-1)
	{
		sprintf(s, "New Line does not contain a G mode.  Backward scan found:\r\rG%d\r\rUse this mode?",G/10);

		if (AfxMessageBox(s,MB_YESNO)==IDNO)
		{
			return 1;
		}
	}

	if (block0.f_number==-1)
	{
		if (FoundF)
		{
			sprintf(s, "New Line does not contain a Feedrate F command.  Backward scan found:\r\rF%g\r\rUse this feedrate?",f);
			if (AfxMessageBox(s,MB_YESNO)==IDYES)
			{
				p_setup->feed_rate=m_StoppedInterpState.feed_rate=f;
			}
			else
			{
				m_StoppedInterpState.feed_rate=p_setup->feed_rate;
			}
		}
		else
		{
			AfxMessageBox("New Line does not contain a Feedrate F command.  Unable to determine previous feedrate");
		}
	}
	else
	{
		p_setup->feed_rate=m_StoppedInterpState.feed_rate=block0.f_number;
	}


	if (!FoundX0 || !FoundY0 ||!FoundZ0 ||!FoundA0 ||!FoundB0 ||!FoundC0)
	{
		char v[32];
		strcpy(s,"Backward scan found previous position as:\r\r");
		if (CoordMotion->x_axis >= 0){ sprintf(v," X%g",x); strcat(s,v);}
		if (CoordMotion->y_axis >= 0){ sprintf(v," Y%g",y); strcat(s,v);}
		if (CoordMotion->z_axis >= 0){ sprintf(v," Z%g",z); strcat(s,v);}
		if (CoordMotion->a_axis >= 0){ sprintf(v," A%g",a); strcat(s,v);}
		if (CoordMotion->b_axis >= 0){ sprintf(v," B%g",b); strcat(s,v);}
		if (CoordMotion->c_axis >= 0){ sprintf(v," C%g",c); strcat(s,v);}
		strcat(s,"\rShould a Safe Z move be made using these coordinates?");
		if (AfxMessageBox(s,MB_YESNO)==IDNO)
		{
			return 1;
		}
	}

	if (!FoundG0)
	{
		int group = _gees[G*10];

		if (group==0)  // G4 = group0
			p_setup->active_g_codes[1]=-1;
		else           // G0,G1,G2,G3
			p_setup->active_g_codes[0]=-1;

		p_setup->motion_mode=G;
	}

	if (FoundX) {CoordMotion->m_Stoppedx=x; p_setup->current_x=x;}
	if (FoundY) {CoordMotion->m_Stoppedy=y; p_setup->current_y=y;}
	if (FoundZ) {CoordMotion->m_Stoppedz=z; p_setup->current_z=z;}
	if (FoundA) {CoordMotion->m_Stoppeda=a; p_setup->AA_current=a;}
	if (FoundB) {CoordMotion->m_Stoppedb=b; p_setup->BB_current=b;}
	if (FoundC) {CoordMotion->m_Stoppedc=c; p_setup->CC_current=c;}

	CoordMotion->m_StoppedMachinex = UserUnitsToInchesX(CoordMotion->m_Stoppedx+_setup.axis_offset_x+_setup.origin_offset_x+_setup.tool_xoffset);
	CoordMotion->m_StoppedMachiney = UserUnitsToInches(CoordMotion->m_Stoppedy+_setup.axis_offset_y+_setup.origin_offset_y+_setup.tool_yoffset);
	CoordMotion->m_StoppedMachinez = UserUnitsToInches(CoordMotion->m_Stoppedz+_setup.axis_offset_z+_setup.origin_offset_z+_setup.tool_length_offset);
	CoordMotion->m_StoppedMachinea  = InchesOrDegToUserUnitsA(CoordMotion->m_Stoppeda) - _setup.AA_axis_offset - _setup.AA_origin_offset;
	CoordMotion->m_StoppedMachineb  = InchesOrDegToUserUnitsB(CoordMotion->m_Stoppedb) - _setup.BB_axis_offset - _setup.BB_origin_offset;
	CoordMotion->m_StoppedMachinec  = InchesOrDegToUserUnitsC(CoordMotion->m_Stoppedc) - _setup.CC_axis_offset - _setup.CC_origin_offset;



	CoordMotion->m_PreviouslyStopped = CoordMotion->m_Stopping = STOPPED_INDEP;

	return 0;
}
コード例 #3
0
int CGCodeInterpreter::DoResumeSafe()
{
	double SafeZ,SafeZMachine,Machinex,Machiney,Machinez,Machinea,Machineb,Machinec;

	if (!m_Resume) return 0;

	m_Resume=false;

	// read the current positions

	if (CoordMotion->ReadCurAbsPosition(&CoordMotion->current_x,&CoordMotion->current_y,&CoordMotion->current_z,
 									    &CoordMotion->current_a,&CoordMotion->current_b,&CoordMotion->current_c,true)) return 1;


	if (CoordMotion->ReadCurAbsPosition(&Machinex,&Machiney,&Machinez,&Machinea,&Machineb,&Machinec,true)) return 1;

	SafeZ = UserUnitsToInches(m_ResumeSafeZ);

	// compute the safe z height as an absolute position
	// in user units
	if (m_ResumeSafeRelAbs)
	{
		// absolute height, convert value to Inches
		SafeZMachine = UserUnitsToInches(m_ResumeSafeZ+_setup.axis_offset_z+_setup.origin_offset_z+_setup.tool_length_offset);
	}
	else
	{
		// relative move, convert value to inches
		SafeZMachine = Machinez + UserUnitsToInches(m_ResumeSafeZ);
	}

	SetupTracker.ClearHistory();
	SetupTracker.InsertState(&_setup);  // save the interpreter state (including current sequence number)

	if (m_ResumeMoveToSafeZ)
	{
		// keep everything where it is, except move z
		Machinez = SafeZMachine;
		if (CoordMotion->StraightTraverse(Machinex,Machiney,Machinez,Machinea,Machineb,Machinec,false,_setup.sequence_number+1)) return 2;
	}

	if (m_ResumeTraverseXY)
	{
		// keep everything where it is, except move xy (and abc)
		Machinex = UserUnitsToInchesX(m_ResumeTraverseSafeX+_setup.axis_offset_x+_setup.origin_offset_x+_setup.tool_xoffset);
		Machiney = UserUnitsToInches(m_ResumeTraverseSafeY+_setup.axis_offset_y+_setup.origin_offset_y+_setup.tool_yoffset);
		Machinea = CoordMotion->m_StoppedMachinea;
		Machineb = CoordMotion->m_StoppedMachineb;
		Machinec = CoordMotion->m_StoppedMachinec;
		if (CoordMotion->StraightTraverse(Machinex,Machiney,Machinez,Machinea,Machineb,Machinec,false,_setup.sequence_number+1)) return 2;
	}

	if (m_ResumeSafeStartSpindle)
	{
		if (m_ResumeSafeSpindleCWCCW==0)
		{
			InvokeAction(3);  // do the defined action for M Code
		}
		else
		{
			InvokeAction(4);  // do the defined action for M Code
		}
	}

	if (m_ResumeDoSafeFeedZ)
	{
		Machinez = GC->UserUnitsToInches(m_ResumeFeedSafeZ+_setup.axis_offset_z+_setup.origin_offset_z+_setup.tool_length_offset);

		if (CoordMotion->StraightFeed(UserUnitsToInches(m_ResumeZFeedRate)/60.0,Machinex,Machiney,Machinez,Machinea,Machineb,Machinec,_setup.sequence_number+1,0)) return 2;
	}

	if (m_ResumeRestoreFeedRate)
	{
		p_setup->feed_rate = m_ResumeResumeFeedRate;
		SetFeedRate(m_ResumeResumeFeedRate);
	}

	if (CoordMotion->FlushSegments()) {CoordMotion->SetAbort(); return 1;}  
	if (CoordMotion->WaitForSegmentsFinished(TRUE)) {CoordMotion->SetAbort(); return 1;}

	return 0;    
}