示例#1
0
文件: main.c 项目: 100takis/grbl
int main(void)
{
  // Initialize system upon power-up.
  serial_init();   // Setup serial baud rate and interrupts
  settings_init(); // Load grbl settings from EEPROM
  stepper_init();  // Configure stepper pins and interrupt timers
  system_init();   // Configure pinout pins and pin-change interrupt
  
  memset(&sys, 0, sizeof(sys));  // Clear all system variables
  sys.abort = true;   // Set abort to complete initialization
  sei(); // Enable interrupts

  // Check for power-up and set system alarm if homing is enabled to force homing cycle
  // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the
  // startup scripts, but allows access to settings and internal commands. Only a homing
  // cycle '$H' or kill alarm locks '$X' will disable the alarm.
  // NOTE: The startup script will run after successful completion of the homing cycle, but
  // not after disabling the alarm locks. Prevents motion startup blocks from crashing into
  // things uncontrollably. Very bad.
  #ifdef HOMING_INIT_LOCK
    if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; }
  #endif
  
  // Grbl initialization loop upon power-up or a system abort. For the latter, all processes
  // will return to this loop to be cleanly re-initialized.
  for(;;) {

    // TODO: Separate configure task that require interrupts to be disabled, especially upon
    // a system abort and ensuring any active interrupts are cleanly reset.
  
    // Reset Grbl primary systems.
    serial_reset_read_buffer(); // Clear serial read buffer
    gc_init(); // Set g-code parser to default state
    spindle_init();
    coolant_init();
    limits_init(); 
    probe_init();
    plan_reset(); // Clear block buffer and planner variables
    st_reset(); // Clear stepper subsystem variables.

    // Sync cleared gcode and planner positions to current system position.
    plan_sync_position();
    gc_sync_position();

    // Reset system variables.
    sys.abort = false;
    sys.execute = 0;
    if (bit_istrue(settings.flags,BITFLAG_AUTO_START)) { sys.auto_start = true; }
    else { sys.auto_start = false; }
          
    // Start Grbl main loop. Processes program inputs and executes them.
    protocol_main_loop();
    
  }
  return 0;   /* Never reached */
}
示例#2
0
static int probe_start_pid(pid_t pid)
{
	struct probe *probe = probe_init(pid);
	if(probe == NULL) {
		fprintf(stderr,"Failed to initiate probe");
		return -1;
	}
	if(thread_map__start_threads(probe->thread_map)) {
		fprintf(stderr,"Failed to start threads");
		return -1;
	}

	probe_enable(probe);
	
	int i;
	for(i=0; i < probe->thread_map->nr; i++)
                pthread_join(probe->thread_map->threads[i].tid,NULL);
	
	probe_buff__flush(probe->buff);

	return 0;
}
示例#3
0
static int probe_start_exec(char *exec, char **args)
{

	sem_t *sem = sem_open(SNAME, O_CREAT, 0644, 0);
	
	pid_t pid = fork();
	if(pid == 0) {

		sem_t *sem = sem_open(SNAME, 0);
		sem_wait(sem);

		if(execv(exec,args) == -1)
			fprintf(stderr,"Failed to exec\n");
		exit(0);

	} else if(pid > 0) {
		struct probe *probe = probe_init(pid);
		if(probe == NULL) {
			fprintf(stderr,"Failed to initiate probe");
			return -1;
		}
		if(thread_map__start_threads(probe->thread_map)) {
			fprintf(stderr,"Failed to start threads");
			return -1;
		}
		probe_enable(probe);
		sem_post(sem);
	
		int i;	
		for(i=0; i < probe->thread_map->nr; i++)
			pthread_join(probe->thread_map->threads[i].tid,NULL);

		probe_buff__flush(probe->buff);
	//	int status;
	//	while(waitpid(-1,&status, 0) != -1);

	}
	return 0;
}
示例#4
0
文件: solve.c 项目: cygy/pbnsolve
int solve(Puzzle *puz, Solution *sol)
{
    Cell *cell;
    line_t besti, bestj;
    color_t bestc;
    int bestnleft;
    int rc;
    int sprint_clock= 0, plod_clock= PLOD_INIT;

    /* One color puzzles are already solved */
    if (puz->ncolor < 2)
    {
        puz->nsolved= puz->ncells;
        return 1;
    }

    /* Start bookkeeping, if we need it */
    if (mayprobe)
        probe_init(puz,sol);
    else
        bookkeeping_on(puz,sol);

    while (1)
    {
        /* Always start with logical solving */
        if (VA) printf("A: LINE SOLVING\n");
        rc= logic_solve(puz, sol, 0);

        if (rc > 0) return 1;   /* Exit if the puzzle is complete */

        if (rc == 0)
        {
            /* Logical solving has stalled. */

            if (VA)
            {
                printf("A: STUCK - Line solving failed\n");
                print_solution(stdout,puz,sol);
            }

            if (maycontradict)
            {
                /* Try a depth-limited search for logical contradictions */
                if (VA) printf("A: SEARCHING FOR CONTRADICTIONS\n");
                rc= contradict(puz,sol);

                if (rc > 0) return 1; /* puzzle complete - stop */

                if (rc < 0) continue; /* found some - resume logic solving */

                /* otherwise, try something else */
            }

            /* Stop if no guessing is allowed */
            if (!maybacktrack) return 1;

            if (hintlog)
            {
                printf("STARTING SEARCH: EXPLANATION SHUTTING DOWN...\n");
                hintlog= 0;
            }

            /* Shut down the exhaustive search once we start searching */
            if (maylinesolve) mayexhaust= 0;

            /* Turn on caching when we first start searching */
            if (maycache && !cachelines)
            {
                cachelines= 1;
                init_cache(puz);
            }

            if (mayprobe && (!mayguess || sprint_clock <= 0))
            {
                /* Do probing to find best guess to make */
                if (VA) printf("A: PROBING\n");
                rc= probe(puz, sol, &besti, &bestj, &bestc);

                if (rc > 0)
                    return 1; /* Stop if accidentally completed the puzzle */
                if (rc < 0)
                    continue; /* Resume logic solving if found contradiction */

                /* Otherwise, use the guess returned from the probe */
                cell= sol->line[0][besti][bestj];
                if (VA)
                {
                    printf("A: PROBING SELECTED ");
                    print_coord(stdout,puz,cell);
                    printf(" COLOR %d\n",bestc);
                }

                /* If a lot of probes have not been finding contradictions,
                 * consider triggering sprint mode
                 */
                if (mayguess && --plod_clock <= 0)
                {
                    float rate= probe_rate();
                    if (rate >= .12)
                    {
                        /* More than 10% have failed to find contradiction,
                         * so try heuristic searching for a while */
                        bookkeeping_on(puz,sol);
                        sprint_clock= SPRINT_LENGTH;
                        nsprint++;
                        /*printf("STARTING SPRINT - probe rate=%.4f\n",rate);*/
                    }
                    else
                    {
                        /* Success rate of probing is still high. Keep on
                         * trucking. */
                        plod_clock= PLOD_LENGTH;
                        nplod++;
                        /*printf("CONTINUING PLOD - probe rate=%.4f\n", rate);*/
                    }
                }
            }
            else
            {
                /* Old guessing algorithm.  Use heuristics to make a guess */
                cell= pick_a_cell(puz, sol);
                if (cell == NULL)
                    return 0;

                bestc= (*pick_color)(puz,sol,cell);
                if (VA || WC(cell->line[0],cell->line[1]))
                {
                    printf("A: GUESSING SELECTED ");
                    print_coord(stdout,puz,cell);
                    printf(" COLOR %d\n",bestc);
                }

                if (mayprobe && --sprint_clock <= 0)
                {
                    /* If we have reached the end of our sprint, try plodding
                     * again.  */
                    probe_init(puz,sol);
                    plod_clock= PLOD_LENGTH;
                    nplod++;
                    /*printf("ENDING SPRINT\n");*/
                }
            }
            guess_cell(puz, sol, cell, bestc);
            guesses++;
        }
        else
        {
            /* We have hit a contradiction - try backtracking */
            if (VA) printf("A: STUCK ON CONTRADICTION - BACKTRACKING\n");

            guesses++;

            /* Back up to last guess point, and invert that guess */
            if (backtrack(puz,sol))
                /* Nothing to backtrack to - puzzle has no solution */
                return 0;
            if (VB) print_solution(stdout,puz,sol);
            if (VB) dump_history(stdout, puz, VV);
        }
    }
}
示例#5
0
文件: main.c 项目: MrSurly/grbl
int main(void)
{
  // Initialize system upon power-up.
  serial_init();   // Setup serial baud rate and interrupts
  settings_init(); // Load Grbl settings from EEPROM
  stepper_init();  // Configure stepper pins and interrupt timers
  system_init();   // Configure pinout pins and pin-change interrupt

  memset(sys_position,0,sizeof(sys_position)); // Clear machine position.
  sei(); // Enable interrupts

  // Initialize system state.
  #ifdef FORCE_INITIALIZATION_ALARM
    // Force Grbl into an ALARM state upon a power-cycle or hard reset.
    sys.state = STATE_ALARM;
  #else
    sys.state = STATE_IDLE;
  #endif
  
  // Check for power-up and set system alarm if homing is enabled to force homing cycle
  // by setting Grbl's alarm state. Alarm locks out all g-code commands, including the
  // startup scripts, but allows access to settings and internal commands. Only a homing
  // cycle '$H' or kill alarm locks '$X' will disable the alarm.
  // NOTE: The startup script will run after successful completion of the homing cycle, but
  // not after disabling the alarm locks. Prevents motion startup blocks from crashing into
  // things uncontrollably. Very bad.
  #ifdef HOMING_INIT_LOCK
    if (bit_istrue(settings.flags,BITFLAG_HOMING_ENABLE)) { sys.state = STATE_ALARM; }
  #endif

  // Grbl initialization loop upon power-up or a system abort. For the latter, all processes
  // will return to this loop to be cleanly re-initialized.
  for(;;) {

    // Reset system variables.
    uint8_t prior_state = sys.state;
    memset(&sys, 0, sizeof(system_t)); // Clear system struct variable.
    sys.state = prior_state;
    sys.f_override = DEFAULT_FEED_OVERRIDE;  // Set to 100%
    sys.r_override = DEFAULT_RAPID_OVERRIDE; // Set to 100%
    sys.spindle_speed_ovr = DEFAULT_SPINDLE_SPEED_OVERRIDE; // Set to 100%
		memset(sys_probe_position,0,sizeof(sys_probe_position)); // Clear probe position.
    sys_probe_state = 0;
    sys_rt_exec_state = 0;
    sys_rt_exec_alarm = 0;
    sys_rt_exec_motion_override = 0;
    sys_rt_exec_accessory_override = 0;

    // Reset Grbl primary systems.
    serial_reset_read_buffer(); // Clear serial read buffer
    gc_init(); // Set g-code parser to default state
    spindle_init();
    coolant_init();
    limits_init();
    probe_init();
    plan_reset(); // Clear block buffer and planner variables
    st_reset(); // Clear stepper subsystem variables.

    // Sync cleared gcode and planner positions to current system position.
    plan_sync_position();
    gc_sync_position();

    // Print welcome message. Indicates an initialization has occured at power-up or with a reset.
    report_init_message();

    // Start Grbl main loop. Processes program inputs and executes them.
    protocol_main_loop();

  }
  return 0;   /* Never reached */
}
示例#6
0
int
main(int argc, char *argv[])
{
	int s, rtsock, maxfd, ch;
	int once = 0;
	struct timeval *timeout;
	struct fd_set fdset;
	char *argv0;
	const char *opts;

	/*
	 * Initialization
	 */
	argv0 = argv[0];

	/* get option */
	if (argv0 && argv0[strlen(argv0) - 1] != 'd') {
		fflag = 1;
		once = 1;
		opts = "adD";
	} else
		opts = "adDfm1";

	while ((ch = getopt(argc, argv, opts)) != -1) {
		switch (ch) {
		case 'a':
			aflag = 1;
			break;
		case 'd':
			dflag = 1;
			break;
		case 'D':
			dflag = 2;
			break;
		case 'f':
			fflag = 1;
			break;
		case 'm':
			mobile_node = 1;
			break;
		case '1':
			once = 1;
			break;
		default:
			usage(argv0);
			/*NOTREACHED*/
		}
	}
	argc -= optind;
	argv += optind;

	if (aflag) {
		int i;

		if (argc != 0) {
			usage(argv0);
			/*NOTREACHED*/
		}

		argv = autoifprobe();
		if (!argv) {
			errx(1, "could not autoprobe interface");
			/*NOTREACHED*/
		}

		for (i = 0; argv[i]; i++)
			;
		argc = i;
	}
	if (argc == 0) {
		usage(argv0);
		/*NOTREACHED*/
	}

	/* set log level */
	if (dflag == 0)
		log_upto = LOG_NOTICE;
	if (!fflag) {
		char *ident;
		ident = strrchr(argv0, '/');
		if (!ident)
			ident = argv0;
		else
			ident++;
		openlog(ident, LOG_NDELAY|LOG_PID, LOG_DAEMON);
		if (log_upto >= 0)
			setlogmask(LOG_UPTO(log_upto));
	}

#ifndef HAVE_ARC4RANDOM
	/* random value initilization */
	srandom((u_long)time(NULL));
#endif

	/* warn if accept_rtadv is down */
	if (!getinet6sysctl(IPV6CTL_ACCEPT_RTADV))
		warnx("kernel is configured not to accept RAs");
	/* warn if forwarding is up */
	if (getinet6sysctl(IPV6CTL_FORWARDING))
		warnx("kernel is configured as a router, not a host");

	/* initialization to dump internal status to a file */
	if (signal(SIGUSR1, rtsold_set_dump_file) == SIG_ERR) {
		errx(1, "failed to set signal for dump status");
		/*NOTREACHED*/
	}

	/*
	 * Open a socket for sending RS and receiving RA.
	 * This should be done before calling ifinit(), since the function
	 * uses the socket.
	 */
	if ((s = sockopen()) < 0) {
		errx(1, "failed to open a socket");
		/*NOTREACHED*/
	}
	maxfd = s;
	if ((rtsock = rtsock_open()) < 0) {
		errx(1, "failed to open a socket");
		/*NOTREACHED*/
	}
	if (rtsock > maxfd)
		maxfd = rtsock;

	/* configuration per interface */
	if (ifinit()) {
		errx(1, "failed to initilizatoin interfaces");
		/*NOTREACHED*/
	}
	while (argc--) {
		if (ifconfig(*argv)) {
			errx(1, "failed to initialize %s", *argv);
			/*NOTREACHED*/
		}
		argv++;
	}

	/* setup for probing default routers */
	if (probe_init()) {
		errx(1, "failed to setup for probing routers");
		/*NOTREACHED*/
	}

	if (!fflag)
		daemon(0, 0);		/* act as a daemon */

	/* dump the current pid */
	if (!once) {
		pid_t pid = getpid();
		FILE *fp;

		if ((fp = fopen(pidfilename, "w")) == NULL)
			warnmsg(LOG_ERR, __func__,
				"failed to open a log file(%s): %s",
				pidfilename, strerror(errno));
		else {
			fprintf(fp, "%d\n", pid);
			fclose(fp);
		}
	}

	FD_ZERO(&fdset);
	FD_SET(s, &fdset);
	FD_SET(rtsock, &fdset);
	while (1) {		/* main loop */
		int e;
		struct fd_set select_fd = fdset;

		if (do_dump) {	/* SIGUSR1 */
			do_dump = 0;
			rtsold_dump_file(dumpfilename);
		}
			
		timeout = rtsol_check_timer();

		if (once) {
			struct ifinfo *ifi;

			/* if we have no timeout, we are done (or failed) */
			if (timeout == NULL)
				break;

			/* if all interfaces have got RA packet, we are done */
			for (ifi = iflist; ifi; ifi = ifi->next) {
				if (ifi->state != IFS_DOWN && ifi->racnt == 0)
					break;
			}
			if (ifi == NULL)
				break;
		}
		e = select(maxfd + 1, &select_fd, NULL, NULL, timeout);
		if (e < 1) {
			if (e < 0 && errno != EINTR) {
				warnmsg(LOG_ERR, __func__, "select: %s",
				       strerror(errno));
			}
			continue;
		}

		/* packet reception */
		if (FD_ISSET(rtsock, &select_fd))
			rtsock_input(rtsock);
		if (FD_ISSET(s, &select_fd))
			rtsol_input(s);
	}
	/* NOTREACHED */

	return 0;
}