Click here to Skip to main content
Click here to Skip to main content

ST Accelerometer Parser

, 12 Feb 2009
Rate this:
Please Sign up or sign in to vote.
A C# class used to communicate with ST's LIS3LV02DL Accelerometer

Introduction

I bought a LIS3LV02DL accelerometer from ST Microelectronics. Some useful code (VB) was attached along with the device itself, but things needed to be sorted out...  Provided here are:

  • LIS3LV02DL_Parser, used to communicate with the device
  • AccelerationMeasurement, used to store and provide acceleration data
  • A sample Console Application, Program.cs, showing how to use the classes above

Background

The accelerometer measures the static (gravity) and dynamic acceleration along the three carthesian axes: x, y, z. It can be configured to run in 40-640 Hz, using two different scales/sensitivities ±2g or ± 6g. More information about the device itself can be found here

Using the Code 

When you plug the device into the USB connector, a virtual Comport should appear. Take note of the port number (Right–click My Computer, click Manage, and then click Device Manager and Ports) that is assigned to your device, you'll need it to make this work.

A new LIS3LV02DL_Parser is created using that very same number:

LIS3LV02DL_Parser memsParser = new LIS3LV02DL_Parser(int portNr, TimeSpan t); 

Or, if you'd like to read the measurements whenever you like:

LIS3LV02DL_Parser memsParser = new LIS3LV02DL_Parser(int portNr); 

Now we can open the SerialPort using memsParser.Open().

The Parser can be configured to send a new AccelerationMeasurement at fixed intervals (first line of code above) or you can use the memsParser.Get() method if you'd like to read it out for yourself. Using the constructor in the first example raises an event, sending a new AccelerationMeasurement when the TimeSpan t has passed.

Before moving on with the Parser tutorial, I'll give you some code snippets from the measurement class.

AccelerationMeasurement

A new A-measurement is created as:

 AccelerationMeasurement A = new AccelerationMeasurement( bool is16Bit,
bool FullScale, short[] X, short[] Y, short[] Z, DateTime _Start); 

Where the X,Y,Z short arrays contains the 16-bit raw data read from the device. But this class contains some properties for refined data as well. Let's have a look at two of them:

// Returns mean acceleration for X, Y and Z axes. Includes static (gravity).
 public int[] Mean_XYZ { get { ...; } }  
// Returns Total mean acceleration.
public int Total_Mean { get { return tot; } }

One note here; the Total Mean Acceleration is the length of the total acceleration vector, calculated as: at = f*sqrt(ax2+ay2+az2), where f is a calibration factor.

This class also contains properties for Max/Min, Frequency, Time, etc.

Moving On...

We'll continue setting up the device using the second constructor above and with the device's standard scale and frequency: 

memsParser.SET_SCALE_2G(); // ±2g
memsParser.SET_FREQ_40Hz(); // 40 Hz

We'll also use 12-bits right justified data blocks (most sensitive to small changes): 

memsParser.SET_MODE_12BIT(); // 12-bits, right

Now we are going to start the device and read 1s data 10 times and print it to Console:

memsParser.Start(); // Start Device in continuous mode 
AccelerationMeasurement Amsmt;
            for (int k = 0; k < 10; k++)
            {
                // Wait one sec  (Collect data)
                System.Threading.Thread.Sleep(1000);
                // Get data
                Amsmt = memsParser.Get();
                // Print (Net) Total Mean Acceleration (mg)
                Console.WriteLine(
                    String.Format("Measurement {0} = {1} mg",
                    k.ToString(),
                    Amsmt.Total_Mean.ToString()));
            } 

The mean is the mean of all at (see above) during the measurement interval (40 Hz so should be around 40 single measurements in the Amsmt class). 

Finally we stop the device and Dispose of the Parser class:

memsParser.Stop();    // Stop Device
memsParser.Dispose(); // Close SerialPort and release resources 

The code is filled with comments so if you thought this article was too brief, don't hesitate to have a look in the studio. Many a question marks will probably vanish along the way...  

Points of Interest 

I had a problem with my device - got "Unable to open Comport - incorrect parameter" errors in Hyperterminal, software provided with the device and my own code. This was probably due to some Windows XP problem with the provided driver.  

Anyway I took it so far as to rewrite the Parser to be used in conjunction with the RS232.vb class written by the manufacturer, thus avoiding the System.IO.Ports.SerialPort class. If anyone has the same problem, you can contact me and I'll be happy to send you the code. ST's support should get credit though, they eventually sent me a new device...    

History

  • 4/2/2009: First rev. published
  • 5/2/2009: Minor changes, added support for 12-bit right parsing 
  • 12/2/2009: Corrected formulae affecting AccMsmt class 

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

flmz_81
Other Lund University
Sweden Sweden
Peder has a background in engineering physics and is currently PhD student in medical radiation physics at Lund University.

Comments and Discussions

 
QuestionLIS3LV02DL Pinmemberpalm_86998-Jun-09 10:14 
GeneralRequest info for model of accelerometer Pinmemberpippopeppe5-May-09 1:05 
GeneralRe: Request info for model of accelerometer Pinmemberflmz_815-May-09 2:39 
GeneralRe: Request info for model of accelerometer Pinmemberpippopeppe5-May-09 23:24 
GeneralRe: Request info for model of accelerometer Pinmemberpippopeppe6-May-09 23:11 
GeneralRe: Request info for model of accelerometer Pinmemberflmz_817-May-09 0:30 
GeneralRe: Request info for model of accelerometer Pinmemberpippopeppe7-May-09 3:26 
GeneralRe: Request info for model of accelerometer Pinmemberflmz_817-May-09 4:44 
GeneralNice! PinmemberNils Thorell5-Feb-09 20:18 
GeneralRe: Nice! Pinmemberflmz_8112-Feb-09 4:51 
GeneralGreat Pinmembersam.hill5-Feb-09 5:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 12 Feb 2009
Article Copyright 2009 by flmz_81
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid