Strange problem for trigger

Just about everything about Red Pitaya
Post Reply
thomasliuchang
Posts: 31
Joined: Mon Apr 18, 2016 12:30 pm

Strange problem for trigger

Post by thomasliuchang » Thu May 19, 2016 8:46 pm

Hello,
I have a strange problem about the trigger in data sampling.
Test environment: A sinewave is generated using OUT1 of RP (frequency 5kHz, Amplitude 0.5V). The data is sampled using IN1 of RP (Decimation 8, trigger level 0.2V, trigger delay 8191, acquire trigger at positive edge of CHA)
I briefly modified the C program provided on the example to achieve above test environment.
The codes are

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "redpitaya/rp.h"

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");
        }
        rp_GenReset();
        rp_GenFreq(RP_CH_1, 5000.0);
        rp_GenAmp(RP_CH_1, 0.5);
        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(RP_DEC_8);
        rp_AcqSetTriggerLevel(0.2); //Trig level is set in Volts while in SCPI 
        rp_AcqSetTriggerDelay(8191);
        rp_AcqStart();
        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);   //get the raw data
	FILE *fp;
	fp=fopen("acq_data.txt","w");
        int i;
        for(i = 0; i < buff_size; i++){
                fprintf(fp,"%f\n",buff[i]);
        }	
 	fclose(fp);
        /* Releasing resources */
        free(buff);
        rp_Release();
        return 0;
}
        }
I plot the sampled data in the following figure. The amplitude and frequency for the sampled data are right. However, the trigger seems to be wrong. In addition, the beginning and ending of the data are different for repetitive testing.
data sampled by C.jpg
Then, I use the Matlab to sample data with same test environment.
The matlab codes are

Code: Select all

IP= 192.168.128.1;           % Input IP of your Red Pitaya...
port = 5000;
tcpipObj=tcpip(IP, port);
tcpipObj.InputBufferSize = 16384*32;
 
%% Open connection with your Red Pitaya
flushinput(tcpipObj);
flushoutput(tcpipObj);
fopen(tcpipObj);
tcpipObj.Terminator = 'CR/LF';
 
fprintf(tcpipObj,'GEN:RST');
fprintf(tcpipObj,'SOUR1:FUNC SINE');       % Set function of output signal
fprintf(tcpipObj,'SOUR1:FREQ:FIX 5000');   % Set frequency of output signal
fprintf(tcpipObj,'SOUR1:VOLT 0.5');          % Set amplitude of output signal
fprintf(tcpipObj,'OUTPUT1:STATE ON');      % Set output to ON
 
fprintf(tcpipObj,'ACQ:RST');
fprintf(tcpipObj,'ACQ:DEC 8');
fprintf(tcpipObj,'ACQ:TRIG:LEV 0.2');
fprintf(tcpipObj,'ACQ:TRIG:DLY 8191');
fprintf(tcpipObj,'ACQ:START');
pause(1)
 
fprintf(tcpipObj,'ACQ:TRIG CH1_PE');  

while 1
     trig_rsp=query(tcpipObj,'ACQ:TRIG:STAT?')
   
     if strcmp('TD',trig_rsp(1:2))  % Read only TD
   
     break
   
     end
 end
 
% Read data from buffer 
signal_str=query(tcpipObj,'ACQ:SOUR1:DATA?');

signal_num=str2num(signal_str(1,2:length(signal_str)-3));

plot(signal_num); grid on; ylabel('Voltage / V');xlabel('samples')
 
%% Close connection with Red Pitaya
fclose(tcpipObj);
The result is right, shown in the following figure.
data sampled by matlab.jpg
I repetitively check the difference between the C and Matlab programs. The functions of both programs are same. It is quite strange and I can’t find the problem with C program.
My system is Ubuntu 14.04. The version of ecosystem I used is ecosystem-0.95-1-6deb253.
Could you please kindly check this problem?
Thank you very much!
You do not have the required permissions to view the files attached to this post.

thomasliuchang
Posts: 31
Joined: Mon Apr 18, 2016 12:30 pm

Re: Strange problem for trigger

Post by thomasliuchang » Fri May 20, 2016 3:18 pm

Hello everyone,

