Programma di lettura sensori compreso laser
Dependencies: HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL VL53L0X
Fork of HelloWorld_ST_Sensors by
main.cpp@8:39ecd15538f9, 2017-10-04 (annotated)
- Committer:
- nikapov
- Date:
- Wed Oct 04 13:29:18 2017 +0000
- Revision:
- 8:39ecd15538f9
- Parent:
- 5:2fdca1624a33
Add VL53L0X range sensor support to B-L475E-IOT01A2.; Few minor changes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikapov | 0:6a97487a0ab3 | 1 | /** |
nikapov | 0:6a97487a0ab3 | 2 | ****************************************************************************** |
nikapov | 0:6a97487a0ab3 | 3 | * @file main.cpp |
nikapov | 0:6a97487a0ab3 | 4 | * @author CLab |
nikapov | 0:6a97487a0ab3 | 5 | * @version V1.0.0 |
nikapov | 0:6a97487a0ab3 | 6 | * @date 5-September-2017 |
nikapov | 0:6a97487a0ab3 | 7 | * @brief Simple Example application for using X_NUCLEO_IKS01A2 |
nikapov | 0:6a97487a0ab3 | 8 | * MEMS Inertial & Environmental Sensor Nucleo expansion and |
nikapov | 0:6a97487a0ab3 | 9 | * B-L475E-IOT01A2 boards. |
nikapov | 0:6a97487a0ab3 | 10 | ****************************************************************************** |
nikapov | 0:6a97487a0ab3 | 11 | * @attention |
nikapov | 0:6a97487a0ab3 | 12 | * |
nikapov | 0:6a97487a0ab3 | 13 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
nikapov | 0:6a97487a0ab3 | 14 | * |
nikapov | 0:6a97487a0ab3 | 15 | * Redistribution and use in source and binary forms, with or without modification, |
nikapov | 0:6a97487a0ab3 | 16 | * are permitted provided that the following conditions are met: |
nikapov | 0:6a97487a0ab3 | 17 | * 1. Redistributions of source code must retain the above copyright notice, |
nikapov | 0:6a97487a0ab3 | 18 | * this list of conditions and the following disclaimer. |
nikapov | 0:6a97487a0ab3 | 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
nikapov | 0:6a97487a0ab3 | 20 | * this list of conditions and the following disclaimer in the documentation |
nikapov | 0:6a97487a0ab3 | 21 | * and/or other materials provided with the distribution. |
nikapov | 0:6a97487a0ab3 | 22 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
nikapov | 0:6a97487a0ab3 | 23 | * may be used to endorse or promote products derived from this software |
nikapov | 0:6a97487a0ab3 | 24 | * without specific prior written permission. |
nikapov | 0:6a97487a0ab3 | 25 | * |
nikapov | 0:6a97487a0ab3 | 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
nikapov | 0:6a97487a0ab3 | 27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
nikapov | 0:6a97487a0ab3 | 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
nikapov | 0:6a97487a0ab3 | 29 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
nikapov | 0:6a97487a0ab3 | 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
nikapov | 0:6a97487a0ab3 | 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
nikapov | 0:6a97487a0ab3 | 32 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
nikapov | 0:6a97487a0ab3 | 33 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
nikapov | 0:6a97487a0ab3 | 34 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
nikapov | 0:6a97487a0ab3 | 35 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nikapov | 0:6a97487a0ab3 | 36 | * |
nikapov | 0:6a97487a0ab3 | 37 | ****************************************************************************** |
nikapov | 0:6a97487a0ab3 | 38 | */ |
nikapov | 0:6a97487a0ab3 | 39 | |
nikapov | 0:6a97487a0ab3 | 40 | /* Includes */ |
nikapov | 0:6a97487a0ab3 | 41 | #include "mbed.h" |
nikapov | 0:6a97487a0ab3 | 42 | #include "HTS221Sensor.h" |
nikapov | 0:6a97487a0ab3 | 43 | #include "LPS22HBSensor.h" |
nikapov | 0:6a97487a0ab3 | 44 | #include "LSM6DSLSensor.h" |
nikapov | 0:6a97487a0ab3 | 45 | |
nikapov | 0:6a97487a0ab3 | 46 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 8:39ecd15538f9 | 47 | |
nikapov | 0:6a97487a0ab3 | 48 | #include "lis3mdl_class.h" |
nikapov | 8:39ecd15538f9 | 49 | #include "VL53L0X.h" |
nikapov | 8:39ecd15538f9 | 50 | |
nikapov | 8:39ecd15538f9 | 51 | #else // Nucleo-XXX + X-Nucleo-IKS01A2 or SensorTile |
nikapov | 8:39ecd15538f9 | 52 | |
nikapov | 8:39ecd15538f9 | 53 | #ifdef TARGET_NUCLEO_L476RG |
nikapov | 8:39ecd15538f9 | 54 | #define TARGET_SENSOR_TILE // comment out to use actual NUCLEO-L476RG instead of SensorTile |
nikapov | 8:39ecd15538f9 | 55 | #endif |
nikapov | 8:39ecd15538f9 | 56 | |
nikapov | 0:6a97487a0ab3 | 57 | #include "LSM303AGRMagSensor.h" |
mapellil | 5:2fdca1624a33 | 58 | #include "LSM303AGRAccSensor.h" |
nikapov | 8:39ecd15538f9 | 59 | |
nikapov | 0:6a97487a0ab3 | 60 | #endif |
nikapov | 0:6a97487a0ab3 | 61 | |
nikapov | 0:6a97487a0ab3 | 62 | |
nikapov | 0:6a97487a0ab3 | 63 | /* Retrieve the composing elements of the expansion board */ |
nikapov | 0:6a97487a0ab3 | 64 | |
nikapov | 8:39ecd15538f9 | 65 | /* Interface definition */ |
nikapov | 0:6a97487a0ab3 | 66 | #ifdef TARGET_DISCO_L475VG_IOT01A |
mapellil | 4:a876fbe13688 | 67 | static DevI2C devI2c(PB_11,PB_10); |
mapellil | 5:2fdca1624a33 | 68 | #else // X-Nucleo-IKS01A2 or SensorTile |
mapellil | 5:2fdca1624a33 | 69 | #ifdef TARGET_SENSOR_TILE |
nikapov | 8:39ecd15538f9 | 70 | #define SPI_TYPE_LPS22HB LPS22HBSensor::SPI3W |
nikapov | 8:39ecd15538f9 | 71 | #define SPI_TYPE_LSM6DSL LSM6DSLSensor::SPI3W |
nikapov | 8:39ecd15538f9 | 72 | SPI devSPI(PB_15, NC, PB_13); // 3-wires SPI on SensorTile |
nikapov | 8:39ecd15538f9 | 73 | static Serial ser(PC_12,PD_2); // Serial with SensorTile Cradle Exp. Board + Nucleo |
mapellil | 5:2fdca1624a33 | 74 | #define printf(...) ser.printf(__VA_ARGS__) |
nikapov | 8:39ecd15538f9 | 75 | #else // Nucleo-XXX + X-Nucleo-IKS01A2 |
mapellil | 2:4d4fa82dbc59 | 76 | static DevI2C devI2c(D14,D15); |
mapellil | 4:a876fbe13688 | 77 | #endif |
mapellil | 4:a876fbe13688 | 78 | #endif |
nikapov | 0:6a97487a0ab3 | 79 | |
nikapov | 8:39ecd15538f9 | 80 | /* Environmental sensors */ |
mapellil | 5:2fdca1624a33 | 81 | #ifdef TARGET_SENSOR_TILE |
mapellil | 5:2fdca1624a33 | 82 | static LPS22HBSensor press_temp(&devSPI, PA_3, NC, SPI_TYPE_LPS22HB); |
nikapov | 8:39ecd15538f9 | 83 | #else // Nucleo-XXX + X-Nucleo-IKS01A2 or B-L475E-IOT01A2 |
mapellil | 2:4d4fa82dbc59 | 84 | static LPS22HBSensor press_temp(&devI2c); |
mapellil | 5:2fdca1624a33 | 85 | static HTS221Sensor hum_temp(&devI2c); |
mapellil | 4:a876fbe13688 | 86 | #endif |
nikapov | 0:6a97487a0ab3 | 87 | |
nikapov | 8:39ecd15538f9 | 88 | /* Motion sensors */ |
nikapov | 0:6a97487a0ab3 | 89 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 3:82e8968382d6 | 90 | static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address |
mapellil | 2:4d4fa82dbc59 | 91 | static LIS3MDL magnetometer(&devI2c); |
nikapov | 8:39ecd15538f9 | 92 | #else // X-NUCLEO-IKS01A2 or SensorTile |
mapellil | 5:2fdca1624a33 | 93 | #if defined (TARGET_SENSOR_TILE) |
mapellil | 5:2fdca1624a33 | 94 | static LSM6DSLSensor acc_gyro(&devSPI,PB_12, NC, PA_2, SPI_TYPE_LSM6DSL); |
mapellil | 5:2fdca1624a33 | 95 | static LSM303AGRMagSensor magnetometer(&devSPI, PB_1); |
mapellil | 5:2fdca1624a33 | 96 | static LSM303AGRAccSensor accelerometer(&devSPI, PC_4); |
mapellil | 5:2fdca1624a33 | 97 | #else |
nikapov | 3:82e8968382d6 | 98 | static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH,D4,D5); // high address |
mapellil | 2:4d4fa82dbc59 | 99 | static LSM303AGRMagSensor magnetometer(&devI2c); |
mapellil | 5:2fdca1624a33 | 100 | static LSM303AGRAccSensor accelerometer(&devI2c); |
nikapov | 0:6a97487a0ab3 | 101 | #endif |
mapellil | 5:2fdca1624a33 | 102 | #endif |
nikapov | 0:6a97487a0ab3 | 103 | |
nikapov | 8:39ecd15538f9 | 104 | /* Range sensor - B-L475E-IOT01A2 only */ |
nikapov | 8:39ecd15538f9 | 105 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 8:39ecd15538f9 | 106 | static DigitalOut shutdown_pin(PC_6); |
nikapov | 8:39ecd15538f9 | 107 | static VL53L0X range(&devI2c, &shutdown_pin, PC_7); |
nikapov | 8:39ecd15538f9 | 108 | #endif |
nikapov | 8:39ecd15538f9 | 109 | |
nikapov | 0:6a97487a0ab3 | 110 | /* Simple main function */ |
nikapov | 0:6a97487a0ab3 | 111 | int main() { |
nikapov | 0:6a97487a0ab3 | 112 | uint8_t id; |
nikapov | 0:6a97487a0ab3 | 113 | float value1, value2; |
mapellil | 5:2fdca1624a33 | 114 | // char buffer1[32], buffer2[32]; |
nikapov | 0:6a97487a0ab3 | 115 | int32_t axes[3]; |
nikapov | 0:6a97487a0ab3 | 116 | |
nikapov | 0:6a97487a0ab3 | 117 | /* Init all sensors with default params */ |
mapellil | 5:2fdca1624a33 | 118 | #ifndef TARGET_SENSOR_TILE |
nikapov | 0:6a97487a0ab3 | 119 | hum_temp.init(NULL); |
mapellil | 5:2fdca1624a33 | 120 | #endif |
nikapov | 8:39ecd15538f9 | 121 | |
nikapov | 0:6a97487a0ab3 | 122 | press_temp.init(NULL); |
nikapov | 0:6a97487a0ab3 | 123 | magnetometer.init(NULL); |
nikapov | 8:39ecd15538f9 | 124 | acc_gyro.init(NULL); |
nikapov | 8:39ecd15538f9 | 125 | |
nikapov | 8:39ecd15538f9 | 126 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 8:39ecd15538f9 | 127 | range.init_sensor(VL53L0X_DEFAULT_ADDRESS); |
nikapov | 8:39ecd15538f9 | 128 | #else // X-NUCLEO-IKS01A2 or SensorTile |
mapellil | 5:2fdca1624a33 | 129 | accelerometer.init(NULL); |
mapellil | 5:2fdca1624a33 | 130 | #endif |
nikapov | 8:39ecd15538f9 | 131 | |
nikapov | 8:39ecd15538f9 | 132 | |
nikapov | 0:6a97487a0ab3 | 133 | |
nikapov | 0:6a97487a0ab3 | 134 | /* Enable all sensors */ |
mapellil | 5:2fdca1624a33 | 135 | #ifndef TARGET_SENSOR_TILE |
nikapov | 0:6a97487a0ab3 | 136 | hum_temp.enable(); |
mapellil | 5:2fdca1624a33 | 137 | #endif |
nikapov | 0:6a97487a0ab3 | 138 | press_temp.enable(); |
nikapov | 0:6a97487a0ab3 | 139 | #ifndef TARGET_DISCO_L475VG_IOT01A // X-NUCLEO-IKS01A2 |
nikapov | 0:6a97487a0ab3 | 140 | magnetometer.enable(); |
mapellil | 5:2fdca1624a33 | 141 | accelerometer.enable(); |
nikapov | 0:6a97487a0ab3 | 142 | #endif |
nikapov | 0:6a97487a0ab3 | 143 | acc_gyro.enable_x(); |
nikapov | 0:6a97487a0ab3 | 144 | acc_gyro.enable_g(); |
nikapov | 0:6a97487a0ab3 | 145 | |
nikapov | 1:0f7838527fef | 146 | printf("\033[2J\033[20A"); |
nikapov | 1:0f7838527fef | 147 | printf ("\r\n--- Starting new run ---\r\n\r\n"); |
nikapov | 0:6a97487a0ab3 | 148 | |
mapellil | 5:2fdca1624a33 | 149 | #ifndef TARGET_SENSOR_TILE |
nikapov | 0:6a97487a0ab3 | 150 | hum_temp.read_id(&id); |
nikapov | 0:6a97487a0ab3 | 151 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
mapellil | 5:2fdca1624a33 | 152 | #endif |
nikapov | 0:6a97487a0ab3 | 153 | press_temp.read_id(&id); |
mapellil | 5:2fdca1624a33 | 154 | printf("LPS22HB pressure & temperature = 0x%X\r\n", id); |
nikapov | 0:6a97487a0ab3 | 155 | magnetometer.read_id(&id); |
nikapov | 0:6a97487a0ab3 | 156 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 1:0f7838527fef | 157 | printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
nikapov | 8:39ecd15538f9 | 158 | #else // X-NUCLEO-IKS01A2 or SensorTile |
nikapov | 0:6a97487a0ab3 | 159 | printf("LSM303AGR magnetometer = 0x%X\r\n", id); |
mapellil | 5:2fdca1624a33 | 160 | accelerometer.read_id(&id); |
mapellil | 5:2fdca1624a33 | 161 | printf("LSM303AGR accelerometer = 0x%X\r\n", id); |
nikapov | 0:6a97487a0ab3 | 162 | #endif |
nikapov | 0:6a97487a0ab3 | 163 | acc_gyro.read_id(&id); |
nikapov | 0:6a97487a0ab3 | 164 | printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); |
nikapov | 1:0f7838527fef | 165 | |
nikapov | 1:0f7838527fef | 166 | printf("\n\r--- Reading sensor values ---\n\r"); ; |
nikapov | 0:6a97487a0ab3 | 167 | |
nikapov | 0:6a97487a0ab3 | 168 | while(1) { |
nikapov | 0:6a97487a0ab3 | 169 | printf("\r\n"); |
nikapov | 0:6a97487a0ab3 | 170 | |
mapellil | 5:2fdca1624a33 | 171 | #ifndef TARGET_SENSOR_TILE |
nikapov | 0:6a97487a0ab3 | 172 | hum_temp.get_temperature(&value1); |
nikapov | 0:6a97487a0ab3 | 173 | hum_temp.get_humidity(&value2); |
nikapov | 8:39ecd15538f9 | 174 | printf("HTS221: [temp] %.2f C, [hum] %.2f%%\r\n", value1, value2); |
mapellil | 5:2fdca1624a33 | 175 | #endif |
mapellil | 4:a876fbe13688 | 176 | value1=value2=0; |
nikapov | 0:6a97487a0ab3 | 177 | press_temp.get_temperature(&value1); |
nikapov | 0:6a97487a0ab3 | 178 | press_temp.get_pressure(&value2); |
nikapov | 8:39ecd15538f9 | 179 | printf("LPS22HB: [temp] %.2f C, [press] %.2f mbar\r\n", value1, value2); |
nikapov | 0:6a97487a0ab3 | 180 | |
nikapov | 0:6a97487a0ab3 | 181 | printf("---\r\n"); |
nikapov | 0:6a97487a0ab3 | 182 | |
nikapov | 0:6a97487a0ab3 | 183 | magnetometer.get_m_axes(axes); |
nikapov | 0:6a97487a0ab3 | 184 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 1:0f7838527fef | 185 | printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 8:39ecd15538f9 | 186 | #else // X-NUCLEO-IKS01A2 or SensorTile |
nikapov | 0:6a97487a0ab3 | 187 | printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
mapellil | 5:2fdca1624a33 | 188 | accelerometer.get_x_axes(axes); |
mapellil | 5:2fdca1624a33 | 189 | printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 190 | #endif |
nikapov | 0:6a97487a0ab3 | 191 | |
nikapov | 0:6a97487a0ab3 | 192 | acc_gyro.get_x_axes(axes); |
nikapov | 1:0f7838527fef | 193 | printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 194 | |
nikapov | 0:6a97487a0ab3 | 195 | acc_gyro.get_g_axes(axes); |
nikapov | 1:0f7838527fef | 196 | printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 197 | |
nikapov | 8:39ecd15538f9 | 198 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 8:39ecd15538f9 | 199 | uint32_t distance; |
nikapov | 8:39ecd15538f9 | 200 | int status = range.get_distance(&distance); |
nikapov | 8:39ecd15538f9 | 201 | if (status == VL53L0X_ERROR_NONE) { |
nikapov | 8:39ecd15538f9 | 202 | printf("VL53L0X [mm]: %6ld\r\n", distance); |
nikapov | 8:39ecd15538f9 | 203 | } else { |
nikapov | 8:39ecd15538f9 | 204 | printf("VL53L0X [mm]: --\r\n"); |
nikapov | 8:39ecd15538f9 | 205 | } |
nikapov | 8:39ecd15538f9 | 206 | #endif |
nikapov | 8:39ecd15538f9 | 207 | |
nikapov | 8:39ecd15538f9 | 208 | #if defined (TARGET_SENSOR_TILE) |
nikapov | 1:0f7838527fef | 209 | printf("\033[7A"); |
mapellil | 5:2fdca1624a33 | 210 | #else |
mapellil | 5:2fdca1624a33 | 211 | printf("\033[8A"); |
mapellil | 5:2fdca1624a33 | 212 | #endif |
nikapov | 8:39ecd15538f9 | 213 | wait(0.5); |
nikapov | 0:6a97487a0ab3 | 214 | } |
nikapov | 0:6a97487a0ab3 | 215 | } |