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 Jul 24 14:36:54 2019 +0000
Revision:
5:672a7705147d
Parent:
0:89cc933f7dbe
Child:
6:dee80d1952bc
Format with Astyle

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 5:672a7705147d 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 5:672a7705147d 37 */
cparata 5:672a7705147d 38
cparata 0:89cc933f7dbe 39 /* Includes */
cparata 0:89cc933f7dbe 40 #include "mbed.h"
cparata 0:89cc933f7dbe 41 #include "XNucleoIKS01A3.h"
cparata 5:672a7705147d 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 5:672a7705147d 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 5:672a7705147d 48
cparata 0:89cc933f7dbe 49 InterruptIn mybutton(USER_BUTTON);
cparata 0:89cc933f7dbe 50 DigitalOut myled(LED1);
cparata 5:672a7705147d 51
cparata 0:89cc933f7dbe 52 volatile int mems_event = 0;
cparata 0:89cc933f7dbe 53 volatile int send_orientation_request = 0;
cparata 5:672a7705147d 54
cparata 0:89cc933f7dbe 55 /* User button callback. */
cparata 5:672a7705147d 56 void pressed_cb()
cparata 5:672a7705147d 57 {
cparata 5:672a7705147d 58 send_orientation_request = 1;
cparata 0:89cc933f7dbe 59 }
cparata 5:672a7705147d 60
cparata 0:89cc933f7dbe 61 /* Interrupt 1 callback. */
cparata 5:672a7705147d 62 void int1_cb()
cparata 5:672a7705147d 63 {
cparata 5:672a7705147d 64 mems_event = 1;
cparata 0:89cc933f7dbe 65 }
cparata 5:672a7705147d 66
cparata 0:89cc933f7dbe 67 /* Print the orientation. */
cparata 5:672a7705147d 68 void send_orientation()
cparata 5:672a7705147d 69 {
cparata 5:672a7705147d 70 uint8_t xl = 0;
cparata 5:672a7705147d 71 uint8_t xh = 0;
cparata 5:672a7705147d 72 uint8_t yl = 0;
cparata 5:672a7705147d 73 uint8_t yh = 0;
cparata 5:672a7705147d 74 uint8_t zl = 0;
cparata 5:672a7705147d 75 uint8_t zh = 0;
cparata 5:672a7705147d 76
cparata 5:672a7705147d 77 acc_gyro->get_6d_orientation_xl(&xl);
cparata 5:672a7705147d 78 acc_gyro->get_6d_orientation_xh(&xh);
cparata 5:672a7705147d 79 acc_gyro->get_6d_orientation_yl(&yl);
cparata 5:672a7705147d 80 acc_gyro->get_6d_orientation_yh(&yh);
cparata 5:672a7705147d 81 acc_gyro->get_6d_orientation_zl(&zl);
cparata 5:672a7705147d 82 acc_gyro->get_6d_orientation_zh(&zh);
cparata 5:672a7705147d 83
cparata 5:672a7705147d 84 if (xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0) {
cparata 5:672a7705147d 85 printf("\r\n ________________ " \
cparata 5:672a7705147d 86 "\r\n | | " \
cparata 5:672a7705147d 87 "\r\n | * | " \
cparata 5:672a7705147d 88 "\r\n | | " \
cparata 5:672a7705147d 89 "\r\n | | " \
cparata 5:672a7705147d 90 "\r\n | | " \
cparata 5:672a7705147d 91 "\r\n | | " \
cparata 5:672a7705147d 92 "\r\n |________________| \r\n");
cparata 5:672a7705147d 93 }
cparata 5:672a7705147d 94
cparata 5:672a7705147d 95 else if (xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0) {
cparata 5:672a7705147d 96 printf("\r\n ________________ " \
cparata 5:672a7705147d 97 "\r\n | | " \
cparata 5:672a7705147d 98 "\r\n | * | " \
cparata 5:672a7705147d 99 "\r\n | | " \
cparata 5:672a7705147d 100 "\r\n | | " \
cparata 5:672a7705147d 101 "\r\n | | " \
cparata 5:672a7705147d 102 "\r\n | | " \
cparata 5:672a7705147d 103 "\r\n |________________| \r\n");
cparata 5:672a7705147d 104 }
cparata 5:672a7705147d 105
cparata 5:672a7705147d 106 else if (xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0) {
cparata 5:672a7705147d 107 printf("\r\n ________________ " \
cparata 5:672a7705147d 108 "\r\n | | " \
cparata 5:672a7705147d 109 "\r\n | | " \
cparata 5:672a7705147d 110 "\r\n | | " \
cparata 5:672a7705147d 111 "\r\n | | " \
cparata 5:672a7705147d 112 "\r\n | | " \
cparata 5:672a7705147d 113 "\r\n | * | " \
cparata 5:672a7705147d 114 "\r\n |________________| \r\n");
cparata 5:672a7705147d 115 }
cparata 5:672a7705147d 116
cparata 5:672a7705147d 117 else if (xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0) {
cparata 5:672a7705147d 118 printf("\r\n ________________ " \
cparata 5:672a7705147d 119 "\r\n | | " \
cparata 5:672a7705147d 120 "\r\n | | " \
cparata 5:672a7705147d 121 "\r\n | | " \
cparata 5:672a7705147d 122 "\r\n | | " \
cparata 5:672a7705147d 123 "\r\n | | " \
cparata 5:672a7705147d 124 "\r\n | * | " \
cparata 5:672a7705147d 125 "\r\n |________________| \r\n");
cparata 5:672a7705147d 126 }
cparata 5:672a7705147d 127
cparata 5:672a7705147d 128 else if (xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1) {
cparata 5:672a7705147d 129 printf("\r\n __*_____________ " \
cparata 5:672a7705147d 130 "\r\n |________________| \r\n");
cparata 5:672a7705147d 131 }
cparata 5:672a7705147d 132
cparata 5:672a7705147d 133 else if (xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0) {
cparata 5:672a7705147d 134 printf("\r\n ________________ " \
cparata 5:672a7705147d 135 "\r\n |________________| " \
cparata 5:672a7705147d 136 "\r\n * \r\n");
cparata 5:672a7705147d 137 }
cparata 5:672a7705147d 138
cparata 5:672a7705147d 139 else {
cparata 5:672a7705147d 140 printf("None of the 6D orientation axes is set in LSM6DSO - accelerometer.\r\n");
cparata 5:672a7705147d 141 }
cparata 0:89cc933f7dbe 142 }
cparata 5:672a7705147d 143
cparata 0:89cc933f7dbe 144 /* Simple main function */
cparata 5:672a7705147d 145 int main()
cparata 5:672a7705147d 146 {
cparata 5:672a7705147d 147 /* Attach callback to User button press */
cparata 5:672a7705147d 148 mybutton.fall(&pressed_cb);
cparata 5:672a7705147d 149 /* Attach callback to LSM6DSO INT1 */
cparata 5:672a7705147d 150 acc_gyro->attach_int1_irq(&int1_cb);
cparata 5:672a7705147d 151
cparata 5:672a7705147d 152 /* Enable LSM6DSO accelerometer */
cparata 5:672a7705147d 153 acc_gyro->enable_x();
cparata 5:672a7705147d 154 /* Enable 6D Orientation. */
cparata 5:672a7705147d 155 acc_gyro->enable_6d_orientation();
cparata 5:672a7705147d 156
cparata 5:672a7705147d 157 printf("\r\n--- Starting new run ---\r\n");
cparata 5:672a7705147d 158
cparata 5:672a7705147d 159 while (1) {
cparata 5:672a7705147d 160 if (mems_event) {
cparata 5:672a7705147d 161 mems_event = 0;
cparata 5:672a7705147d 162 LSM6DSO_Event_Status_t status;
cparata 5:672a7705147d 163 acc_gyro->get_event_status(&status);
cparata 5:672a7705147d 164 if (status.D6DOrientationStatus) {
cparata 5:672a7705147d 165 /* Send 6D Orientation */
cparata 5:672a7705147d 166 send_orientation();
cparata 5:672a7705147d 167
cparata 5:672a7705147d 168 /* Led blinking. */
cparata 5:672a7705147d 169 myled = 1;
cparata 5:672a7705147d 170 wait(0.2);
cparata 5:672a7705147d 171 myled = 0;
cparata 5:672a7705147d 172 }
cparata 5:672a7705147d 173 }
cparata 5:672a7705147d 174
cparata 5:672a7705147d 175 if (send_orientation_request) {
cparata 5:672a7705147d 176 send_orientation_request = 0;
cparata 5:672a7705147d 177 send_orientation();
cparata 5:672a7705147d 178 }
cparata 0:89cc933f7dbe 179 }
cparata 0:89cc933f7dbe 180 }