Skip to content

Commit a8a0640

Browse files
committed
lis3dh library update
Tilt functions added.
1 parent 3f120b0 commit a8a0640

File tree

8 files changed

+232
-9
lines changed

8 files changed

+232
-9
lines changed
1.94 KB
Binary file not shown.

Learn/Simple Libraries/Sensor/liblis3dh/init.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*
1313
*/
1414

15-
#include <stdlib.h>
16-
//#include "simpletools.h"
15+
//#include <stdlib.h>
16+
#include "simpletools.h"
1717
#include "lis3dh.h"
1818

1919

@@ -36,7 +36,7 @@ lis3dh_t *lis3dh_init4wire(int pinSCK, int pinSDI, int pinSDO, int pinCS)
3636
if (!(device = (lis3dh_t *)malloc(sizeof(lis3dh_t))))
3737
return NULL;
3838

39-
39+
// Store IO pins
4040
device->sdi_pin = pinSDI;
4141
device->sdo_pin = pinSDO; // Set the same for 3-wire SPI mode
4242
device->sck_pin = pinSCK;
@@ -73,6 +73,16 @@ lis3dh_t *lis3dh_init4wire(int pinSCK, int pinSDI, int pinSDO, int pinCS)
7373
lis3dh_setResolution(device, 12); // 8, 10 or 12 (bit)
7474
lis3dh_setRange(device, 2); // 2, 4, 8 or 16 (g)
7575

76+
pause(100);
77+
78+
79+
// Initialise tilt running averages based on current position of sensor
80+
lis3dh_accel_mg(device, &device->tiltavgX, &device->tiltavgY, &device->tiltavgZ);
81+
82+
// Set tilt moving average filter. Value represents the % of new data, for example 75 would represent 75% of new data, 25% old data
83+
device->tiltavg_factor = 100; // default to 100% of new data (ie. filter is off)
84+
85+
7686
return device;
7787

7888
}

