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