Example of 6D orientation recognition for LSM6DSO in X-NUCLEO-IKS01A3

Dependencies:   X_NUCLEO_IKS01A3

6D Orientation Demo Application with LSM6DSO based on sensor expansion board X-NUCLEO-IKS01A3

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:
Wed Mar 06 13:26:58 2019 +0000
Revision:
0:89cc933f7dbe
Child:
5:672a7705147d
First version of 6DOrientation_LSM6DSO_IKS01A3 application

Who changed what in which revision?

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