Exemplo n.º 1
0
//------------------------------------------------------------------------------
int __attribute__((OS_main)) main(void)
{
	setupA();
	setupB();
	setupVariables();

	dnaUsbInit();

	INSTALL_MORLOCK_DEFAULTS;
	saveEEPROMConstants();

	loadEEPROMConstants();

	// 12000000 / (64 * 94) = 1994.680 interrupts per second
	// accomplished by using timer0 in waveform generation mode 2
	TCCR0B = 1<<CS01 | 1<<CS00; // set 8-bit timer prescaler to div/64
	OCR0A = 94;
	TCCR0A = 1<<WGM01; 
	TIMSK0 = 1<<OCIE0A; // mode 2, reset counter when it reaches OCROA

	TCCR1B = 1<<WGM12 | 1<<CS12;// CTC for OCR1A, clock select

	// set up A2D
	ADMUX = 1<<MUX0; // A1, Vcc ref
	ADCSRA = 1<<ADEN | 1<<ADPS2; // enable A2D, x16
	DIDR0 = ADC1D; // disable all digital function on A1
	ADCSRB = 1<<ADLAR; // knock off lower two bits, implementation is not that accurate

	PRR = 1<<PRUSI; // not using USI (yet)
	
	rnaInit();

	sei();


	_delay_ms(2); // let state settle, and make sure housekeeping ISR runs

	if( !triggerState && !consts.locked )
	{
		timesToBlinkLight = 1;
		inProgramMode = true;
	}

	for(;;)
	{
		PRR |= 1<<PRTIM1; // power down timer, don't waste power

		// spin until a fire condition is triggered from the ISR
		while( !startFireCycle )
		{
			sampleEye = false;

			if ( rnaRequestsConfigData )
			{
				usbRNAPacket[0] = RNATypeSetConfigData;
				for( unsigned char c=0; c<sizeof(consts); c++ )
				{
					usbRNAPacket[c+1] = ((unsigned char *)&consts)[c];
				}
				rnaSend( rnaRequestsConfigData, usbRNAPacket, sizeof(consts) + 1 );
				rnaRequestsConfigData = 0;
			}

			if ( !millisecondCountBox && consts.eyeEnabled )
			{
				millisecondCountBox--;
				digitizeEye();
				isLedOn = eyeBlocked ? true : false;
			}

			if ( eepromConstantsDirty )
			{
				isLedOn = true;
				setLedOn();
				eepromConstantsDirty = false;
				saveEEPROMConstants();
				isLedOn = false;
			}

			if ( rnaPacketAvail )
			{
				isLedOn = true;
				setLedOn();
				rnaPacketAvail = false;
				_delay_ms( 50 );
				rnaSend( usbRNATo, usbRNAPacket, usbRNAPacketExpected );
				isLedOn = false;
			}
		}

		sampleEye = true;

		// set up the timer
		PRR &= ~(1<<PRTIM1); // timer back on
		TCNT1 = 0; // reset the timer
		TIFR1 |= 1<<OCF1A; // reset compare match

		if ( shotsInString < consts.rampEnableCount )
		{
			shotsInString++;
		}

		// setup complete, cycle the marker
		if ( consts.singleSolenoid )
		{
			cycleSingleSolenoid();
		}
		else
		{
			cycleDoubleSolenoid();
		}

		// bursting? if so count it down
		if ( burstCount && --burstCount )
		{
			startFireCycle = true;
		}
		else if ( currentFireMode != ceFullAuto )
		{
			startFireCycle = false;
		}

		isLedOn = false;

		// make sure at least this much time elapses at the end of a fire cycle
		millisecondCountBox = consts.shortCyclePreventionInterval;
		while( millisecondCountBox );

		while( !(TIFR1 & 1<<OCF1A) ); // wait for end of fire cycle

		if ( currentFireMode == ceRamp && startFireCycle && (consts.rampTopMode != ceSemi) ) // officially hit top rate, blow guts out
		{
			// a shot was scheduled at the maximum rate it could be, go
			// ahead and shift up to whatever top mode the user wanted
			currentFireMode = consts.rampTopMode;
			scheduleShotBox = 0;
			scheduleShotRate = 0;
		}
	}
}
Exemplo n.º 2
0
int main(int argc, char **argv) {
    int i, j, id, np, processor_name_len;
    int maxit, idleft, idright, flag, iter;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int dimensions, N, l_N, n, l_n;
    double *l_r, *l_u, *l_p, *l_q;
    double *x, *y, *gl, *gr;
    double h, tol, relres;

    MPI_Init(&argc, &argv);

    /* Check processes: */
    MPI_Comm_size(MPI_COMM_WORLD, &np);
    MPI_Comm_rank(MPI_COMM_WORLD, &id);
    MPI_Get_processor_name(processor_name, &processor_name_len);

    /* process command-line inputs: */
    if (argc != 3)
    {
        if (id == 0)
        {
            printf("Usage: \"./poisson N dim\" \n");
        }
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

    N = atoi(argv[1]);
    dimensions = atoi(argv[2]);

    if (dimensions == 2) {
        n = N*N;
    }
    else if (dimensions == 3) {
        n = N*N*N;
    }
    else {
        printf("Error: dimensions must equal 2 or 3");
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

    /* number of processes np must divide n: */
    if ((n % np) != 0)
    {
        if (id == 0)
        {
            printf("Error: np must divide n!\n");
            printf("  n = %d, np = %d, n%%np = %d\n", n, np, (n%np));
        }
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

    /* calculate size of local blocks: */
    l_N = N / np;
    l_n = n / np;

    if (id == 0)
    {
        printf("n = %d, np = %d, l_n = %d\n", n, np, l_n);
        printf("\n");
        fflush(stdout);
    }
    /*
    vectors l_u, l_r, l_p, l_q should be of
    length l_n and gl and gr should be of length l_n / l_N

    */
    x = allocate_double_vector(N);          /* x is a N length vector*/
    y = allocate_double_vector(N);          /* y is a N length vector*/
    gl = allocate_double_vector(l_n / l_N); 
    gr = allocate_double_vector(l_n / l_N);
    
    l_u = allocate_double_vector(l_n);                          
    l_r = allocate_double_vector(l_n);
    l_p = allocate_double_vector(l_n);
    l_q = allocate_double_vector(l_n);

    double start_time, end_time;

    /*Beginning of cg method: follows matlab code*/
    h = 1.0 / (N + 1.0);
    for (i = 1; i <= N; i++) {
        x[i - 1] = i*h;
        y[i - 1] = i*h;
    }

    /*Setup B*/
    setupB(l_r, x, y, l_N, N, h, id);

    tol = 1.0e-6;
    maxit = 99999;

    if (id>0) {
        idleft = id - 1;
    }
    else {
        idleft = MPI_PROC_NULL;
    }

    if (id<np - 1) {
        idright = id + 1;
    }
    else {
        idright = MPI_PROC_NULL;
    }

    MPI_Barrier(MPI_COMM_WORLD);
    start_time = MPI_Wtime();
    

    cg(l_u, &flag, &relres, &iter,  /*output*/
        l_r, tol, maxit,            /*input*/
        l_p, l_q, l_n, l_N, N, id, idleft, idright, np, MPI_COMM_WORLD, gl, gr);

    MPI_Barrier(MPI_COMM_WORLD);
    end_time = MPI_Wtime();

    double l_diff_norm = fd_norm(l_u, x, y, l_N, N, h, id);
    double diff_norm;
    MPI_Reduce(&l_diff_norm, &diff_norm, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
    
    /*
    double *full;
    if (id == 0) full = allocate_double_vector(n);
    MPI_Gather(l_u, l_n, MPI_DOUBLE, full, l_n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
    if (id == 0) {
        for (int qrx = 0; qrx < n; qrx++)
            printf("u[%i]=%f\n", qrx, full[qrx]);
        free_vector(full);
    }
    */

    if (id == 0) {
        printf("%15s %15s %22s %15s %22s\n", "N", "DOF", "relres", "iter", "time");
        printf("%15d %15.f %22.16e %15d %22.16e\n", N, (double)N*N, relres, iter, (end_time - start_time));
        printf("||u-u_h||=%22.16e\n", diff_norm);
        /*
        printf("N:            %d\n", N);
        printf("DOF:          %d\n", N*(double)N);
        printf("relres:       %22.16e\n", relres);
        printf("iter:         %d\n", iter);
        printf("elapsed time: %22.16e\n", (end_time - start_time));
        */
    }


    free_vector(x);
    free_vector(y);
    free_vector(l_r);
    free_vector(gl);
    free_vector(gr);
    free_vector(l_u);
    free_vector(l_p);
    free_vector(l_q);
    MPI_Finalize();

    return 0;
}