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: MPU9250_SPI
Fork of WearableDevice_Nucleo by
main.cpp@28:c7e977a19564, 2017-12-19 (annotated)
- Committer:
- Muglug
- Date:
- Tue Dec 19 10:15:19 2017 +0000
- Revision:
- 28:c7e977a19564
- Parent:
- 27:c4b2ce6fa5b8
- Child:
- 29:ab809198c1ba
Newest Version.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| gusteibolt | 1:a6dc717fb060 | 1 | /* |
| gusteibolt | 1:a6dc717fb060 | 2 | * Los Putacos |
| gusteibolt | 1:a6dc717fb060 | 3 | * Copyright (C) 2017, All rights reserved. |
| Muglug | 18:7f9c2b8541e1 | 4 | * ________________________________________ |
| gusteibolt | 1:a6dc717fb060 | 5 | * |
| gusteibolt | 1:a6dc717fb060 | 6 | * Created by: Gustavo Campana, Michael Schmidt, Miguel Lopez |
| Muglug | 28:c7e977a19564 | 7 | * Date: 02-Dec-2017 |
| gusteibolt | 1:a6dc717fb060 | 8 | * Version: V0.1 |
| gusteibolt | 1:a6dc717fb060 | 9 | */ |
| Muglug | 23:aad5fd1b3ef9 | 10 | //----------------------------------------------------------------- |
| gusteibolt | 1:a6dc717fb060 | 11 | |
| gusteibolt | 1:a6dc717fb060 | 12 | //----------------------------------------------------------------- |
| Muglug | 28:c7e977a19564 | 13 | // "THE BEER-WARE LICENSE" (Revision 42): |
| Muglug | 28:c7e977a19564 | 14 | // The "Los Putacos" team wrote this file. As long as you retain this notice you |
| Muglug | 28:c7e977a19564 | 15 | // can do whatever you want with this stuff. If we meet some day, and you think |
| Muglug | 28:c7e977a19564 | 16 | // this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp |
| gusteibolt | 1:a6dc717fb060 | 17 | //----------------------------------------------------------------- |
| gusteibolt | 1:a6dc717fb060 | 18 | |
| gusteibolt | 1:a6dc717fb060 | 19 | //----------------------------------------------------------------- |
| gusteibolt | 1:a6dc717fb060 | 20 | // Includes |
| Muglug | 25:86137c182a17 | 21 | #include "mbed.h" |
| Muglug | 28:c7e977a19564 | 22 | #include "event.h" |
| Muglug | 28:c7e977a19564 | 23 | #include "MPU9250.h" |
| Muglug | 28:c7e977a19564 | 24 | #include "TCPSocket.h" |
| Muglug | 23:aad5fd1b3ef9 | 25 | #include "configuration.h" |
| Muglug | 28:c7e977a19564 | 26 | #include "ESP8266Interface.h" |
| gusteibolt | 3:26aeff25f610 | 27 | |
| gusteibolt | 1:a6dc717fb060 | 28 | //----------------------------------------------------------------- |
| gusteibolt | 0:73cd0cb02330 | 29 | |
| gusteibolt | 1:a6dc717fb060 | 30 | //----------------------------------------------------------------- |
| gusteibolt | 1:a6dc717fb060 | 31 | // Declarations |
| Muglug | 28:c7e977a19564 | 32 | Timer Testing; |
| Muglug | 26:4bc56ce08d15 | 33 | Timer TimeStamp; // Timer µS time-stamp |
| Muglug | 26:4bc56ce08d15 | 34 | Serial PC(USBTX, USBRX); // Create an Serial PC Object - USBTX, USBRX |
| Muglug | 28:c7e977a19564 | 35 | |
| Muglug | 28:c7e977a19564 | 36 | SPI spi(IMU_MOSI, IMU_MISO,IMU_SCK); // Create an SPI Object for MPU9250 - MOSI, MISO, SCK |
| Muglug | 28:c7e977a19564 | 37 | mpu9250_spi imu(spi, IMU_CS); // Create an MPU9250 Object - SPI Object, CS |
| Muglug | 28:c7e977a19564 | 38 | |
| Muglug | 28:c7e977a19564 | 39 | ESP8266Interface WiFi(ESP_TX, ESP_RX); // ESP8266 WiFi Interface |
| Muglug | 28:c7e977a19564 | 40 | TCPSocket Socket_TCP; // TCP Socket |
| Muglug | 25:86137c182a17 | 41 | |
| Muglug | 25:86137c182a17 | 42 | // Events |
| Muglug | 25:86137c182a17 | 43 | EventQueue queue(32 * EVENTS_EVENT_SIZE); // Event Setup |
| Muglug | 25:86137c182a17 | 44 | |
| Muglug | 25:86137c182a17 | 45 | // Tickers |
| Muglug | 26:4bc56ce08d15 | 46 | Ticker Ticker_IMU; |
| Muglug | 28:c7e977a19564 | 47 | Ticker Ticker_ReceiveCommand; |
| Muglug | 25:86137c182a17 | 48 | |
| Muglug | 25:86137c182a17 | 49 | // Threads |
| Muglug | 26:4bc56ce08d15 | 50 | Thread Thread_IMU(osPriorityRealtime); |
| Muglug | 28:c7e977a19564 | 51 | Thread Thread_ReceiveCommand(osPriorityNormal); |
| gusteibolt | 3:26aeff25f610 | 52 | |
| Muglug | 26:4bc56ce08d15 | 53 | // Global Variables |
| Muglug | 28:c7e977a19564 | 54 | uint32_t len = 0; |
| Muglug | 26:4bc56ce08d15 | 55 | uint16_t Time_Data = 0; |
| Muglug | 27:c4b2ce6fa5b8 | 56 | uint32_t Data_Size = 0; |
| Muglug | 27:c4b2ce6fa5b8 | 57 | uint16_t LineCount = 0; |
| Muglug | 26:4bc56ce08d15 | 58 | volatile uint16_t readPointer = 0; |
| Muglug | 26:4bc56ce08d15 | 59 | volatile uint16_t readPointer_MIC = 0; |
| Muglug | 26:4bc56ce08d15 | 60 | volatile uint16_t writePointer = 0; |
| Muglug | 26:4bc56ce08d15 | 61 | volatile uint16_t writePointer_MIC = 0; |
| Muglug | 24:eed68c95160c | 62 | |
| Muglug | 27:c4b2ce6fa5b8 | 63 | uint8_t ReadIMUDone_Flag = 0; |
| Muglug | 28:c7e977a19564 | 64 | uint8_t CheckCommandDone_Flag = 0; |
| Muglug | 27:c4b2ce6fa5b8 | 65 | |
| Muglug | 26:4bc56ce08d15 | 66 | uint8_t Requested_Time = 0; |
| Muglug | 26:4bc56ce08d15 | 67 | uint8_t Current_Position = 0; |
| Muglug | 28:c7e977a19564 | 68 | |
| Muglug | 26:4bc56ce08d15 | 69 | char Time_Buffer[2]; |
| Muglug | 28:c7e977a19564 | 70 | char Data_Buffer[2048]; |
| Muglug | 26:4bc56ce08d15 | 71 | const char DeviceNr[6] = "DEV01"; |
| Muglug | 26:4bc56ce08d15 | 72 | int16_t Data_Storage[BufferSize]; // BufferSize defined in "event.h" |
| Muglug | 26:4bc56ce08d15 | 73 | uint16_t Data_Storage_MIC[BufferSize_MIC]; // BufferSize_MIC defined in "event.h" |
| Muglug | 23:aad5fd1b3ef9 | 74 | |
| gusteibolt | 1:a6dc717fb060 | 75 | //----------------------------------------------------------------- |
| gusteibolt | 1:a6dc717fb060 | 76 | |
| gusteibolt | 1:a6dc717fb060 | 77 | //----------------------------------------------------------------- |
| Muglug | 18:7f9c2b8541e1 | 78 | void Setup() |
| Muglug | 18:7f9c2b8541e1 | 79 | { |
| Muglug | 26:4bc56ce08d15 | 80 | PC.baud(230400); // Initialize PC Serial Connection |
| Muglug | 24:eed68c95160c | 81 | PC.printf("\r\n------------- Booting! -------------\r\n"); |
| Muglug | 23:aad5fd1b3ef9 | 82 | PC.printf("CPU SystemCoreClock is %d Hz", SystemCoreClock); |
| Muglug | 28:c7e977a19564 | 83 | |
| Muglug | 28:c7e977a19564 | 84 | PC.printf("\nConnecting to %s with %s...\n", WiFi_SSID, WiFi_Pass); |
| Muglug | 28:c7e977a19564 | 85 | int ret = WiFi.connect(WiFi_SSID, WiFi_Pass, NSAPI_SECURITY_WPA_WPA2); |
| Muglug | 28:c7e977a19564 | 86 | if (ret != 0) { |
| Muglug | 28:c7e977a19564 | 87 | printf("\nConnection error\n"); |
| Muglug | 28:c7e977a19564 | 88 | } |
| Muglug | 28:c7e977a19564 | 89 | |
| Muglug | 28:c7e977a19564 | 90 | nsapi_error_t response; |
| Muglug | 28:c7e977a19564 | 91 | Socket_TCP.open(&WiFi); |
| Muglug | 28:c7e977a19564 | 92 | Socket_TCP.set_blocking(false); |
| Muglug | 26:4bc56ce08d15 | 93 | wait(0.5); |
| Muglug | 18:7f9c2b8541e1 | 94 | |
| Muglug | 28:c7e977a19564 | 95 | response = Socket_TCP.connect("192.168.43.96", 8080); |
| Muglug | 28:c7e977a19564 | 96 | while (0 != response) { |
| Muglug | 28:c7e977a19564 | 97 | wait(0.5); |
| Muglug | 28:c7e977a19564 | 98 | response = Socket_TCP.connect("192.168.43.96", 8080); |
| Muglug | 28:c7e977a19564 | 99 | } |
| Muglug | 28:c7e977a19564 | 100 | |
| Muglug | 18:7f9c2b8541e1 | 101 | |
| Muglug | 25:86137c182a17 | 102 | // Initialize IMU SPI Connection |
| Muglug | 26:4bc56ce08d15 | 103 | if(imu.init(1, BITS_DLPF_CFG_188HZ)) // Initialize the MPU9250 |
| Muglug | 24:eed68c95160c | 104 | PC.printf("\nCouldn't initialize MPU9250 via SPI!"); |
| Muglug | 26:4bc56ce08d15 | 105 | PC.printf("\nWHOAMI = 0x%2x", imu.whoami()); // Output I2C Address to check SPI (correct: 104 - 0x68) |
| Muglug | 25:86137c182a17 | 106 | PC.printf("\nAcc_Scale = %u\n", imu.set_acc_scale(BITS_FS_16G)); // Set Full Range for Acc. |
| Muglug | 26:4bc56ce08d15 | 107 | imu.calib_acc(); // Calibrate Acceleration Sensor |
| Muglug | 24:eed68c95160c | 108 | |
| Muglug | 26:4bc56ce08d15 | 109 | TimeStamp.reset(); // Reset Timer TimeStamp |
| Muglug | 28:c7e977a19564 | 110 | Thread_ReceiveCommand.start(callback(&queue, &EventQueue::dispatch_forever)); // Start Command-Receiving Thread |
| Muglug | 28:c7e977a19564 | 111 | Ticker_ReceiveCommand.attach(queue.event(&ReceiveCommand), 0.250); // Attach 250 ms Ticker to "ReceiveCommand" |
| Muglug | 28:c7e977a19564 | 112 | |
| Muglug | 26:4bc56ce08d15 | 113 | led1 = 1; // Turn ON LED to display 'Ready!' |
| Muglug | 26:4bc56ce08d15 | 114 | PC.printf("\r\n------------- Ready! ---------------\r\n"); |
| Muglug | 28:c7e977a19564 | 115 | } |
| Muglug | 24:eed68c95160c | 116 | |
| Muglug | 23:aad5fd1b3ef9 | 117 | //----------------------------------------------------------------- |
| Muglug | 18:7f9c2b8541e1 | 118 | |
| Mister_Lopez | 4:56e903769e94 | 119 | //----------------------------------------------------------------- |
| Muglug | 27:c4b2ce6fa5b8 | 120 | void Reset() // Reset All Variables |
| Muglug | 27:c4b2ce6fa5b8 | 121 | { |
| Muglug | 28:c7e977a19564 | 122 | len = 0; |
| Muglug | 27:c4b2ce6fa5b8 | 123 | |
| Muglug | 27:c4b2ce6fa5b8 | 124 | |
| Muglug | 27:c4b2ce6fa5b8 | 125 | memset(Data_Storage, 0, sizeof(Data_Storage)); |
| Muglug | 27:c4b2ce6fa5b8 | 126 | memset(Data_Storage_MIC, 0, sizeof(Data_Storage_MIC)); |
| Muglug | 27:c4b2ce6fa5b8 | 127 | } |
| Muglug | 28:c7e977a19564 | 128 | |
| Muglug | 27:c4b2ce6fa5b8 | 129 | //----------------------------------------------------------------- |
| Muglug | 27:c4b2ce6fa5b8 | 130 | |
| Muglug | 27:c4b2ce6fa5b8 | 131 | //----------------------------------------------------------------- |
| gusteibolt | 0:73cd0cb02330 | 132 | int main() |
| gusteibolt | 0:73cd0cb02330 | 133 | { |
| Muglug | 18:7f9c2b8541e1 | 134 | Setup(); // Initial Setups |
| Muglug | 18:7f9c2b8541e1 | 135 | |
| Muglug | 26:4bc56ce08d15 | 136 | while (true) { |
| Muglug | 28:c7e977a19564 | 137 | if ((Requested_Time != 0) && (CheckCommandDone_Flag == 1)) { |
| Muglug | 28:c7e977a19564 | 138 | CheckCommandDone_Flag = 0; |
| Muglug | 27:c4b2ce6fa5b8 | 139 | |
| Muglug | 28:c7e977a19564 | 140 | TimeStamp.start(); |
| Muglug | 28:c7e977a19564 | 141 | Thread_IMU.start(callback(&queue, &EventQueue::dispatch_forever)); // Start IMU-Reading Thread |
| Muglug | 28:c7e977a19564 | 142 | Ticker_IMU.attach_us(queue.event(&ReadIMU), 1000); // Attach 1 ms Ticker to "ReadIMU" |
| Muglug | 26:4bc56ce08d15 | 143 | } |
| Muglug | 26:4bc56ce08d15 | 144 | |
| Muglug | 28:c7e977a19564 | 145 | while ((writePointer != readPointer) && (writePointer_MIC != readPointer_MIC) && (ReadIMUDone_Flag == 1)) { |
| Muglug | 28:c7e977a19564 | 146 | Testing.start(); |
| Muglug | 28:c7e977a19564 | 147 | |
| Muglug | 28:c7e977a19564 | 148 | while ((Data_Size + len) < 1024) { |
| Muglug | 28:c7e977a19564 | 149 | if ((writePointer != readPointer) && (writePointer_MIC != readPointer_MIC)) { |
| Muglug | 28:c7e977a19564 | 150 | 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++]); |
| Muglug | 28:c7e977a19564 | 151 | len = 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]); |
| Muglug | 28:c7e977a19564 | 152 | } else |
| Muglug | 28:c7e977a19564 | 153 | break; |
| Muglug | 28:c7e977a19564 | 154 | } |
| Muglug | 28:c7e977a19564 | 155 | |
| Muglug | 28:c7e977a19564 | 156 | PC.printf("Package Size: %d\n", Data_Size); |
| Muglug | 28:c7e977a19564 | 157 | Socket_TCP.send(Data_Buffer, Data_Size); |
| Muglug | 28:c7e977a19564 | 158 | |
| Muglug | 28:c7e977a19564 | 159 | memset(Data_Buffer, 0, sizeof(Data_Buffer)); |
| Muglug | 28:c7e977a19564 | 160 | PC.printf("Time: %d\n\n\n", Testing.read_ms()); |
| Muglug | 25:86137c182a17 | 161 | |
| Muglug | 28:c7e977a19564 | 162 | |
| Muglug | 28:c7e977a19564 | 163 | Data_Size = 0; |
| Muglug | 28:c7e977a19564 | 164 | wait(0.1); |
| Muglug | 28:c7e977a19564 | 165 | Testing.stop(); |
| Muglug | 28:c7e977a19564 | 166 | Testing.reset(); |
| Muglug | 26:4bc56ce08d15 | 167 | |
| Muglug | 28:c7e977a19564 | 168 | if ((readPointer == writePointer) && (writePointer_MIC == readPointer_MIC)) { |
| Muglug | 28:c7e977a19564 | 169 | ReadIMUDone_Flag = 0; |
| Muglug | 28:c7e977a19564 | 170 | PC.printf("Sending Done! - Time Taken: %d ms", TimeStamp.read_ms()); |
| Muglug | 28:c7e977a19564 | 171 | TimeStamp.stop(); |
| Muglug | 28:c7e977a19564 | 172 | TimeStamp.reset(); |
| Muglug | 28:c7e977a19564 | 173 | |
| Muglug | 28:c7e977a19564 | 174 | Reset(); |
| Muglug | 28:c7e977a19564 | 175 | Thread_ReceiveCommand.start(callback(&queue, &EventQueue::dispatch_forever)); // Start Command-Receiving Thread |
| Muglug | 28:c7e977a19564 | 176 | Ticker_ReceiveCommand.attach(queue.event(&ReceiveCommand), 0.250); // Attach 250 ms Ticker to "ReceiveCommand |
| Muglug | 28:c7e977a19564 | 177 | } |
| Muglug | 26:4bc56ce08d15 | 178 | } |
| Muglug | 26:4bc56ce08d15 | 179 | |
| Muglug | 28:c7e977a19564 | 180 | /* sprintf(Data_Buffer, "%d;%d;%d;", (int)(1000*imu.accelerometer_data[0]), (int)(1000*imu.accelerometer_data[1]), (int)(1000*imu.accelerometer_data[2])); |
| Muglug | 28:c7e977a19564 | 181 | Socket_TCP.send(Data_Buffer, strlen(Data_Buffer)); |
| Muglug | 28:c7e977a19564 | 182 | memset(Data_Buffer, 0, sizeof(Data_Buffer));*/ |
| Muglug | 25:86137c182a17 | 183 | |
| Muglug | 19:0a3ae902722e | 184 | } |
| gusteibolt | 0:73cd0cb02330 | 185 | } |
| Muglug | 28:c7e977a19564 | 186 | |
| Muglug | 28:c7e977a19564 | 187 | //----------------------------------------------------------------- |
| Muglug | 28:c7e977a19564 | 188 | |
| Muglug | 18:7f9c2b8541e1 | 189 | //----------------------------------------------------------------- |
