ISSDK  1.7
IoT Sensing Software Development Kit
motionCheck.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*! \file motionCheck.c
36  \brief check to see if the board is moving.
37 
38  This function would normally be called from your fusion_tasks in your main.c.
39  See main_freertos_two_tasks_power_cycling.c for example usage.
40 */
41 
42 // Sensor Fusion Headers
43 #include "sensor_fusion.h" // top level magCal and sensor fusion interfaces
44 
45 /// The motionCheck() function is not a sensor fusion function. It is a function
46 /// that simply monitors an accelerometer or magnetometer tri-axial sensor output,
47 /// returning Boolean true if the sensor appears to be stationary, and false otherwise.
48 /// This function would normally be called from your fusion_tasks in your main().
50  float sample[3], ///< processed triaxial sensor sample (accel or mag)
51  float baseline[3], ///< previous value to compare to
52  float tolerance, ///< how much tolerance you can stand
53  uint32_t winLength, ///< how many samples need to be stable to assert "noMotion"
54  uint32_t *count ///< how many samples so far we've been not moving
55 )
56 {
57  float change[3];
58  bool changed;
59  change[CHX] = fabs(baseline[CHX] - sample[CHX]);
60  change[CHY] = fabs(baseline[CHY] - sample[CHY]);
61  change[CHZ] = fabs(baseline[CHZ] - sample[CHZ]);
62  changed = (change[CHX]>tolerance) ||
63  (change[CHY]>tolerance) ||
64  (change[CHZ]>tolerance);
65  if (changed) {
66  baseline[CHX] = sample[CHX];
67  baseline[CHY] = sample[CHY];
68  baseline[CHZ] = sample[CHZ];
69  *count = 0;
70  } else {
71  if ((*count) <= winLength) (*count) += 1;
72  }
73  return(*count > winLength);
74 }
#define CHY
Used to access Y-channel entries in various data data structures.
Definition: sensor_fusion.h:87
The sensor_fusion.h file implements the top level programming interface.
#define CHZ
Used to access Z-channel entries in various data data structures.
Definition: sensor_fusion.h:88
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:86
bool motionCheck(float sample[3], float baseline[3], float tolerance, uint32_t winLength, uint32_t *count)
Definition: motionCheck.c:49