logs sensor data

Dependencies:   X_NUCLEO_IKS01A2

Committer:
GuillaumeFISH
Date:
Fri May 17 22:33:33 2019 +0000
Revision:
0:2c1725825eaa
May 17 2019 4:33pm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GuillaumeFISH 0:2c1725825eaa 1 #include "mbed.h"
GuillaumeFISH 0:2c1725825eaa 2 /**
GuillaumeFISH 0:2c1725825eaa 3 ******************************************************************************
GuillaumeFISH 0:2c1725825eaa 4 * @file main.cpp
GuillaumeFISH 0:2c1725825eaa 5 * @author NW
GuillaumeFISH 0:2c1725825eaa 6 * @version V1.0.0
GuillaumeFISH 0:2c1725825eaa 7 * @date 07-May-2019
GuillaumeFISH 0:2c1725825eaa 8 * @brief Modified Example application for using the X_NUCLEO_IKS01A2
GuillaumeFISH 0:2c1725825eaa 9 * MEMS Inertial & Environmental Sensor Nucleo expansion board
GuillaumeFISH 0:2c1725825eaa 10 * Using a ticker timer and event queuing.
GuillaumeFISH 0:2c1725825eaa 11 ******************************************************************************
GuillaumeFISH 0:2c1725825eaa 12 * @attention
GuillaumeFISH 0:2c1725825eaa 13 *
GuillaumeFISH 0:2c1725825eaa 14 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
GuillaumeFISH 0:2c1725825eaa 15 *
GuillaumeFISH 0:2c1725825eaa 16 * Redistribution and use in source and binary forms, with or without modification,
GuillaumeFISH 0:2c1725825eaa 17 * are permitted provided that the following conditions are met:
GuillaumeFISH 0:2c1725825eaa 18 * 1. Redistributions of source code must retain the above copyright notice,
GuillaumeFISH 0:2c1725825eaa 19 * this list of conditions and the following disclaimer.
GuillaumeFISH 0:2c1725825eaa 20 * 2. Redistributions in binary form must reproduce the above copyright notice,
GuillaumeFISH 0:2c1725825eaa 21 * this list of conditions and the following disclaimer in the documentation
GuillaumeFISH 0:2c1725825eaa 22 * and/or other materials provided with the distribution.
GuillaumeFISH 0:2c1725825eaa 23 * 3. Neither the name of STMicroelectronics nor the names of its contributors
GuillaumeFISH 0:2c1725825eaa 24 * may be used to endorse or promote products derived from this software
GuillaumeFISH 0:2c1725825eaa 25 * without specific prior written permission.
GuillaumeFISH 0:2c1725825eaa 26 *
GuillaumeFISH 0:2c1725825eaa 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
GuillaumeFISH 0:2c1725825eaa 28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
GuillaumeFISH 0:2c1725825eaa 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
GuillaumeFISH 0:2c1725825eaa 30 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
GuillaumeFISH 0:2c1725825eaa 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
GuillaumeFISH 0:2c1725825eaa 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
GuillaumeFISH 0:2c1725825eaa 33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
GuillaumeFISH 0:2c1725825eaa 34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
GuillaumeFISH 0:2c1725825eaa 35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
GuillaumeFISH 0:2c1725825eaa 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
GuillaumeFISH 0:2c1725825eaa 37 *
GuillaumeFISH 0:2c1725825eaa 38 ******************************************************************************
GuillaumeFISH 0:2c1725825eaa 39 */
GuillaumeFISH 0:2c1725825eaa 40
GuillaumeFISH 0:2c1725825eaa 41 /* Includes */
GuillaumeFISH 0:2c1725825eaa 42 #include "mbed.h"
GuillaumeFISH 0:2c1725825eaa 43 #include "XNucleoIKS01A2.h"
GuillaumeFISH 0:2c1725825eaa 44
GuillaumeFISH 0:2c1725825eaa 45 /* Configures PC serial port */
GuillaumeFISH 0:2c1725825eaa 46 Serial pc(USBTX, USBRX);
GuillaumeFISH 0:2c1725825eaa 47 DigitalOut led1(LED1);
GuillaumeFISH 0:2c1725825eaa 48
GuillaumeFISH 0:2c1725825eaa 49 uint8_t id;
GuillaumeFISH 0:2c1725825eaa 50 float temp1, temp2, humid1, humid2;
GuillaumeFISH 0:2c1725825eaa 51 char buffer1[32], buffer2[32], buffer3[32], buffer4[32];
GuillaumeFISH 0:2c1725825eaa 52 int32_t axes1[3], axes2[3], axes3[3], axes4[3];
GuillaumeFISH 0:2c1725825eaa 53 int64_t usTime1 = 0, usTime2 = 0, usDeltaTime = 0;
GuillaumeFISH 0:2c1725825eaa 54
GuillaumeFISH 0:2c1725825eaa 55 /* Defines the two queues used, one for events and one for printing to the screen */
GuillaumeFISH 0:2c1725825eaa 56 EventQueue printfQueue;
GuillaumeFISH 0:2c1725825eaa 57 EventQueue eventQueue;
GuillaumeFISH 0:2c1725825eaa 58
GuillaumeFISH 0:2c1725825eaa 59 /* Defines the timer */
GuillaumeFISH 0:2c1725825eaa 60 Timer t;
GuillaumeFISH 0:2c1725825eaa 61 time_t whattime;
GuillaumeFISH 0:2c1725825eaa 62
GuillaumeFISH 0:2c1725825eaa 63 /* Instantiate the expansion board */
GuillaumeFISH 0:2c1725825eaa 64 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
GuillaumeFISH 0:2c1725825eaa 65
GuillaumeFISH 0:2c1725825eaa 66 /* Retrieve the composing elements of the expansion board */
GuillaumeFISH 0:2c1725825eaa 67 static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
GuillaumeFISH 0:2c1725825eaa 68 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
GuillaumeFISH 0:2c1725825eaa 69 static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
GuillaumeFISH 0:2c1725825eaa 70 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
GuillaumeFISH 0:2c1725825eaa 71 static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
GuillaumeFISH 0:2c1725825eaa 72
GuillaumeFISH 0:2c1725825eaa 73 /* Helper function for printing floats & doubles */
GuillaumeFISH 0:2c1725825eaa 74 static char *print_double(char* str, double v, int decimalDigits=2)
GuillaumeFISH 0:2c1725825eaa 75 {
GuillaumeFISH 0:2c1725825eaa 76 int i = 1;
GuillaumeFISH 0:2c1725825eaa 77 int intPart, fractPart;
GuillaumeFISH 0:2c1725825eaa 78 int len;
GuillaumeFISH 0:2c1725825eaa 79 char *ptr;
GuillaumeFISH 0:2c1725825eaa 80
GuillaumeFISH 0:2c1725825eaa 81 /* prepare decimal digits multiplicator */
GuillaumeFISH 0:2c1725825eaa 82 for (;decimalDigits!=0; i*=10, decimalDigits--);
GuillaumeFISH 0:2c1725825eaa 83
GuillaumeFISH 0:2c1725825eaa 84 /* calculate integer & fractinal parts */
GuillaumeFISH 0:2c1725825eaa 85 intPart = (int)v;
GuillaumeFISH 0:2c1725825eaa 86 fractPart = (int)((v-(double)(int)v)*i);
GuillaumeFISH 0:2c1725825eaa 87
GuillaumeFISH 0:2c1725825eaa 88 /* fill in integer part */
GuillaumeFISH 0:2c1725825eaa 89 sprintf(str, "%i.", intPart);
GuillaumeFISH 0:2c1725825eaa 90
GuillaumeFISH 0:2c1725825eaa 91 /* prepare fill in of fractional part */
GuillaumeFISH 0:2c1725825eaa 92 len = strlen(str);
GuillaumeFISH 0:2c1725825eaa 93 ptr = &str[len];
GuillaumeFISH 0:2c1725825eaa 94
GuillaumeFISH 0:2c1725825eaa 95 /* fill in leading fractional zeros */
GuillaumeFISH 0:2c1725825eaa 96 for (i/=10;i>1; i/=10, ptr++) {
GuillaumeFISH 0:2c1725825eaa 97 if (fractPart >= i) {
GuillaumeFISH 0:2c1725825eaa 98 break;
GuillaumeFISH 0:2c1725825eaa 99 }
GuillaumeFISH 0:2c1725825eaa 100 *ptr = '0';
GuillaumeFISH 0:2c1725825eaa 101 }
GuillaumeFISH 0:2c1725825eaa 102
GuillaumeFISH 0:2c1725825eaa 103 /* fill in (rest of) fractional part */
GuillaumeFISH 0:2c1725825eaa 104 sprintf(ptr, "%i", fractPart);
GuillaumeFISH 0:2c1725825eaa 105
GuillaumeFISH 0:2c1725825eaa 106 return str;
GuillaumeFISH 0:2c1725825eaa 107 }
GuillaumeFISH 0:2c1725825eaa 108
GuillaumeFISH 0:2c1725825eaa 109 /* Reads the sensor board sensors */
GuillaumeFISH 0:2c1725825eaa 110 /* Reads the current board time */
GuillaumeFISH 0:2c1725825eaa 111 /* Compares the current time to the last time it was measured */
GuillaumeFISH 0:2c1725825eaa 112 void Read_Sensors() {
GuillaumeFISH 0:2c1725825eaa 113 // this runs in the normal priority thread
GuillaumeFISH 0:2c1725825eaa 114 led1 = !led1;
GuillaumeFISH 0:2c1725825eaa 115 hum_temp->get_temperature(&temp1);
GuillaumeFISH 0:2c1725825eaa 116 hum_temp->get_humidity(&humid1);
GuillaumeFISH 0:2c1725825eaa 117 press_temp->get_temperature(&temp2);
GuillaumeFISH 0:2c1725825eaa 118 press_temp->get_pressure(&humid2);
GuillaumeFISH 0:2c1725825eaa 119 magnetometer->get_m_axes(axes1);
GuillaumeFISH 0:2c1725825eaa 120 accelerometer->get_x_axes(axes2);
GuillaumeFISH 0:2c1725825eaa 121 acc_gyro->get_x_axes(axes3);
GuillaumeFISH 0:2c1725825eaa 122 acc_gyro->get_g_axes(axes4);
GuillaumeFISH 0:2c1725825eaa 123 usTime2 = usTime1;
GuillaumeFISH 0:2c1725825eaa 124 usTime1 = t.read_high_resolution_us();
GuillaumeFISH 0:2c1725825eaa 125 usDeltaTime = usTime1 - usTime2;
GuillaumeFISH 0:2c1725825eaa 126 whattime = time(NULL);
GuillaumeFISH 0:2c1725825eaa 127 }
GuillaumeFISH 0:2c1725825eaa 128
GuillaumeFISH 0:2c1725825eaa 129 /* Prints to the serial console */
GuillaumeFISH 0:2c1725825eaa 130 void Print_Sensors() {
GuillaumeFISH 0:2c1725825eaa 131 // this runs in the lower priority thread
GuillaumeFISH 0:2c1725825eaa 132 printf("%u ", (unsigned int)whattime);
GuillaumeFISH 0:2c1725825eaa 133 printf("%lld ", usDeltaTime);
GuillaumeFISH 0:2c1725825eaa 134 printf("%lld ", usTime1);
GuillaumeFISH 0:2c1725825eaa 135 printf("%7s %s ", print_double(buffer1, temp1), print_double(buffer2, humid1));
GuillaumeFISH 0:2c1725825eaa 136 printf("%7s %s ", print_double(buffer3, temp2), print_double(buffer4, humid2));
GuillaumeFISH 0:2c1725825eaa 137 printf("%6ld %6ld %6ld ", axes1[0], axes1[1], axes1[2]);
GuillaumeFISH 0:2c1725825eaa 138 printf("%6ld %6ld %6ld", axes2[0], axes2[1], axes2[2]);
GuillaumeFISH 0:2c1725825eaa 139 printf("%6ld %6ld %6ld", axes3[0], axes3[1], axes3[2]);
GuillaumeFISH 0:2c1725825eaa 140 printf("%6ld %6ld %6ld\r\n", axes4[0], axes4[1], axes4[2]);
GuillaumeFISH 0:2c1725825eaa 141
GuillaumeFISH 0:2c1725825eaa 142
GuillaumeFISH 0:2c1725825eaa 143 }
GuillaumeFISH 0:2c1725825eaa 144
GuillaumeFISH 0:2c1725825eaa 145 /* Converts standard time into Epoch time. Could delete this if no longer needed.*/
GuillaumeFISH 0:2c1725825eaa 146 time_t asUnixTime(int year, int mon, int mday, int hour, int min, int sec) {
GuillaumeFISH 0:2c1725825eaa 147 struct tm t;
GuillaumeFISH 0:2c1725825eaa 148 t.tm_year = year - 1900;
GuillaumeFISH 0:2c1725825eaa 149 t.tm_mon = mon - 1; // convert to 0 based month
GuillaumeFISH 0:2c1725825eaa 150 t.tm_mday = mday;
GuillaumeFISH 0:2c1725825eaa 151 t.tm_hour = hour;
GuillaumeFISH 0:2c1725825eaa 152 t.tm_min = min;
GuillaumeFISH 0:2c1725825eaa 153 t.tm_sec = sec;
GuillaumeFISH 0:2c1725825eaa 154 t.tm_isdst = -1; // Is Daylight saving time on? 1 = yes, 0 = no, -1 = unknown
GuillaumeFISH 0:2c1725825eaa 155
GuillaumeFISH 0:2c1725825eaa 156 return mktime(&t); // returns seconds elapsed since January 1, 1970 (begin of the Epoch)
GuillaumeFISH 0:2c1725825eaa 157 }
GuillaumeFISH 0:2c1725825eaa 158
GuillaumeFISH 0:2c1725825eaa 159
GuillaumeFISH 0:2c1725825eaa 160 /* Simple main function */
GuillaumeFISH 0:2c1725825eaa 161 int main() {
GuillaumeFISH 0:2c1725825eaa 162 pc.baud(115200);
GuillaumeFISH 0:2c1725825eaa 163 /* Sets an arbitrary starting date */
GuillaumeFISH 0:2c1725825eaa 164 /* TODO: read in from serial console to start */
GuillaumeFISH 0:2c1725825eaa 165 //set_time(asUnixTime(2019,03,24,16,10,30)); Could get rid of asUnixTime as well
GuillaumeFISH 0:2c1725825eaa 166
GuillaumeFISH 0:2c1725825eaa 167
GuillaumeFISH 0:2c1725825eaa 168 /*Guillaume's addition to read in from serial*/
GuillaumeFISH 0:2c1725825eaa 169 //Prompts the user to input the current unix time and uses input
GuillaumeFISH 0:2c1725825eaa 170 //to set the RTC
GuillaumeFISH 0:2c1725825eaa 171 int int_time=0;
GuillaumeFISH 0:2c1725825eaa 172 char buffer[10];
GuillaumeFISH 0:2c1725825eaa 173
GuillaumeFISH 0:2c1725825eaa 174 pc.printf("Enter the current unix time:");
GuillaumeFISH 0:2c1725825eaa 175 pc.scanf("%s", buffer);
GuillaumeFISH 0:2c1725825eaa 176 sscanf(buffer, "%d", &int_time);
GuillaumeFISH 0:2c1725825eaa 177 pc.printf("received %d\n",int_time);
GuillaumeFISH 0:2c1725825eaa 178
GuillaumeFISH 0:2c1725825eaa 179 set_time(int_time); // Set RTC time
GuillaumeFISH 0:2c1725825eaa 180
GuillaumeFISH 0:2c1725825eaa 181 /* resets and starts the timer */
GuillaumeFISH 0:2c1725825eaa 182 t.reset();
GuillaumeFISH 0:2c1725825eaa 183 t.start();
GuillaumeFISH 0:2c1725825eaa 184 usTime1 = t.read_high_resolution_us();
GuillaumeFISH 0:2c1725825eaa 185
GuillaumeFISH 0:2c1725825eaa 186 /* Enable all sensors */
GuillaumeFISH 0:2c1725825eaa 187 hum_temp->enable();
GuillaumeFISH 0:2c1725825eaa 188 press_temp->enable();
GuillaumeFISH 0:2c1725825eaa 189 magnetometer->enable();
GuillaumeFISH 0:2c1725825eaa 190 accelerometer->enable();
GuillaumeFISH 0:2c1725825eaa 191 acc_gyro->enable_x();
GuillaumeFISH 0:2c1725825eaa 192 acc_gyro->enable_g();
GuillaumeFISH 0:2c1725825eaa 193 wait(1.5);
GuillaumeFISH 0:2c1725825eaa 194 //Prints headers for each measurement. Unsure if the acc, mag, and gyro
GuillaumeFISH 0:2c1725825eaa 195 //directions are accurate. (Don't know if accx actually measures in x direction)
GuillaumeFISH 0:2c1725825eaa 196 printf("\r\nDATE TIME EPOC DELT RUNT TEP1 HUM TEP2 PRES MAGX MAGY MAGZ AC1X AC1Y AC1Z AC2X AC2Y AC2Z GYRX GYRY GYRZ\r\n");
GuillaumeFISH 0:2c1725825eaa 197 /*
GuillaumeFISH 0:2c1725825eaa 198 hum_temp->read_id(&id);
GuillaumeFISH 0:2c1725825eaa 199 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
GuillaumeFISH 0:2c1725825eaa 200 press_temp->read_id(&id);
GuillaumeFISH 0:2c1725825eaa 201 printf("LPS22HB pressure & temperature = 0x%X\r\n", id);
GuillaumeFISH 0:2c1725825eaa 202 magnetometer->read_id(&id);
GuillaumeFISH 0:2c1725825eaa 203 printf("LSM303AGR magnetometer = 0x%X\r\n", id);
GuillaumeFISH 0:2c1725825eaa 204 accelerometer->read_id(&id);
GuillaumeFISH 0:2c1725825eaa 205 printf("LSM303AGR accelerometer = 0x%X\r\n", id);
GuillaumeFISH 0:2c1725825eaa 206 acc_gyro->read_id(&id);
GuillaumeFISH 0:2c1725825eaa 207 printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
GuillaumeFISH 0:2c1725825eaa 208 printf("---\r\n");
GuillaumeFISH 0:2c1725825eaa 209 */
GuillaumeFISH 0:2c1725825eaa 210 // normal priority thread for other events
GuillaumeFISH 0:2c1725825eaa 211 Thread eventThread(osPriorityNormal);
GuillaumeFISH 0:2c1725825eaa 212 eventThread.start(callback(&eventQueue, &EventQueue::dispatch_forever));
GuillaumeFISH 0:2c1725825eaa 213
GuillaumeFISH 0:2c1725825eaa 214 // low priority thread for calling printf()
GuillaumeFISH 0:2c1725825eaa 215 Thread printfThread(osPriorityLow);
GuillaumeFISH 0:2c1725825eaa 216 printfThread.start(callback(&printfQueue, &EventQueue::dispatch_forever));
GuillaumeFISH 0:2c1725825eaa 217
GuillaumeFISH 0:2c1725825eaa 218 // call read_sensors 1 every second, automatically defering to the eventThread
GuillaumeFISH 0:2c1725825eaa 219 Ticker ReadTicker;
GuillaumeFISH 0:2c1725825eaa 220 Ticker PrintTicker;
GuillaumeFISH 0:2c1725825eaa 221 ReadTicker.attach(eventQueue.event(&Read_Sensors), 1.0f);
GuillaumeFISH 0:2c1725825eaa 222 PrintTicker.attach(printfQueue.event(&Print_Sensors), 1.0f);
GuillaumeFISH 0:2c1725825eaa 223
GuillaumeFISH 0:2c1725825eaa 224 wait(osWaitForever);
GuillaumeFISH 0:2c1725825eaa 225 }