Exemplo n.º 1
0
void init()
{
    bSmartDiagnostics = true; //true to enable smart diagnostic screen
    bCompetitionMode = true; //true to enable competition mode

    displaySplash("Mecanum Bot", "", true);

    bool ok = false;
    while(!ok)
    {
        const int testcount = 2;
	    bool test[testcount] = {
	        errorcheck(1,0,1,MOTORCON),
	        errorcheck(1,0,2,MOTORCON)};
	    string desc[testcount] = {"MC1-1","MC1-2"};
	    ok = error_display(test,desc,testcount);
	    if (!ok) {
	        PlayTone(440, 50);
	        if (test[0] == false && test[1] == false){
	            nxtDisplayCenteredTextLine(7, "Reboot MC!");
	        }
	    }
	    else { ClearSounds(); }
    }

    eraseDisplay();
    gyro_init(HTGYRO);
    wait1Msec(50);
    nxtbarOn();
    return;
}
Exemplo n.º 2
0
int main()
{
    int n, ip[NMAXSQRT + 2];
    double a[NMAX + 1], w[NMAX * 5 / 4], t[NMAX / 2 + 1], err;

    printf("data length n=? (must be 2^m)\n");
    scanf("%d", &n);
    ip[0] = 0;

    /* check of CDFT */
    putdata(0, n - 1, a);
    cdft(n, 1, a, ip, w);
    cdft(n, -1, a, ip, w);
    err = errorcheck(0, n - 1, 2.0 / n, a);
    printf("cdft err= %g \n", err);

    /* check of RDFT */
    putdata(0, n - 1, a);
    rdft(n, 1, a, ip, w);
    rdft(n, -1, a, ip, w);
    err = errorcheck(0, n - 1, 2.0 / n, a);
    printf("rdft err= %g \n", err);

    /* check of DDCT */
    putdata(0, n - 1, a);
    ddct(n, 1, a, ip, w);
    ddct(n, -1, a, ip, w);
    a[0] *= 0.5;
    err = errorcheck(0, n - 1, 2.0 / n, a);
    printf("ddct err= %g \n", err);

    /* check of DDST */
    putdata(0, n - 1, a);
    ddst(n, 1, a, ip, w);
    ddst(n, -1, a, ip, w);
    a[0] *= 0.5;
    err = errorcheck(0, n - 1, 2.0 / n, a);
    printf("ddst err= %g \n", err);

    /* check of DFCT */
    putdata(0, n, a);
    a[0] *= 0.5;
    a[n] *= 0.5;
    dfct(n, a, t, ip, w);
    a[0] *= 0.5;
    a[n] *= 0.5;
    dfct(n, a, t, ip, w);
    err = errorcheck(0, n, 2.0 / n, a);
    printf("dfct err= %g \n", err);

    /* check of DFST */
    putdata(1, n - 1, a);
    dfst(n, a, t, ip, w);
    dfst(n, a, t, ip, w);
    err = errorcheck(1, n - 1, 2.0 / n, a);
    printf("dfst err= %g \n", err);

    return 0;
}
Exemplo n.º 3
0
double czasWykonaniaSortowania(int tab[], int iloscElementow, int zakresOd, int zakresDo, int Niter)
{
	int* tabout = (int*)malloc(sizeof(int) * iloscElementow);
	errorcheck(tabout == NULL, "Brak pamieci!");

	double time = clock();
	for (int i = 0; i < Niter; i++)
	{
		CountSort(tab, tabout, iloscElementow, zakresOd, zakresDo);
	}
	time = (clock() - time) / CLOCKS_PER_SEC;

	free(tabout);
	return time;
}
Exemplo n.º 4
0
//implementacja sortowania przez zliczanie zgodna z wyk³adem
void wykladCountSort(int A[], int B[], int iloscElementow, int from, int to)
{
	int rozpietoscElementow = to - from + 1;
	int* C = (int*)calloc(rozpietoscElementow, sizeof(int));
	errorcheck(C == NULL, "Brak pamieci!");

	for (int i = 0; i < iloscElementow; i++)
		C[A[i] - from]++;

	for (int i = 1; i < rozpietoscElementow; i++)
		C[i] += C[i - 1];

	for (int i = iloscElementow - 1; i >= 0; i--)
	{
		int eidx = A[i] - from;
		B[--C[eidx]] = eidx;
	}
	free(C);
}
Exemplo n.º 5
0
//moja implementacja sortowania przez zliczanie
void myCountSort(int A[], int B[], int iloscElementow, int from, int to)
{
	int rozpietoscElementow = to - from + 1;
	int* C = (int*)calloc(rozpietoscElementow, sizeof(int));
	errorcheck(C == NULL, "Brak pamieci!");

	for (int i = 0; i < iloscElementow; i++)
		C[A[i] - from]++;

	int idx = 0;
	for (int i = 0; i < rozpietoscElementow; i++)
	{
		for (int j = 0; j < C[i]; j++)
		{
			B[idx++] = from + i;
		}
	}
	free(C);
}
Exemplo n.º 6
0
/* governing function, calls others */
int process_date(char *s,struct tm *thetime,int usesymm)
{
	int i,j,k;
	int month;

	k = parse_for_year(s,thetime);
	if (k == 0) {
		thetime->tm_mon = 0;
		thetime->tm_mday = 1;
	}
	month = parse_for_alpha_month(s,thetime);
	if (month == -1)
		month = parse_for_hyphen_month(s,thetime);
	if (month == -1)
		month = parse_for_slash_month(s,thetime);
	if (month < 0)
		i = parse_for_weekday(s,thetime);
	j = parse_sexa_time(s,thetime);
	if (j == -1)
		j = parse_tgm_time(s,thetime);
	if (month == -1 && i == -1 && j == -1) {
		thetime->tm_mon = 0;
		thetime->tm_mday = 1;
	} else if (month >= 0) {
		thetime->tm_mon = month;
	}
	if ((usesymm == NEITHER) || (usesymm == OUT)) {
		thetime->tm_wday = dayofweek(thetime->tm_year+1900,
			thetime->tm_mon+1,thetime->tm_mday);
		thetime->tm_yday = ydays_from_date(thetime);
	}
	if ((usesymm == IN) || (usesymm == BOTH)) {
	} else
		errorcheck(s,thetime);
	return 0;
}
Exemplo n.º 7
0
void mexFunction(
  int nlhs, mxArray *plhs[],
  int nrhs, const mxArray *prhs[]
) {
	int imfr, idev;
	char *serno_str = NULL;
	int ialt;
	int errlev = 0;

	if((nlhs<1)||(nlhs>2)) mexErrMsgTxt("Need 1 or 2 outputs");

	/* Process the Manufacturer code. */
	if(nrhs>=1) imfr = mex_to_16b(prhs[0]) ;
	else        imfr = -1; /* value not specified. */
	errorcheck(imfr,"MFRcode");

	/* Process the Device code. */
	if(nrhs>=2) idev = mex_to_16b(prhs[1]) ;
	else        idev = -1; /* value not specified. */
	errorcheck(idev,"DeviceID");

	/* Process the serial number string. */
	if(nrhs>=3) {
		const int *dims;
		if(!mxIsChar(prhs[2])) {
			mexErrMsgIdAndTxt("USB:open","Serial number must be a string\n");
		}
		if(mxGetNumberOfDimensions(prhs[2])!=2) {
			mexErrMsgIdAndTxt("USB:open","Serial number must be a string vector\n");
		}
		dims = mxGetDimensions(prhs[2]);
		if(dims[0]==0) {
			serno_str = NULL;
		} else if(dims[0]==1) {
			size_t buflen;
			buflen = (dims[0]*dims[1])*sizeof(mxChar) + 1;
			serno_str = mxCalloc(buflen,sizeof(char));
			if(serno_str==NULL) {
				mexErrMsgIdAndTxt("USB:open","MxCalloc problem for serial number.\n");
			} else {
				mxGetString(prhs[2],serno_str,buflen);
			}
		} else {
			mexErrMsgIdAndTxt("USB:open","Serial number must be a single string\n");
		}
	} else {
		serno_str = NULL;
	}

	/* Process the altif number. */
	if(nrhs>=4) ialt = mex_to_16b(prhs[3]) ;
	else        ialt = -1; /* value not specified. */

	/* Process the error level. */
	if(nrhs>=5) errlev = mex_to_16b(prhs[4]) ;
	else        errlev = -1; /* value not specified. */

	if(errlev == -1) errlev = 0; /* No value specified becomes 0 in this case. */

	// mexPrintf("Seeking mfr 0x%x dev 0x%x serial \"%s\" config 0x%x errlev 0x%x\n",
	// 	imfr,idev,serno_str,ialt,errlev);
	
	if(errlev>= 0xffff) return;  /* Debugging option. */

	if( (imfr == -1) && (idev == -1) ) {
		mexErrMsgIdAndTxt("USB:open","Must specify at least 1 of mfr and device.\n");
	}

	/**********************************************************
	 * Done processing the args. Find the specified device.  */

	int rtn;
	char errorstring[200];
	errorstring[0]='\0';
	rtn = initialize(errlev);
	// mexPrintf("initialize returned %d\n",rtn);
	if(rtn < 0) {
		snprintf(errorstring,200,"Trouble accessing USB bus.");
	}

	struct usb_device *thedev;
	int numdev;
	thedev = findvps(imfr, idev,serno_str,&numdev) ;
	// mexPrintf("findvps numdev %d thedev %d\n",numdev,thedev);
	if(numdev==0) {
		snprintf(errorstring,200,"USB device 0x%x 0x%x %s not found.",
			imfr,idev,serno_str);
	} else if(numdev > 1) {
		mexWarnMsgTxt("Multiple devices found. Using the first one.\n");
	}

	UINT64_T inthandle = 0;
	if(errorstring[0]=='\0') {
		static struct usb_dev_handle *devhandle, *dummyhandle;
		devhandle = openinterface(errorstring,200,thedev, ialt) ;
		// mexPrintf("Handle: %d 0x%x\n",devhandle,devhandle);
		if(errorstring[0]=='\0') {
			/* Return the handle cast to a 64-bit integer value.     **
			** This is nonportable, but should work on most systems. */
			/* With GCC on 32-bit system, expect:                         **
			** warning: cast to/from pointer to integer of different size */
			inthandle = (UINT64_T)devhandle;
			dummyhandle = (struct usb_dev_handle *)inthandle;
			/* Check. */
			if(dummyhandle != devhandle) {
				mexErrMsgIdAndTxt("USB:open",
					"Internal error: bad usb device handle.\n");
				/* If this error happens, the devhandle pointer
				 * isn't being represented correctly as a 64-bit
				 * integer. Need to look at the internals and change
				 * to something else. */
			}
		}
	}

	// mexPrintf("Done. errorstring \"%s\"\n",errorstring);
	if(errorstring[0]!='\0') {
		int ndims = 2;
		int dims[2] = {0,0};
		/* Return the handle. */
		plhs[0] = mxCreateNumericArray(ndims,dims,mxUINT64_CLASS,mxREAL);
		if(plhs[0]==NULL) {
			mexErrMsgIdAndTxt("USB:open","Internal error: Can't create numeric array.\n");
		}
		/* return the error string. */
		if(nlhs>1) {
			plhs[1] = mxCreateString(errorstring);
			if(plhs[1]==NULL) {
				mexErrMsgIdAndTxt("USB:open","Internal error: Can't create string.\n");
			}
		} else {
			mexErrMsgIdAndTxt("USB:open","%s\n",errorstring);
		}
		
	} else {
		int ndims = 2;
		int dims[2] = {1,1};
		UINT64_T *val;
		/* Return the handle. */
		plhs[0] = mxCreateNumericArray(ndims,dims,mxUINT64_CLASS,mxREAL);
		if(plhs[0]==NULL) {
			mexErrMsgIdAndTxt("USB:open","Internal error: Can't create numeric array.\n");
		}
		val = mxGetData(plhs[0]);
		*val = inthandle;
		/* return a zero-length error string. */
		if(nlhs>1) {
			plhs[1] = mxCreateString("");
			if(plhs[1]==NULL) {
				mexErrMsgIdAndTxt("USB:open","Internal error: Can't create string.\n");
			}
		}
	}

	if(serno_str!=NULL) mxFree(serno_str);
}
Exemplo n.º 8
0
// 
// read parameters from input file.
// 
void read_parameters( std::ifstream &in )
{
	const int unset = -1;
	int op;
	char  s[100] = "";
	char  s2[100] = "";

	while( strcmp( s, "BEGIN" ) != 0 ) 
	{
		in.getline( s, 100 );
		std::cout << s << std::endl;
	}

	in >> s >> parameter::lchrom;
	std::cout << s << "   " << parameter::lchrom << std::endl; 
	errorcheck("Check lchrom.", parameter::lchrom > 0 );

	in >> s >> parameter::seed;
	std::cout << s << "   "  << parameter::seed << std::endl;
	errorcheck("seed must be in 0..1", parameter::seed > 0 && parameter::seed < 1 );

	in >> s >> parameter::popsize;
	std::cout << s << "   "  << parameter::popsize << std::endl;
	errorcheck("population size must be even.", parameter::popsize % 2 == 0 );
	errorcheck("population size must be positive.", parameter::popsize > 0 );

	in >> s >> parameter::pcross;
	std::cout << s << "   "  << parameter::pcross << std::endl;
	errorcheck("pcross must be in 0..1", parameter::pcross >= 0 && parameter::pcross <= 1 );

	in >> s >> parameter::tournament_size;
	std::cout << s << "   "  << parameter::tournament_size << std::endl;

	in >> s >> s2; parameter::learn_MPM        = (strcmp(s2,"on") == 0);
	std::cout << s << "   "  << s2 << std::endl;

	in >> s >> s2; op = find_stop_criteria( s2 );
	std::cout << s << "   "  << s2 << std::endl;
	parameter::stop_criteria = op;
	errorcheck("stop criteria not available.", op != -1 );

	in >> s >> parameter::stop_criteria_arg;
	std::cout << s << "   "  << parameter::stop_criteria_arg << std::endl;

	in.getline( s, 100 );

	in.getline( s, 100 );
	in.getline( s, 100 );
	in.getline( s, 100 );

	in >> s >> s2;
	parameter::report_pop = (strcmp(s2,"on") == 0); 
	std::cout << s << "   "  << s2 << std::endl;
	in >> s >> s2;
	parameter::report_string = (strcmp(s2,"on") == 0);
	std::cout << s << "   "  << s2 << std::endl;
	in >> s >> s2; 
	parameter::report_fitness = (strcmp(s2,"on") == 0);
	std::cout << s << "   "  << s2 << std::endl;
	in >> s >> s2;
	parameter::report_MPM = (strcmp(s2,"on") == 0);
	std::cout << s << "   "  << s2 << std::endl;

	std::cout << "--------------------------------------------------------------------" << std::endl;


} // end of read_parameters
Exemplo n.º 9
0
int main( int argc, char *argv[] )
{
	// process command line
	if( argc != 3 )   
	{
		std::cout << "Usage: " << argv[0] << " inputfile outputfile" << std::endl;
		std::cout << "       Please read the README file." << std::endl;
		exit(1);
	}

	// read parameters from input file
	std::ifstream infile( argv[1] ); 
	read_parameters( infile );
	if(parameter::objfunc == TRAP) 
	{
		errorcheck("TRAP_K doesn't divide lchrom" , parameter::lchrom %TRAP_K == 0); 
		infile.close();
	}
	// open output file
	std::ofstream outfile( argv[2] );  

	// initilalize random number generator
	RANDOM.randomize( parameter::seed );

	// run the ECGA algorithm


	//ecga ECGA;
	//ECGA.run( outfile );


	int lower = 0;
	int higher = 30000;
	int mid = 200;
	char *best  = new char[parameter::lchrom];
	for(int i = 0 ; i < parameter::lchrom ; i++)
	{
		best[i] == '1';
	}

	while(1)
	{
		int i;
		parameter::popsize = mid;
		printf("[%d]",parameter::popsize);
		for( i = 0 ; i < 10 ; i++)
		{
			parameter::found_optima = false;
			ecga ECGA;
			ECGA.run(outfile);
			if(ECGA.best == best)
			{
				printf("+");
				continue;
			}
			else 
			{
				printf("-");
				break;
			}
		}
		std::cout<<std::endl;
		if(i==10)
		{
			higher = mid;
			mid = (mid+lower)/2;
		}
		else 
		{
			lower = mid ;
			mid = (mid+higher)/2; 
		}
		if(lower *1.05 >= higher)
			break;
		mid = mid / 2 * 2 ;
	}
	delete[] best;
	FILE *Result = fopen("bisectionresult","w");
	fprintf(Result , "%d\n" , mid);
	fclose(Result);

	std::cout << "bisection result : " << higher <<std::endl;
	std::cout << "ECGA done" << std::endl;

	outfile.close();
	return 1;
}
Exemplo n.º 10
0
static void
glob_dir(glob_t* gp, globlist_t* ap)
{
	register char*		rescan;
	register char*		prefix;
	register char*		pat;
	register char*		name;
	register int		c;
	char*			dirname;
	void*			dirf;
	char			first;
	regex_t*		ire;
	regex_t*		pre;
	regex_t			rec;
	regex_t			rei;
	int			notdir;
	int			t1;
	int			t2;
	int			bracket;

	int			anymeta = ap->gl_flags & MATCH_META;
	int			complete = 0;
	int			err = 0;
	int			meta = ((gp->re_flags & REG_ICASE) && *ap->gl_begin != '/') ? MATCH_META : 0;
	int			quote = 0;
	int			savequote = 0;
	char*			restore1 = 0;
	char*			restore2 = 0;
	regex_t*		prec = 0;
	regex_t*		prei = 0;
	char*			matchdir = 0;
	int			starstar = 0;

	if (*gp->gl_intr)
	{
		gp->gl_error = GLOB_INTR;
		return;
	}
	pat = rescan = ap->gl_begin;
	prefix = dirname = ap->gl_path + gp->gl_extra;
	first = (rescan == prefix);
again:
	bracket = 0;
	for (;;)
	{
		switch (c = *rescan++)
		{
		case 0:
			if (meta)
			{
				rescan = 0;
				break;
			}
			if (quote)
			{
				trim(ap->gl_begin, rescan, &t1, NiL, NiL);
				rescan -= t1;
			}
			if (!first && !*rescan && *(rescan - 2) == gp->gl_delim)
			{
				*(rescan - 2) = 0;
				c = (*gp->gl_type)(gp, prefix, 0);
				*(rescan - 2) = gp->gl_delim;
				if (c == GLOB_DIR)
					addmatch(gp, NiL, prefix, NiL, rescan - 1, anymeta);
			}
			else if ((anymeta || !(gp->gl_flags & GLOB_NOCHECK)) && (*gp->gl_type)(gp, prefix, 0))
				addmatch(gp, NiL, prefix, NiL, NiL, anymeta);
			return;
		case '[':
			if (!bracket)
			{
				bracket = MATCH_META;
				if (*rescan == '!' || *rescan == '^')
					rescan++;
				if (*rescan == ']')
					rescan++;
			}
			continue;
		case ']':
			meta |= bracket;
			continue;
		case '(':
			if (!(gp->gl_flags & GLOB_AUGMENTED))
				continue;
		case '*':
		case '?':
			meta = MATCH_META;
			continue;
		case '\\':
			if (!(gp->gl_flags & GLOB_NOESCAPE))
			{
				quote = 1;
				if (*rescan)
					rescan++;
			}
			continue;
		default:
			if (c == gp->gl_delim)
			{
				if (meta)
					break;
				pat = rescan;
				bracket = 0;
				savequote = quote;
			}
			continue;
		}
		break;
	}
	anymeta |= meta;
	if (matchdir)
		goto skip;
	if (pat == prefix)
	{
		prefix = 0;
		if (!rescan && (gp->gl_flags & GLOB_COMPLETE))
		{
			complete = 1;
			dirname = 0;
		}
		else
			dirname = ".";
	}
	else
	{
		if (pat == prefix + 1)
			dirname = "/";
		if (savequote)
		{
			quote = 0;
			trim(ap->gl_begin, pat, &t1, rescan, &t2);
			pat -= t1;
			if (rescan)
				rescan -= t2;
		}
		*(restore1 = pat - 1) = 0;
	}
	if (!complete && (gp->gl_flags & GLOB_STARSTAR))
		while (pat[0] == '*' && pat[1] == '*' && (pat[2] == '/'  || pat[2]==0))
		{
			matchdir = pat;
			if (pat[2])
			{
				pat += 3;
				while (*pat=='/') pat++;
				if (*pat)
					continue;
			}
			rescan = *pat?0:pat;
			pat = "*";
			goto skip;
		}
	if (matchdir)
	{
		rescan = pat;
		goto again;
	}
skip:
	if (rescan)
		*(restore2 = rescan - 1) = 0;
	if (rescan && !complete && (gp->gl_flags & GLOB_STARSTAR))
	{
		register char *p = rescan;
		while (p[0] == '*' && p[1] == '*' && (p[2] == '/'  || p[2]==0))
		{
			rescan = p;
			if (starstar = (p[2]==0))
				break;
			p += 3;
			while (*p=='/')
				p++;
			if (*p==0)
			{
				starstar = 2;
				break;
			}
		}
	}
	if (matchdir)
		gp->gl_starstar++;
	if (gp->gl_opt)
		pat = strcpy(gp->gl_opt, pat);
	for (;;)
	{
		if (complete)
		{
			if (!(dirname = (*gp->gl_nextdir)(gp, dirname)))
				break;
			prefix = streq(dirname, ".") ? (char*)0 : dirname;
		}
		if ((!starstar && !gp->gl_starstar || (*gp->gl_type)(gp, dirname, GLOB_STARSTAR) == GLOB_DIR) && (dirf = (*gp->gl_diropen)(gp, dirname)))
		{
			if (!(gp->re_flags & REG_ICASE) && ((*gp->gl_attr)(gp, dirname, 0) & GLOB_ICASE))
			{
				if (!prei)
				{
					if (err = regcomp(&rei, pat, gp->re_flags|REG_ICASE))
						break;
					prei = &rei;
				}
				pre = prei;
				if (gp->gl_ignore)
				{
					if (!gp->gl_ignorei)
					{
						if (regcomp(&gp->re_ignorei, gp->gl_fignore, gp->re_flags|REG_ICASE))
						{
							gp->gl_error = GLOB_APPERR;
							break;
						}
						gp->gl_ignorei = &gp->re_ignorei;
					}
					ire = gp->gl_ignorei;
				}
				else
					ire = 0;
			}
			else
			{
				if (!prec)
				{
					if (err = regcomp(&rec, pat, gp->re_flags))
						break;
					prec = &rec;
				}
				pre = prec;
				ire = gp->gl_ignore;
			}
			if (restore2)
				*restore2 = gp->gl_delim;
			while ((name = (*gp->gl_dirnext)(gp, dirf)) && !*gp->gl_intr)
			{
				if (notdir = (gp->gl_status & GLOB_NOTDIR))
					gp->gl_status &= ~GLOB_NOTDIR;
				if (ire && !regexec(ire, name, 0, NiL, 0))
					continue;
				if (matchdir && (name[0] != '.' || name[1] && (name[1] != '.' || name[2])) && !notdir)
					addmatch(gp, prefix, name, matchdir, NiL, anymeta);
				if (!regexec(pre, name, 0, NiL, 0))
				{
					if (!rescan || !notdir)
						addmatch(gp, prefix, name, rescan, NiL, anymeta);
					if (starstar==1 || (starstar==2 && !notdir))
						addmatch(gp, prefix, name, starstar==2?"":NiL, NiL, anymeta);
				}
				errno = 0;
			}
			(*gp->gl_dirclose)(gp, dirf);
			if (err || errno && !errorcheck(gp, dirname))
				break;
		}
		else if (!complete && !errorcheck(gp, dirname))
			break;
		if (!complete)
			break;
		if (*gp->gl_intr)
		{
			gp->gl_error = GLOB_INTR;
			break;
		}
	}
	if (restore1)
		*restore1 = gp->gl_delim;
	if (restore2)
		*restore2 = gp->gl_delim;
	if (prec)
		regfree(prec);
	if (prei)
		regfree(prei);
	if (err == REG_ESPACE)
		gp->gl_error = GLOB_NOSPACE;
}