⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iocontrol.cc

📁 Source code for an Numeric Cmputer
💻 CC
📖 第 1 页 / 共 3 页
字号:
/********************************************************************* Description: IoControl.cc*           Simply accepts NML messages sent to the IO controller*           outputs those to a HAL pin,*           and sends back a "Done" message.***  ENABLE logic:  this module exports three HAL pins related to ENABLE.*  The first is emc-enable-in.  It is an input from the HAL, when FALSE,*  EMC will go into the STOPPED state (regardless of the state of*  the other two pins).  When it goes TRUE, EMC will go into the*  ESTOP_RESET state (also known as READY).**  The second HAL pin is an output to the HAL.  It is controlled by*  the NML messages ESTOP_ON and ESTOP_OFF, which normally result from*  user actions at the GUI.  For the simplest system, loop user-enable-out *  back to emc-enable-in in the HAL.  The GUI controls user-enable-out, and EMC*  responds to that once it is looped back.**  If external ESTOP inputs are desired, they can be*  used in a classicladder rung, in series with user-enable-out.*  It will look like this:**  -----|UEO|-----|EEST|--+--|EEI|--+--(EEI)----*                         |         |*                         +--|URE|--+*  UEO=user-enable-out*  EEST=external ESTOP circuitry*  EEI=machine is enabled*  URE=user request enable**  This will work like this: EMC will be enabled (by EEI, emc-enabled-in),*  only if UEO, EEST and EEI are closed. *  If any of UEO (user requested stop) or EEST (external estop) have been*  opened, then EEI will open aswell.*  After restoring normal condition (UEO and EEST closed), an aditional*  URE (user-request-enable) is needed, this is either sent by the GUI*  (using the EMC_AUX_ESTOP_RESET NML message), or by a hardware button*  connected to the ladder driving URE.**  NML messages are sent usually from the user hitting F1 on the GUI.*  *   Derived from a work by Fred Proctor & Will Shackleford** Author:* License: GPL Version 2* System: Linux*    * Copyright (c) 2004 All rights reserved.** Last change: * $Revision: 1.29.2.1 $* $Author: cradek $* $Date: 2006/05/26 01:56:03 $********************************************************************/#include <stdio.h>#include <string.h>#include <stdlib.h>#include <signal.h>#include "hal.h"		/* access to HAL functions/definitions */#include "rtapi.h"		/* rtapi_print_msg */#include "rcs.hh"		/* RCS_CMD_CHANNEL */#include "emc.hh"		/* EMC NML */#include "emcglb.h"		/* EMC_NMLFILE, EMC_INIFILE, TOOL_TABLE_FILE */#include "inifile.hh"		/* INIFILE */#include "initool.hh"		/* iniTool() */static RCS_CMD_CHANNEL *emcioCommandBuffer = 0;static RCS_CMD_MSG *emcioCommand = 0;static RCS_STAT_CHANNEL *emcioStatusBuffer = 0;static EMC_IO_STAT emcioStatus;static NML *emcErrorBuffer = 0;struct iocontrol_str {    hal_bit_t *user_enable_out;	/* output, TRUE when EMC wants stop */    hal_bit_t *emc_enable_in;	/* input, TRUE on any external stop */    hal_bit_t *user_request_enable;	/* output, used to reset ENABLE latch */    hal_bit_t *coolant_mist;	/* coolant mist output pin */    hal_bit_t *coolant_flood;	/* coolant flood output pin */    hal_bit_t *lube;		/* lube output pin */    hal_bit_t *lube_level;	/* lube level input pin */    // the following pins are needed for toolchanging    //tool-prepare    hal_bit_t *tool_prepare;	/* output, pin that notifies HAL it needs to prepare a tool */    hal_u8_t  *tool_prep_number;/* output, pin that holds the tool number to be prepared, only valid when tool-prepare=TRUE */    hal_bit_t *tool_prepared;	/* input, pin that notifies that the tool has been prepared */    //tool-change    hal_bit_t *tool_change;	/* output, notifies a tool-change should happen (emc should be in the tool-change position) */    hal_bit_t *tool_changed;	/* input, notifies tool has been changed */    // creating a lot of pins for spindle control to be very flexible    // the user needs only a subset of these    // simplest way of spindle control (output start/stop)    hal_bit_t *spindle_on;	/* spindle spin output */    // same thing for 2 directions    hal_bit_t *spindle_forward;	/* spindle spin-forward output */    hal_bit_t *spindle_reverse;	/* spindle spin-reverse output */    // simple velocity control (as long as the output is active the spindle    //                          should accelerate/decelerate    hal_bit_t *spindle_incr_speed;	/* spindle spin-increase output */    hal_bit_t *spindle_decr_speed;	/* spindle spin-decrease output */    // simple output for brake    hal_bit_t *spindle_brake;	/* spindle brake output */    // output of a prescribed speed (to hook-up to a velocity controller)    hal_float_t *spindle_speed_out;	/* spindle speed output */    hal_float_t *spindle_speed_in;	/* spindle speed measured */} * iocontrol_data;			//pointer to the HAL-struct//static iocontrol_struct *iocontrol_data;	static int comp_id;				/* component ID *//********************************************************************** Description: emcIoNmlGet()*		Attempts to connect to NML buffers and set the relevant*		pointers.** Return Value: Zero on success or -1 if can not connect to a buffer.** Side Effects: None.** Called By: main()*********************************************************************/static int emcIoNmlGet(){    int retval = 0;    /* Try to connect to EMC IO command buffer */    if (emcioCommandBuffer == 0) {	emcioCommandBuffer =	    new RCS_CMD_CHANNEL(emcFormat, "toolCmd", "tool", EMC_NMLFILE);	if (!emcioCommandBuffer->valid()) {	    rtapi_print_msg(RTAPI_MSG_ERR,			    "emcToolCmd buffer not available\n");	    delete emcioCommandBuffer;	    emcioCommandBuffer = 0;	    retval = -1;	} else {	    /* Get our command data structure */	    emcioCommand = emcioCommandBuffer->get_address();	}    }    /* try to connect to EMC IO status buffer */    if (emcioStatusBuffer == 0) {	emcioStatusBuffer =	    new RCS_STAT_CHANNEL(emcFormat, "toolSts", "tool",				 EMC_NMLFILE);	if (!emcioStatusBuffer->valid()) {	    rtapi_print_msg(RTAPI_MSG_ERR,			    "toolSts buffer not available\n");	    delete emcioStatusBuffer;	    emcioStatusBuffer = 0;	    retval = -1;	} else {	    /* initialize and write status */	    emcioStatus.heartbeat = 0;	    emcioStatus.command_type = 0;	    emcioStatus.echo_serial_number = 0;	    emcioStatus.status = RCS_DONE;	    emcioStatusBuffer->write(&emcioStatus);	}    }    /* try to connect to EMC error buffer */    if (emcErrorBuffer == 0) {	emcErrorBuffer =	    new NML(nmlErrorFormat, "emcError", "tool", EMC_NMLFILE);	if (!emcErrorBuffer->valid()) {	    rtapi_print_msg(RTAPI_MSG_ERR,			    "emcError buffer not available\n");	    delete emcErrorBuffer;	    emcErrorBuffer = 0;	    retval = -1;	}    }    return retval;}static int iniLoad(const char *filename){    Inifile inifile;    const char *inistring;    char version[LINELEN], machine[LINELEN];    /* Open the ini file */    if (inifile.open(filename) == false) {	return -1;    }    if (NULL != (inistring = inifile.find("DEBUG", "EMC"))) {	/* copy to global */	if (1 != sscanf(inistring, "%i", &EMC_DEBUG)) {	    EMC_DEBUG = 0;	}    } else {	/* not found, use default */	EMC_DEBUG = 0;    }    if (EMC_DEBUG & EMC_DEBUG_VERSIONS) {	if (NULL != (inistring = inifile.find("VERSION", "EMC"))) {	    if(sscanf(inistring, "$Revision: %s", version) != 1) {		strncpy(version, "unknown", LINELEN-1);	    }	} else {	    strncpy(version, "unknown", LINELEN-1);	}	if (NULL != (inistring = inifile.find("MACHINE", "EMC"))) {	    strncpy(machine, inistring, LINELEN-1);	} else {	    strncpy(machine, "unknown", LINELEN-1);	}	rtapi_print("iocontrol: machine: '%s'  version '%s'\n", machine, version);    }    if (NULL != (inistring = inifile.find("NML_FILE", "EMC"))) {	strcpy(EMC_NMLFILE, inistring);    } else {	// not found, use default    }    double temp;    temp = EMC_IO_CYCLE_TIME;    if (NULL != (inistring = inifile.find("CYCLE_TIME", "EMCIO"))) {	if (1 == sscanf(inistring, "%lf", &EMC_IO_CYCLE_TIME)) {	    // found it	} else {	    // found, but invalid	    EMC_IO_CYCLE_TIME = temp;	    rtapi_print		("invalid [EMCIO] CYCLE_TIME in %s (%s); using default %f\n",		 filename, inistring, EMC_IO_CYCLE_TIME);	}    } else {	// not found, using default	rtapi_print	    ("[EMCIO] CYCLE_TIME not found in %s; using default %f\n",	     filename, EMC_IO_CYCLE_TIME);    }    // close it    inifile.close();    return 0;}/********************************************************************** Description: loadToolTable(const char *filename, CANON_TOOL_TABLE toolTable[])*		Loads the tool table from file filename into toolTable[] array.*		  Array is CANON_TOOL_MAX + 1 entries, since 0 is included.** Return Value: Zero on success or -1 if file not found.** Side Effects: Default setting used if the parameter not found in*		the ini file.** Called By: main()*********************************************************************/static int loadToolTable(const char *filename,			 CANON_TOOL_TABLE toolTable[]){    int t;    FILE *fp;    char buffer[CANON_TOOL_ENTRY_LEN];    const char *name;    // check filename    if (filename[0] == 0) {	name = TOOL_TABLE_FILE;    } else {	// point to name provided	name = filename;    }    //AJ: for debug reasons    //rtapi_print("loadToolTable called with %s\n", filename);    // open tool table file    if (NULL == (fp = fopen(name, "r"))) {	// can't open file	return -1;    }    // clear out tool table    for (t = 0; t <= CANON_TOOL_MAX; t++) {	// unused tools are 0, 0.0, 0.0	toolTable[t].id = 0;	toolTable[t].length = 0.0;	toolTable[t].diameter = 0.0;    }    /*       Override 0's with codes from tool file       File format is:       <header>       <pocket # 0..CANON_TOOL_MAX> <FMS id> <length> <diameter>       ...     */    // read and discard header    if (NULL == fgets(buffer, 256, fp)) {	// nothing in file at all	rtapi_print("IO: toolfile exists, but is empty\n");	fclose(fp);	return -1;    }    while (!feof(fp)) {	int pocket;	int id;	double length;	double diameter;	// just read pocket, ID, and length offset	if (NULL == fgets(buffer, CANON_TOOL_ENTRY_LEN, fp)) {	    break;	}	if (4 !=	    sscanf(buffer, "%d %d %lf %lf", &pocket, &id, &length,		   &diameter)) {	    // bad entry-- skip	    continue;	} else {	    if (pocket < 0 || pocket > CANON_TOOL_MAX) {		continue;	    } else {		toolTable[pocket].id = id;		toolTable[pocket].length = length;		toolTable[pocket].diameter = diameter;	    }	}    }    // close the file    fclose(fp);    return 0;}/********************************************************************** Description: saveToolTable(const char *filename, CANON_TOOL_TABLE toolTable[])*		Saves the tool table from toolTable[] array into file filename.*		  Array is CANON_TOOL_MAX + 1 entries, since 0 is included.** Return Value: Zero on success or -1 if file not found.** Side Effects: Default setting used if the parameter not found in*		the ini file.** Called By: main()*********************************************************************/static int saveToolTable(const char *filename,			 CANON_TOOL_TABLE toolTable[]){    int pocket;    FILE *fp;    const char *name;    // check filename    if (filename[0] == 0) {	name = TOOL_TABLE_FILE;    } else {	// point to name provided	name = filename;    }    // open tool table file    if (NULL == (fp = fopen(name, "w"))) {	// can't open file	return -1;    }    // write header    fprintf(fp, "POC\tFMS\tLEN\t\tDIAM\n");    for (pocket = 1; pocket <= CANON_TOOL_MAX; pocket++) {	fprintf(fp, "%d\t%d\t%f\t%f\n",		pocket,		toolTable[pocket].id,		toolTable[pocket].length, toolTable[pocket].diameter);    }    // close the file    fclose(fp);    return 0;}static int done = 0;/********************************************************************** Description: quit(int sig)*		Signal handler for SIGINT - Usually generated by a*		Ctrl C sequence from the keyboard.** Return Value: None.** Side Effects: Sets the termination condition of the main while loop.** Called By: Operating system.*********************************************************************/static void quit(int sig){    done = 1;}/********************************************************************** Description: iocontrol_hal_init(void)** Side Effects: Exports HAL pins.** Called By: main********************************************************************/int iocontrol_hal_init(void){    char name[HAL_NAME_LEN + 2];	//name of the pin to be registered    int n = 0, retval;		//n - number of the hal component (only one for iocotrol)    /* STEP 1: initialise the hal component */    comp_id = hal_init("iocontrol");    if (comp_id < 0) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -