Repository that contains example code for using the KX134-1211 accelerometer
Dependencies: KX134-1211 Driver
Revision 0:1610faecade6, committed 2020-09-22
- Comitter:
- Jasper Swallen
- Date:
- Tue Sep 22 20:17:13 2020 -0400
- Commit message:
- port code to example repo
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Tue Sep 22 20:17:13 2020 -0400 @@ -0,0 +1,2 @@ +BUILD/ +__pycache__/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.mbed Tue Sep 22 20:17:13 2020 -0400 @@ -0,0 +1,3 @@ +ROOT=. +TARGET=NUCLEO_H743ZI2 +TOOLCHAIN=GCC_ARM
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/KX134-1211-Driver.lib Tue Sep 22 20:17:13 2020 -0400 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/jasperswallen/code/KX134-1211-Driver/#01d5616ba355
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example-stddev.cpp Tue Sep 22 20:17:13 2020 -0400 @@ -0,0 +1,100 @@ +#include "KX134.h" +#include "math.h" +#include "mbed.h" + +#include <inttypes.h> + +#define PIN_SPI_MOSI PB_5 +#define PIN_SPI_MISO PB_4 +#define PIN_SPI_CS PA_4 +#define PIN_SPI_SCK PB_3 +#define PIN_INT1 PA_0 // placeholder +#define PIN_INT2 PA_0 // placeholder +#define PIN_RST PA_0 // placeholder + +int main(void) +{ + KX134 kx134Obj(PIN_SPI_MOSI, PIN_SPI_MISO, PIN_SPI_SCK, PIN_SPI_CS, + PIN_INT1, PIN_INT2, PIN_RST); + + if(!kx134Obj.init()) + { + printf("Failed to initialize KX134\r\n"); + return 1; + } + + printf("Successfully initialized KX134\r\n"); + + int hz = 6400; + + // initialize asynch reading + kx134Obj.enableRegisterWriting(); + kx134Obj.setOutputDataRateHz(hz); + kx134Obj.setAccelRange(KX134::Range::RANGE_64G); + kx134Obj.disableRegisterWriting(); + + size_t numTrials = 200; + float gravs[numTrials][3]; + + for(size_t trialIndex = 0; trialIndex < numTrials; ++trialIndex) + { + float time = 1.0 / (float)hz * 1000.0; + ThisThread::sleep_for(time); + + int16_t output[3]; + kx134Obj.getAccelerations(output); + float ax = kx134Obj.convertRawToGravs(output[0]); + float ay = kx134Obj.convertRawToGravs(output[1]); + float az = kx134Obj.convertRawToGravs(output[2]); + + printf("KX134 Accel: X: %" PRIi16 " LSB, Y: %" PRIi16 + " LSB, Z: %" PRIi16 " LSB \r\n", + output[0], output[1], output[2]); + printf("KX134 Accel in Gravs: X: %f g, Y: %f g, Z: %f g \r\n", ax, ay, + az); + + gravs[trialIndex][0] = ax; + gravs[trialIndex][1] = ay; + gravs[trialIndex][2] = az; + } + + float average[3] = {0}; + float maxGravs[3] = {0}; + float minGravs[3] = {std::numeric_limits<float>::max()}; + + for(size_t trialIndex = 0; trialIndex < numTrials; ++trialIndex) + { + for(int i = 0; i < 3; ++i) + { + average[i] += gravs[trialIndex][i]; + maxGravs[i] = std::max(gravs[trialIndex][i], maxGravs[i]); + minGravs[i] = std::min(gravs[trialIndex][i], minGravs[i]); + } + } + + for(int i = 0; i < 3; ++i) + { + average[i] /= numTrials; + } + + float variance[3] = {0}; + for(size_t trialIndex = 0; trialIndex < numTrials; ++trialIndex) + { + for(int i = 0; i < 3; ++i) + { + variance[i] += pow(gravs[trialIndex][i] - average[i], 2); + } + } + + float stdDeviation[3]; + for(int i = 0; i < 3; ++i) + { + variance[i] /= numTrials - 1; + stdDeviation[i] = sqrt(variance[i]); + } + + printf("Average Gravs at %d Hz: %f x, %f y, %f z\n", hz, average[0], + average[1], average[2]); + printf("Standard Deviation at %d Hz: %f x, %f y, %f z\n", hz, + stdDeviation[0], stdDeviation[1], stdDeviation[2]); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue Sep 22 20:17:13 2020 -0400 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#0db72d0cf26539016efbe38f80d6f2cb7a3d4414
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Tue Sep 22 20:17:13 2020 -0400 @@ -0,0 +1,7 @@ +{ + "target_overrides": { + "K64F": { + "platform.stdio-baud-rate": 9600 + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_settings.py Tue Sep 22 20:17:13 2020 -0400 @@ -0,0 +1,45 @@ +""" +mbed SDK +Copyright (c) 2016 ARM Limited + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" + +from os.path import join, abspath, dirname + +#ROOT = abspath(join(dirname(__file__), ".")) + +############################################################################## +# Build System Settings +############################################################################## +#BUILD_DIR = abspath(join(ROOT, "build")) + +# ARM +#ARM_PATH = "C:/Program Files/ARM" + +# GCC ARM +#GCC_ARM_PATH = "" + +# IAR +#IAR_PATH = "C:/Program Files (x86)/IAR Systems/Embedded Workbench 7.0/arm" + +# Goanna static analyser. Please overload it in private_settings.py +#GOANNA_PATH = "c:/Program Files (x86)/RedLizards/Goanna Central 3.2.3/bin" + +#BUILD_OPTIONS = [] + +# mbed.org username +#MBED_ORG_USER = "" + +# Print compiler warnings and errors as link format +#PRINT_COMPILER_OUTPUT_AS_LINK = False