Example of 6D orientation recognition for LSM6DSL in X-NUCLEO-IKS01A2

Dependencies:   X_NUCLEO_IKS01A2 mbed

Fork of 6DOrientation_IKS01A2 by ST Expansion SW Team

6D Orientation Demo Application based on sensor expansion board X-NUCLEO-IKS01A2

Main function is to show how to use sensor expansion board to find out the 6D orientation and send data using UART to a connected PC or Desktop and display it on terminal applications like TeraTerm.
After connection has been established:
- the user can rotate the board to change the 6D orientation and then view the data using an hyper terminal.
- the user button can be used to display the current 6D orientation.

Committer:
cparata
Date:
Mon Nov 21 14:57:06 2016 +0000
Revision:
4:1adb1ce17b05
Parent:
0:485458fca2bd
Child:
6:f215defe10e4
Add muti-event support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:485458fca2bd 1 /**
cparata 0:485458fca2bd 2 ******************************************************************************
cparata 0:485458fca2bd 3 * @file main.cpp
cparata 0:485458fca2bd 4 * @author AST / EST
cparata 0:485458fca2bd 5 * @version V0.0.1
cparata 0:485458fca2bd 6 * @date 9-August-2016
cparata 0:485458fca2bd 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A2
cparata 0:485458fca2bd 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
cparata 0:485458fca2bd 9 ******************************************************************************
cparata 0:485458fca2bd 10 * @attention
cparata 0:485458fca2bd 11 *
cparata 0:485458fca2bd 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
cparata 0:485458fca2bd 13 *
cparata 0:485458fca2bd 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:485458fca2bd 15 * are permitted provided that the following conditions are met:
cparata 0:485458fca2bd 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:485458fca2bd 17 * this list of conditions and the following disclaimer.
cparata 0:485458fca2bd 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:485458fca2bd 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:485458fca2bd 20 * and/or other materials provided with the distribution.
cparata 0:485458fca2bd 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:485458fca2bd 22 * may be used to endorse or promote products derived from this software
cparata 0:485458fca2bd 23 * without specific prior written permission.
cparata 0:485458fca2bd 24 *
cparata 0:485458fca2bd 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:485458fca2bd 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:485458fca2bd 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:485458fca2bd 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:485458fca2bd 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:485458fca2bd 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:485458fca2bd 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:485458fca2bd 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:485458fca2bd 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:485458fca2bd 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:485458fca2bd 35 *
cparata 0:485458fca2bd 36 ******************************************************************************
cparata 0:485458fca2bd 37 */
cparata 0:485458fca2bd 38
cparata 0:485458fca2bd 39 /* Includes */
cparata 0:485458fca2bd 40 #include "mbed.h"
cparata 0:485458fca2bd 41 #include "x_nucleo_iks01a2.h"
cparata 0:485458fca2bd 42
cparata 0:485458fca2bd 43 /* Instantiate the expansion board */
cparata 0:485458fca2bd 44 static X_NUCLEO_IKS01A2 *mems_expansion_board = X_NUCLEO_IKS01A2::Instance(D14, D15);
cparata 0:485458fca2bd 45
cparata 0:485458fca2bd 46 /* Retrieve the composing elements of the expansion board */
cparata 0:485458fca2bd 47 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
cparata 0:485458fca2bd 48
cparata 0:485458fca2bd 49 InterruptIn lsm6dsl_int1(IKS01A2_PIN_LSM6DSL_INT1);
cparata 0:485458fca2bd 50 InterruptIn mybutton(USER_BUTTON);
cparata 0:485458fca2bd 51 DigitalOut myled(LED1);
cparata 0:485458fca2bd 52
cparata 0:485458fca2bd 53 volatile int orientation = 0;
cparata 0:485458fca2bd 54 volatile int send_orientation_request = 0;
cparata 0:485458fca2bd 55
cparata 0:485458fca2bd 56 void pressed();
cparata 0:485458fca2bd 57 void orientation_cb();
cparata 0:485458fca2bd 58 void sendOrientation( void );
cparata 0:485458fca2bd 59
cparata 0:485458fca2bd 60 /* Simple main function */
cparata 0:485458fca2bd 61 int main() {
cparata 0:485458fca2bd 62 /* Attach callback to User button press */
cparata 0:485458fca2bd 63 mybutton.fall(&pressed);
cparata 0:485458fca2bd 64 /* Attach callback to LSM6DSL INT1 */
cparata 0:485458fca2bd 65 lsm6dsl_int1.rise(&orientation_cb);
cparata 0:485458fca2bd 66
cparata 0:485458fca2bd 67 /* Enable LSM6DSL accelerometer */
cparata 0:485458fca2bd 68 acc_gyro->Enable_X();
cparata 0:485458fca2bd 69 /* Enable 6D Orientation. */
cparata 0:485458fca2bd 70 acc_gyro->Enable_6D_Orientation();
cparata 0:485458fca2bd 71
cparata 0:485458fca2bd 72 printf("\r\n--- Starting new run ---\r\n");
cparata 0:485458fca2bd 73
cparata 0:485458fca2bd 74 while(1) {
cparata 0:485458fca2bd 75 if (orientation) {
cparata 0:485458fca2bd 76 orientation = 0;
cparata 4:1adb1ce17b05 77 LSM6DSL_Event_Status_t status;
cparata 4:1adb1ce17b05 78 acc_gyro->Get_Event_Status(&status);
cparata 4:1adb1ce17b05 79 if (status.D6DOrientationStatus) {
cparata 0:485458fca2bd 80 /* Send 6D Orientation */
cparata 0:485458fca2bd 81 sendOrientation();
cparata 0:485458fca2bd 82
cparata 0:485458fca2bd 83 /* Led blinking. */
cparata 0:485458fca2bd 84 myled = 1;
cparata 0:485458fca2bd 85 wait(0.2);
cparata 0:485458fca2bd 86 myled = 0;
cparata 0:485458fca2bd 87 }
cparata 0:485458fca2bd 88 }
cparata 0:485458fca2bd 89
cparata 0:485458fca2bd 90 if(send_orientation_request) {
cparata 0:485458fca2bd 91 send_orientation_request = 0;
cparata 0:485458fca2bd 92 sendOrientation();
cparata 0:485458fca2bd 93 }
cparata 0:485458fca2bd 94 }
cparata 0:485458fca2bd 95 }
cparata 0:485458fca2bd 96
cparata 0:485458fca2bd 97 void pressed() {
cparata 0:485458fca2bd 98 send_orientation_request = 1;
cparata 0:485458fca2bd 99 }
cparata 0:485458fca2bd 100
cparata 0:485458fca2bd 101 void orientation_cb() {
cparata 0:485458fca2bd 102 orientation = 1;
cparata 0:485458fca2bd 103 }
cparata 0:485458fca2bd 104
cparata 0:485458fca2bd 105 void sendOrientation( void ) {
cparata 0:485458fca2bd 106 uint8_t xl = 0;
cparata 0:485458fca2bd 107 uint8_t xh = 0;
cparata 0:485458fca2bd 108 uint8_t yl = 0;
cparata 0:485458fca2bd 109 uint8_t yh = 0;
cparata 0:485458fca2bd 110 uint8_t zl = 0;
cparata 0:485458fca2bd 111 uint8_t zh = 0;
cparata 0:485458fca2bd 112
cparata 0:485458fca2bd 113 acc_gyro->Get_6D_Orientation_XL(&xl);
cparata 0:485458fca2bd 114 acc_gyro->Get_6D_Orientation_XH(&xh);
cparata 0:485458fca2bd 115 acc_gyro->Get_6D_Orientation_YL(&yl);
cparata 0:485458fca2bd 116 acc_gyro->Get_6D_Orientation_YH(&yh);
cparata 0:485458fca2bd 117 acc_gyro->Get_6D_Orientation_ZL(&zl);
cparata 0:485458fca2bd 118 acc_gyro->Get_6D_Orientation_ZH(&zh);
cparata 0:485458fca2bd 119
cparata 0:485458fca2bd 120 if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
cparata 0:485458fca2bd 121 {
cparata 0:485458fca2bd 122 printf( "\r\n ________________ " \
cparata 0:485458fca2bd 123 "\r\n | | " \
cparata 0:485458fca2bd 124 "\r\n | * | " \
cparata 0:485458fca2bd 125 "\r\n | | " \
cparata 0:485458fca2bd 126 "\r\n | | " \
cparata 0:485458fca2bd 127 "\r\n | | " \
cparata 0:485458fca2bd 128 "\r\n | | " \
cparata 0:485458fca2bd 129 "\r\n |________________| \r\n" );
cparata 0:485458fca2bd 130 }
cparata 0:485458fca2bd 131
cparata 0:485458fca2bd 132 else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
cparata 0:485458fca2bd 133 {
cparata 0:485458fca2bd 134 printf( "\r\n ________________ " \
cparata 0:485458fca2bd 135 "\r\n | | " \
cparata 0:485458fca2bd 136 "\r\n | * | " \
cparata 0:485458fca2bd 137 "\r\n | | " \
cparata 0:485458fca2bd 138 "\r\n | | " \
cparata 0:485458fca2bd 139 "\r\n | | " \
cparata 0:485458fca2bd 140 "\r\n | | " \
cparata 0:485458fca2bd 141 "\r\n |________________| \r\n" );
cparata 0:485458fca2bd 142 }
cparata 0:485458fca2bd 143
cparata 0:485458fca2bd 144 else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 )
cparata 0:485458fca2bd 145 {
cparata 0:485458fca2bd 146 printf( "\r\n ________________ " \
cparata 0:485458fca2bd 147 "\r\n | | " \
cparata 0:485458fca2bd 148 "\r\n | | " \
cparata 0:485458fca2bd 149 "\r\n | | " \
cparata 0:485458fca2bd 150 "\r\n | | " \
cparata 0:485458fca2bd 151 "\r\n | | " \
cparata 0:485458fca2bd 152 "\r\n | * | " \
cparata 0:485458fca2bd 153 "\r\n |________________| \r\n" );
cparata 0:485458fca2bd 154 }
cparata 0:485458fca2bd 155
cparata 0:485458fca2bd 156 else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
cparata 0:485458fca2bd 157 {
cparata 0:485458fca2bd 158 printf( "\r\n ________________ " \
cparata 0:485458fca2bd 159 "\r\n | | " \
cparata 0:485458fca2bd 160 "\r\n | | " \
cparata 0:485458fca2bd 161 "\r\n | | " \
cparata 0:485458fca2bd 162 "\r\n | | " \
cparata 0:485458fca2bd 163 "\r\n | | " \
cparata 0:485458fca2bd 164 "\r\n | * | " \
cparata 0:485458fca2bd 165 "\r\n |________________| \r\n" );
cparata 0:485458fca2bd 166 }
cparata 0:485458fca2bd 167
cparata 0:485458fca2bd 168 else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 )
cparata 0:485458fca2bd 169 {
cparata 0:485458fca2bd 170 printf( "\r\n __*_____________ " \
cparata 0:485458fca2bd 171 "\r\n |________________| \r\n" );
cparata 0:485458fca2bd 172 }
cparata 0:485458fca2bd 173
cparata 0:485458fca2bd 174 else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 )
cparata 0:485458fca2bd 175 {
cparata 0:485458fca2bd 176 printf( "\r\n ________________ " \
cparata 0:485458fca2bd 177 "\r\n |________________| " \
cparata 0:485458fca2bd 178 "\r\n * \r\n" );
cparata 0:485458fca2bd 179 }
cparata 0:485458fca2bd 180
cparata 0:485458fca2bd 181 else
cparata 0:485458fca2bd 182 {
cparata 0:485458fca2bd 183 printf( "None of the 6D orientation axes is set in LSM6DSL - accelerometer.\r\n" );
cparata 0:485458fca2bd 184 }
cparata 0:485458fca2bd 185 }