int main(int argc, char **argv){

        /* Print error, if rp_Init() function failed */
        if(rp_Init() != RP_OK){
                fprintf(stderr, "Rp api init failed!\n");
        }

        /*LOOB BACK FROM OUTPUT 2 - ONLY FOR TESTING*/
        rp_GenReset();
        rp_GenFreq(RP_CH_1, 20000.0);
        rp_GenAmp(RP_CH_1, 1.0);
        rp_GenWaveform(RP_CH_1, RP_WAVEFORM_SINE);
        rp_GenOutEnable(RP_CH_1);


        uint32_t buff_size = 16384;
        float *buff = (float *)malloc(buff_size * sizeof(float));

        rp_AcqReset();
        rp_AcqSetDecimation(1);
        rp_AcqSetTriggerLevel(0.1); //Trig level is set in Volts while in SCPI 
        rp_AcqSetTriggerDelay(0);

        rp_AcqStart();

        /* After acquisition is started some time delay is needed in order to acquire fresh samples in to buffer*/
        /* Here we have used time delay of one second but you can calculate exact value taking in to account buffer*/
        /*length and smaling rate*/

        sleep(1);
        rp_AcqSetTriggerSrc(RP_TRIG_SRC_CHA_PE);
        rp_acq_trig_state_t state = RP_TRIG_STATE_TRIGGERED;

        while(1){
                rp_AcqGetTriggerState(&state);
                if(state == RP_TRIG_STATE_TRIGGERED){
                sleep(1);
                break;
                }
                
        rp_AcqGetOldestDataV(RP_CH_1, &buff_size, buff);
        int i;
        for(i = 0; i < buff_size; i++){
                printf("%f\n", buff[i]);
        }
        /* Releasing resources */
        free(buff);
        rp_Release();
        return 0;
}

        }