Learn/Simple Libraries/Sensor/liblis3dh/liblis3dh.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ lis3dh *LIS3DH;
3434
int main() // Main function
3535
{
3636

37-
int x, y, z;
38-
37+
int x, y, z, ax, ay, az, motion;
38+
3939
pause(1000); // Start-up pause for debug terminal
4040
term_cmd(CLS);
4141
term_cmd(HOME);
@@ -44,6 +44,8 @@ int main() // Main function
4444

4545

4646
LIS3DH = lis3dh_init(8, 7, 6); // pinSCK, pinSDI, pinCS
47+
48+
4749
pause(100);
4850

4951
print("%c \r", CLREOL);
@@ -67,6 +69,10 @@ int main() // Main function
6769

6870
print("Acceleration range is +-%dg, Resolution is %d bit %c \r\r", lis3dh_getRange(LIS3DH), lis3dh_getResolution(LIS3DH), CLREOL);
6971

72+
73+
74+
//lis3dh_tiltConfig(LIS3DH, 75); // Set tilt sensor moving average factor (low-pass filter)
75+
7076

7177
while(1) {
7278

@@ -100,9 +106,17 @@ int main() // Main function
100106
print(" temp : %dC, sensor relative value is %d %c \r", lis3dh_temp_C(LIS3DH), lis3dh_tempRaw(LIS3DH), CLREOL); // Display measurements
101107

102108

109+
// Angles
110+
if (lis3dh_tilt(LIS3DH, &ax, &ay, &az, &motion)) {
111+
112+
print(" tilt : ax = %d, ay = %d, az = %d, motion = %d%c \r", ax, ay, az, motion, CLREOL ); // Display measurements
113+
114+
} else { print(" tilt : data not ready %c \r", CLREOL ); }
115+
116+
103117
// Debug terminal adjustment
104118

105-
print("%c%c%c%c%c", CRSRUP, CRSRUP, CRSRUP, CRSRUP, CRSRUP); // Terminal up two lines, so results write back over same two lines when viewing on SimpleIDE Terminal or Parallax Serial Terminal
119+
print("%c%c%c%c%c%c", CRSRUP, CRSRUP, CRSRUP, CRSRUP, CRSRUP, CRSRUP); // Terminal up two lines, so results write back over same two lines when viewing on SimpleIDE Terminal or Parallax Serial Terminal
106120

107121

108122
pause(250); // Minimum pause must be ODR * 2 in milliseconds (Examples: 50Hz ODR = 40ms pause, 400Hz ODR = 5ms pause)

Learn/Simple Libraries/Sensor/liblis3dh/liblis3dh.side

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ temp.c
1919
setADCmode.c
2020
adc.c
2121
adc_mV.c
22+
tilt.c
2223
>compiler=C
23-
>memtype=cmm main ram compact
24+
>memtype=lmm main ram
2425
>optimize=-Os
2526
>-m32bit-doubles
2627
>-Wall

Learn/Simple Libraries/Sensor/liblis3dh/lis3dh.h

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ extern "C" {
122122
typedef struct lis3dh_st
123123
{
124124
volatile int tempcalC; // for degrees Celcius
125-
125+
126+
volatile int tiltavgX;
127+
volatile int tiltavgY;
128+
volatile int tiltavgZ;
129+
volatile int tiltavg_factor;
130+
126131
int sdi_pin;
127132
int sdo_pin;
128133
int sck_pin;
@@ -148,6 +153,8 @@ typedef lis3dh_t lis3dh;
148153
*/
149154

150155

156+
157+
151158
/**
152159
* @brief Initialize the sensor with typical configuration for reading axis and adc. Uses 3-Wire SPI mode
153160
*
@@ -541,6 +548,53 @@ int lis3dh_getAccel_mg(lis3dh_t *device, int axis);
541548

542549

543550

551+
/**
552+
* @brief Gets tilt angle measurements for each axis, and magnitude of motion.
553+
*
554+
* @details Angle to each axis in degrees,
555+
X relative to ground
556+
Y relative to ground
557+
Z relative to gravity.
558+
Motion is the sum of g-force on all axis relative to gravity at ground level (1G).
559+
0 is motionless, larger positve or negative values represent more intense motion.
560+
The motion value could be used on it's own for projects requiring vibration sensing.
561+
562+
*
563+
* @param device Pointer to the sensor device structure
564+
*
565+
* @param *ax Variable to store angle of x axis relative to ground.
566+
*
567+
* @param *ay Variable to store angle of y axis relative to ground.
568+
*
569+
* @param *az Variable to store angle of z axis relative to gravity.
570+
*
571+
* @param *motion Variable to store positive or negative value representing intensity of motion, with 0 being motionless at ground level.
572+
*
573+
* @returns 1 if new data is available, 0 if no new data is available, or invalid request.
574+
*
575+
*/
576+
int lis3dh_tilt(lis3dh_t *device, int *ax, int *ay, int *az, int *motion);
577+
578+
579+
/**
580+
* @brief Optional configuration for tilt sensor.
581+
*
582+
* @details Values stored in RAM. Reasonable defaults are set by the library.
583+
*
584+
* @param device Pointer to the sensor device structure
585+
*
586+
* @param avg_factor Percentage of new data added to tilt readings low pass filter (moving average).
587+
* Valid values are 0 to 100. (Setting 0 or 100 will disable the filter.
588+
* For example, if avg_factor is 75, moving average calculation will include 75% new data and 25% existing data.
589+
* Default value = 100 (moving average low pass filter disabled).
590+
*
591+
* @returns 1 if operation successful, 0 if operation failed (invalid values).
592+
*
593+
*/
594+
int lis3dh_tiltConfig(lis3dh_t *device, int avg_factor);
595+
596+
597+
544598
/**
545599
* @brief write a byte value to an LIS3DH register
546600
*
8.23 KB
Binary file not shown.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
/*
3+
* @file tilt.c
4+
*
5+
* @author Michael Mulholland
6+
*
7+
* @version 1.0.0
8+
*
9+
* @copyright
10+
* Copyright (C) Parallax, Inc. 2019. All Rights MIT Licensed.
11+
*
12+
* @brief Gets tilt angle measurements for all axis for the Parallax LIS3DH 3-Axis Accelerometer Module with ADC.
13+
*
14+
* @detail Resolution is user-configured in the sensor as 8, 10 or 12 bit. (Refer to setResolution and getResolution functions)
15+
* Recommended to use 2g acceleration range (setRange) for tilt sensing
16+
*
17+
* @returns 1 if data available and angles for all 3 axis (x, y, z). Otherwise returns 0.
18+
*
19+
*/
20+
21+
22+
#include "simpletools.h"
23+
#include "lis3dh.h"
24+
25+
26+
27+
// Helper function for Tilt angle value limiting
28+
29+
int lis3dh_limitangle(int angle) {
30+
31+
if (angle < -90) { return -90; }
32+
if (angle > 90) { return 90; }
33+
return angle;
34+
35+
}
36+
37+
// Helper function to apply scale and rounding to tilt angles
38+
39+
int lis3dh_scaleresult(int value) {
40+
41+
int i, r;
42+
43+
i = value / 100;
44+
r = value - (i * 100);
45+
46+
return (r > 49) ? ++i : i;
47+
48+
}
49+
50+
51+
int lis3dh_tilt(lis3dh_t *device, int *ax, int *ay, int *az, int *motion)
52+
{
53+
54+
int x, y, z;
55+
int avg_factor = device->tiltavg_factor;
56+
57+
if (lis3dh_accel_mg(device, &x, &y, &z)) {
58+
59+
// Calculate moving average (low pass filter)
60+
// Setting the avg_factor value to 0 or 100 will disable the filter.
61+
62+
if ((avg_factor > 0) && (avg_factor < 100)) {
63+
64+
x = lis3dh_scaleresult((x * avg_factor) + (device->tiltavgX * (100 - avg_factor )));
65+
y = lis3dh_scaleresult((y * avg_factor) + (device->tiltavgY * (100 - avg_factor )));
66+
z = lis3dh_scaleresult((z * avg_factor) + (device->tiltavgZ * (100 - avg_factor )));
67+
68+
device->tiltavgX = x; // Write back averages for next time
69+
device->tiltavgY = y;
70+
device->tiltavgZ = z;
71+
72+
}
73+
74+
75+
// Calculate angle to each axis in radians
76+
77+
// X relative to ground
78+
// Y relative to ground
79+
// Z relative to gravity
80+
81+
float y_radians = atan(((float)y/(sqrt(x*x + z*z))));
82+
float x_radians = atan(((float)x/(sqrt(y*y + z*z))));
83+
float z_radians = atan((sqrt(x*x + y*y) / z));
84+
85+
// Convert angles from radians to degrees
86+
// 1 radian × 180/pi = 57.296°
87+
88+
*ay = lis3dh_limitangle( (int) (y_radians * 57.3) );
89+
*ax = lis3dh_limitangle( (int) (x_radians * 57.3) );
90+
*az = lis3dh_limitangle( (int) (z_radians * 57.3) );
91+
92+
// Calculate motion value; 0 at 1G, increases or decreases with motion detected (sign represents dominant direction)
93+
*motion = (int) (sqrt(x*x + y*y + z*z)) - 1000; // Deduct 1G (1000 milliG's) from result to offset from ground level.
94+
95+
96+
return 1; // OK !
97+
98+
}
99+
100+
return 0; // Return 0 if no data available
101+
102+
}
103+
104+
105+
106+
int lis3dh_tiltConfig(lis3dh_t *device, int avg_factor) {
107+
108+
if ((avg_factor < 0) || (avg_factor > 100))
109+
{
110+
return 0; // Invalid ratio
111+
}
112+
113+
114+
device->tiltavg_factor = avg_factor;
115+
return 1; // Configuration values accepted OK
116+
117+
}
118+
119+
120+
121+
/**
122+
* TERMS OF USE: MIT License
123+
*
124+
* Permission is hereby granted, free of charge, to any person obtaining a
125+
* copy of this software and associated documentation files (the "Software"),
126+
* to deal in the Software without restriction, including without limitation
127+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
128+
* and/or sell copies of the Software, and to permit persons to whom the
129+
* Software is furnished to do so, subject to the following conditions:
130+
*
131+
* The above copyright notice and this permission notice shall be included in
132+
* all copies or substantial portions of the Software.
133+
*
134+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
135+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
136+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
137+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
138+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
139+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
140+
* DEALINGS IN THE SOFTWARE.
141+
*/
142+
143+
144+

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.4.136
1+
v1.4.137

0 commit comments

Comments
 (0)