Acquisition for GNSS1A1
Dependencies: F7_Ethernet X_NUCLEO_IKS01A2 mbed-rtos mbed
Fork of Test2Boards by
main.cpp@1:ef1bbf9b6205, 2018-01-26 (annotated)
- Committer:
- nirnakern
- Date:
- Fri Jan 26 09:52:12 2018 +0000
- Revision:
- 1:ef1bbf9b6205
- Parent:
- 0:4e088cbb2dbf
- Child:
- 2:1eb73fe8ab8f
First commit of GPS acquisition program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nirnakern | 1:ef1bbf9b6205 | 1 | /*QUESTA è LA VERSIONE PER IL GPS SU UART*/ |
nirnakern | 1:ef1bbf9b6205 | 2 | |
nirnakern | 0:4e088cbb2dbf | 3 | #include "mbed.h" |
nirnakern | 0:4e088cbb2dbf | 4 | #include "rtos/rtos.h" |
nirnakern | 0:4e088cbb2dbf | 5 | #include "Teseo-LIV3F.h" |
nirnakern | 0:4e088cbb2dbf | 6 | #include <cstdlib> |
nirnakern | 0:4e088cbb2dbf | 7 | #include <vector> |
nirnakern | 0:4e088cbb2dbf | 8 | #include "XNucleoIKS01A2.h" |
nirnakern | 1:ef1bbf9b6205 | 9 | #include <algorithm> |
nirnakern | 1:ef1bbf9b6205 | 10 | #include "EthernetInterface.h" |
nirnakern | 1:ef1bbf9b6205 | 11 | #include "NTPClient.h" |
nirnakern | 1:ef1bbf9b6205 | 12 | |
nirnakern | 1:ef1bbf9b6205 | 13 | |
nirnakern | 1:ef1bbf9b6205 | 14 | /*IP E AFFINI*/ |
nirnakern | 1:ef1bbf9b6205 | 15 | |
nirnakern | 1:ef1bbf9b6205 | 16 | const char * IP = "169.254.112.125"; |
nirnakern | 1:ef1bbf9b6205 | 17 | const char * MASK = "255.255.255.0"; |
nirnakern | 1:ef1bbf9b6205 | 18 | const char * GATEWAY = "169.254.112.1"; |
nirnakern | 1:ef1bbf9b6205 | 19 | |
nirnakern | 1:ef1bbf9b6205 | 20 | |
nirnakern | 1:ef1bbf9b6205 | 21 | |
nirnakern | 1:ef1bbf9b6205 | 22 | /*cose imu*/ |
nirnakern | 1:ef1bbf9b6205 | 23 | |
nirnakern | 1:ef1bbf9b6205 | 24 | #define ECHO_SERVER_PORT 7 |
nirnakern | 1:ef1bbf9b6205 | 25 | |
nirnakern | 1:ef1bbf9b6205 | 26 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
nirnakern | 1:ef1bbf9b6205 | 27 | Timer t; |
nirnakern | 1:ef1bbf9b6205 | 28 | |
nirnakern | 1:ef1bbf9b6205 | 29 | DigitalOut myled1(LED1); |
nirnakern | 1:ef1bbf9b6205 | 30 | DigitalOut myled2(LED2); |
nirnakern | 1:ef1bbf9b6205 | 31 | DigitalOut myled3(LED3); |
nirnakern | 1:ef1bbf9b6205 | 32 | |
nirnakern | 1:ef1bbf9b6205 | 33 | |
nirnakern | 0:4e088cbb2dbf | 34 | |
nirnakern | 0:4e088cbb2dbf | 35 | /*cose per gps*/ |
nirnakern | 0:4e088cbb2dbf | 36 | static Thread t1; |
nirnakern | 0:4e088cbb2dbf | 37 | static Thread t2; |
nirnakern | 0:4e088cbb2dbf | 38 | |
nirnakern | 1:ef1bbf9b6205 | 39 | static Mutex message_access; |
nirnakern | 0:4e088cbb2dbf | 40 | |
nirnakern | 0:4e088cbb2dbf | 41 | static Timer system_timer; |
nirnakern | 0:4e088cbb2dbf | 42 | static Serial usb_serial(USBTX, USBRX, 9600); |
nirnakern | 0:4e088cbb2dbf | 43 | static Serial *serial_debug= &usb_serial; |
nirnakern | 0:4e088cbb2dbf | 44 | |
nirnakern | 1:ef1bbf9b6205 | 45 | static std::vector<int> ids; |
nirnakern | 1:ef1bbf9b6205 | 46 | |
nirnakern | 1:ef1bbf9b6205 | 47 | |
nirnakern | 0:4e088cbb2dbf | 48 | |
nirnakern | 0:4e088cbb2dbf | 49 | static Teseo_LIV3F Teseo(D8, D13, D6, PE_7, PE_8, &usb_serial); |
nirnakern | 0:4e088cbb2dbf | 50 | |
nirnakern | 0:4e088cbb2dbf | 51 | |
nirnakern | 0:4e088cbb2dbf | 52 | struct gps_coordinate{ |
nirnakern | 0:4e088cbb2dbf | 53 | float lat; |
nirnakern | 0:4e088cbb2dbf | 54 | char lat_cardinal; |
nirnakern | 0:4e088cbb2dbf | 55 | float longitud; |
nirnakern | 0:4e088cbb2dbf | 56 | char long_cardinal; |
nirnakern | 0:4e088cbb2dbf | 57 | bool valid; |
nirnakern | 0:4e088cbb2dbf | 58 | |
nirnakern | 0:4e088cbb2dbf | 59 | |
nirnakern | 0:4e088cbb2dbf | 60 | |
nirnakern | 0:4e088cbb2dbf | 61 | }; |
nirnakern | 0:4e088cbb2dbf | 62 | |
nirnakern | 0:4e088cbb2dbf | 63 | gps_coordinate myPosition; |
nirnakern | 0:4e088cbb2dbf | 64 | |
nirnakern | 0:4e088cbb2dbf | 65 | |
nirnakern | 0:4e088cbb2dbf | 66 | |
nirnakern | 0:4e088cbb2dbf | 67 | /*cose per sensori*/ |
nirnakern | 0:4e088cbb2dbf | 68 | |
nirnakern | 0:4e088cbb2dbf | 69 | static Thread t3; |
nirnakern | 0:4e088cbb2dbf | 70 | |
nirnakern | 1:ef1bbf9b6205 | 71 | |
nirnakern | 0:4e088cbb2dbf | 72 | |
nirnakern | 0:4e088cbb2dbf | 73 | |
nirnakern | 0:4e088cbb2dbf | 74 | /* Retrieve the composing elements of the expansion board */ |
nirnakern | 1:ef1bbf9b6205 | 75 | |
nirnakern | 1:ef1bbf9b6205 | 76 | |
nirnakern | 1:ef1bbf9b6205 | 77 | /*THIS FUNCTION IS SIMPLY USED TO PRINT ON SERIAL THE NMEA MSG*/ |
nirnakern | 1:ef1bbf9b6205 | 78 | /* |
nirnakern | 1:ef1bbf9b6205 | 79 | void print_function (enum nmea_msg_id id, char *buf, int len){ |
nirnakern | 1:ef1bbf9b6205 | 80 | |
nirnakern | 1:ef1bbf9b6205 | 81 | if (id == NMEA_END__) { |
nirnakern | 1:ef1bbf9b6205 | 82 | puts("Not found\n\r"); |
nirnakern | 1:ef1bbf9b6205 | 83 | return; |
nirnakern | 1:ef1bbf9b6205 | 84 | } |
nirnakern | 0:4e088cbb2dbf | 85 | |
nirnakern | 1:ef1bbf9b6205 | 86 | if (!len) { |
nirnakern | 1:ef1bbf9b6205 | 87 | puts("Not valid\n\r"); |
nirnakern | 1:ef1bbf9b6205 | 88 | return; |
nirnakern | 1:ef1bbf9b6205 | 89 | } |
nirnakern | 1:ef1bbf9b6205 | 90 | printf("notify: %d " , id); |
nirnakern | 1:ef1bbf9b6205 | 91 | for (int i = 0; i < len; ++i) |
nirnakern | 1:ef1bbf9b6205 | 92 | printf("%c",buf[i]); |
nirnakern | 1:ef1bbf9b6205 | 93 | puts("\n\r"); |
nirnakern | 1:ef1bbf9b6205 | 94 | |
nirnakern | 1:ef1bbf9b6205 | 95 | |
nirnakern | 1:ef1bbf9b6205 | 96 | |
nirnakern | 1:ef1bbf9b6205 | 97 | } |
nirnakern | 1:ef1bbf9b6205 | 98 | */ |
nirnakern | 1:ef1bbf9b6205 | 99 | /*THIS FUNCTION PRINT ONLY THE MESSAGE WITH ID SAVED IN STATIC VARIALBE ids*/ |
nirnakern | 1:ef1bbf9b6205 | 100 | /* |
nirnakern | 1:ef1bbf9b6205 | 101 | void getSpectificMsg (){ |
nirnakern | 0:4e088cbb2dbf | 102 | |
nirnakern | 0:4e088cbb2dbf | 103 | enum nmea_msg_id id; |
nirnakern | 0:4e088cbb2dbf | 104 | |
nirnakern | 0:4e088cbb2dbf | 105 | Teseo.Reset(serial_debug); |
nirnakern | 0:4e088cbb2dbf | 106 | Teseo.startListener(serial_debug); |
nirnakern | 0:4e088cbb2dbf | 107 | |
nirnakern | 0:4e088cbb2dbf | 108 | while(1) { |
nirnakern | 1:ef1bbf9b6205 | 109 | |
nirnakern | 0:4e088cbb2dbf | 110 | osEvent evt = Teseo.queue.get(); |
nirnakern | 1:ef1bbf9b6205 | 111 | if (evt.status == osEventMessage){ |
nirnakern | 1:ef1bbf9b6205 | 112 | |
nirnakern | 1:ef1bbf9b6205 | 113 | |
nirnakern | 0:4e088cbb2dbf | 114 | struct teseo_msg *message = (struct teseo_msg *)evt.value.p; |
nirnakern | 0:4e088cbb2dbf | 115 | if (message->len){ |
nirnakern | 0:4e088cbb2dbf | 116 | id = Teseo.MsgDetect(message->buf, message->len, serial_debug); |
nirnakern | 1:ef1bbf9b6205 | 117 | } |
nirnakern | 1:ef1bbf9b6205 | 118 | |
nirnakern | 1:ef1bbf9b6205 | 119 | if ( find(ids.begin(), ids.end(), id) != ids.end() ){ |
nirnakern | 1:ef1bbf9b6205 | 120 | print_function (id, message->buf, message->len); |
nirnakern | 1:ef1bbf9b6205 | 121 | } |
nirnakern | 0:4e088cbb2dbf | 122 | |
nirnakern | 1:ef1bbf9b6205 | 123 | |
nirnakern | 1:ef1bbf9b6205 | 124 | |
nirnakern | 1:ef1bbf9b6205 | 125 | |
nirnakern | 0:4e088cbb2dbf | 126 | |
nirnakern | 1:ef1bbf9b6205 | 127 | Teseo.mpool.free(message); |
nirnakern | 1:ef1bbf9b6205 | 128 | |
nirnakern | 1:ef1bbf9b6205 | 129 | } |
nirnakern | 0:4e088cbb2dbf | 130 | |
nirnakern | 0:4e088cbb2dbf | 131 | |
nirnakern | 1:ef1bbf9b6205 | 132 | |
nirnakern | 0:4e088cbb2dbf | 133 | |
nirnakern | 1:ef1bbf9b6205 | 134 | |
nirnakern | 0:4e088cbb2dbf | 135 | |
nirnakern | 0:4e088cbb2dbf | 136 | } |
nirnakern | 0:4e088cbb2dbf | 137 | |
nirnakern | 0:4e088cbb2dbf | 138 | } |
nirnakern | 0:4e088cbb2dbf | 139 | |
nirnakern | 0:4e088cbb2dbf | 140 | |
nirnakern | 0:4e088cbb2dbf | 141 | |
nirnakern | 0:4e088cbb2dbf | 142 | |
nirnakern | 1:ef1bbf9b6205 | 143 | */ |
nirnakern | 0:4e088cbb2dbf | 144 | |
nirnakern | 0:4e088cbb2dbf | 145 | |
nirnakern | 0:4e088cbb2dbf | 146 | int main() { |
nirnakern | 0:4e088cbb2dbf | 147 | |
nirnakern | 1:ef1bbf9b6205 | 148 | |
nirnakern | 1:ef1bbf9b6205 | 149 | |
nirnakern | 1:ef1bbf9b6205 | 150 | //cpose per comunicazioni |
nirnakern | 1:ef1bbf9b6205 | 151 | |
nirnakern | 1:ef1bbf9b6205 | 152 | |
nirnakern | 1:ef1bbf9b6205 | 153 | |
nirnakern | 1:ef1bbf9b6205 | 154 | |
nirnakern | 1:ef1bbf9b6205 | 155 | EthernetInterface eth; |
nirnakern | 1:ef1bbf9b6205 | 156 | eth.init(IP,MASK,GATEWAY); //Use DHCP |
nirnakern | 1:ef1bbf9b6205 | 157 | eth.connect(); |
nirnakern | 1:ef1bbf9b6205 | 158 | printf("IP Address is %s\n", eth.getIPAddress()); |
nirnakern | 1:ef1bbf9b6205 | 159 | |
nirnakern | 0:4e088cbb2dbf | 160 | |
nirnakern | 1:ef1bbf9b6205 | 161 | |
nirnakern | 1:ef1bbf9b6205 | 162 | |
nirnakern | 1:ef1bbf9b6205 | 163 | UDPSocket server; |
nirnakern | 1:ef1bbf9b6205 | 164 | server.bind(ECHO_SERVER_PORT); |
nirnakern | 1:ef1bbf9b6205 | 165 | |
nirnakern | 1:ef1bbf9b6205 | 166 | |
nirnakern | 1:ef1bbf9b6205 | 167 | Endpoint client; |
nirnakern | 1:ef1bbf9b6205 | 168 | char buffer[256]; |
nirnakern | 1:ef1bbf9b6205 | 169 | char outBuffer[256]; |
nirnakern | 1:ef1bbf9b6205 | 170 | int32_t packetnumber =0; |
nirnakern | 1:ef1bbf9b6205 | 171 | |
nirnakern | 1:ef1bbf9b6205 | 172 | |
nirnakern | 1:ef1bbf9b6205 | 173 | NTPClient ntp; |
nirnakern | 1:ef1bbf9b6205 | 174 | ntp.setTime("169.254.92.70", 123); |
nirnakern | 1:ef1bbf9b6205 | 175 | |
nirnakern | 1:ef1bbf9b6205 | 176 | |
nirnakern | 1:ef1bbf9b6205 | 177 | time_t ctTime; |
nirnakern | 1:ef1bbf9b6205 | 178 | ctTime = time(NULL); |
nirnakern | 1:ef1bbf9b6205 | 179 | printf("Time is set to (UTC): %s\r", ctime(&ctTime)); |
nirnakern | 1:ef1bbf9b6205 | 180 | |
nirnakern | 1:ef1bbf9b6205 | 181 | while(1) { |
nirnakern | 1:ef1bbf9b6205 | 182 | |
nirnakern | 1:ef1bbf9b6205 | 183 | printf("\nWaiting for UDP packet...\n"); |
nirnakern | 1:ef1bbf9b6205 | 184 | |
nirnakern | 1:ef1bbf9b6205 | 185 | myled1=1; |
nirnakern | 1:ef1bbf9b6205 | 186 | int n = server.receiveFrom(client, buffer, sizeof(buffer)); |
nirnakern | 1:ef1bbf9b6205 | 187 | buffer[n] = '\0'; |
nirnakern | 1:ef1bbf9b6205 | 188 | |
nirnakern | 1:ef1bbf9b6205 | 189 | printf("Received packet from: %s\n", client.get_address()); |
nirnakern | 1:ef1bbf9b6205 | 190 | printf("Packet contents : '%s'\n",buffer); |
nirnakern | 1:ef1bbf9b6205 | 191 | |
nirnakern | 1:ef1bbf9b6205 | 192 | |
nirnakern | 1:ef1bbf9b6205 | 193 | //set time |
nirnakern | 1:ef1bbf9b6205 | 194 | NTPClient ntp; |
nirnakern | 1:ef1bbf9b6205 | 195 | ntp.setTime(client.get_address(), 123,10000); |
nirnakern | 0:4e088cbb2dbf | 196 | |
nirnakern | 1:ef1bbf9b6205 | 197 | |
nirnakern | 1:ef1bbf9b6205 | 198 | time_t ctTime; |
nirnakern | 1:ef1bbf9b6205 | 199 | ctTime = time(NULL); |
nirnakern | 1:ef1bbf9b6205 | 200 | printf("Time is set to (UTC): %s\r", ctime(&ctTime)); |
nirnakern | 1:ef1bbf9b6205 | 201 | time_t offset = time(NULL); |
nirnakern | 1:ef1bbf9b6205 | 202 | t.start(); |
nirnakern | 1:ef1bbf9b6205 | 203 | |
nirnakern | 1:ef1bbf9b6205 | 204 | |
nirnakern | 1:ef1bbf9b6205 | 205 | printf("Start Sending GPS messages\n"); |
nirnakern | 1:ef1bbf9b6205 | 206 | |
nirnakern | 1:ef1bbf9b6205 | 207 | myled2=1; |
nirnakern | 1:ef1bbf9b6205 | 208 | |
nirnakern | 1:ef1bbf9b6205 | 209 | myled3=1; |
nirnakern | 1:ef1bbf9b6205 | 210 | server.sendTo(client, buffer, n); |
nirnakern | 1:ef1bbf9b6205 | 211 | |
nirnakern | 1:ef1bbf9b6205 | 212 | |
nirnakern | 1:ef1bbf9b6205 | 213 | memset (&buffer[0], '\0', sizeof(buffer)); |
nirnakern | 1:ef1bbf9b6205 | 214 | while (1){ |
nirnakern | 1:ef1bbf9b6205 | 215 | packetnumber =packetnumber+1; |
nirnakern | 1:ef1bbf9b6205 | 216 | //send imu |
nirnakern | 1:ef1bbf9b6205 | 217 | |
nirnakern | 1:ef1bbf9b6205 | 218 | if (Teseo._uart.readable()) { |
nirnakern | 1:ef1bbf9b6205 | 219 | int c = Teseo._uart.getc(); |
nirnakern | 1:ef1bbf9b6205 | 220 | sprintf (buffer + strlen(buffer), "%c", c); |
nirnakern | 1:ef1bbf9b6205 | 221 | serial_debug->putc(c); |
nirnakern | 1:ef1bbf9b6205 | 222 | if (c =='\n'){ |
nirnakern | 1:ef1bbf9b6205 | 223 | // sprintf (outBuffer , "%d,%s",(t.read_ms()+(offset*1000)),buffer); |
nirnakern | 1:ef1bbf9b6205 | 224 | sprintf (outBuffer , "%s",buffer); |
nirnakern | 1:ef1bbf9b6205 | 225 | |
nirnakern | 1:ef1bbf9b6205 | 226 | n =sizeof (outBuffer); |
nirnakern | 1:ef1bbf9b6205 | 227 | server.sendTo(client, outBuffer, n); |
nirnakern | 1:ef1bbf9b6205 | 228 | memset (&buffer[0], '\0', sizeof(buffer)); |
nirnakern | 1:ef1bbf9b6205 | 229 | memset (&outBuffer[0], '\0', sizeof(outBuffer)); |
nirnakern | 1:ef1bbf9b6205 | 230 | |
nirnakern | 1:ef1bbf9b6205 | 231 | |
nirnakern | 1:ef1bbf9b6205 | 232 | } |
nirnakern | 1:ef1bbf9b6205 | 233 | } |
nirnakern | 1:ef1bbf9b6205 | 234 | |
nirnakern | 1:ef1bbf9b6205 | 235 | |
nirnakern | 1:ef1bbf9b6205 | 236 | |
nirnakern | 1:ef1bbf9b6205 | 237 | |
nirnakern | 1:ef1bbf9b6205 | 238 | //printf (buffer); |
nirnakern | 1:ef1bbf9b6205 | 239 | |
nirnakern | 1:ef1bbf9b6205 | 240 | //wait (1); |
nirnakern | 1:ef1bbf9b6205 | 241 | } |
nirnakern | 1:ef1bbf9b6205 | 242 | |
nirnakern | 1:ef1bbf9b6205 | 243 | |
nirnakern | 0:4e088cbb2dbf | 244 | |
nirnakern | 1:ef1bbf9b6205 | 245 | |
nirnakern | 1:ef1bbf9b6205 | 246 | } |
nirnakern | 0:4e088cbb2dbf | 247 | } |