Beispiel #2
0
// Pitaya code in itself
int main(int argc, char **argv){
	clock_t tStart, tTop, tEnd;
	/*time_t     now;
	struct tm  ts;

	char       bufdate[24];
	char       buftime[24];
	time(&now);
	ts = *localtime(&now);
    	strftime(buftime, sizeof(buftime), "%H%M%S", &ts);
	strftime(bufdate, sizeof(bufdate), "%y%m%d", &ts);
	int LTime = atoi(buftime);
	int LDate = atoi(bufdate);
   	printf("Starting at : %i\n", LTime);
	printf("Starting at : %i\n", LDate);
	printf("Starting at : %i\n", LDate+LTime);*/
	FILE * fm;
	fm = fopen ("moy.txt", "w+");

	//int PORT = 7536;
	printf("DEBUG COMMENCE\n");
	/* Initialize UDP */
	struct sockaddr_in si_me, si_other;
	int s;
	int slen = sizeof(si_other);
	//char buf[BUFLEN];
	//create a UDP socket
	if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
	{
	die("socket");
	}

	// zero out the structure
	memset((char *) &si_me, 0, sizeof(si_me));
	si_me.sin_family = AF_INET;
	si_other.sin_family = AF_INET;
	si_me.sin_port = htons(PORT); // Port
	si_other.sin_port = htons(PORT);
	si_other.sin_addr.s_addr = inet_addr("192.168.1.23"); //specific binding to listenser
	//si_me.sin_addr.s_addr = htonl(INADDR_ANY);
	//bind socket to port
	if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1){die("bind");}
	/* End of UDP initialization */




	/*number of acquision*/
	uint32_t N = 8;
	int LineImage = 66;
	/*size of the acquisition buffer*/
	uint32_t buff_size = 16384;
	uint32_t HalfSignal = buff_size/2;
	/*allocation of buffer size in memory*/

	tStart = clock();
	/* Print error, if rp_Init() function failed */
	if(rp_Init() != RP_OK){
		fprintf(stderr, "Rp api init failed!\n");
	}


		

	for (int k=0; k < nbline ; k++){

	float *buff = (float *)malloc(buff_size * sizeof(float));
	int *ibuff = (int *)malloc(buff_size * sizeof(int));

	/*initialise to 0 the buffer*/
	for (int i=0 ; i<buff_size ; i++){buff[i]=0.0;}
		rp_AcqSetDecimation(RP_DEC_64);
		uint32_t buff_sizeW = 12000;
		float *tempW = (float *)malloc(buff_sizeW * sizeof(float));
		float *buffW = (float *)malloc(buff_sizeW * sizeof(float));
	
		rp_acq_trig_state_t stateW = RP_TRIG_STATE_TRIGGERED;
		/*start acquisition must be set before trigger initiation*/
		rp_AcqStart();
		/*allocation of temporary buffer size in memory*/

	
		/*trigger source -- Channel B, Negatif*/ 
		rp_AcqSetTriggerSrc(RP_TRIG_SRC_CHA_PE);
		/*level of the trigger activation in volt*/
		double PWMTriggerLevel = 0.05;
		rp_AcqSetTriggerLevel(PWMTriggerLevel);
		/*waiting for trigger*/
		while(1){
			rp_AcqGetTriggerState(&stateW);
			if(stateW == RP_TRIG_STATE_TRIGGERED){
				break;
			}
		}		
		/*putt acquisition data in the temporary buffer*/
		rp_AcqGetOldestDataV(RP_CH_1, &buff_sizeW, tempW);
		/*additionning the N signals*/
		for (int j = 0; j < buff_sizeW; j++){
			buffW[j]+=tempW[j];
		}
		//printf("%i\n", k);
		/*release memory*/
		free(tempW);
		int PWM_Position = 0;
		for (i=0 ; i<buff_sizeW ; i++) {
			if (buffW[i] > PWMTriggerLevel ){PWM_Position++;}	
			//printf("%f ",buffW[i]);
		}
		PWM_Position = PWM_Position -2000; //lowering the base, staying over max though
		//printf("PWM : %i\n",PWM_Position);




		// On commence alors l'acquisition des échantillons
		/*init trigger state*/
		/* decimation n (=1,8,64...) : frequency = 125/n MHz*/
		rp_AcqSetDecimation(RP_DEC_1);
		rp_acq_trig_state_t state = RP_TRIG_STATE_TRIGGERED;
		tTop = clock();
		for (int i=0 ; i<N ; i++) {

			/*start acquisition must be set before trigger initiation*/
			rp_AcqStart();

			/*allocation of temporary buffer size in memory*/
			float *temp = (float *)malloc(buff_size * sizeof(float));

			/*trigger source -- Channel B, Negatif*/ 
			rp_AcqSetTriggerSrc(RP_TRIG_SRC_CHB_NE);

			/*level of the trigger activation in volt*/
			rp_AcqSetTriggerLevel(0.01); 

			/*acquisition trigger delay*/
			rp_AcqSetTriggerDelay(HalfSignal);
		
			/*waiting for trigger*/
			while(1){
				rp_AcqGetTriggerState(&state);
				if(state == RP_TRIG_STATE_TRIGGERED){
					break;
				}
			}		
			//printf("%i\n",i);
			/*putt acquisition data in the temporary buffer*/
			rp_AcqGetOldestDataV(RP_CH_2, &buff_size, temp);

			/*additionning the N signals*/
			for (int j = 0; j < buff_size; j++){
				buff[j]+=1000*temp[j];
			}

			/*release memory*/
			free(temp);
		}
	
		double MoyenneFichier = 0;
		for (int i=HalfSignal ; i<buff_size ; i++) {
			MoyenneFichier += buff[i];	
		}
		MoyenneFichier = MoyenneFichier / HalfSignal;
		//printf("MoyenneFichier : %f\n",MoyenneFichier);

		float XCarre=0.0;
		float SCarre=0.0;

		for (int i=HalfSignal ; i<buff_size ; i++) {
			XCarre = (buff[i]-MoyenneFichier);	
			SCarre += XCarre*XCarre;
		}
		SCarre = SCarre/HalfSignal;

		//printf("SCarre : %f\n",SCarre);
		/*Nettoyage et simplification du signal */
		double ttmp = 0;
		for (int i=0 ; i<buff_size ; i++) {
			ttmp = 255*(buff[i]- MoyenneFichier)*(SCarre/100.0);
			
			/* Ecretage moche */
			
	
			if(ttmp > (SMAX)){
				ttmp = SMAX;
			}
			if(ttmp < (SMIN)){
				ttmp = SMIN;
			}		
						
			


			/* Fin ecretage */
			ibuff[i] = ttmp;
			//printf("%i\t",ibuff[i]);
			// On ecrit dans le fichier les int
			fprintf(fm, "%i\t", (int)ibuff[i]);
		}
		free(buff);
		fprintf(fm, "\n");

		/* End of cleaning*/

		//Sending the image
		int sampling = 16;
		int samples = 256;
		int PointsNb = buff_size/sampling;

		int *iToBuff = (int *)malloc(PointsNb * sizeof(int));
		double *tmp = (double *)malloc(PointsNb * sizeof(double));


		for (int i = 0 ; i < PointsNb ; ++i) {
			tmp[i] = 0.0;
			for (int j = 0 ; j < sampling ; ++j) {

		   		 tmp[i] += (ibuff[i*sampling+j]*ibuff[i*sampling+j]) ;
			
			}
			iToBuff[i] = (int)(((tmp[i] / sampling)));
			//printf("%i ", iToBuff[i]);
		}
		free(ibuff);



		// Checking for a file 
		



		

		/* sending to UDP */
		//int DATA_Line = 0; // Number of the line in the image (ie 1 to 40 in our case)
		int DATA_Image = 0; //Number of the image in the acq 
		int MaxPointsPerPacket = 256; // (in bytes)
		int NbPackets = PointsNb/MaxPointsPerPacket;
		
		int TaillerBuffer = samples+3+4;
		
		int UDPBuffer[TaillerBuffer];
		/*
		printf("\nTaillerBuffer : %i\n",TaillerBuffer);
		printf("buff number of points : %i\n",buff_size);
		printf("Nb of points of raw signal : %i\n",PointsNb);


		printf("Sampling (sum the squares over XX points) : %i\n",sampling);
		printf("Number of points in the data : %i\n",samples);
		printf("UDP Port used : %i\n",PORT);
		printf("Packet size (in bytes) : %i\n",TaillerBuffer);
		printf("Nb packet : %i\n",NbPackets);
		
*/
		printf("Ligne k : %i\n",k);
		int DATA_Line = LineImage;
		for (int i=0 ; i<TaillerBuffer ; i++){UDPBuffer[i]=0;}
		for (i=0 ; i < NbPackets ; i++) { // iteration sur l'ensemble des packets du buffer

			UDPBuffer[0] = i; // Position of the packet inside the line
			UDPBuffer[1] = DATA_Image; // Number of the line
			UDPBuffer[2] = DATA_Line; //Number of the image
			UDPBuffer[3] = PWM_Position; //BUG @corriger
			UDPBuffer[4] = k; //BUG @corriger à la reception -- je voulais mettre datetimestamp
			tEnd = clock();
			UDPBuffer[5] =  (int)timediff(tStart, tTop); //BUG @corriger -- début de l'acquisition de la ligne
			UDPBuffer[6] = (int)timediff(tStart, tEnd); //BUG @corriger -- fin de l'acquisition de la ligne

			for (j=0; j < samples ; j++){ // iteration par points du packet
				UDPBuffer[7+j] = iToBuff[i*samples+j];
			}

			CheckUDP = sendto(s,UDPBuffer,sizeof(UDPBuffer),0,(struct sockaddr*) &si_other,slen);
	
			usleep(2);
			if (CheckUDP==-1) {die("sendto()");}
		}
	
		/* end of emission */
	free(iToBuff);
	free(tmp);
	usleep(2);
	}
	/* Release rp resources */
	fclose(fm);
	rp_Release();

	return 0;
}
Beispiel #3
0
void *Read_CT_Data(void *arg)
{
    rp_pinState_t heart_beat_state; 
    uint32_t buff_size = RP_BUF_SIZE;
    uint32_t trig_pos;
    float *buff_ch1 = (float *)malloc(buff_size * sizeof(float));
    float *buff_ch2 = (float *)malloc(buff_size * sizeof(float));
    float val_sum_ch1,val_sum_ch2,val_ch1,val_ch2;
    float tmp_val_ch1, tmp_val_ch2;
    float charge_ch1;
    float charge_ch2;  
    int counter;
    clock_t start_time, diff;
    int msec;
    int endpoints;
    int startpoints;
    
    val_sum_ch1=0;
    counter=0;
    msec=0;
    new_data=0;
    
    /* Print error, if rp_Init() function failed */
    if(rp_Init() != RP_OK)
    {
        fprintf(stderr, "Rp api init failed!n");
    }
    
    setup_output();

    //rp_AcqSetDecimation(RP_DEC_8 );
    rp_AcqSetSamplingRate(RP_SMP_125M);
    //rp_AcqSetSamplingRate(RP_SMP_15_625M);
    rp_AcqSetTriggerLevel(trig_level); //Trig level is set in Volts while in SCPI is set in mV
    rp_AcqSetTriggerDelayNs(0);
    //rp_AcqSetTriggerDelay(0);
    rp_AcqSetAveraging(true);
    rp_AcqStart();

    //rp_AcqSetTriggerSrc(RP_TRIG_SRC_EXT_PE);
    //rp_AcqSetTriggerSrc(RP_TRIG_SRC_CHA_NE);			// Trig on signal
    rp_AcqSetTriggerSrc(trig_source);			
    rp_acq_trig_state_t state = RP_TRIG_STATE_TRIGGERED;

    // Get clock
    start_time = clock();

    rp_DpinSetDirection ( RP_LED4, RP_OUT);
    rp_DpinSetDirection ( RP_LED3, RP_OUT);
    rp_DpinSetDirection ( RP_DIO1_P, RP_OUT);
    rp_AcqStart();    
    //Clear count_table
    count_table[0]=0;
    free_counter=0;
    heart_beat_state=RP_LOW;

	while(1)
	{
	    // Wait for trig
	    
	    while(1)
	    {
		diff=clock()-start_time;
		msec=diff*1000/CLOCKS_PER_SEC;
		rp_AcqGetTriggerState(&state);
		if(state == RP_TRIG_STATE_TRIGGERED)
		{
		    usleep(10);
		    triggered=1;
		    break;
		}
		if (msec>2000)
		{
		    rp_AcqStart();
		    rp_AcqSetTriggerSrc(trig_source);
		    rp_GenAmp(RP_CH_2, 0);
		    triggered=0;
		    printf("No trig for 2 s... Set output analog output to zero");
		}

	    }
	    // Get data
	    rp_DpinSetState( RP_LED4, RP_HIGH);
	    rp_AcqGetWritePointerAtTrig(&trig_pos);
	    //printf("Trig pos: %d", trig_pos);
	    // Check if trig_pos is too close to buffer start/end
	    if ((trig_pos+num_data_points_after_trig)>(RP_BUF_SIZE-1))
	    {
		endpoints=(RP_BUF_SIZE-1)-(trig_pos-num_data_points_before_trig);
		startpoints=num_data_points_after_trig-((RP_BUF_SIZE-1)-trig_pos);
		rp_AcqGetDataPosV(RP_CH_1,0,(RP_BUF_SIZE-1),buff_ch1,&buff_size);			// Get entire buffer 
		rp_AcqGetDataPosV(RP_CH_2,0,(RP_BUF_SIZE-1),buff_ch2,&buff_size);			// Get entire buffer

		val_ch1=integrated_charge(buff_ch1,((RP_BUF_SIZE-1)-endpoints),(RP_BUF_SIZE-1));
		tmp_val_ch1=integrated_charge(buff_ch1,0,startpoints);
		val_ch2=integrated_charge(buff_ch2,((RP_BUF_SIZE-1)-endpoints),(RP_BUF_SIZE-1));
		tmp_val_ch2=integrated_charge(buff_ch2,0,startpoints);
		val_ch1=val_ch1+tmp_val_ch1;
		val_ch2=val_ch2+tmp_val_ch2;
	
		//Debug
		//printf("High Ring Buffer overflow\n");
		//printf("ch1: %f ch2: %f\n",val_ch1,val_ch2);
		//printf("Trig pos: %d end %d start %d\n", trig_pos, endpoints, startpoints);
		
	    }
	    else if ((trig_pos-num_data_points_before_trig)<0)
	    {	
		
		endpoints=(trig_pos-num_data_points_before_trig);
		startpoints=num_data_points_after_trig-endpoints;
		rp_AcqGetDataPosV(RP_CH_1,0,(RP_BUF_SIZE-1),buff_ch1,&buff_size);			// Get entire buffer
		rp_AcqGetDataPosV(RP_CH_2,0,(RP_BUF_SIZE-1),buff_ch2,&buff_size);			// Get entire buffer

		val_ch1=integrated_charge(buff_ch1,((RP_BUF_SIZE-1)-endpoints),(RP_BUF_SIZE-1));
		tmp_val_ch1=integrated_charge(buff_ch1,0,startpoints);
		val_ch2=integrated_charge(buff_ch2,((RP_BUF_SIZE-1)-endpoints),(RP_BUF_SIZE-1));
		tmp_val_ch2=integrated_charge(buff_ch2,0,startpoints);
		val_ch1=val_ch1+tmp_val_ch1;
		val_ch2=val_ch2+tmp_val_ch2;
	
		//Debug
		printf("Low Ring Buffer underrun\n");
		printf("ch1: %f ch2: %f\n",val_ch1,val_ch2);
		printf("Trig pos: %d end %d start %d\n", trig_pos, endpoints, startpoints);
	    }
	    else
	    {
		rp_AcqGetDataPosV(RP_CH_1,(trig_pos-num_data_points_before_trig),(trig_pos+num_data_points_after_trig),buff_ch1,&buff_size);
		rp_AcqGetDataPosV(RP_CH_2,(trig_pos-num_data_points_before_trig),(trig_pos+num_data_points_after_trig),buff_ch2,&buff_size);
		val_ch1=integrated_charge(buff_ch1,0,(num_data_points_after_trig+num_data_points_before_trig));
		val_ch2=integrated_charge(buff_ch2,0,(num_data_points_after_trig+num_data_points_before_trig));
	    }
	        
	    val_sum_ch1=val_sum_ch1+val_ch1;
	    val_sum_ch2=val_sum_ch2+val_ch2;
	    counter++;
	    charge[0]=val_ch1+val_ch2;

	    //rp_DpinSetState( RP_DIO1_P, RP_HIGH);
	    //usleep(20);
	    //rp_DpinSetState( RP_DIO1_P, RP_LOW);
	    
	    // Update data each s, 1050 ms to make sure the buffers update even @ 1 Hz trigger rate
	    if (msec>1050)
	    {
		start_time = clock();
		charge_ch1=(val_sum_ch1/(counter-1));
		charge_ch2=(val_sum_ch2/(counter-1));
		pthread_mutex_lock( &mutex1 );
		charge[1]=charge_ch1+charge_ch2;
		// Update table of volt & counters for sec 1-9
		for (int i=9;i>1;i--)
		{
		   count_table[i]=count_table[i-1]+counter;
		   charge[i]=charge[i-1]+charge[1];
		}
		count_table[1]=counter;
		charge[1]=charge[1];
		printf("\nNo of Triggers: %d, Time betweem (msec): %d\n", count_table[1], msec);

		/* Generating amplitude */
		//Check if overflow for the DAC
		if (charge[1]<1)
		{
		    rp_GenAmp(RP_CH_2, charge[1]);
		}
		else
		{
		    rp_GenAmp(RP_CH_2, 1);
		}

		printf("Charge: %f nC, Charge Ch1: %f nC, Charge Ch2: %f nC\n",charge[1], charge_ch1,charge_ch2);
		fflush(stdout);
		val_sum_ch1=0;
		val_sum_ch2=0;
		counter=0;
		free_counter++;
		// Digital output heartbeat
		rp_DpinGetState(RP_DIO1_P, &heart_beat_state);
		if (heart_beat_state==RP_LOW)
		{
		    rp_DpinSetState( RP_DIO1_P, RP_HIGH);
		}
		else
		{
		    rp_DpinSetState( RP_DIO1_P, RP_LOW);  
		}
		// Get FPGA temp
		rp_HealthGetValue(RP_TEMP_FPGA, &fpga_temp);
		printf("FPGA temp: %.01f C\n",fpga_temp);
		
		
	    }
	    new_data=1;
	    pthread_mutex_unlock( &mutex1 );
	    rp_AcqStart();
	    rp_AcqSetTriggerSrc(trig_source);
	    rp_DpinSetState( RP_LED4, RP_LOW);
	    
	    
	}
	/* Releasing resources */

	fflush(stdout);
	free(buff_ch1);
	free(buff_ch2);
	rp_Release();
	
	return NULL;


}