Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

ST Accelerometer Parser

Rate me:
Please Sign up or sign in to vote.
4.11/5 (6 votes)
12 Feb 2009GPL33 min read 37.4K   417   13   11
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:

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

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

C#
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:

C#
 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:

C#
// Returns mean acceleration for X, Y and Z axes. Includes static (gravity).
 public int[] Mean_XYZ { get { ...; } }  
C#
// 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: a<sub>t</sub> = f*sqrt(a<sub>x</sub><sup>2</sup>+a<sub>y</sub><sup>2</sup>+a<sub>z</sub><sup>2</sup>), 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: 

C#
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): 

C#
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:

C#
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 a<sub>t</sub> (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:

C#
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)


Written By
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 Pin
palm_86998-Jun-09 10:14
palm_86998-Jun-09 10:14 
GeneralRequest info for model of accelerometer Pin
pippopeppe5-May-09 1:05
pippopeppe5-May-09 1:05 
GeneralRe: Request info for model of accelerometer Pin
flmz_815-May-09 2:39
flmz_815-May-09 2:39 
GeneralRe: Request info for model of accelerometer Pin
pippopeppe5-May-09 23:24
pippopeppe5-May-09 23:24 
GeneralRe: Request info for model of accelerometer Pin
pippopeppe6-May-09 23:11
pippopeppe6-May-09 23:11 
GeneralRe: Request info for model of accelerometer Pin
flmz_817-May-09 0:30
flmz_817-May-09 0:30 
GeneralRe: Request info for model of accelerometer Pin
pippopeppe7-May-09 3:26
pippopeppe7-May-09 3:26 
GeneralRe: Request info for model of accelerometer Pin
flmz_817-May-09 4:44
flmz_817-May-09 4:44 
GeneralNice! Pin
Nils Thorell5-Feb-09 20:18
Nils Thorell5-Feb-09 20:18 
I like your article, but I seriously doubt that the equation "at = sqrt(ax2+ay2+az2) - g" makes sense. For example, if you have an acceleration of 'g' perpendicular to gravity, the result would be "at = g * (sqrt(2) - 1)". But the result should be 'g', right?
GeneralRe: Nice! Pin
flmz_8112-Feb-09 4:51
flmz_8112-Feb-09 4:51 
GeneralGreat Pin
sam.hill5-Feb-09 5:39
sam.hill5-Feb-09 5:39 

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

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