example using LOF sensor board and MEMS sensor board
Dependencies: ST_INTERFACES X_NUCLEO_53L0A1 X_NUCLEO_COMMON X_NUCLEO_IKS01A1 Senet_Packet libmDot-mbed5
main.cpp@1:a2c5a8a74527, 2017-04-08 (annotated)
- Committer:
- Mike Fiore
- Date:
- Sat Apr 08 08:10:19 2017 -0400
- Revision:
- 1:a2c5a8a74527
- Parent:
- 0:06fc5a68ead2
- Child:
- 2:a0fb0b785bd6
finish custom printf. add mutex to protect sensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:06fc5a68ead2 | 1 | #include "mbed.h" |
mfiore | 0:06fc5a68ead2 | 2 | #include "rtos.h" |
mfiore | 0:06fc5a68ead2 | 3 | #include "x_nucleo_iks01a1.h" |
mfiore | 0:06fc5a68ead2 | 4 | #include "x_nucleo_53l0a1.h" |
mfiore | 0:06fc5a68ead2 | 5 | |
mfiore | 0:06fc5a68ead2 | 6 | Thread mems_thread; |
mfiore | 0:06fc5a68ead2 | 7 | Thread range_thread; |
mfiore | 0:06fc5a68ead2 | 8 | Mutex serial_mutex; |
Mike Fiore |
1:a2c5a8a74527 | 9 | //Mutex sensor_mutex; |
mfiore | 0:06fc5a68ead2 | 10 | |
mfiore | 0:06fc5a68ead2 | 11 | DigitalOut led(D3); |
mfiore | 0:06fc5a68ead2 | 12 | |
mfiore | 0:06fc5a68ead2 | 13 | DevI2C i2c(D14, D15); |
mfiore | 0:06fc5a68ead2 | 14 | |
mfiore | 0:06fc5a68ead2 | 15 | /* Instantiate the expansion board */ |
mfiore | 0:06fc5a68ead2 | 16 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(&i2c); |
mfiore | 0:06fc5a68ead2 | 17 | // MAKE SURE YOU JUMPER PIN D7 TO PIN D8! |
Mike Fiore |
1:a2c5a8a74527 | 18 | static X_NUCLEO_53L0A1 *range_board = X_NUCLEO_53L0A1::Instance(&i2c, A2, D7, D2); |
mfiore | 0:06fc5a68ead2 | 19 | |
mfiore | 0:06fc5a68ead2 | 20 | /* Retrieve the composing elements of the expansion board */ |
mfiore | 0:06fc5a68ead2 | 21 | static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope(); |
mfiore | 0:06fc5a68ead2 | 22 | static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer(); |
mfiore | 0:06fc5a68ead2 | 23 | static MagneticSensor *magnetometer = mems_expansion_board->magnetometer; |
mfiore | 0:06fc5a68ead2 | 24 | static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; |
mfiore | 0:06fc5a68ead2 | 25 | static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; |
mfiore | 0:06fc5a68ead2 | 26 | static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; |
mfiore | 0:06fc5a68ead2 | 27 | static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; |
mfiore | 0:06fc5a68ead2 | 28 | |
mfiore | 0:06fc5a68ead2 | 29 | /* Helper function for printing from multiple threads */ |
Mike Fiore |
1:a2c5a8a74527 | 30 | void ts_printf(const char* format, ...) { |
mfiore | 0:06fc5a68ead2 | 31 | serial_mutex.lock(); |
Mike Fiore |
1:a2c5a8a74527 | 32 | va_list args; |
Mike Fiore |
1:a2c5a8a74527 | 33 | va_start(args, format); |
Mike Fiore |
1:a2c5a8a74527 | 34 | printf(format, args); |
mfiore | 0:06fc5a68ead2 | 35 | serial_mutex.unlock(); |
mfiore | 0:06fc5a68ead2 | 36 | } |
mfiore | 0:06fc5a68ead2 | 37 | |
mfiore | 0:06fc5a68ead2 | 38 | /* Helper function for printing floats & doubles */ |
mfiore | 0:06fc5a68ead2 | 39 | static char *printDouble(char* str, double v, int decimalDigits=2) { |
mfiore | 0:06fc5a68ead2 | 40 | int i = 1; |
mfiore | 0:06fc5a68ead2 | 41 | int intPart, fractPart; |
mfiore | 0:06fc5a68ead2 | 42 | int len; |
mfiore | 0:06fc5a68ead2 | 43 | char *ptr; |
mfiore | 0:06fc5a68ead2 | 44 | |
mfiore | 0:06fc5a68ead2 | 45 | /* prepare decimal digits multiplicator */ |
mfiore | 0:06fc5a68ead2 | 46 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
mfiore | 0:06fc5a68ead2 | 47 | |
mfiore | 0:06fc5a68ead2 | 48 | /* calculate integer & fractinal parts */ |
mfiore | 0:06fc5a68ead2 | 49 | intPart = (int)v; |
mfiore | 0:06fc5a68ead2 | 50 | fractPart = (int)((v-(double)(int)v)*i); |
mfiore | 0:06fc5a68ead2 | 51 | |
mfiore | 0:06fc5a68ead2 | 52 | /* fill in integer part */ |
mfiore | 0:06fc5a68ead2 | 53 | sprintf(str, "%i.", intPart); |
mfiore | 0:06fc5a68ead2 | 54 | |
mfiore | 0:06fc5a68ead2 | 55 | /* prepare fill in of fractional part */ |
mfiore | 0:06fc5a68ead2 | 56 | len = strlen(str); |
mfiore | 0:06fc5a68ead2 | 57 | ptr = &str[len]; |
mfiore | 0:06fc5a68ead2 | 58 | |
mfiore | 0:06fc5a68ead2 | 59 | /* fill in leading fractional zeros */ |
mfiore | 0:06fc5a68ead2 | 60 | for (i/=10;i>1; i/=10, ptr++) { |
mfiore | 0:06fc5a68ead2 | 61 | if(fractPart >= i) break; |
mfiore | 0:06fc5a68ead2 | 62 | *ptr = '0'; |
mfiore | 0:06fc5a68ead2 | 63 | } |
mfiore | 0:06fc5a68ead2 | 64 | |
mfiore | 0:06fc5a68ead2 | 65 | /* fill in (rest of) fractional part */ |
mfiore | 0:06fc5a68ead2 | 66 | sprintf(ptr, "%i", fractPart); |
mfiore | 0:06fc5a68ead2 | 67 | |
mfiore | 0:06fc5a68ead2 | 68 | return str; |
mfiore | 0:06fc5a68ead2 | 69 | } |
mfiore | 0:06fc5a68ead2 | 70 | |
mfiore | 0:06fc5a68ead2 | 71 | /* Function to run the mems board */ |
mfiore | 0:06fc5a68ead2 | 72 | void mems_func() { |
mfiore | 0:06fc5a68ead2 | 73 | uint8_t id; |
mfiore | 0:06fc5a68ead2 | 74 | float value1, value2; |
mfiore | 0:06fc5a68ead2 | 75 | char buffer1[32], buffer2[32]; |
mfiore | 0:06fc5a68ead2 | 76 | int32_t axes[3]; |
Mike Fiore |
1:a2c5a8a74527 | 77 | int ret; |
mfiore | 0:06fc5a68ead2 | 78 | |
mfiore | 0:06fc5a68ead2 | 79 | ts_printf("\r\n--- Starting mems board ---\r\n"); |
mfiore | 0:06fc5a68ead2 | 80 | |
Mike Fiore |
1:a2c5a8a74527 | 81 | //sensor_mutex.lock(); |
mfiore | 0:06fc5a68ead2 | 82 | humidity_sensor->read_id(&id); |
Mike Fiore |
1:a2c5a8a74527 | 83 | //sensor_mutex.unlock(); |
mfiore | 0:06fc5a68ead2 | 84 | ts_printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
Mike Fiore |
1:a2c5a8a74527 | 85 | |
Mike Fiore |
1:a2c5a8a74527 | 86 | //sensor_mutex.lock(); |
mfiore | 0:06fc5a68ead2 | 87 | pressure_sensor->read_id(&id); |
Mike Fiore |
1:a2c5a8a74527 | 88 | //sensor_mutex.unlock(); |
mfiore | 0:06fc5a68ead2 | 89 | ts_printf("LPS25H pressure & temperature = 0x%X\r\n", id); |
Mike Fiore |
1:a2c5a8a74527 | 90 | |
Mike Fiore |
1:a2c5a8a74527 | 91 | //sensor_mutex.lock(); |
mfiore | 0:06fc5a68ead2 | 92 | magnetometer->read_id(&id); |
Mike Fiore |
1:a2c5a8a74527 | 93 | //sensor_mutex.unlock(); |
mfiore | 0:06fc5a68ead2 | 94 | ts_printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
Mike Fiore |
1:a2c5a8a74527 | 95 | |
Mike Fiore |
1:a2c5a8a74527 | 96 | //sensor_mutex.lock(); |
mfiore | 0:06fc5a68ead2 | 97 | gyroscope->read_id(&id); |
Mike Fiore |
1:a2c5a8a74527 | 98 | //sensor_mutex.unlock(); |
mfiore | 0:06fc5a68ead2 | 99 | ts_printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id); |
mfiore | 0:06fc5a68ead2 | 100 | |
mfiore | 0:06fc5a68ead2 | 101 | wait(3); |
mfiore | 0:06fc5a68ead2 | 102 | |
mfiore | 0:06fc5a68ead2 | 103 | while (true) { |
Mike Fiore |
1:a2c5a8a74527 | 104 | //sensor_mutex.lock(); |
Mike Fiore |
1:a2c5a8a74527 | 105 | ret = temp_sensor1->get_temperature(&value1); |
Mike Fiore |
1:a2c5a8a74527 | 106 | //sensor_mutex.unlock(); |
Mike Fiore |
1:a2c5a8a74527 | 107 | if (ret) { |
Mike Fiore |
1:a2c5a8a74527 | 108 | ts_printf("failed to get temp C\r\n"); |
Mike Fiore |
1:a2c5a8a74527 | 109 | } |
Mike Fiore |
1:a2c5a8a74527 | 110 | //sensor_mutex.lock(); |
Mike Fiore |
1:a2c5a8a74527 | 111 | ret = humidity_sensor->get_humidity(&value2); |
Mike Fiore |
1:a2c5a8a74527 | 112 | //sensor_mutex.unlock(); |
Mike Fiore |
1:a2c5a8a74527 | 113 | if (ret) { |
Mike Fiore |
1:a2c5a8a74527 | 114 | ts_printf("failed to get humidity\r\n"); |
Mike Fiore |
1:a2c5a8a74527 | 115 | } |
Mike Fiore |
1:a2c5a8a74527 | 116 | //ts_printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
Mike Fiore |
1:a2c5a8a74527 | 117 | ts_printf("HTS221: [temp] %f°C\r\n", value1); |
Mike Fiore |
1:a2c5a8a74527 | 118 | ts_printf("HTS221: [hum] %f%%\r\n", value2); |
mfiore | 0:06fc5a68ead2 | 119 | |
Mike Fiore |
1:a2c5a8a74527 | 120 | //sensor_mutex.lock(); |
Mike Fiore |
1:a2c5a8a74527 | 121 | ret = temp_sensor2->get_fahrenheit(&value1); |
Mike Fiore |
1:a2c5a8a74527 | 122 | //sensor_mutex.unlock(); |
Mike Fiore |
1:a2c5a8a74527 | 123 | if (ret) { |
Mike Fiore |
1:a2c5a8a74527 | 124 | ts_printf("failed to get temp F\r\n"); |
Mike Fiore |
1:a2c5a8a74527 | 125 | } |
Mike Fiore |
1:a2c5a8a74527 | 126 | //sensor_mutex.lock(); |
Mike Fiore |
1:a2c5a8a74527 | 127 | ret = pressure_sensor->get_pressure(&value2); |
Mike Fiore |
1:a2c5a8a74527 | 128 | //sensor_mutex.unlock(); |
Mike Fiore |
1:a2c5a8a74527 | 129 | if (ret) { |
Mike Fiore |
1:a2c5a8a74527 | 130 | ts_printf("failed to get pressure F\r\n"); |
Mike Fiore |
1:a2c5a8a74527 | 131 | } |
Mike Fiore |
1:a2c5a8a74527 | 132 | //ts_printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
Mike Fiore |
1:a2c5a8a74527 | 133 | ts_printf("LPS25H: [temp] %f°F\r\n", value1); |
Mike Fiore |
1:a2c5a8a74527 | 134 | ts_printf("LPS25H: [press] %fmbar\r\n", value2); |
mfiore | 0:06fc5a68ead2 | 135 | |
Mike Fiore |
1:a2c5a8a74527 | 136 | //sensor_mutex.lock(); |
Mike Fiore |
1:a2c5a8a74527 | 137 | ret = magnetometer->get_m_axes(axes); |
Mike Fiore |
1:a2c5a8a74527 | 138 | //sensor_mutex.unlock(); |
Mike Fiore |
1:a2c5a8a74527 | 139 | if (ret) { |
Mike Fiore |
1:a2c5a8a74527 | 140 | ts_printf("failed to get magnetometer\r\n"); |
Mike Fiore |
1:a2c5a8a74527 | 141 | } |
mfiore | 0:06fc5a68ead2 | 142 | ts_printf("LIS3MDL [mag/mgauss]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
mfiore | 0:06fc5a68ead2 | 143 | |
Mike Fiore |
1:a2c5a8a74527 | 144 | //sensor_mutex.lock(); |
Mike Fiore |
1:a2c5a8a74527 | 145 | ret = accelerometer->get_x_axes(axes); |
Mike Fiore |
1:a2c5a8a74527 | 146 | //sensor_mutex.unlock(); |
Mike Fiore |
1:a2c5a8a74527 | 147 | if (ret) { |
Mike Fiore |
1:a2c5a8a74527 | 148 | ts_printf("failed to get accelerometer\r\n"); |
Mike Fiore |
1:a2c5a8a74527 | 149 | } |
mfiore | 0:06fc5a68ead2 | 150 | ts_printf("LSM6DS0 [acc/mg]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
mfiore | 0:06fc5a68ead2 | 151 | |
Mike Fiore |
1:a2c5a8a74527 | 152 | //sensor_mutex.lock(); |
Mike Fiore |
1:a2c5a8a74527 | 153 | ret = gyroscope->get_g_axes(axes); |
Mike Fiore |
1:a2c5a8a74527 | 154 | //sensor_mutex.unlock(); |
Mike Fiore |
1:a2c5a8a74527 | 155 | if (ret) { |
Mike Fiore |
1:a2c5a8a74527 | 156 | ts_printf("failed to get gyroscope\r\n"); |
Mike Fiore |
1:a2c5a8a74527 | 157 | } |
mfiore | 0:06fc5a68ead2 | 158 | ts_printf("LSM6DS0 [gyro/mdps]: %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]); |
mfiore | 0:06fc5a68ead2 | 159 | |
mfiore | 0:06fc5a68ead2 | 160 | wait(1.5); |
mfiore | 0:06fc5a68ead2 | 161 | } |
mfiore | 0:06fc5a68ead2 | 162 | } |
mfiore | 0:06fc5a68ead2 | 163 | |
mfiore | 0:06fc5a68ead2 | 164 | /* Function to run the range board */ |
mfiore | 0:06fc5a68ead2 | 165 | void range_func() { |
mfiore | 0:06fc5a68ead2 | 166 | int status; |
mfiore | 0:06fc5a68ead2 | 167 | uint32_t distance; |
mfiore | 0:06fc5a68ead2 | 168 | |
mfiore | 0:06fc5a68ead2 | 169 | ts_printf("\r\n--- Starting range board ---\r\n"); |
mfiore | 0:06fc5a68ead2 | 170 | |
mfiore | 0:06fc5a68ead2 | 171 | /* init the 53L0A1 expansion board with default values */ |
mfiore | 0:06fc5a68ead2 | 172 | while (true) { |
Mike Fiore |
1:a2c5a8a74527 | 173 | //sensor_mutex.lock(); |
Mike Fiore |
1:a2c5a8a74527 | 174 | status=range_board->InitBoard(); |
Mike Fiore |
1:a2c5a8a74527 | 175 | //sensor_mutex.unlock(); |
mfiore | 0:06fc5a68ead2 | 176 | if (status) { |
mfiore | 0:06fc5a68ead2 | 177 | ts_printf("Failed to init range board!\r\n"); |
mfiore | 0:06fc5a68ead2 | 178 | wait(1); |
mfiore | 0:06fc5a68ead2 | 179 | } else { |
mfiore | 0:06fc5a68ead2 | 180 | ts_printf("Range board initialized\r\n"); |
mfiore | 0:06fc5a68ead2 | 181 | break; |
mfiore | 0:06fc5a68ead2 | 182 | } |
mfiore | 0:06fc5a68ead2 | 183 | } |
mfiore | 0:06fc5a68ead2 | 184 | |
mfiore | 0:06fc5a68ead2 | 185 | while (true) { |
Mike Fiore |
1:a2c5a8a74527 | 186 | //sensor_mutex.lock(); |
Mike Fiore |
1:a2c5a8a74527 | 187 | status = range_board->sensor_centre->GetDistance(&distance); |
Mike Fiore |
1:a2c5a8a74527 | 188 | //sensor_mutex.unlock(); |
mfiore | 0:06fc5a68ead2 | 189 | if (status == VL53L0X_ERROR_NONE) { |
mfiore | 0:06fc5a68ead2 | 190 | ts_printf("Distance : %ld\n", distance); |
mfiore | 0:06fc5a68ead2 | 191 | } |
mfiore | 0:06fc5a68ead2 | 192 | |
Mike Fiore |
1:a2c5a8a74527 | 193 | wait(0.8); |
mfiore | 0:06fc5a68ead2 | 194 | } |
mfiore | 0:06fc5a68ead2 | 195 | } |
mfiore | 0:06fc5a68ead2 | 196 | |
mfiore | 0:06fc5a68ead2 | 197 | int main() { |
mfiore | 0:06fc5a68ead2 | 198 | ts_printf("\r\n--- Application starting up ---\r\n"); |
mfiore | 0:06fc5a68ead2 | 199 | |
Mike Fiore |
1:a2c5a8a74527 | 200 | mems_thread.start(mbed::Callback<void()> (mems_func)); |
Mike Fiore |
1:a2c5a8a74527 | 201 | range_thread.start(mbed::Callback<void()> (range_func)); |
mfiore | 0:06fc5a68ead2 | 202 | |
mfiore | 0:06fc5a68ead2 | 203 | while (true) { |
mfiore | 0:06fc5a68ead2 | 204 | led = !led; |
mfiore | 0:06fc5a68ead2 | 205 | wait(0.25); |
mfiore | 0:06fc5a68ead2 | 206 | } |
Mike Fiore |
1:a2c5a8a74527 | 207 | } |