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:
Thu Jun 04 16:50:16 2020 +0000
Revision:
6:dee80d1952bc
Parent:
5:672a7705147d
Child:
8:29f0c9c80d94
Update the code to be compatible with mbed OS 6

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 6:dee80d1952bc 41 #include "rtos.h"
cparata 0:89cc933f7dbe 42 #include "XNucleoIKS01A3.h"
cparata 5:672a7705147d 43
cparata 0:89cc933f7dbe 44 /* Instantiate the expansion board */
cparata 0:89cc933f7dbe 45 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
cparata 5:672a7705147d 46
cparata 0:89cc933f7dbe 47 /* Retrieve the composing elements of the expansion board */
cparata 0:89cc933f7dbe 48 static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro;
cparata 5:672a7705147d 49
cparata 0:89cc933f7dbe 50 InterruptIn mybutton(USER_BUTTON);
cparata 0:89cc933f7dbe 51 DigitalOut myled(LED1);
cparata 5:672a7705147d 52
cparata 0:89cc933f7dbe 53 volatile int mems_event = 0;
cparata 0:89cc933f7dbe 54 volatile int send_orientation_request = 0;
cparata 5:672a7705147d 55
cparata 0:89cc933f7dbe 56 /* User button callback. */
cparata 5:672a7705147d 57 void pressed_cb()
cparata 5:672a7705147d 58 {
cparata 5:672a7705147d 59 send_orientation_request = 1;
cparata 0:89cc933f7dbe 60 }
cparata 5:672a7705147d 61
cparata 0:89cc933f7dbe 62 /* Interrupt 1 callback. */
cparata 5:672a7705147d 63 void int1_cb()
cparata 5:672a7705147d 64 {
cparata 5:672a7705147d 65 mems_event = 1;
cparata 0:89cc933f7dbe 66 }
cparata 5:672a7705147d 67
cparata 0:89cc933f7dbe 68 /* Print the orientation. */
cparata 5:672a7705147d 69 void send_orientation()
cparata 5:672a7705147d 70 {
cparata 5:672a7705147d 71 uint8_t xl = 0;
cparata 5:672a7705147d 72 uint8_t xh = 0;
cparata 5:672a7705147d 73 uint8_t yl = 0;
cparata 5:672a7705147d 74 uint8_t yh = 0;
cparata 5:672a7705147d 75 uint8_t zl = 0;
cparata 5:672a7705147d 76 uint8_t zh = 0;
cparata 5:672a7705147d 77
cparata 5:672a7705147d 78 acc_gyro->get_6d_orientation_xl(&xl);
cparata 5:672a7705147d 79 acc_gyro->get_6d_orientation_xh(&xh);
cparata 5:672a7705147d 80 acc_gyro->get_6d_orientation_yl(&yl);
cparata 5:672a7705147d 81 acc_gyro->get_6d_orientation_yh(&yh);
cparata 5:672a7705147d 82 acc_gyro->get_6d_orientation_zl(&zl);
cparata 5:672a7705147d 83 acc_gyro->get_6d_orientation_zh(&zh);
cparata 5:672a7705147d 84
cparata 5:672a7705147d 85 if (xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0) {
cparata 5:672a7705147d 86 printf("\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 | | " \
cparata 5:672a7705147d 93 "\r\n |________________| \r\n");
cparata 5:672a7705147d 94 }
cparata 5:672a7705147d 95
cparata 5:672a7705147d 96 else if (xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0) {
cparata 5:672a7705147d 97 printf("\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 | | " \
cparata 5:672a7705147d 104 "\r\n |________________| \r\n");
cparata 5:672a7705147d 105 }
cparata 5:672a7705147d 106
cparata 5:672a7705147d 107 else if (xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0) {
cparata 5:672a7705147d 108 printf("\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 | * | " \
cparata 5:672a7705147d 115 "\r\n |________________| \r\n");
cparata 5:672a7705147d 116 }
cparata 5:672a7705147d 117
cparata 5:672a7705147d 118 else if (xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0) {
cparata 5:672a7705147d 119 printf("\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 | * | " \
cparata 5:672a7705147d 126 "\r\n |________________| \r\n");
cparata 5:672a7705147d 127 }
cparata 5:672a7705147d 128
cparata 5:672a7705147d 129 else if (xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1) {
cparata 5:672a7705147d 130 printf("\r\n __*_____________ " \
cparata 5:672a7705147d 131 "\r\n |________________| \r\n");
cparata 5:672a7705147d 132 }
cparata 5:672a7705147d 133
cparata 5:672a7705147d 134 else if (xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0) {
cparata 5:672a7705147d 135 printf("\r\n ________________ " \
cparata 5:672a7705147d 136 "\r\n |________________| " \
cparata 5:672a7705147d 137 "\r\n * \r\n");
cparata 5:672a7705147d 138 }
cparata 5:672a7705147d 139
cparata 5:672a7705147d 140 else {
cparata 5:672a7705147d 141 printf("None of the 6D orientation axes is set in LSM6DSO - accelerometer.\r\n");
cparata 5:672a7705147d 142 }
cparata 0:89cc933f7dbe 143 }
cparata 5:672a7705147d 144
cparata 0:89cc933f7dbe 145 /* Simple main function */
cparata 5:672a7705147d 146 int main()
cparata 5:672a7705147d 147 {
cparata 5:672a7705147d 148 /* Attach callback to User button press */
cparata 5:672a7705147d 149 mybutton.fall(&pressed_cb);
cparata 5:672a7705147d 150 /* Attach callback to LSM6DSO INT1 */
cparata 5:672a7705147d 151 acc_gyro->attach_int1_irq(&int1_cb);
cparata 5:672a7705147d 152
cparata 5:672a7705147d 153 /* Enable LSM6DSO accelerometer */
cparata 5:672a7705147d 154 acc_gyro->enable_x();
cparata 5:672a7705147d 155 /* Enable 6D Orientation. */
cparata 5:672a7705147d 156 acc_gyro->enable_6d_orientation();
cparata 5:672a7705147d 157
cparata 5:672a7705147d 158 printf("\r\n--- Starting new run ---\r\n");
cparata 5:672a7705147d 159
cparata 5:672a7705147d 160 while (1) {
cparata 5:672a7705147d 161 if (mems_event) {
cparata 5:672a7705147d 162 mems_event = 0;
cparata 5:672a7705147d 163 LSM6DSO_Event_Status_t status;
cparata 5:672a7705147d 164 acc_gyro->get_event_status(&status);
cparata 5:672a7705147d 165 if (status.D6DOrientationStatus) {
cparata 5:672a7705147d 166 /* Send 6D Orientation */
cparata 5:672a7705147d 167 send_orientation();
cparata 5:672a7705147d 168
cparata 5:672a7705147d 169 /* Led blinking. */
cparata 5:672a7705147d 170 myled = 1;
cparata 6:dee80d1952bc 171 ThisThread::sleep_for(200);
cparata 5:672a7705147d 172 myled = 0;
cparata 5:672a7705147d 173 }
cparata 5:672a7705147d 174 }
cparata 5:672a7705147d 175
cparata 5:672a7705147d 176 if (send_orientation_request) {
cparata 5:672a7705147d 177 send_orientation_request = 0;
cparata 5:672a7705147d 178 send_orientation();
cparata 5:672a7705147d 179 }
cparata 0:89cc933f7dbe 180 }
cparata 0:89cc933f7dbe 181 }