Could anyone check the c code for me? I am very urgent to use this function, but it still doesn't work well.

Thank you very much!

Best regards,
Chang

Nils Roos
Posts: 1441
Joined: Sat Jun 07, 2014 12:49 pm
Location: Königswinter

Re: Strange problem for trigger

Post by Nils Roos » Sat May 21, 2016 2:37 am

I just ran the program - corrected for the proper end of the while-loop - and the result was as desired in every attempt, samples starting at around 0.2V.

If this is your actual source, the loop should be

Code: Select all

	while(1){
		rp_AcqGetTriggerState(&state);
		if(state == RP_TRIG_STATE_TRIGGERED){
			sleep(1);
			break;
		}
	}

thomasliuchang
Posts: 31
Joined: Mon Apr 18, 2016 12:30 pm

Re: Strange problem for trigger

Post by thomasliuchang » Sat May 21, 2016 7:52 pm

Hi Nils,

I have found the problem. Before the loop, the trigger state should be set as RP_TRIG_STATE_WAITING, instead of RP_TRIG_STATE_TRIGGERED. Thanks a lot.

Code: Select all

       rp_AcqSetTriggerSrc(RP_TRIG_SRC_CHA_PE);
        rp_acq_trig_state_t state = RP_TRIG_STATE_WAITING;
        while(1){
                rp_AcqGetTriggerState(&state);
                if(state == RP_TRIG_STATE_TRIGGERED){
                sleep(1);
                break;
                }
Best wishes,
Chang

h30x
Posts: 2
Joined: Fri Jul 01, 2016 5:14 pm

Re: Strange problem for trigger

Post by h30x » Thu Jul 14, 2016 6:26 pm

Hello, I have quite the same problem of the trigger which does not trigger when it should (cf my post : viewtopic.php?f=14&t=1546).

Here is my code :

Code: Select all

  rp_AcqReset();
/* set the trigger */
    rp_AcqSetSamplingRate(RP_SMP_125M);
    rp_AcqSetTriggerSrc(RP_TRIG_SRC_CHA_PE); // CHA positive edge
    rp_AcqSetTriggerLevel(0.1);
    rp_acq_trig_state_t state = RP_TRIG_STATE_TRIGGERED;
    
/* start the acquisition and stop it when trigger happened */
/* first */
	rp_AcqStart();
	sleep(0.01); // waiting to fill the buffer
    state = RP_TRIG_STATE_WAITING;
    while(1){ 
		printf("1");rp_AcqGetTriggerState(&state);
		if(state == RP_TRIG_STATE_TRIGGERED){
			rp_AcqStop();
			 break;}
		}
                
/* again */
    rp_AcqStart();
	sleep(0.01); // waiting to fill the buffer
    state = RP_TRIG_STATE_WAITING;

    while(1){ 
		printf("2");rp_AcqGetTriggerState(&state);
		if(state == RP_TRIG_STATE_TRIGGERED){
			rp_AcqStop();
			break;}
		}
I get sometimes :
- just "1" which means I am stuck in the first loop
- one "1" and some "2" which means that the first loop just does once and the second works as expected

It is strange, do you have an idea to solve it ?

majtom
Posts: 9
Joined: Thu Nov 08, 2018 10:29 am

Re: Strange problem for trigger

Post by majtom » Fri Nov 30, 2018 11:42 am

Sorry for posting in this old thread. But I have a very similar problem and your fixes don't work for me. Maybe some of you are still active and can reply?

Well, having a 1 second sleep before the break in the while loop, does give me values after the trigger, but just for 1 second. It is logical that it delays the stopping of filling of the buffer by 1 second. But the rp_AcqSetTriggerDelay(); functions still does not work and I can only control the length of the buffer after the trigger by the sleep command.

Also, I don't understand the difference between setting the state to RP_TRIG_STATE_WAITING in stead of RP_TRIG_STATE_WAITING; I get the exact same result.

Can someone explain?

Cheers, Maja

Post Reply
jadalnie klasyczne ekskluzywne meble wypoczynkowe do salonu ekskluzywne meble tapicerowane ekskluzywne meble do sypialni ekskluzywne meble włoskie

Who is online

Users browsing this forum: No registered users and 2 guests