Modfying the eMonLib to sample Sample time/V/I

Hello, my project is using the eMonLib in an Arduino Zero board (ARM Cortex M0+ ), the library works because some modifications were made by Glidhuson., to include a 12bits ADC mainly. So, the standard Arduino eMonLib has worked very well, but I wanted to push the limits and trying to get a grpahical representation of the instantaneous values (millis, sampleV & sampleI), and I modified the existing library to passed three int arrays by reference to the emon.calcVI,to gather this information. I know there should be errors, like the analogread function delay. I did the following changes to the standard code: in the EMonLib.h, I changed the CalcVI definition:

kinematik's picture

Re: Modfying the eMonLib to sample Sample time/V/I

Hello, my project is using the eMonLib in an Arduino Zero board (ARM Cortex M0+ ), the library works because some modifications were made by Glidhuson, to include a 12bits ADC mainly.

So, the standard Arduino eMonLib has been measuring very well, also Arduino Zero is much faster than the standard Arduino AVR based boards, and not so expensive. A Zero board is a perfect candidate to run the eMonLib.

I wanted to push the limits and trying to get a graphical representation of the instantaneous values (millis, sampleV & sampleI). I have seen this kind of graphical representation in the openenergymonitor website, to check the PHASECAL and calibrations. I know there should be errors, like the analogread function delay.

I modified the existing library to pass three int arrays by reference to the emon.calcVI, gather the sampled information in the main loop, and return this information to the Arduino sketch (by reference)

I did the following changes to the standard code: in the EMonLib.h,

1. Changed the CalcVI definition:

void calcVI(unsigned int crossings, unsigned int timeout, uint32_t* t_Samples, 
    uint16_t* V_Samples, uint16_t* I_Samples);   

2. Changed the calcVIFuntion:

  //------------------------------------------------------------------------------
  // 2) Main measurement loop
  //------------------------------------------------------------------------------
  start = millis();
  uint16_t i=0;       // NEW

  while ((crossCount < crossings) && ((millis()-start)<timeout))
  {
    numberOfSamples++;                       //Count number of times looped.
    lastFilteredV = filteredV;               //Used for delay/phase compensation

    t_muestreos[i] = millis();  // By Quino
    //-----------------------------------------------------------------------------
    // A) Read in raw voltage and current samples
    //-----------------------------------------------------------------------------
    sampleV = analogRead(inPinV);                 //Read in raw voltage signal
      V_Samples[i]=sampleV;            // NEW!!
    sampleI = analogRead(inPinI);                 //Read in raw current signal
      I_Samples[i] = sampleI;          // NEW!!
    i++;                               // NEW!!

    //-----------------------------------------------------------------------------
    // B) Apply digital low pass filters to extract the 2.5 V or 1.65 V dc offset,
    //     then subtract this - signal is now centred on 0 counts.
    //-----------------------------------------------------------------------------

3. I call the library inside my sketch the following way:

  uint32_t t_Samples[1000];
  uint16_t V_Samples[1000];
  uint16_t I_Samples[1000];

  uUSB.print("Starting eMon.calcVI....");
  emon1.calcVI(20,1000, t_Samples, V_Sampples, I_Samples); 

However the sketch / board freezes after claling the function. Can you guide me or help me?, I am convinced that many people have done this before.

dBC's picture

Re: Modfying the eMonLib to sample Sample time/V/I

Have you ruled out the possibility that you're steaming right off the end of those [1000] element sample arrays?  From memory, I think I've heard ~50 sample-pairs per cycle quoted for the standard AVR OEM monitors.  You've asked it to sample for 20 half-cycles, or 10 full cycles so that would represent 500 sample pairs on an AVR monitor.   If the SAM based Arduino was just 2x faster than the AVR based one, you'd be hitting your 1000 limit.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.