Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

maximize ADC sampling rate

ADC sampling rate

  • Please log in to reply
9 replies to this topic

#1 aballung

aballung

    New Member

  • Members
  • Pip
  • 4 posts

Posted 15 May 2013 - 08:22 PM

Hi all,

 

On the datasheet, it says the ADC on the Netduino Plus 2 can sample at 2.4MSPS but i can only get it to sample at 500SPS.  How do i change the sampling rate in visual studio 2010?

At the moment my code is as below:

 

  const double maxVoltage = 3.3f;   const double maxAdcValue = 4095;   var voltagePort = new AnalogInput(Cpu.AnalogChannel.ANALOG_1);   var lowPort = new OutputPort(Pins.GPIO_PIN_A0, false);   var highPort = new OutputPort(Pins.GPIO_PIN_A2, true);   var num = 0;     while (num < 1000)   {   int rawValue = voltagePort.ReadRaw();   double value = (rawValue * maxVoltage) / maxAdcValue;   Debug.Print(value.ToString("f"));   num = num + 1;     }

 



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 16 May 2013 - 04:09 AM

Hi aballung, Managed code is interpreted, so you'll be able to pull quite a few samples...but not millions per second. You can write native code to do this if you want, or create a native code extension and build your own firmware...if you need that kind of super high speed. The hardware is certainly capable of a lot. Welcome to the Netduino community, Chris

#3 aballung

aballung

    New Member

  • Members
  • Pip
  • 4 posts

Posted 17 May 2013 - 07:59 PM

Thank you for your response. How do you go about writing and implementing native code?  I just need the N2+ to sample at about 2MSPS and store it into memory.  Do you have any suggestions?

 

Angelo



#4 aballung

aballung

    New Member

  • Members
  • Pip
  • 4 posts

Posted 20 May 2013 - 05:55 AM

I tried following these steps on this website:

http://adeneo-embedd...-framework.html

 

But I get this error:

An unhandled exception of type 'System.NotSupportedException' occurred in LEDInterop.dll

 

I'm not sure if I'm doing this right.  Do you have any advice on this?  Would this strategy increase the speed of the program?

 

 

Or if its easy enough, if somebody could translate this to native code for me :)

I see walkthroughs online, but can't seem to get them to work and none post the files to see how they actually did it.

Any help is appreciated.  I need this done for my thesis due in a couple weeks!

 

 

 

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.IO;


namespace Thesis
{
  class FlashUtility
  {

  public class Program
  {

  public static void Main()
  {
  // write your code here


  using (var filestream = new FileStream(@"SDdata.txt", FileMode.Create))
  {
  const double maxVoltage = 3.3f;
  const double maxAdcValue = 4095;
  StreamWriter streamWriter = new StreamWriter(filestream);

  var voltagePort = new AnalogInput(Cpu.AnalogChannel.ANALOG_1);
  var lowPort = new OutputPort(Pins.GPIO_PIN_A0, false);
  var highPort = new OutputPort(Pins.GPIO_PIN_A2, true);
  var num = 0;

  while (true)
  {
  double rawValue = voltagePort.ReadRaw();
  if (rawValue > 500)
  {
  while (num < 30)
  {

  rawValue = voltagePort.ReadRaw();
  double value = (rawValue * maxVoltage) / maxAdcValue;
  Debug.Print(rawValue.ToString("f"));
  Thread.Sleep(1000); // 1 seconds
  num = num + 1;

  streamWriter.WriteLine(value.ToString("f"));

  }
  streamWriter.Close();
  break;
  }
  else
  {
  }
  }
  }

  }

  }
  }
}



#5 cutlass

cutlass

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationNew England. :)

Posted 20 May 2013 - 09:36 PM

See the following post, I even included the project files, just download, unzip, compile, and load. :)

 

http://forums.netdui...etduino-plus-2/

 

 

Good Luck.



#6 cutlass

cutlass

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationNew England. :)

Posted 20 May 2013 - 09:50 PM

I'm not sure if I'm doing this right.  Do you have any advice on this?  Would this strategy increase the speed of the program?

To get the max speed from the ADC, you have to store the raw values in memory.

Then, do post processing.  Note, you have only so much memory. :)

 

Doing anything else, takes significant time under NETMF. :)

 

NETMF is not like .NET where you have good/great optimizations.

 

NETMF is meant to be small in code size.  It trades off optimizations, etc in favor of code-size.  There are a few books on .NETMF, check amazon, library, etc. 

