Anlog vs i2c question

Hello,

I am wondering if anyone has some suggestions concerning connection of 2 analog sensors (with 6 analog inputs) to the Panda II.

I have been playing around with the i2C Adxl345 from sparkfun and I am wondering if it is even possible for me to get a sample rate of 2 kHz over the i2c.

If I cannot get this sample rate then I need to go back to analog sensors in which case I need 6 analog inputs (2 times x,y,z).

Any suggestions?

two things.

Do you need 2khz? Please justify :slight_smile:

How much other processing do you need to do of the data (that often is a bigger impact on overall sample rate irrespective of platform)

OK, three things.

Have you tried? If so, how fast did you get? I love the fact that Fez is quick to test this and to push the envelope to see how plausible that rate is for your use.

Well I am studying mechanical vibrations where the frequency may exceed 1 kHz. So 2 kHz is the minimum. All I need is to read the values and write them to an SD card. I have managed to do this with an analog sensor. The problem is the choice of analog sensors I have been able to find ready made are +/- 3g which is lower than the expected g rating. Perhaps I need to buy a surface mounted chip and do some soldering…

I have the ADXL345 using I2C and it works now but I have not measured the read times but I suspect it will not get close to these values. I am a noob in NETMF but I am learning fast. I suspect for my application I will need to explore RLP.

The only to do this would be by reading multiple samples using RLP and buffering them and then later flush to an SD card. But I am afraid you will not have enough RAM to do much buffering.

Thanks Gus,

I think my problem comes down to the RAM (as you suggested). I need a whole lot more to play with. As it is I am creating array as shown below. The problem is the system runs out of memory with anything more than a 250X4 int array. Then I have to go and write it to an sd card which takes 3 or 4 seconds making this all useless because I need to read for at least 5 continuous seconds (instead of 20 milliseconds). I think what I need is a board with a lot more memory and maybe you can recommend something…I need to think in terms of megabytes!

for (int i = 0; i < 200; i++)
{

                 //approximate read time or sample freq
                closer = nowTime1 - nowTime;

                //Current time
                nowTime = unchecked((int)Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks);


                //reading the sensor values
                x1 = xRead.Read();
                y1 = yRead.Read();
                z1 = zread.Read();

                jagged[i] = new int[4];

                jagged[i][0] = closer;
                jagged[i][1] = x1;
                jagged[i][2] = y1;
                jagged[i][3] = z1;
       

                nowTime1 = unchecked((int)Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks);

}

Please use code tags

Is this right for code tages?



for (int i = 0; i < 200; i++)
 {
 
//approximate read time or sample freq
 closer = nowTime1 - nowTime;
 
//Current time
 nowTime = unchecked((int)Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks);
 

//reading the sensor values
 x1 = xRead.Read();
 y1 = yRead.Read();
 z1 = zread.Read();
 
jagged[i] = new int[4];
 
jagged[i][0] = closer;
 jagged[i][1] = x1;
 jagged[i][2] = y1;
 jagged[i][3] = z1;

 
nowTime1 = unchecked((int)Microsoft.SPOT.Hardware.Utility.GetMachineTime().Ticks);
 }


Some of the most beautiful code tags I’ve ever seen… :wink:

Thanks!

Could be numbered though!

rob

Any ideas though about the array…no matter how I slice it the RAM is too low or the SD card write too slow…If I write each line to sd it takes 2 to 4 milliseconds per line…

The array is the only way around this…but it runs out of memory…arrrgghhh…

What i realy need is a fez spider in an arduino form factor…costing around 50 dollars.

Some tips to speed things up.

Don’t ‘new’ things up inside a time critical loop if you can avoid it.

jagged[i] = new int[4];

Since your going to collect a given number of samples then write that out to the SD card declare your array size at the start of the program.

Better still do away with the whole jagged array. You have four values you are storing with each read, X, Y, Z and ticks. Just declare an array of int at the beginning of your program:


// NumberOfSamples is # of samples you read between each write to SD card
int[] myArray = new int[4*NumberOfSamples];

Then your sampling code woudl look like:


//approximate read time or sample freq
myArray[index] = nowTime1 - nowTime; // save time
 
//reading the sensor values
myArray[index+1] = xRead.Read();
myArray[index+2] = yRead.Read();
myArray[index+3] = zread.Read();

index += 4;

Now all you have is a simple array of int to write to the SD card. When you read these values into a PC you can sort the values back out into a jagged array if you wish.

Oh yes, when you are done with your samples you can use the Array Clear method to zero out yoru array (very quickly) to get ready for your next set of samples.

Jeff,

That is GREAT! Thanks!

I had thought about using ushort arrays but the analog read() values are default int values and I don’t know how to change a int to uint. Not sure if that makes sense…

By using a uint array the computer would allocate much less memory to each element and would be able to save a lot more at a time.

Thanks a lot!!

An int and uint are the same number of bits. The only difference is that in an int the msb (most significant bit) indicates if the value is positive or negative.

Ooops I meant short array.

A short array would allocate 16 bits for each element whereas the int would allocate 32 bits.

Is this correct?

That would save me a lot of space.

Funny It reminds me of my numerical methods course I took where the professor was an eccentric from the early days of programming (talking 50s and 60s). He was obsessed with memory usage. I never understood it as we had 8 gigs to play with. Now I understand what he meant.

you can hover over a data type in VS and it’ll tell you how many bits it is…

Int === System.Int32
Short === System.Int16

oh and it might be easier to explain “an array of shorts” rather than a “short array”, I scratched my head for a minute thinking a short array was only part of what you really needed.

And as for type casting… myShort = (short)myInt;
or in your case, myShort = (short) xValue.Read();

This topic sparked my curiosity with regards to SHORT vs INT. I found this “discussion” around that and was wondering if the same applies to NETMF where INT is generally faster to use than SHORT?

[url]asp.net - Why should I use int instead of a byte or short in C# - Stack Overflow

In general terms, “think small” says use the appropriate data type to maintain your small data model - this discussion spawned from lack of memory, and theoretically using an array of smaller sized chunks should give you less space consumed. Without knowing the underlying constructs that the framework uses, you can’t always assume that for say one or two INT vs SMALL variables because the framework may still allocate them the same way, but you would hope that an array would be able to be allocated in a more efficient way.

The discussion in that thread is about speed though, and again without knowing what native constructs the netmf port uses you are somewhat guessing what will be faster. What is telling though is that there have been many discussions about allocating a big array for data and filling it by using your sampling in RLP that can be mega-fast in comparison.