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
Diff: main.cpp
- Revision:
- 19:00a099052986
- Parent:
- 18:91a38b13d21d
- Child:
- 20:e444e7dd815a
diff -r 91a38b13d21d -r 00a099052986 main.cpp
--- a/main.cpp Thu Oct 14 12:50:50 2021 +0000
+++ b/main.cpp Wed Nov 03 12:18:19 2021 +0000
@@ -1,189 +1,241 @@
-/**
- ******************************************************************************
- * @file main.cpp
- * @author CLab
- * @version V1.0.0
- * @date 2-December-2016
- * @brief Simple Example application for using the X_NUCLEO_IKS01A1
- * MEMS Inertial & Environmental Sensor Nucleo expansion board.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
-*/
-
/* Includes */
#include "mbed.h"
#include "XNucleoIKS01A2.h"
-#include "SDFileSystem.h"
+#include "SDFileSystemDMA.h"
#include "FATFileSystem.h"
#include "iostream"
+#include "main.h"
+
Serial pc(USBTX, USBRX);
-// SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd"); // MOSI, MISO, SCK, CS
SDFileSystem sd(D11, D12, D13, D10, "sd");// the pinout on the mbed Cool Components workshop board
FILE *fp;
+//SDFileSystem sd( D11, D12, D13, D10, "sd");
+
+
+Serial gps(D8, D2);
+
+
+
/* Instantiate the expansion board */
static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
+
/* Retrieve the composing elements of the expansion board */
-static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
-static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
-static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
-/* Helper function for printing floats & doubles */
-static char *print_double(char* str, double v, int decimalDigits=2)
-{
- int i = 1;
- int intPart, fractPart;
- int len;
- char *ptr;
-
- /* prepare decimal digits multiplicator */
- for (;decimalDigits!=0; i*=10, decimalDigits--);
-
- /* calculate integer & fractinal parts */
- intPart = (int)v;
- fractPart = (int)((v-(double)(int)v)*i);
-
- /* fill in integer part */
- sprintf(str, "%i.", intPart);
+DigitalIn userButton(PC_13); //USER BUTTON
+bool live_writing = false;
- /* prepare fill in of fractional part */
- len = strlen(str);
- ptr = &str[len];
-
- /* fill in leading fractional zeros */
- for (i/=10;i>1; i/=10, ptr++) {
- if (fractPart >= i) {
- break;
+// GPS
+char c;
+void parse(char *cmd, int n)
+{
+
+ char ns, ew, tf, status;
+ int fq, nst, fix, date; // fix quality, Number of satellites being tracked, 3D fix
+ float latitude, longitude, timefix, speed, altitude;
+
+
+ // Global Positioning System Fix Data
+ if(strncmp(cmd,"$GPGGA", 6) == 0)
+ {
+ sscanf(cmd, "$GPGGA,%f,%f,%c,%f,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude);
+ pc.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);
}
- *ptr = '0';
- }
+
+ // Satellite status
+ if(strncmp(cmd,"$GPGSA", 6) == 0)
+ {
+ sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst);
+ pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst);
+ }
+
+ // Geographic position, Latitude and Longitude
+ if(strncmp(cmd,"$GPGLL", 6) == 0)
+ {
+ sscanf(cmd, "$GPGLL,%f,%c,%f,%c,%f", &latitude, &ns, &longitude, &ew, &timefix);
+ pc.printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\r\n", latitude, ns, longitude, ew, timefix);
+ }
+
+ // Geographic position, Latitude and Longitude
+ if(strncmp(cmd,"$GPRMC", 6) == 0)
+ {
+ sscanf(cmd, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date);
+ pc.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);
+ }
+}
- /* fill in (rest of) fractional part */
- sprintf(ptr, "%i", fractPart);
-
- return str;
-}
/* Simple main function */
int main() {
+ //GPS gps = GPS(D8,D2);
+ gps.baud(9600);
+ pc.baud(115200);
uint8_t id;
- float value1, value2;
- char buffer1[32], buffer2[32];
- int32_t axes[3];
+ int32_t x_axes[3];
+ int32_t g_axes[3];
+ float x_freq[2];
+ float g_freq[2];
+ float x_sens[2];
+ float x_fs[2];
+ // Take time (https://unixtime.org/)
+ set_time(1634379120);
+
+ // DATE
+ char s[100];
+ time_t t_l = time (NULL);
+ struct tm *tp = localtime (&t_l);
+ //strftime (s,100, "\t %d %B %Y %H:%M:%S\r\n", tp);
+ strftime (s,100, "\n\t %d %B %Y %H:%M:%S\r\n", tp);
+ printf ("%s", s);
/* Enable all sensors */
- hum_temp->enable();
- press_temp->enable();
- magnetometer->enable();
- accelerometer->enable();
- acc_gyro->enable_x();
- acc_gyro->enable_g();
+ accelerometer->enable(); //Data from LSM303AGR
+ acc_gyro->enable_x(); //Data from LSM6DSL
+ acc_gyro->enable_g(); //Data from LSM6DSL
printf("\r\n--- Starting new run ---\r\n");
- hum_temp->read_id(&id);
- printf("HTS221 humidity & temperature = 0x%X\r\n", id);
- press_temp->read_id(&id);
- printf("LPS22HB pressure & temperature = 0x%X\r\n", id);
- magnetometer->read_id(&id);
- printf("LSM303AGR magnetometer = 0x%X\r\n", id);
accelerometer->read_id(&id);
printf("LSM303AGR accelerometer = 0x%X\r\n", id);
acc_gyro->read_id(&id);
printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
-
- pc.printf("Check SD\r\n");
- mkdir("/sd/ISK01A2", 0777);
- fp = fopen("/sd/ISK01A2/test.txt", "r");
- if (fp != NULL) {
- fclose(fp);
- remove("/sd/test.txt");
- pc.printf("Remove an existing file with the same name\r\n");
- }
-
- fp = fopen("/sd/ISK01A2/test.txt", "w");
- if (fp == NULL) {
- error("Unable to write the file\r\n");
- } /*else {
- fprintf(fp, "mbed SDCard application!");
- fclose(fp);
- pc.printf("File successfully written!\r\n");
- }*/
+
int n=0;
- printf("SD ok, we can start");
- fprintf(fp, " DATA of STM32F401RE with ISK01A2\r\n");
- printf("\r\n");
+ int arr_lenght = 500;
+
+ Timer t;
+ Timer t_down;
+ float time[arr_lenght];
+// int acc_x[arr_lenght];
+// int acc_y[arr_lenght];
+ //int acc_z[arr_lenght];
+ //int gyro_x[arr_lenght];
+ //int gyro_y[arr_lenght];
+ //int gyro_z[arr_lenght];
+ float frequency = 1000.0;
+ float range = 8.0;
+ //SET
+ acc_gyro->set_x_odr(frequency);
+ acc_gyro->set_g_odr(frequency);
+ acc_gyro->set_x_fs(range);
+ //GET
+ acc_gyro->get_x_odr(x_freq);
+ printf("Accelerometer frequency: %.1f, %.1f\r\n", x_freq[0],x_freq[1]);
+ acc_gyro->get_g_odr(g_freq);
+ printf("Gryroscope frequency: %.1f, %.1f\r\n", g_freq[0],g_freq[1]);
+ acc_gyro->get_x_sensitivity(x_sens);
+ printf("Accelerometer sensitivity: %.3f, %.1f\r\n", x_sens[0],x_sens[1]);
+ acc_gyro->get_x_fs(x_fs);
+ printf("Accelerometer meas. range: %.1f, %.1f\r\n", x_fs[0],x_fs[1]);
+
+ pc.printf("Check GPS\r\n");
+ while(1){
+ if(gps.readable()){
+ if(gps.getc() == '$');{ // wait a $
+ for(int i=0; i<sizeof(cDataBuffer); i++){
+ c = gps.getc();
+ if( c == '\r' ){
+ //pc.printf("%s\n", cDataBuffer);
+ parse(cDataBuffer, i);
+ i = sizeof(cDataBuffer);
+ }else{
+ cDataBuffer[i] = c;
+ }
+ }
+ }
+ }
+ if(!userButton){
+ break;
+ }
+ }
+
- while(n<60) {
-
-
- printf("\r\n Number of cycle: %d \r\n", n);
- printf("\r\n");
- fprintf(fp, "\r\n Number of cycle: %d \r\n", n);
- fprintf(fp, "\r\n");
-
- hum_temp->get_temperature(&value1);
- hum_temp->get_humidity(&value2);
- printf("Temperature & Humidity: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
- fprintf(fp, "Temperature & Humidity: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
-
- press_temp->get_temperature(&value1);
- press_temp->get_pressure(&value2);
- printf("Temperature & Pressure: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
- fprintf(fp, "Temperature & Pressure: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
-
- printf("---\r\n");
- fprintf(fp, "---\r\n");
+ fprintf(fp, " DATA of STM32F401RE with ISK01A2\r\n");
+ printf("OK\r\n");
+ printf("\r\n");
+ /* Start the cycle*/
+ while(true){ //run forever
+ if (!userButton) { // button is pressed
+ if (live_writing==false) {
+ live_writing = true;
+ n = 0;
+ //Initialize SD CARD
+ char filename[64];
+ char date[64];
+ pc.printf("Check SD\r\n");
+ mkdir("/sd/ISK01A2", 0777);
+ int name = 0;
+ // Save name with next name test_001,test_002, ecc...
+ while(1) {
+ sprintf(filename, "/sd/ISK01A2/test_%03d.txt", name);
+ FILE *fp = fopen(filename, "r");
+ if(fp == NULL) {
+ break;
+ }
+ fclose(fp);
+ name++;
+ }
+
+ fp = fopen(filename, "w");
+ if (fp == NULL) {
+ error("Unable to write the file\r\n");
+ }
+
+ // Name file on monitor
+ printf("\n\t test_%03d \r\n", name);
+
+ strftime (date,64, "%d %B %Y %H:%M", tp);
+ fprintf(fp, "\t %s \n", date); //DATE
+ 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
+
+ printf("\r\n START NEW RUNNING \r\n");
+ t.start();
+ } else { // button is pressed but live_writing is true
+ t_down.stop();
+ pc.printf(" timer download sd: %3.3f\r\n",t_down.read());
+ t_down.reset();
+
+ // Now we write into sd card all data storaged
+ t.stop();
+ t.reset();
+ fclose(fp);
+ pc.printf("File successfully written!\r\n");
+ live_writing = false;
+ wait(2);
+ }//end else
+ } //end if userbutton
+ if (live_writing==true){
+ t_down.start();
+ acc_gyro->get_x_axes(x_axes); //Read data from LSM6DSL accelerometer
+ //acc_x[n] = x_axes[0];
+ //acc_y[n] = x_axes[1];
+ //acc_z[n] = x_axes[2];
+
+ acc_gyro->get_g_axes(g_axes); //Read data from LSM6DSL gyroscope
+ //gyro_x[n] = g_axes[0];
+ //gyro_y[n] = g_axes[1];
+ //gyro_z[n] = g_axes[2];
+
+ time[n] = t.read();
+ fprintf(fp,"%3d,%3.3f,%6ld,%6ld,%6ld,%6ld,%6ld,%6ld\r\n", n, time[n], x_axes[0], x_axes[1], x_axes[2], g_axes[0], g_axes[1], g_axes[2]);
+ n+=1;
+
+ if (n==arr_lenght){
+ t_down.stop();
+ pc.printf(" timer download sd: %3.3f\r\n",t_down.read());
+ t_down.reset();
+ t.stop();
+ t.reset();
+ fclose(fp);
+ pc.printf("File successfully written!\r\n");
+ live_writing = false;
+ wait(2);
+ }
+ }//end while writing
+ }//end while
+}//end main
- magnetometer->get_m_axes(axes);
- printf("Magnetometer [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
- fprintf(fp,"Magnetometer [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
-
- accelerometer->get_x_axes(axes);
- printf("Accelerometer [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
- fprintf(fp, "Accelerometer [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
-
- acc_gyro->get_x_axes(axes);
- printf("Accelerometer & Gyroscope x [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
- fprintf(fp, "Accelerometer & Gyroscope x [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
-
- acc_gyro->get_g_axes(axes);
- printf("Accelerometer & Gyroscope g [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
- fprintf(fp, "Accelerometer & Gyroscope g [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
-
- n+=1;
- wait(1);
- }
- fclose(fp);
- pc.printf("File successfully written!\r\n");
-}