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:
public int[] Mean_XYZ { get { ...; } }
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:
memsParser.SET_SCALE_2G();
memsParser.SET_FREQ_40Hz();
We'll also use 12-bits right justified data blocks (most sensitive to small changes):
memsParser.SET_MODE_12BIT();
Now we are going to start the device and read 1s data 10 times and print it to Console:
memsParser.Start();
AccelerationMeasurement Amsmt;
for (int k = 0; k < 10; k++)
{
System.Threading.Thread.Sleep(1000);
Amsmt = memsParser.Get();
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:
memsParser.Stop();
memsParser.Dispose();
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
Peder has a background in engineering physics and is currently PhD student in medical radiation physics at Lund University.