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

📄 control.c

📁 Source code for an Numeric Cmputer
💻 C
📖 第 1 页 / 共 5 页
字号:
	    emcmotDebug->teleop_data.desiredAccell.tran.z = 0.0;	    emcmotDebug->teleop_data.desiredAccell.a = 0.0;	    emcmotDebug->teleop_data.desiredAccell.b = 0.0;	    emcmotDebug->teleop_data.desiredAccell.c = 0.0;	    SET_MOTION_TELEOP_FLAG(1);	    SET_MOTION_ERROR_FLAG(0);	} else {	    /* not in position-- don't honor mode change */	    emcmotDebug->teleoperating = 0;	}    } else {	if (GET_MOTION_INPOS_FLAG()) {	    if (!emcmotDebug->teleoperating && GET_MOTION_TELEOP_FLAG()) {		SET_MOTION_TELEOP_FLAG(0);		if (!emcmotDebug->coordinating) {		    for (joint_num = 0; joint_num < EMCMOT_MAX_AXIS;			joint_num++) {			/* point to joint data */			joint = &joints[joint_num];			/* update free planner positions */			joint->free_pos_cmd = joint->pos_cmd;		    }		}	    }	}	/* check for entering coordinated mode */	if (emcmotDebug->coordinating && !GET_MOTION_COORD_FLAG()) {	    if (GET_MOTION_INPOS_FLAG()) {		/* preset traj planner to current position */		tpSetPos(&emcmotDebug->queue, emcmotStatus->carte_pos_fb);		/* drain the cubics so they'll synch up */		for (joint_num = 0; joint_num < EMCMOT_MAX_AXIS; joint_num++) {		    /* point to joint data */		    joint = &joints[joint_num];		    cubicDrain(&(joint->cubic));		}		/* clear the override limits flags */		emcmotDebug->overriding = 0;		emcmotStatus->overrideLimits = 0;		SET_MOTION_COORD_FLAG(1);		SET_MOTION_TELEOP_FLAG(0);		SET_MOTION_ERROR_FLAG(0);	    } else {		/* not in position-- don't honor mode change */		emcmotDebug->coordinating = 0;	    }	}	/* check entering free space mode */	if (!emcmotDebug->coordinating && GET_MOTION_COORD_FLAG()) {	    if (GET_MOTION_INPOS_FLAG()) {		for (joint_num = 0; joint_num < EMCMOT_MAX_AXIS; joint_num++) {		    /* point to joint data */		    joint = &joints[joint_num];		    /* set joint planner pos cmd to current location */		    joint->free_pos_cmd = joint->pos_cmd;		    /* but it can stay disabled until a move is required */		    joint->free_tp_enable = 0;		}		SET_MOTION_COORD_FLAG(0);		SET_MOTION_TELEOP_FLAG(0);		SET_MOTION_ERROR_FLAG(0);	    } else {		/* not in position-- don't honor mode change */		emcmotDebug->coordinating = 1;	    }	}    }    /*! \todo FIXME - this code is temporary - eventually this function will be       cleaned up and simplified, and 'motion_state' will become the master       for this info, instead of having to gather it from several flags */    if (!GET_MOTION_ENABLE_FLAG()) {	emcmotStatus->motion_state = EMCMOT_MOTION_DISABLED;    } else if (GET_MOTION_TELEOP_FLAG()) {	emcmotStatus->motion_state = EMCMOT_MOTION_TELEOP;    } else if (GET_MOTION_COORD_FLAG()) {	emcmotStatus->motion_state = EMCMOT_MOTION_COORD;    } else {	emcmotStatus->motion_state = EMCMOT_MOTION_FREE;    }}/* Beginning of homing related code *//* Length of delay between homing motions - this is intended to   ensure that all motion has ceased and switch bouncing has   ended.  We might want to make this user adjustable, but for   now it's a constant.  It is in seconds */#define HOME_DELAY 0.100/* variable used internally by do_homing, but global so that   'home_do_moving_checks()' can access it */static int immediate_state;/* a couple of helper functions with code that would otherwise be   repeated in several different states of the homing state machine *//* 'home_start_move()' starts a move at the specified velocity.  The   length of the move is equal to twice the overall range of the axis,   but the intent is that something (like a home switch or index pulse)   will stop it before that point. */void home_start_move(emcmot_joint_t * joint, double vel){    double axis_range;    /* set up a long move */    axis_range = joint->max_pos_limit - joint->min_pos_limit;    if (vel > 0.0) {	joint->free_pos_cmd = joint->pos_cmd + 2.0 * axis_range;    } else {	joint->free_pos_cmd = joint->pos_cmd - 2.0 * axis_range;    }    joint->free_vel_lim = fabs(vel);    /* start the move */    joint->free_tp_enable = 1;}/* 'home_do_moving_checks()' is called from states where the machine   is supposed to be moving.  It checks to see if the machine has   hit a limit, or if the move has stopped.  (Normally such moves   will be terminated by the home switch or an index pulse or some   other event, if the move goes to completion, something is wrong.) */static void home_do_moving_checks(emcmot_joint_t * joint){    /* check for limit switches */    if (joint->pos_limit_latch || joint->neg_limit_latch) {	/* on limit, check to see if we should trip */	if (!(joint->home_flags & HOME_IGNORE_LIMITS)) {	    /* not ignoring limits, time to quit */	    reportError("hit limit in home state %d", joint->home_state);	    joint->home_state = HOME_ABORT;	    immediate_state = 1;	    return;	}    }    /* check for reached end of move */    if (!joint->free_tp_active) {	/* reached end of move without hitting switch */	joint->free_tp_enable = 0;	reportError("end of move in home state %d", joint->home_state);	joint->home_state = HOME_ABORT;	immediate_state = 1;	return;    }}static void do_homing(void){/* this is still very much under construction */    int joint_num;    emcmot_joint_t *joint;    double offset, tmp;    int home_sw_new, home_sw_rise, home_sw_fall;    if (emcmotStatus->motion_state != EMCMOT_MOTION_FREE) {	/* can't home unless in free mode */	return;    }    /* loop thru axis, treat each one individually */    for (joint_num = 0; joint_num < EMCMOT_MAX_AXIS; joint_num++) {	/* point to joint struct */	joint = &joints[joint_num];	/* detect rising and falling edges on home switch */	home_sw_rise = 0;	home_sw_fall = 0;	home_sw_new = GET_JOINT_HOME_SWITCH_FLAG(joint);	if (home_sw_new) {	    if (!joint->home_sw_old) {		home_sw_rise = 1;	    }	} else {	    if (joint->home_sw_old) {		home_sw_fall = 1;	    }	}	joint->home_sw_old = home_sw_new;	/* when an axis is homing, 'check_for_faults()' ignores its limit	   switches, so that this code can do the right thing with them. Once	   the homing process is finished, the 'check_for_faults()' resumes	   checking */	/* homing state machine */	/* Some portions of the homing sequence can run thru two or more	   states during a single servo period.  This is done using	   'immediate_state'.  If a state transition sets it true (non-zero),	   this 'do-while' will loop executing switch(home_state) immediately	   to run the new state code.  Otherwise, the loop will fall thru, and	   switch(home_state) runs only once per servo period. Do _not_ set	   'immediate_state' true unless you also change 'home_state', unless	   you want an infinite loop! */	do {	    immediate_state = 0;	    switch (joint->home_state) {	    case HOME_IDLE:		/* nothing to do */		break;	    case HOME_START:		/* This state is responsible for getting the homing process		   started.  It doesn't actually do anything, it simply		   determines what state is next */		/* set flags that communicate with the rest of EMC */		SET_JOINT_HOMING_FLAG(joint, 1);		SET_JOINT_HOMED_FLAG(joint, 0);		SET_JOINT_AT_HOME_FLAG(joint, 0);		/* stop any existing motion */		joint->free_tp_enable = 0;		/* reset delay counter */		joint->home_pause_timer = 0;		/* figure out exactly what homing sequence is needed */		if (joint->home_search_vel == 0.0) {		    /* vel == 0 means no switch, home at current position */		    joint->home_state = HOME_SET_FINAL_POSITION;		    immediate_state = 1;		    break;		} else {		    /* need to find home switch */		    joint->home_state = HOME_INITIAL_SEARCH_START;		    immediate_state = 1;		    break;		}		break;	    case HOME_INITIAL_BACKOFF_START:		/* This state is called if the homing sequence starts at a		   location where the home switch is already tripped. It		   starts a move away from the switch. */		/* is the joint still moving? */		if (joint->free_tp_active) {		    /* yes, reset delay, wait until joint stops */		    joint->home_pause_timer = 0;		    break;		}		/* has delay timed out? */		if (joint->home_pause_timer < (HOME_DELAY * servo_freq)) {		    /* no, update timer and wait some more */		    joint->home_pause_timer++;		    break;		}		joint->home_pause_timer = 0;		/* set up a move at '-search_vel' to back off of switch */		home_start_move(joint, -joint->home_search_vel);		/* next state */		joint->home_state = HOME_INITIAL_BACKOFF_WAIT;		break;	    case HOME_INITIAL_BACKOFF_WAIT:		/* This state is called while the machine is moving off of		   the home switch.  It terminates when the switch is cleared		   successfully.  If the move ends or hits a limit before it		   clears the switch, the home is aborted. */		/* are we off home switch yet? */		if (home_sw_fall) {		    /* yes, stop motion */		    joint->free_tp_enable = 0;		    /* begin initial search */		    joint->home_state = HOME_INITIAL_SEARCH_START;		    immediate_state = 1;		    break;		}		home_do_moving_checks(joint);		break;	    case HOME_INITIAL_SEARCH_START:		/* This state is responsible for starting a move toward the		   home switch.  This move is at 'search_vel', which can be		   fairly fast, because once the switch is found another		   slower move will be used to set the exact home position. */		/* is the joint already moving? */		if (joint->free_tp_active) {		    /* yes, reset delay, wait until joint stops */		    joint->home_pause_timer = 0;		    break;		}		/* has delay timed out? */		if (joint->home_pause_timer < (HOME_DELAY * servo_freq)) {		    /* no, update timer and wait some more */		    joint->home_pause_timer++;		    break;		}		joint->home_pause_timer = 0;		/* make sure we aren't already on home switch */		if (GET_JOINT_HOME_SWITCH_FLAG(joint)) {		    /* already on switch, need to back off it first */		    joint->home_state = HOME_INITIAL_BACKOFF_START;		    immediate_state = 1;		    break;		}		/* set up a move at 'search_vel' to find switch */		home_start_move(joint, joint->home_search_vel);		/* next state */		joint->home_state = HOME_INITIAL_SEARCH_WAIT;		break;	    case HOME_INITIAL_SEARCH_WAIT:		/* This state is called while the machine is looking for the		   home switch.  It terminates when the switch is found.  If		   the move ends or hits a limit before it finds the switch,		   the home is aborted. */		/* have we hit home switch yet? */		if (home_sw_rise) {		    /* yes, stop motion */		    joint->free_tp_enable = 0;		    /* go to next step */		    joint->home_state = HOME_SET_COARSE_POSITION;		    immediate_state = 1;		    break;		}		home_do_moving_checks(joint);		break;	    case HOME_SET_COARSE_POSITION:		/* This state is called after the first time the switch is		   found.  At this point, we are approximately home. Although		   we will do another slower pass to get the exact home		   location, we reset the joint coordinates now so that screw		   error comp will be appropriate for this portion of the		   screw (previously we didn't know where we were at all). */		/* set the current position to 'home_offset' */		offset = joint->home_offset - joint->pos_fb;		/* this moves the internal position but does not affect the		   motor position */		joint->pos_cmd += offset;		joint->pos_fb += offset;		joint->free_pos_cmd += offset;		joint->motor_offset -= offset;		/* The next state depends on the signs of 'search_vel' and		   'latch_vel'.  If they are the same, that means we must		   back up, then do the final homing moving the same		   direction as the initial search, on a rising edge of the		   switch.  If they are opposite, it means that the final		   homing will take place on a falling edge as the machine		   moves off of the switch. */		tmp = joint->home_search_vel * joint->home_latch_vel;		if (tmp > 0.0) {		    /* search and latch vel are same direction */		    joint->home_state = HOME_FINAL_BACKOFF_START;		} else if (tmp < 0.0) {		    /* search and latch vel are opposite directions */		    joint->home_state = HOME_FALL_SEARCH_START;		} else {		    /* latch vel is zero - error */		    reportError("home latch velocity is zero");		    joint->home_state = HOME_ABORT;		}		immediate_state = 1;		break;	    case HOME_FINAL_BACKOFF_START:		/* This state is called once the approximate location of the		   switch has been found.  It is responsible for starting a		   move that will back off of the switch in preparation for a		   final slow move that captures the exact switch location. */		/* is the joint already moving? */		if (joint->free_tp_active) {		    /* yes, reset delay, wait until joint stops */		    joint->home_pause_timer = 0;		    break;		}		/* has delay timed out? */		if (joint->home_pause_timer < (HOME_DELAY * servo_freq)) {		    /* no, update timer and wait some more */		    joint->home_pause_timer++;		    break;		}		joint->home_pause_timer = 0;		/* set up a move at '-search_vel' to back off of switch */		home_start_move(joint, -joint->home_search_vel);

⌨️ 快捷键说明

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