IMHO, forget about the interweb (unless it's Microsoft's website) for good, accurate, detailed info on .NETMF. :)

 

 

There are many things that make the speed of .NET on an Intel Desktop very different from NETMF on an ARM.

And, there are many aspects to "performance". :)



#7 cutlass

cutlass

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationNew England. :)

Posted 21 May 2013 - 02:44 AM

I was interested to see what the ADC max sample rate was with the N+2.  So, I did a test.  The code is below, and the project is attached.

 

Quick summary:  The ADC max sample rate is roughly between 10,000 and 11,000 samples/second.

 

The max size array that I could allocate was 40,000 shorts.  I'm not sure why.  But, that gives ~3 seconds of samples.

To do the test, I ran the code, waited for "OuterLoop Counter: 5" to be displayed, then counted until "OuterLoop Counter: 15" was displayed.  Good enough to get a ballpark figure for the max sample rate.

 

 

Some notes, unlike with .NET.
1) No difference was found between the release and debug versions.

2) No difference was found with the optimize option checked or unchecked.

3) The Microsoft suggested method (as shown below) to eliminate range checking did not improve performance.

for (int i = 0; i < ADC_samples_int.Length; i++) {
  ADC_samples_int[i] = (short)adcPort_A0.Read();
}
 

 

 

My ADC max sample rate test code:

using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;namespace AnalogTestMaxSampleRate {    public class Program {        public static void Main() {            // Define and initialize variables             int inner_loop_counter_int = 0;            int outer_loop_counter_int = 0;            short[] ADC_samples_int;            ADC_samples_int = new short[40000];            SecretLabs.NETMF.Hardware.AnalogInput adcPort_A0 = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0);                        inner_loop_counter_int = 0;            outer_loop_counter_int = 1;            // do forever...            while (true) {                inner_loop_counter_int = 0;                while (inner_loop_counter_int < ADC_samples_int.Length) {                    // read from ADC Port_A0                    ADC_samples_int[inner_loop_counter_int++] = (short)adcPort_A0.Read();                }                Debug.Print("OuterLoop Counter: " + outer_loop_counter_int++.ToString());            }        }    }}// NOTES:// 1) No difference was found between the release and debug versions.//// 2) No difference was found with the optimize option checked or unchecked.//// 3) The Microsoft suggested method (as shown below) to eliminate range checking did not improve performance. // for (int i = 0; i < ADC_samples_int.Length; i++) {//    ADC_samples_int[i] = (short)adcPort_A0.Read();// }//

Attached Files



#8 cutlass

cutlass

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationNew England. :)

Posted 21 May 2013 - 04:17 AM

I need this done for my thesis due in a couple weeks!
 

 

Is this an undergrad thesis?

Gee, even the engineers that I know that are super great writers took ~1 semester to write their thesis.

 

My masters thesis took me over ~1-1/2 semesters to write.  Ugh!  I thought that I could do it in ~1/2 semester.  And, that was after all of my research was done.  I guess it also depends on how thesis pre-reviews are done at the school.  And, how much feedback/requests/etc the thesis committees give.



#9 Symbiatch

Symbiatch

    New Member

  • Members
  • Pip
  • 2 posts

Posted 22 May 2013 - 11:40 AM

The max size array that I could allocate was 40,000 shorts.  I'm not sure why.

 

Netduino Plus 2 has "100k+" of RAM. 40000 shorts is already over 78 kB, then you need memory for other things as well. That explains it quite well.



#10 cutlass

cutlass

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationNew England. :)

Posted 22 May 2013 - 03:04 PM

Netduino Plus 2 has "100k+" of RAM. 40000 shorts is already over 78 kB, then you need memory for other things as well. That explains it quite well.

Thanks!

Yea, I was thinking about the size of the ram.  But, I thought that the N+2 had at least 256K of ram. :) Because of work, I have the L2 size of the Intel i7's on my mind.  Because of contention/performance, I have to keep some cores(program threads) within the 256K limit and be aware of my shared variable accesses (with regards to performance/time).

Doing fast real-time stuff in a mobile i7 - very neat!  And, impressive 64bit Floating Point performance.  Download the Intel LINPACK benchmark test (http://software.inte...cense-agreement) and run it on any decent i-series processor - wow! :)

BTW, using the Intel Compiler/etc is a very good idea for anyone looking for max performance from an Intel processor.







Also tagged with one or more of these keywords: ADC, sampling rate

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.