Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed X_NUCLEO_IKS01A2
main.cpp
00001 /* Includes */ 00002 #include "mbed.h" 00003 #include "XNucleoIKS01A2.h" 00004 #include "SDFileSystem.h" 00005 #include "FATFileSystem.h" 00006 #include "iostream" 00007 00008 Serial pc(USBTX, USBRX, 115200); 00009 Serial gps(D8, D2, 9800); 00010 00011 SDFileSystem sd(D11, D12, D13, D10, "sd");// the pinout on the mbed Cool Components workshop board 00012 FILE *fp; 00013 00014 /* Instantiate the expansion board */ 00015 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); 00016 00017 /* Retrieve the composing elements of the expansion board */ 00018 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; 00019 static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; 00020 00021 DigitalIn userButton(PC_13); //USER BUTTON 00022 bool live_writing = false; 00023 00024 // GPS 00025 char cDataBuffer[500]; 00026 int i = 0; 00027 char c; 00028 void parse(char *cmd, int n){ 00029 char ns, ew, tf, status; 00030 int fq, nst, fix, date; // fix quality, Number of satellites being tracked, 3D fix 00031 float latitude, longitude, timefix, speed, altitude; 00032 00033 // Global Positioning System Fix Data 00034 if(strncmp(cmd,"$GPGGA", 6) == 0){ 00035 sscanf(cmd, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude); 00036 printf("GPGGA Fix taken at: %f, Latitude: %f %c, Longitude: %f %c, Fix quality: %d, Number of sat: %d, Altitude: %f M\r\n", timefix, latitude, ns, longitude, ew, fq, nst, altitude); 00037 } 00038 00039 // Satellite status 00040 if(strncmp(cmd,"$GPGSA", 6) == 0){ 00041 sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst); 00042 printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst); 00043 } 00044 00045 // Geographic position, Latitude and Longitude 00046 if(strncmp(cmd,"$GPGLL", 6) == 0){ 00047 sscanf(cmd, "$GPGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix); 00048 printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\r\n", latitude, ns, longitude, ew, timefix); 00049 } 00050 00051 // Geographic position, Latitude and Longitude 00052 if(strncmp(cmd,"$GPRMC", 6) == 0){ 00053 sscanf(cmd, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date); 00054 printf("GPRMC Fix taken at: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\r\n", timefix, status, latitude, ns, longitude, ew, speed, date); 00055 } 00056 } 00057 00058 /* Simple main function */ 00059 int main() { 00060 uint8_t id; 00061 int32_t x_axes[3]; 00062 int32_t g_axes[3]; 00063 float x_freq[2]; 00064 float g_freq[2]; 00065 float x_sens[2]; 00066 float x_fs[2]; 00067 // Take time (https://unixtime.org/) 00068 set_time(1635956827); 00069 00070 // DATE 00071 char s[100]; 00072 time_t t_l = time (NULL); 00073 struct tm *tp = localtime (&t_l); 00074 strftime (s,100, "\n\t %d %B %Y %H:%M:%S\r\n", tp); 00075 printf ("%s", s); 00076 00077 /* Enable all sensors */ 00078 accelerometer->enable(); //Data from LSM303AGR 00079 acc_gyro->enable_x(); //Data from LSM6DSL 00080 acc_gyro->enable_g(); //Data from LSM6DSL 00081 00082 printf("\r\n--- Starting new run ---\r\n"); 00083 00084 accelerometer->read_id(&id); 00085 printf("LSM303AGR accelerometer = 0x%X\r\n", id); 00086 acc_gyro->read_id(&id); 00087 printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); 00088 00089 int n=0; 00090 int arr_lenght = 500; 00091 00092 Timer t; 00093 Timer t_down; 00094 float time[arr_lenght]; 00095 int acc_x[arr_lenght]; 00096 int acc_y[arr_lenght]; 00097 int acc_z[arr_lenght]; 00098 int gyro_x[arr_lenght]; 00099 int gyro_y[arr_lenght]; 00100 int gyro_z[arr_lenght]; 00101 float frequency = 1000.0; 00102 float range = 8.0; 00103 //SET 00104 acc_gyro->set_x_odr(frequency); 00105 acc_gyro->set_g_odr(frequency); 00106 acc_gyro->set_x_fs(range); 00107 //GET 00108 acc_gyro->get_x_odr(x_freq); 00109 printf("Accelerometer frequency: %.1f, %.1f\r\n", x_freq[0],x_freq[1]); 00110 acc_gyro->get_g_odr(g_freq); 00111 printf("Gryroscope frequency: %.1f, %.1f\r\n", g_freq[0],g_freq[1]); 00112 acc_gyro->get_x_sensitivity(x_sens); 00113 printf("Accelerometer sensitivity: %.3f, %.1f\r\n", x_sens[0],x_sens[1]); 00114 acc_gyro->get_x_fs(x_fs); 00115 printf("Accelerometer meas. range: %.1f, %.1f\r\n", x_fs[0],x_fs[1]); 00116 00117 printf("Check GPS\r\n"); 00118 while(1){ 00119 if(gps.readable()){ 00120 if(gps.getc() == '$');{ // wait a $ 00121 for(int i=0; i<sizeof(cDataBuffer); i++){ 00122 c = gps.getc(); 00123 if( c == '\r' ){ 00124 parse(cDataBuffer, i); 00125 i = sizeof(cDataBuffer); 00126 }else{ 00127 cDataBuffer[i] = c; 00128 } 00129 } 00130 } 00131 } 00132 if(!userButton){ 00133 break; 00134 } 00135 } 00136 00137 printf("OK\r\n"); 00138 printf("\r\n"); 00139 /* Start the cycle*/ 00140 while(true){ //run forever 00141 if (!userButton) { // button is pressed 00142 if (live_writing==false) { 00143 live_writing = true; 00144 n = 0; 00145 //Initialize SD CARD 00146 char filename[64]; 00147 char date[64]; 00148 printf("Check SD\r\n"); 00149 mkdir("/sd/ISK01A2", 0777); 00150 int name = 0; 00151 // Save name with next name test_001,test_002, ecc... 00152 while(1) { 00153 sprintf(filename, "/sd/ISK01A2/test_%03d.txt", name); 00154 FILE *fp = fopen(filename, "r"); 00155 if(fp == NULL) { 00156 break; 00157 } 00158 fclose(fp); 00159 name++; 00160 } 00161 00162 fp = fopen(filename, "w"); 00163 if (fp == NULL) { 00164 error("Unable to write the file\r\n"); 00165 } 00166 00167 // Name file on monitor 00168 printf("\n\t test_%03d \r\n", name); 00169 strftime (date,64, "%d %B %Y %H:%M", tp); 00170 fprintf(fp, "\t %s \n", date); //DATE 00171 fprintf(fp, "N,Time,Temp[C],Acc_x[mg],Acc_y[mg],Acc_z[mg],Gyro_x[mdps],Gyro_y[mdps],Gyro_z[mdps]\r\n"); //Header in the file text 00172 00173 printf("\r\n START NEW RUNNING \r\n"); 00174 t.start(); 00175 } else { // button is pressed but live_writing is true 00176 00177 // Now we write into sd card all data storaged 00178 t.stop(); 00179 int i; 00180 //int elementi = sizeof(acc_x)/sizeof(int); 00181 00182 for (i=0; i<=n; i++){ 00183 fprintf(fp,"%3d,%3.3f,%6ld,%6ld,%6ld,%6ld,%6ld,%6ld\r\n", i, time[i], acc_x[i], acc_y[i], acc_z[i], gyro_x[i], gyro_y[i], gyro_z[i]); //Save current data into file txt 00184 //printf("%3d,%3.3f,%6ld,%6ld,%6ld,%6ld,%6ld,%6ld\r\n", i, time[i], acc_x[i], acc_y[i], acc_z[i], gyro_x[i], gyro_y[i], gyro_z[i]); 00185 } 00186 t.reset(); 00187 fclose(fp); 00188 printf("File successfully written!\r\n"); 00189 live_writing = false; 00190 wait(2); 00191 }//end else 00192 } //end if userbutton 00193 if (live_writing==true){ 00194 acc_gyro->get_x_axes(x_axes); //Read data from LSM6DSL accelerometer 00195 acc_x[n] = x_axes[0]; 00196 acc_y[n] = x_axes[1]; 00197 acc_z[n] = x_axes[2]; 00198 00199 acc_gyro->get_g_axes(g_axes); //Read data from LSM6DSL gyroscope 00200 gyro_x[n] = g_axes[0]; 00201 gyro_y[n] = g_axes[1]; 00202 gyro_z[n] = g_axes[2]; 00203 00204 time[n] = t.read(); 00205 n+=1; 00206 00207 if (n==arr_lenght){ 00208 t.stop(); 00209 int i; 00210 00211 for (i=0; i<=n; i++){ 00212 fprintf(fp,"%3d,%3.3f,%6ld,%6ld,%6ld,%6ld,%6ld,%6ld\r\n", i, time[i], acc_x[i], acc_y[i], acc_z[i], gyro_x[i], gyro_y[i], gyro_z[i]); //Save current data into file txt 00213 //printf("%3d,%3.3f,%6ld,%6ld,%6ld,%6ld,%6ld,%6ld\r\n", i, time[i], acc_x[i], acc_y[i], acc_z[i], gyro_x[i], gyro_y[i], gyro_z[i]); 00214 } 00215 t.reset(); 00216 fclose(fp); 00217 printf("File successfully written!\r\n"); 00218 live_writing = false; 00219 wait(2); 00220 } 00221 }//end while writing 00222 }//end while 00223 }//end main 00224
Generated on Wed Aug 17 2022 15:46:09 by
1.7.2