Los Putacos / Mbed OS WearableDevice_Nucleo_New

Dependencies:   MPU9250_SPI

Fork of WearableDevice_Nucleo by Los Putacos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 * Los Putacos
00003 * Copyright (C) 2017, All rights reserved.
00004 * ________________________________________
00005 *
00006 * Created by: Gustavo Campana, Michael Schmidt, Miguel Lopez
00007 *       Date: 02-Dec-2017
00008 *    Version: V0.1
00009 */
00010 //-----------------------------------------------------------------
00011 
00012 //-----------------------------------------------------------------
00013 // "THE BEER-WARE LICENSE" (Revision 42):
00014 // The "Los Putacos" team wrote this file. As long as you retain this notice you
00015 // can do whatever you want with this stuff. If we meet some day, and you think
00016 // this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp
00017 //-----------------------------------------------------------------
00018 
00019 //-----------------------------------------------------------------
00020 // Includes
00021 #include "mbed.h"
00022 #include "event.h"
00023 #include "MPU9250.h"
00024 #include "TCPSocket.h"
00025 #include "configuration.h"
00026 #include "ESP8266Interface.h"
00027 
00028 //-----------------------------------------------------------------
00029 
00030 //-----------------------------------------------------------------
00031 // General Declarations
00032 Timer TimeStamp;                // Timer µS time-stamp
00033 Serial PC(USBTX, USBRX);        // Create an Serial PC Object - USBTX, USBRX
00034 
00035 // IMU-SPI Declaration
00036 SPI spi(IMU_MOSI, IMU_MISO,IMU_SCK);        // Create an SPI Object for MPU9250 - MOSI, MISO, SCK
00037 mpu9250_spi imu(spi, IMU_CS);               // Create an MPU9250 Object - SPI Object, CS
00038 
00039 // WiFi & TCP Socket Declaration
00040 ESP8266Interface WiFi(ESP_TX, ESP_RX);      // ESP8266 WiFi Interface
00041 TCPSocket Socket_TCP;                       // TCP Socket
00042 nsapi_error_t response;                     // TCP Socket Response
00043 
00044 // EventQueue Declarations
00045 EventQueue queue(32 * EVENTS_EVENT_SIZE);   // Event Setup
00046 
00047 // Ticker Declarations
00048 Ticker Ticker_IMU;
00049 Ticker Ticker_ReadBattery;
00050 Ticker Ticker_ReceiveCommand;
00051 
00052 // Thread Declarations
00053 Thread Thread_IMU(osPriorityRealtime);
00054 Thread Thread_ReadBattery(osPriorityRealtime);
00055 Thread Thread_ReceiveCommand(osPriorityRealtime);
00056 
00057 // Global Variables
00058 uint16_t LineCount = 0;
00059 uint32_t Line_Size = 0;
00060 uint32_t Data_Size = 0;
00061 
00062 // Read & Writepointer Declarations
00063 uint8_t Current_Position = 0;
00064 volatile uint16_t readPointer = 0;
00065 volatile uint16_t readPointer_MIC = 0;
00066 volatile uint16_t writePointer = 0;
00067 volatile uint16_t writePointer_MIC = 0;
00068 
00069 // Flag Declarations
00070 uint8_t ReadIMUDone_Flag = 0;
00071 uint8_t CheckCommandDone_Flag = 0;
00072 
00073 // Battery Voltage & Power Consumption Declaration
00074 float Battery_Status = 0;
00075 float Power_Consumption = 0;
00076 
00077 // Time Buffer Declaration
00078 char Time_Buffer[2];
00079 uint16_t Time_Data = 0;
00080 uint8_t Requested_Time = 0;
00081 
00082 // Data Storage Declaration
00083 char Data_Buffer[1024];
00084 const char DeviceNr[6] = "DEV01";       // Device specific ID
00085 int16_t Data_Storage[BufferSize];               // BufferSize defined in "event.h"
00086 uint16_t Data_Storage_MIC[BufferSize_MIC];      // BufferSize_MIC defined in "event.h"
00087 
00088 //-----------------------------------------------------------------
00089 
00090 //-----------------------------------------------------------------
00091 void Setup()
00092 {
00093     LED_Blue1 = 1;      // Turn OFF all LEDs
00094     LED_Blue2 = 1;
00095     LED_Red1 = 1;
00096     LED_Red2 = 1;
00097 
00098     PC.baud(230400);            // Initialize PC Serial Connection
00099     PC.printf("\r\n------------- Booting! -------------\r\n");
00100     PC.printf("CPU SystemCoreClock is %d Hz", SystemCoreClock);     // Show SystemCoreClock Speed
00101 
00102     PC.printf("\nConnecting to %s with %s...\n",  WiFi_SSID, WiFi_Pass);
00103     int ret = WiFi.connect(WiFi_SSID, WiFi_Pass, NSAPI_SECURITY_WPA_WPA2);      // Connect WiFi
00104 
00105     while (ret != 0) {          // WiFi Connection Not Successful
00106         PC.printf("\nConnecting to %s with %s...\n",  WiFi_SSID, WiFi_Pass);
00107         ret = WiFi.connect(WiFi_SSID, WiFi_Pass, NSAPI_SECURITY_WPA_WPA2);
00108         wait(0.5);
00109     }
00110 
00111     printf("Connection Done! - IP: %s\r\n", WiFi.get_ip_address());     // Show IP Address
00112     Socket_TCP.open(&WiFi);             // Open TCP_Socket on WiFi
00113     Socket_TCP.set_blocking(false);     // Set Non-Blocking Mode
00114 
00115     // Initialize IMU-SPI Connection
00116     if(imu.init(1, BITS_DLPF_CFG_188HZ))        // Initialize the MPU9250
00117         PC.printf("\nCouldn't initialize MPU9250 via SPI!");
00118     PC.printf("\nWHOAMI = 0x%2x", imu.whoami());                        // Output I2C Address to check SPI (correct: 104 - 0x68)
00119     PC.printf("\nAcc_Scale = %u\n", imu.set_acc_scale(BITS_FS_16G));    // Set Full Range for Acc.
00120     imu.calib_acc();        // Calibrate Acceleration Sensor
00121 
00122     ReadBattery();          // Read Battery Level
00123     TimeStamp.reset();      // Reset Timer TimeStamp
00124     Thread_ReceiveCommand.start(callback(&queue, &EventQueue::dispatch_forever));       // Start Command-Receiving Thread
00125     Ticker_ReceiveCommand.attach(queue.event(&ReceiveCommand), 0.500);                  // Attach 500ms Ticker to "ReceiveCommand"
00126 
00127     Thread_ReadBattery.start(callback(&queue, &EventQueue::dispatch_forever));          // Start Battery-Reading Thread
00128     Ticker_ReadBattery.attach(queue.event(&ReadBattery), 30);                           // Attach 30s Ticker to "ReadBattery"
00129     
00130     Buzzer = 0;             // Disable Buzzer
00131     Buzzer.period(0.001);   // 1kHz Buzzer Frequency
00132     
00133     LED_Blue1 = 0;          // Turn ON LED to display 'Booting Done!'
00134     PC.printf("\r\n------------- Ready! ---------------\r\n");
00135 }
00136 
00137 //-----------------------------------------------------------------
00138 
00139 //-----------------------------------------------------------------
00140 void Reset()            // Reset All Variables
00141 {
00142     Line_Size = 0;
00143     Data_Size = 0;
00144     LineCount = 0;
00145     Time_Data = 0;
00146     Requested_Time = 0;
00147     Current_Position = 0;
00148 
00149     readPointer = 0;
00150     readPointer_MIC = 0;
00151     writePointer = 0;
00152     writePointer_MIC = 0;
00153 
00154     ReadIMUDone_Flag = 0;
00155     CheckCommandDone_Flag = 0;
00156 
00157     memset(Data_Storage, 0, sizeof(Data_Storage));
00158     memset(Data_Storage_MIC, 0, sizeof(Data_Storage_MIC));
00159 }
00160 
00161 //-----------------------------------------------------------------
00162 
00163 //-----------------------------------------------------------------
00164 int main()
00165 {
00166     Setup();    // Initial Setups
00167 
00168     while (true) {
00169         if ((Requested_Time != 0) && (CheckCommandDone_Flag == 1)) {        // Check Flag & Requested Time
00170             CheckCommandDone_Flag = 0;          // Reset Flag
00171             memset(Data_Buffer, 0, sizeof(Data_Buffer));                    // Clear Data_Buffer
00172 
00173             TimeStamp.start();                  // Start Timer TimeStamp
00174             Thread_IMU.start(callback(&queue, &EventQueue::dispatch_forever));      // Start "ReadIMU" Thread
00175             Ticker_IMU.attach_us(queue.event(&ReadIMU), 2000);                      // Attach 2ms Ticker to "ReadIMU"
00176         }
00177 
00178         while ((writePointer != readPointer) && (writePointer_MIC != readPointer_MIC) && (ReadIMUDone_Flag == 1)) {     // Check Flag & Pointer Position
00179             while ((Data_Size + Line_Size) < 1024) {        // Check Maximum Package Size
00180                 if ((writePointer != readPointer) && (writePointer_MIC != readPointer_MIC)) {
00181                     Data_Size += sprintf(&Data_Buffer[Data_Size], "%d;%d;%d;%d;%d\n", Data_Storage[readPointer++], Data_Storage_MIC[readPointer_MIC++], Data_Storage[readPointer++], Data_Storage[readPointer++], Data_Storage[readPointer++]);
00182                     Line_Size = snprintf(NULL, 0, "%d;%d;%d;%d;%d\n", Data_Storage[readPointer], Data_Storage_MIC[readPointer_MIC], Data_Storage[readPointer], Data_Storage[readPointer], Data_Storage[readPointer]);
00183                 } else
00184                     break;
00185             }
00186 
00187             response = Socket_TCP.send(Data_Buffer, Data_Size);        // Send Data via TCP_Socket
00188             while (response != Data_Size)                              // Sending Not Successful
00189                 response = Socket_TCP.send(Data_Buffer, Data_Size);    // Resend Data via TCP_Socket
00190                 
00191             Data_Size = 0;      // Reset Data_Size
00192             memset(Data_Buffer, 0, sizeof(Data_Buffer));    // Clear Data_Buffer
00193             wait(0.1);          // Wait 100ms between Packages for ESP8266 Module
00194 
00195             if ((readPointer == writePointer) && (writePointer_MIC == readPointer_MIC)) {       // Check if Readpointer reached Writepointer Position
00196                 ReadIMUDone_Flag = 0;           // Reset Flag for Sending
00197                 PC.printf("Sending Done! - Time Taken: %d ms\n\n", TimeStamp.read_ms());        // Show Time used for Data Sending
00198                 TimeStamp.stop();           // Stop Timer TimeStamp
00199                 TimeStamp.reset();          // Reset Timer TimeStamp
00200 
00201                 Reset();        // Reset All Variables
00202                 Thread_ReceiveCommand.start(callback(&queue, &EventQueue::dispatch_forever));       // Start "ReceiveCommand" Thread
00203                 Ticker_ReceiveCommand.attach(queue.event(&ReceiveCommand), 0.500);                  // Attach 500ms Ticker to "ReceiveCommand"
00204 
00205                 Thread_ReadBattery.start(callback(&queue, &EventQueue::dispatch_forever));       // Start "ReadBattery"
00206                 Ticker_ReadBattery.attach(queue.event(&ReadBattery), 30);                        // Attach 30s Ticker to "ReadBattery"
00207             }
00208         }
00209     }
00210 }
00211 
00212 //-----------------------------------------------------------------
00213 
00214 //-----------------------------------------------------------------