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

📄 iocontrol.cc

📁 Source code for an Numeric Cmputer
💻 CC
📖 第 1 页 / 共 3 页
字号:
	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: hal_init() failed\n");	return -1;    }    /* STEP 2: allocate shared memory for iocontrol data */    iocontrol_data = (iocontrol_str *) hal_malloc(sizeof(iocontrol_str));    if (iocontrol_data == 0) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: hal_malloc() failed\n");	hal_exit(comp_id);	return -1;    }    /* STEP 3a: export the out-pin(s) */    // user-enable-out    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.user-enable-out", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->user_enable_out), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin user-enable-out export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // user-request-enable    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.user-request-enable", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->user_request_enable), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin user-request-enable export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // coolant-flood    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.coolant-flood", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->coolant_flood),	comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin coolant-flood export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // coolant-mist    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.coolant-mist", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->coolant_mist),			comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin coolant-mist export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // lube    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.lube", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->lube), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin lube export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // tool-prepare    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.tool-prepare", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->tool_prepare), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin tool-prepare export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // tool-number    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.tool-prep-number", n);    retval =	hal_pin_u8_new(name, HAL_WR, &(iocontrol_data->tool_prep_number), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin tool-prep-number export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // tool-prepared    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.tool-prepared", n);    retval =	hal_pin_bit_new(name, HAL_RD, &(iocontrol_data->tool_prepared), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin tool-prepared export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // tool-change    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.tool-change", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->tool_change), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin tool-change export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // tool-changed    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.tool-changed", n);    retval =	hal_pin_bit_new(name, HAL_RD, &(iocontrol_data->tool_changed), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin tool-changed export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // spindle-on    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.spindle-on", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->spindle_on), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin spindle-on export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // spindle-forward    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.spindle-forward", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->spindle_forward), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin spindle-forward export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // spindle-reverse    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.spindle-reverse", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->spindle_reverse), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin spindle-reverse export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // spindle-incr-speed    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.spindle-incr-speed",		   n);    retval =	hal_pin_bit_new(name, HAL_WR,			&(iocontrol_data->spindle_incr_speed), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin spindle-incr-speed export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // spindle-decr-speed    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.spindle-decr-speed",		   n);    retval =	hal_pin_bit_new(name, HAL_WR,			&(iocontrol_data->spindle_decr_speed), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin spindle-decr-speed export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // spindle-brake    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.spindle-brake", n);    retval =	hal_pin_bit_new(name, HAL_WR, &(iocontrol_data->spindle_brake),	comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin spindle-brake export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // spindle-speed-out    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.spindle-speed-out",		   n);    retval =	hal_pin_float_new(name, HAL_WR,			  &(iocontrol_data->spindle_speed_out), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin spindle-speed-out export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    /* STEP 3b: export the in-pin(s) */    // emc-enable-in    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.emc-enable-in", n);    retval =	hal_pin_bit_new(name, HAL_RD, &(iocontrol_data->emc_enable_in), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin emc-enable-in export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // lube_level    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.lube_level", n);    retval =	hal_pin_bit_new(name, HAL_RD, &(iocontrol_data->lube_level), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin lube_level export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    // spindle-speed-in    rtapi_snprintf(name, HAL_NAME_LEN, "iocontrol.%d.spindle-speed-in", n);    retval =	hal_pin_float_new(name, HAL_RD,			  &(iocontrol_data->spindle_speed_in), comp_id);    if (retval != HAL_SUCCESS) {	rtapi_print_msg(RTAPI_MSG_ERR,			"IOCONTROL: ERROR: iocontrol %d pin spindle-speed-in export failed with err=%i\n",			n, retval);	hal_exit(comp_id);	return -1;    }    return 0;}/********************************************************************** Description: hal_init_pins(void)** Side Effects: Sets HAL pins default values.** Called By: main********************************************************************/void hal_init_pins(void){    *(iocontrol_data->user_enable_out)=0;	/* output, FALSE when EMC wants stop */    *(iocontrol_data->user_request_enable)=0;	/* output, used to reset HAL latch */    *(iocontrol_data->coolant_mist)=0;		/* coolant mist output pin */    *(iocontrol_data->coolant_flood)=0;		/* coolant flood output pin */    *(iocontrol_data->lube)=0;			/* lube output pin */    *(iocontrol_data->tool_prepare)=0;		/* output, pin that notifies HAL it needs to prepare a tool */    *(iocontrol_data->tool_prep_number)=0;	/* output, pin that holds the tool number to be prepared, only valid when tool-prepare=TRUE */    *(iocontrol_data->tool_change)=0;		/* output, notifies a tool-change should happen (emc should be in the tool-change position) */    *(iocontrol_data->spindle_on)=0;		/* spindle spin output */    *(iocontrol_data->spindle_forward)=0;	/* spindle spin-forward output */    *(iocontrol_data->spindle_reverse)=0;	/* spindle spin-reverse output */    *(iocontrol_data->spindle_incr_speed)=0;	/* spindle spin-increase output */    *(iocontrol_data->spindle_decr_speed)=0;	/* spindle spin-decrease output */    *(iocontrol_data->spindle_brake)=0;		/* spindle brake output */    *(iocontrol_data->spindle_speed_out)=0.0;	/* spindle speed output */}/********************************************************************** Description: read_hal_inputs(void)*			Reads the pin values from HAL *			this function gets called once per cycle*			It sets the values for the emcioStatus.aux.*** Returns:	returns > 0 if any of the status has changed*		we then need to update through NML** Side Effects: updates values** Called By: main every CYCLE********************************************************************/int read_hal_inputs(void){    int oldval, retval = 0;    oldval = emcioStatus.aux.estop;    if ( *(iocontrol_data->emc_enable_in)==0) //check for estop from HW	emcioStatus.aux.estop = 1;    else	emcioStatus.aux.estop = 0;        if (oldval != emcioStatus.aux.estop) {	retval = 1;    }            oldval = emcioStatus.lube.level;    emcioStatus.lube.level = *(iocontrol_data->lube_level);	//check for lube_level from HW    if (oldval != emcioStatus.lube.level) {	retval = 1;    }    return retval;}/********************************************************************** Description: read_tool_inputs(void)*			Reads the tool-pin values from HAL *			this function gets called once per cycle*			It sets the values for the emcioStatus.aux.*** Returns:	returns which of the status has changed*		we then need to update through NML (a bit different as read_hal_inputs)** Side Effects: updates values** Called By: main every CYCLE********************************************************************/int read_tool_inputs(void){    int oldval, retval = 0;    oldval = emcioStatus.tool.toolPrepped;    if ((*(iocontrol_data->tool_prepare) == 1) && (*(iocontrol_data->tool_prepared) == 1)) {	emcioStatus.tool.toolPrepped = *(iocontrol_data->tool_prep_number); //check if tool has been prepared	*(iocontrol_data->tool_prepare) = 0;	emcioStatus.status = RCS_DONE;  // we finally finished to do tool-changing, signal task with RCS_DONE	return 10; //prepped finished    }        oldval = emcioStatus.tool.toolInSpindle;    if ((*(iocontrol_data->tool_change) != 0) && (*(iocontrol_data->tool_changed)==1)) {	emcioStatus.tool.toolInSpindle = emcioStatus.tool.toolPrepped; //check if tool has been prepared	emcioStatus.tool.toolPrepped = -1; //reset the tool preped number, -1 to permit tool 0 to be loaded	*(iocontrol_data->tool_prep_number) = 0; //likewise in HAL	*(iocontrol_data->tool_change) = 0; //also reset the tool change signal	emcioStatus.status = RCS_DONE;	// we finally finished to do tool-changing, signal task with RCS_DONE	return 11; //change finished    }    return retval;}/********************************************************************** Description: main(int argc, char * argv[])*		Connects to NML buffers and enters an endless loop*		processing NML IO commands. Print statements are*		sent to the console indicating which IO command was*		executed if debug level is set to RTAPI_MSG_DBG.** Return Value: Zero or -1 if ini file not found or failure to connect*		to NML buffers.** Side Effects: None.** Called By:*********************************************************************/int main(int argc, char *argv[]){    int t, tool_status;    NMLTYPE type;    for (t = 1; t < argc; t++) {	if (!strcmp(argv[t], "-ini")) {	    if (t == argc - 1) {		return -1;	    } else {		strcpy(EMC_INIFILE, argv[t + 1]);		t++;	    }	    continue;	}	/* do other args similarly here */    }    if (0 != iniLoad(EMC_INIFILE)) {	rtapi_print_msg(RTAPI_MSG_ERR, "can't open ini file %s\n",			EMC_INIFILE);	return -1;    }    if (0 != emcIoNmlGet()) {	rtapi_print_msg(RTAPI_MSG_ERR,			"can't connect to NML buffers in %s\n",			EMC_NMLFILE);	return -1;    }    // used only for getting TOOL_TABLE_FILE out of the ini file    if (0 != iniTool(EMC_INIFILE)) {	rcs_print_error("iniTool failed.\n");	return -1;    }    if (0 != loadToolTable(TOOL_TABLE_FILE, emcioStatus.tool.toolTable)) {	rcs_print_error("can't load tool table.\n");    }    done = 0;    /* Register the routine that catches the SIGINT signal */    signal(SIGINT, quit);    /* catch SIGTERM too - the run script uses it to shut things down */    signal(SIGTERM, quit);    if (iocontrol_hal_init() != 0) {	rtapi_print_msg(RTAPI_MSG_ERR, "can't initialize the HAL\n");	return -1;    }    /* set status values to 'normal' */    emcioStatus.aux.estop = 1; //estop=1 means to emc that ESTOP condition is met    emcioStatus.tool.toolPrepped = -1;    emcioStatus.tool.toolInSpindle = 0;    emcioStatus.spindle.speed = 0.0;    emcioStatus.spindle.direction = 0;    emcioStatus.spindle.brake = 1;    emcioStatus.spindle.increasing = 0;    emcioStatus.spindle.enabled = 1;    emcioStatus.coolant.mist = 0;    emcioStatus.coolant.flood = 0;    emcioStatus.lube.on = 0;    emcioStatus.lube.level = 1;    while (!done) {	// check for inputs from HAL (updates emcioStatus)	// returns 1 if any of the HAL pins changed from the last time we checked	/* if an external ESTOP is activated (or another hal-pin has changed)	   a NML message has to be pushed to EMC.	   the way it was done status was only checked at the end of a command */	if (read_hal_inputs() > 0) {

⌨️ 快捷键说明

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