thank you so much for the answer, Mr. William. According do the documentation of the libpruio, the variable io->DRam[0] stores the index of the last data read. This data, is stored in the vector io->Adc->Value[]. I Just want 128 samples of each chanel.
In fact, I completely agree with you: there is garbage in the resoults, and this is why it look so strange.. Em segunda-feira, 6 de fevereiro de 2017 16:51:02 UTC+1, William Hermans escreveu: > > > > On Mon, Feb 6, 2017 at 4:21 AM, <[email protected] <javascript:>> > wrote: > >> Hello Mr. TJF >> >> First of All, thank you so much for providing support on real-time tasks >> using a low cost plataform. >> I'm trying to apply the libary "libpruio" to make a system for energy >> meansurement using the beaglebone black. To do this, I need to enable 2 >> ADC channels, set a sample time, fil a buffer with the samples and make >> some calculation that includes the FFT. I'm really newbie on >> microprocessors, but i wrote a simple C code. The idea of this code is just >> to make 128 samples os the two channels and print the values. Just it: >> >> #include "unistd.h" >> #include "../c_wrapper/pruio.h" // include header >> #include "time.h" >> >> >> //! The main function. >> int main(/*int argc, char **argv*/) >> { >> float n[256]; >> int a[256]; >> int i=0; >> >> >> pruIo *io = pruio_new(PRUIO_DEF_ACTIVE, 0x98, 0, 1); //! create new >> driver structure >> pruio_adc_setStep(io, 9, 1, 0, 0, 0); >> // step 9, AIN-0 >> pruio_adc_setStep(io, 10, 2, 0, 0, 0); >> >> >> if (pruio_config(io, 128, 9<<10 , 156250, 4)){ // upload (default) >> settings, start IO mode >> printf("config failed (%s)\n", io->Errr);} >> else { >> >> >> if (pruio_rb_start(io)) printf("rb_start failed (%s)\n", io->Errr); // >> start measurement >> >> >> else{ >> sleep(1); >> >> >> i=io->DRam[0]; >> a[i] = i; >> n[i] = io->Adc->Value[i]; >> do{ >> if(i != io->DRam[0]){ >> i=io->DRam[0]; >> a[i] = i; >> n[i] = io->Adc->Value[i]; >> } >> }while(io->DRam[0] < 126); >> a[io->DRam[0]] = io->DRam[0]; >> n[io->DRam[0]] = io->Adc->Value[io->DRam[0]]; >> >> for(i = 0; i<=127; i++ ){ >> printf("amostra %d ----- %f \n", >> a[i], (n[i]/65536)*1.8); >> } >> >> } >> /* we're done */ >> } >> pruio_destroy(io); /* destroy driver structure */ >> return 0; >> } >> >> Right from the start I see several problems with your code. > > 125 zero based indexes. Or is it 128 ? Or do you really want 127 indexes ? > It's impossible for us to know. > > do{ > . . . > }while(io->DRam[0] < 126); > > This is inconsistent with: > > for(i = 0; i<=127; i++ ){ > . . . > } > > So, I have no clue what io-DRam[] *is*, but I can assume, somehow, you're > storing ADC values in DRAM *somehow* Based on your comments. However, first > off, your indexes to not match one another from buffer "packaging" to > buffer "unpacking". Secondly, you're not clearing your buffers(arrays) > before using them. This is what the "garbage" numbers are coming from. It's > just random values in memory, which by the way is very bad from a couple > perspectives. But this also leaves these arrays without a NULL termination > at the end. Which is very unsafe, as a potential stack overflow. At least > this applies for type char[], I'd have to double check the C standard that > you're using for your particular case to make sure. Which you can do more > easily than I. > > Additionally, your code is hard to follow. With variable names such as a, > n, and i, and zero helpful comments. The code is not exactly self > documenting. But here is what seems to be happening. You're only storing > one ADC channel value into the first half of your array. Or maybe the > conditional if statement is testing for this somehow( unclear ), and taking > care of that ? > > Assuming what you really want is 128 samples of the two ADC channels you > mention, your code needs to change. You need to check and make sure you're > never going to overflow from your arrays. Which may mean your arrays need > be of size 256 + 1 for each given type. Secondly, your loops need to be > consistent at whichever number of values you wish to store. Thirdly, you > need to clear your arrays before you use them, which can be done at array > initialization, or by using memset(), after initialization. > > A couple of things worth mentioning. In your printf() statement I'm not > sure of libpruio's implementation, but from my experience with the > beaglebone's ADC, this does not seem right ---> (n[i]/65536)*1.8) Values > output from the ADC are in range 0-4095, I'd double check to make sure that > is correct. It could be that libpruio's values are different in > implementation through some sort of conversation. Secondly, for some > reason, it may become readily apparent that your index value may contain a > lot of zero's in the middle indexes. You're going to need to look into why > that it happening after you clean your code up some. As I said above. I am > not familiar with libpruio's implementation, and the rest of your code is > not clear enough to make a determination at a glance. > > > > > -- For more options, visit http://beagleboard.org/discuss --- You received this message because you are subscribed to the Google Groups "BeagleBoard" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/1e9547ad-9485-4e22-b616-7c438043f09c%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
