|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file LSM6DSO32_6DOrientation.ino |
| 4 | + * @author Nathan Garnier, based on LSM6DSO lib. |
| 5 | + * @version V1.0.0 |
| 6 | + * @date March 2026 |
| 7 | + * @brief Arduino test application for the STMicrolectronics |
| 8 | + * LSM6DSO32 MEMS IMU 6-axis sensor. |
| 9 | + * This application makes use of C++ classes obtained from the C |
| 10 | + * components' drivers. |
| 11 | + ****************************************************************************** |
| 12 | + */ |
| 13 | + |
| 14 | +#include <LSM6DSO32Sensor.h> |
| 15 | + |
| 16 | +#ifdef ARDUINO_SAM_DUE |
| 17 | +#define DEV_I2C Wire1 |
| 18 | +#elif defined(ARDUINO_ARCH_STM32) |
| 19 | +#define DEV_I2C Wire |
| 20 | +#elif defined(ARDUINO_ARCH_AVR) |
| 21 | +#define DEV_I2C Wire |
| 22 | +#else |
| 23 | +#define DEV_I2C Wire |
| 24 | +#endif |
| 25 | +#define SerialPort Serial |
| 26 | + |
| 27 | +#define IMU_INT_1 4 |
| 28 | + |
| 29 | +LSM6DSO32Sensor accGyr(&DEV_I2C); |
| 30 | + |
| 31 | +//Interrupts. |
| 32 | +volatile int mems_event = 0; |
| 33 | + |
| 34 | +char report[256]; |
| 35 | + |
| 36 | +void INT1Event_cb(); |
| 37 | +void sendOrientation(); |
| 38 | + |
| 39 | +void setup() { |
| 40 | + // Led. |
| 41 | + pinMode(LED_BUILTIN, OUTPUT); |
| 42 | + // Initialize serial for output. |
| 43 | + SerialPort.begin(115200); |
| 44 | + |
| 45 | + // Initialize I2C bus. |
| 46 | + DEV_I2C.begin(); |
| 47 | + |
| 48 | + //Int1 pin input |
| 49 | + pinMode(IMU_INT_1, INPUT); |
| 50 | + |
| 51 | + accGyr.begin(); |
| 52 | + accGyr.Enable_X(); |
| 53 | + accGyr.Enable_6D_Orientation(LSM6DSO32_INT1_PIN); |
| 54 | +} |
| 55 | + |
| 56 | +void loop() { |
| 57 | + |
| 58 | + int int_val = digitalRead(IMU_INT_1); |
| 59 | + if(int_val && !mems_event) |
| 60 | + { |
| 61 | + mems_event = 1; |
| 62 | + } |
| 63 | + |
| 64 | + if (mems_event) |
| 65 | + { |
| 66 | + mems_event=0; |
| 67 | + LSM6DSO32_Event_Status_t status; |
| 68 | + accGyr.Get_X_Event_Status(&status); |
| 69 | + if (status.D6DOrientationStatus) |
| 70 | + { |
| 71 | + sendOrientation(); |
| 72 | + // Led blinking. |
| 73 | + digitalWrite(LED_BUILTIN, HIGH); |
| 74 | + delay(100); |
| 75 | + digitalWrite(LED_BUILTIN, LOW); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void sendOrientation() |
| 81 | +{ |
| 82 | + uint8_t xl = 0; |
| 83 | + uint8_t xh = 0; |
| 84 | + uint8_t yl = 0; |
| 85 | + uint8_t yh = 0; |
| 86 | + uint8_t zl = 0; |
| 87 | + uint8_t zh = 0; |
| 88 | + |
| 89 | + accGyr.Get_6D_Orientation_XL(&xl); |
| 90 | + accGyr.Get_6D_Orientation_XH(&xh); |
| 91 | + accGyr.Get_6D_Orientation_YL(&yl); |
| 92 | + accGyr.Get_6D_Orientation_YH(&yh); |
| 93 | + accGyr.Get_6D_Orientation_ZL(&zl); |
| 94 | + accGyr.Get_6D_Orientation_ZH(&zh); |
| 95 | + |
| 96 | + if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 ) |
| 97 | + { |
| 98 | + sprintf( report, "\r\n ________________ " \ |
| 99 | + "\r\n | | " \ |
| 100 | + "\r\n | * | " \ |
| 101 | + "\r\n | | " \ |
| 102 | + "\r\n | | " \ |
| 103 | + "\r\n | | " \ |
| 104 | + "\r\n | | " \ |
| 105 | + "\r\n |________________| \r\n" ); |
| 106 | + } |
| 107 | + |
| 108 | + else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 ) |
| 109 | + { |
| 110 | + sprintf( report, "\r\n ________________ " \ |
| 111 | + "\r\n | | " \ |
| 112 | + "\r\n | * | " \ |
| 113 | + "\r\n | | " \ |
| 114 | + "\r\n | | " \ |
| 115 | + "\r\n | | " \ |
| 116 | + "\r\n | | " \ |
| 117 | + "\r\n |________________| \r\n" ); |
| 118 | + } |
| 119 | + |
| 120 | + else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 ) |
| 121 | + { |
| 122 | + sprintf( report, "\r\n ________________ " \ |
| 123 | + "\r\n | | " \ |
| 124 | + "\r\n | | " \ |
| 125 | + "\r\n | | " \ |
| 126 | + "\r\n | | " \ |
| 127 | + "\r\n | | " \ |
| 128 | + "\r\n | * | " \ |
| 129 | + "\r\n |________________| \r\n" ); |
| 130 | + } |
| 131 | + |
| 132 | + else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 ) |
| 133 | + { |
| 134 | + sprintf( report, "\r\n ________________ " \ |
| 135 | + "\r\n | | " \ |
| 136 | + "\r\n | | " \ |
| 137 | + "\r\n | | " \ |
| 138 | + "\r\n | | " \ |
| 139 | + "\r\n | | " \ |
| 140 | + "\r\n | * | " \ |
| 141 | + "\r\n |________________| \r\n" ); |
| 142 | + } |
| 143 | + |
| 144 | + else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 ) |
| 145 | + { |
| 146 | + sprintf( report, "\r\n __*_____________ " \ |
| 147 | + "\r\n |________________| \r\n" ); |
| 148 | + } |
| 149 | + |
| 150 | + else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 ) |
| 151 | + { |
| 152 | + sprintf( report, "\r\n ________________ " \ |
| 153 | + "\r\n |________________| " \ |
| 154 | + "\r\n * \r\n" ); |
| 155 | + } |
| 156 | + |
| 157 | + else |
| 158 | + { |
| 159 | + sprintf( report, "None of the 6D orientation axes is set in LSM6DSO - accelerometer.\r\n" ); |
| 160 | + } |
| 161 | + |
| 162 | + SerialPort.print(report); |
| 163 | +} |
0 commit comments