Accelerometer Example
1. Understanding the Script
This example demonstrates the process of getting accelerometer values and processing them
1.1. Initial Setup
The main function primarily performs the initial setup, including debug interface initialization, debug_printinitialization and accelerometer initialization.
main()
{
Init(RS232, RS232_SPEED, WORD_LENGTH, STOP_BITS, PARITY);
debug_init(RS232);
Delay(1000);
Init(ACCELEROMETER, false);
for(;;)
{
Delay(100);
}
}1.2. Accelerometer Data Callback
Immediately after the Init(ACCELEROMETER,...); function is invoked, the script begins receiving accelerometer data. The ACCELEROMETER callback is triggered whenever new data is received, this is where accelerometer_read function is called.
forward public callback (event);
public callback (event)
{
switch(event)
{
case ACCELEROMETER:
{
accelerometer_read();
}
}
return 0;
}1.3. Accelerometer Data Parsing
The accelerometer_read function performs the following operations:
Data Acquisition: Retrieves raw acceleration data from the accelerometer along the X, Y, and Z axes using
AxesReadfunction.Vector Magnitude Calculation: Computes the magnitude of the acceleration vector using the formula: vector_magnitute = (x2+y2+z2)1/2
Peak Detection: Compares the current magnitude to previous values to track the maximum (peak) vector magnitude observed.
G-force Conversion: Converts the maximum magnitude to maximum G-force units based on sensor scaling factor.
Data Output: Formats and transmits the results over the RS232 serial interface.
accelerometer_read()
{
AxesRead(axes);
vector = floatsqroot(float((axes.x*axes.x) + (axes.y*axes.y) +(axes.z*axes.z)));
if (vector_max < vector)
{
vector_max = vector;
}
debug_print("Accelerometer values (milli-g forces): X: %i Y: %i Z: %i \r\n", axes.x, axes.y, axes.z);
debug_print("Current vector value: %f \r\n", vector);
debug_print("Max g force value: %f \r\n\r\n", floatdiv(vector_max, 4096.0));
}1.4. Printed Data
The data output to the RS232 interface using the debug_print() function will appear as follows:
Accelerometer values (milli-g forces): X: -224 Y: 176 Z: 4016 Accelerometer values (milli-g forces): X: -224 Y: 176 Z: 4016 Accelerometer values (milli-g forces): X: -240 Y: 176 Z: 4016 |
|---|
2. Accelerometer Example
#include <io>
#include <read>
#include <float>
#include <string>
#include <core>
#include <write>
#include <define>
#include <socket>
#include <debug>
#define RS232_SPEED 115200
#define WORD_LENGTH WORDLENGTH_8
#define STOP_BITS STOPBITS_1
#define PARITY PARITY_NONE
new Float:vector_max = 0.0;
new axes[.x, .y, .z];
new Float:vector = 0.0;
forward public callback (event);
public callback (event)
{
switch(event)
{
case ACCELEROMETER:
{
accelerometer_read();
}
}
return 0;
}
accelerometer_read()
{
AxesRead(axes);
vector = floatsqroot(float((axes.x*axes.x) + (axes.y*axes.y) +(axes.z*axes.z)));
if (vector_max < vector)
{
vector_max = vector;
}
debug_print("Accelerometer values (milli-g forces): X: %i Y: %i Z: %i \r\n", axes.x, axes.y, axes.z);
debug_print("Current vector value: %f \r\n", vector);
debug_print("Max g force value: %f \r\n\r\n", floatdiv(vector_max, 4096.0));
}
main()
{
Init(RS232, RS232_SPEED, WORD_LENGTH, STOP_BITS, PARITY);
debug_init(RS232);
Delay(1000);
Init(ACCELEROMETER, false);
for(;;)
{
Delay(100);
}
}