Guillaume Raymond-Fauteux / Mbed OS Sensor_Log

Dependencies:   X_NUCLEO_IKS01A2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 #include "mbed.h"
00002 /**
00003  ******************************************************************************
00004  * @file    main.cpp
00005  * @author  NW
00006  * @version V1.0.0
00007  * @date    07-May-2019
00008  * @brief   Modified Example application for using the X_NUCLEO_IKS01A2 
00009  *          MEMS Inertial & Environmental Sensor Nucleo expansion board
00010  *          Using a ticker timer and event queuing.
00011  ******************************************************************************
00012  * @attention
00013  *
00014  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00015  *
00016  * Redistribution and use in source and binary forms, with or without modification,
00017  * are permitted provided that the following conditions are met:
00018  *   1. Redistributions of source code must retain the above copyright notice,
00019  *      this list of conditions and the following disclaimer.
00020  *   2. Redistributions in binary form must reproduce the above copyright notice,
00021  *      this list of conditions and the following disclaimer in the documentation
00022  *      and/or other materials provided with the distribution.
00023  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00024  *      may be used to endorse or promote products derived from this software
00025  *      without specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00030  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00031  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00032  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00033  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00034  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00035  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  ******************************************************************************
00039 */ 
00040 
00041 /* Includes */
00042 #include "mbed.h"
00043 #include "XNucleoIKS01A2.h"
00044 
00045 /* Configures PC serial port */
00046 Serial pc(USBTX, USBRX);
00047 DigitalOut led1(LED1);
00048 
00049 uint8_t id;
00050 float temp1, temp2, humid1, humid2;
00051 char buffer1[32], buffer2[32], buffer3[32], buffer4[32];
00052 int32_t axes1[3], axes2[3], axes3[3], axes4[3];
00053 int64_t usTime1 = 0, usTime2 = 0, usDeltaTime = 0;
00054 
00055 /* Defines the two queues used, one for events and one for printing to the screen */
00056 EventQueue printfQueue;
00057 EventQueue eventQueue;
00058 
00059 /* Defines the timer */
00060 Timer t;
00061 time_t whattime;
00062 
00063 /* Instantiate the expansion board */
00064 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
00065 
00066 /* Retrieve the composing elements of the expansion board */
00067 static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
00068 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
00069 static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
00070 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
00071 static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
00072 
00073 /* Helper function for printing floats & doubles */
00074 static char *print_double(char* str, double v, int decimalDigits=2)
00075 {
00076   int i = 1;
00077   int intPart, fractPart;
00078   int len;
00079   char *ptr;
00080 
00081   /* prepare decimal digits multiplicator */
00082   for (;decimalDigits!=0; i*=10, decimalDigits--);
00083 
00084   /* calculate integer & fractinal parts */
00085   intPart = (int)v;
00086   fractPart = (int)((v-(double)(int)v)*i);
00087 
00088   /* fill in integer part */
00089   sprintf(str, "%i.", intPart);
00090 
00091   /* prepare fill in of fractional part */
00092   len = strlen(str);
00093   ptr = &str[len];
00094 
00095   /* fill in leading fractional zeros */
00096   for (i/=10;i>1; i/=10, ptr++) {
00097     if (fractPart >= i) {
00098       break;
00099     }
00100     *ptr = '0';
00101   }
00102 
00103   /* fill in (rest of) fractional part */
00104   sprintf(ptr, "%i", fractPart);
00105 
00106   return str;
00107 }
00108 
00109 /* Reads the sensor board sensors */
00110 /* Reads the current board time */
00111 /* Compares the current time to the last time it was measured */
00112 void Read_Sensors() {
00113   // this runs in the normal priority thread
00114   led1 = !led1;
00115   hum_temp->get_temperature(&temp1);
00116   hum_temp->get_humidity(&humid1);
00117   press_temp->get_temperature(&temp2);
00118   press_temp->get_pressure(&humid2);
00119   magnetometer->get_m_axes(axes1);
00120   accelerometer->get_x_axes(axes2);
00121   acc_gyro->get_x_axes(axes3);
00122   acc_gyro->get_g_axes(axes4);
00123   usTime2 = usTime1;
00124   usTime1 = t.read_high_resolution_us();
00125   usDeltaTime = usTime1 - usTime2;
00126   whattime = time(NULL);
00127 }
00128 
00129 /* Prints to the serial console */
00130 void Print_Sensors() {
00131   // this runs in the lower priority thread
00132   printf("%u ", (unsigned int)whattime);
00133   printf("%lld ", usDeltaTime);
00134   printf("%lld ", usTime1);
00135   printf("%7s %s ", print_double(buffer1, temp1), print_double(buffer2, humid1));
00136   printf("%7s %s ", print_double(buffer3, temp2), print_double(buffer4, humid2));
00137   printf("%6ld %6ld %6ld ", axes1[0], axes1[1], axes1[2]);
00138   printf("%6ld %6ld %6ld", axes2[0], axes2[1], axes2[2]);
00139   printf("%6ld %6ld %6ld", axes3[0], axes3[1], axes3[2]);
00140   printf("%6ld %6ld %6ld\r\n", axes4[0], axes4[1], axes4[2]);
00141 
00142  
00143 }
00144 
00145 /* Converts standard time into Epoch time. Could delete this if no longer needed.*/
00146 time_t asUnixTime(int year, int mon, int mday, int hour, int min, int sec) {
00147     struct tm   t;
00148     t.tm_year = year - 1900;
00149     t.tm_mon =  mon - 1;        // convert to 0 based month
00150     t.tm_mday = mday;
00151     t.tm_hour = hour;
00152     t.tm_min = min;
00153     t.tm_sec = sec;
00154     t.tm_isdst = -1;            // Is Daylight saving time on? 1 = yes, 0 = no, -1 = unknown
00155  
00156     return mktime(&t);          // returns seconds elapsed since January 1, 1970 (begin of the Epoch)
00157 }
00158 
00159 
00160 /* Simple main function */
00161 int main() {
00162   pc.baud(115200);
00163   /* Sets an arbitrary starting date */
00164   /* TODO: read in from serial console to start */
00165   //set_time(asUnixTime(2019,03,24,16,10,30));  Could get rid of asUnixTime as well
00166 
00167 
00168   /*Guillaume's addition to read in from serial*/
00169   //Prompts the user to input the current unix time and uses input
00170   //to set the RTC
00171     int int_time=0;
00172     char buffer[10];
00173 
00174         pc.printf("Enter the current unix time:");
00175         pc.scanf("%s", buffer);
00176         sscanf(buffer, "%d", &int_time);
00177         pc.printf("received %d\n",int_time);
00178 
00179         set_time(int_time);  // Set RTC time
00180   
00181   /* resets and starts the timer */
00182   t.reset();
00183   t.start();
00184   usTime1 = t.read_high_resolution_us();
00185   
00186   /* Enable all sensors */
00187   hum_temp->enable();
00188   press_temp->enable();
00189   magnetometer->enable();
00190   accelerometer->enable();
00191   acc_gyro->enable_x();
00192   acc_gyro->enable_g();
00193   wait(1.5);
00194   //Prints headers for each measurement. Unsure if the acc, mag, and gyro
00195   //directions are accurate. (Don't know if accx actually measures in x direction)
00196   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");
00197 /*
00198   hum_temp->read_id(&id);
00199   printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00200   press_temp->read_id(&id);
00201   printf("LPS22HB  pressure & temperature   = 0x%X\r\n", id);
00202   magnetometer->read_id(&id);
00203   printf("LSM303AGR magnetometer            = 0x%X\r\n", id);
00204   accelerometer->read_id(&id);
00205   printf("LSM303AGR accelerometer           = 0x%X\r\n", id);
00206   acc_gyro->read_id(&id);
00207   printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
00208   printf("---\r\n");
00209  */   
00210     // normal priority thread for other events
00211   Thread eventThread(osPriorityNormal);
00212   eventThread.start(callback(&eventQueue, &EventQueue::dispatch_forever));
00213   
00214   // low priority thread for calling printf()
00215   Thread printfThread(osPriorityLow);
00216   printfThread.start(callback(&printfQueue, &EventQueue::dispatch_forever));
00217   
00218   // call read_sensors 1 every second, automatically defering to the eventThread
00219   Ticker ReadTicker;
00220   Ticker PrintTicker;
00221   ReadTicker.attach(eventQueue.event(&Read_Sensors), 1.0f);
00222   PrintTicker.attach(printfQueue.event(&Print_Sensors), 1.0f);
00223  
00224   wait(osWaitForever);
00225 }