nothing
Dependents: mbed-os-example-mbed5-wifi
stm32l475e_iot01_gps.cpp@0:81d4b52c0083, 2021-01-15 (annotated)
- Committer:
- ctlee
- Date:
- Fri Jan 15 13:21:24 2021 +0000
- Revision:
- 0:81d4b52c0083
nothing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ctlee | 0:81d4b52c0083 | 1 | |
ctlee | 0:81d4b52c0083 | 2 | #include "stm32l475e_iot01_gps.h" |
ctlee | 0:81d4b52c0083 | 3 | #include <stdio.h> |
ctlee | 0:81d4b52c0083 | 4 | #include <string.h> |
ctlee | 0:81d4b52c0083 | 5 | |
ctlee | 0:81d4b52c0083 | 6 | Serial pc1(USBTX, USBRX); |
ctlee | 0:81d4b52c0083 | 7 | |
ctlee | 0:81d4b52c0083 | 8 | GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) { |
ctlee | 0:81d4b52c0083 | 9 | _gps.baud(9600); |
ctlee | 0:81d4b52c0083 | 10 | |
ctlee | 0:81d4b52c0083 | 11 | longitude = 0.0; |
ctlee | 0:81d4b52c0083 | 12 | latitude = 0.0; |
ctlee | 0:81d4b52c0083 | 13 | ns = 'N'; |
ctlee | 0:81d4b52c0083 | 14 | ew = 'E'; |
ctlee | 0:81d4b52c0083 | 15 | alt = 0.0; |
ctlee | 0:81d4b52c0083 | 16 | |
ctlee | 0:81d4b52c0083 | 17 | } |
ctlee | 0:81d4b52c0083 | 18 | |
ctlee | 0:81d4b52c0083 | 19 | int GPS::sample() { |
ctlee | 0:81d4b52c0083 | 20 | float time, knots, angle, mag, mag_angle; |
ctlee | 0:81d4b52c0083 | 21 | int date, checksum; |
ctlee | 0:81d4b52c0083 | 22 | |
ctlee | 0:81d4b52c0083 | 23 | char lock; |
ctlee | 0:81d4b52c0083 | 24 | |
ctlee | 0:81d4b52c0083 | 25 | while(1) { |
ctlee | 0:81d4b52c0083 | 26 | getline(); |
ctlee | 0:81d4b52c0083 | 27 | // Check if it is a GPGGA msg (matches both locked and non-locked msg) |
ctlee | 0:81d4b52c0083 | 28 | //pc1.printf(msg); |
ctlee | 0:81d4b52c0083 | 29 | pc1.printf("\n"); |
ctlee | 0:81d4b52c0083 | 30 | wait(5); |
ctlee | 0:81d4b52c0083 | 31 | pc1.printf("display msg\r\n"); |
ctlee | 0:81d4b52c0083 | 32 | pc1.printf(msg); |
ctlee | 0:81d4b52c0083 | 33 | pc1.printf("\n"); |
ctlee | 0:81d4b52c0083 | 34 | if(sscanf(msg, "GPRMC,%f,%c,%f,%c,%f,%c,%f,%f,%d,%f,%f*%d", &time, &lock, &latitude, &ns, &longitude, &ew, &knots, &angle, &date, &mag, &mag_angle, &checksum) >= 1) { |
ctlee | 0:81d4b52c0083 | 35 | if(lock=='V') { |
ctlee | 0:81d4b52c0083 | 36 | pc1.printf("not valid!!!!!!!\n"); |
ctlee | 0:81d4b52c0083 | 37 | longitude = 0.0; |
ctlee | 0:81d4b52c0083 | 38 | latitude = 0.0; |
ctlee | 0:81d4b52c0083 | 39 | ns = 'Z'; |
ctlee | 0:81d4b52c0083 | 40 | ew = 'Z'; |
ctlee | 0:81d4b52c0083 | 41 | date =0; |
ctlee | 0:81d4b52c0083 | 42 | return 0; |
ctlee | 0:81d4b52c0083 | 43 | } else { |
ctlee | 0:81d4b52c0083 | 44 | //if(ns == 'S') { latitude *= -1.0; } |
ctlee | 0:81d4b52c0083 | 45 | // if(ew == 'W') { longitude *= -1.0; } |
ctlee | 0:81d4b52c0083 | 46 | // float degrees = trunc(latitude / 100.0f); |
ctlee | 0:81d4b52c0083 | 47 | // float minutes = latitude - (degrees * 100.0f); |
ctlee | 0:81d4b52c0083 | 48 | // latitude = degrees + minutes / 60.0f; |
ctlee | 0:81d4b52c0083 | 49 | // degrees = trunc(longitude / 100.0f * 0.01f); |
ctlee | 0:81d4b52c0083 | 50 | // minutes = longitude - (degrees * 100.0f); |
ctlee | 0:81d4b52c0083 | 51 | // longitude = degrees + minutes / 60.0f; |
ctlee | 0:81d4b52c0083 | 52 | // pc1.printf(msg); |
ctlee | 0:81d4b52c0083 | 53 | pc1.printf("\n\rlongitude is %f\n\r", longitude); |
ctlee | 0:81d4b52c0083 | 54 | pc1.printf("\n\rtime is %f\n\r", time); |
ctlee | 0:81d4b52c0083 | 55 | pc1.printf("ns is %c\n\r", ns); |
ctlee | 0:81d4b52c0083 | 56 | pc1.printf("ew is %c\n\r", ew); |
ctlee | 0:81d4b52c0083 | 57 | pc1.printf("alt is %d\n\r", date); |
ctlee | 0:81d4b52c0083 | 58 | |
ctlee | 0:81d4b52c0083 | 59 | latitude /= 100; |
ctlee | 0:81d4b52c0083 | 60 | longitude /= 100; |
ctlee | 0:81d4b52c0083 | 61 | |
ctlee | 0:81d4b52c0083 | 62 | return 1; |
ctlee | 0:81d4b52c0083 | 63 | } |
ctlee | 0:81d4b52c0083 | 64 | |
ctlee | 0:81d4b52c0083 | 65 | } |
ctlee | 0:81d4b52c0083 | 66 | return 0; |
ctlee | 0:81d4b52c0083 | 67 | } |
ctlee | 0:81d4b52c0083 | 68 | } |
ctlee | 0:81d4b52c0083 | 69 | |
ctlee | 0:81d4b52c0083 | 70 | float GPS::trunc(float v) { |
ctlee | 0:81d4b52c0083 | 71 | if(v < 0.0) { |
ctlee | 0:81d4b52c0083 | 72 | v*= -1.0; |
ctlee | 0:81d4b52c0083 | 73 | v = floor(v); |
ctlee | 0:81d4b52c0083 | 74 | v*=-1.0; |
ctlee | 0:81d4b52c0083 | 75 | } else { |
ctlee | 0:81d4b52c0083 | 76 | v = floor(v); |
ctlee | 0:81d4b52c0083 | 77 | } |
ctlee | 0:81d4b52c0083 | 78 | return v; |
ctlee | 0:81d4b52c0083 | 79 | } |
ctlee | 0:81d4b52c0083 | 80 | |
ctlee | 0:81d4b52c0083 | 81 | void GPS::getline() { |
ctlee | 0:81d4b52c0083 | 82 | int i; |
ctlee | 0:81d4b52c0083 | 83 | char a; |
ctlee | 0:81d4b52c0083 | 84 | int n; |
ctlee | 0:81d4b52c0083 | 85 | while(1) { |
ctlee | 0:81d4b52c0083 | 86 | memset(msg, '\0', sizeof(msg)); |
ctlee | 0:81d4b52c0083 | 87 | pc1.printf("\r\n\n New Message: "); |
ctlee | 0:81d4b52c0083 | 88 | pc1.printf(msg); |
ctlee | 0:81d4b52c0083 | 89 | pc1.printf("\r\n\n"); |
ctlee | 0:81d4b52c0083 | 90 | i = 0; |
ctlee | 0:81d4b52c0083 | 91 | pc1.printf("waiting...\n"); |
ctlee | 0:81d4b52c0083 | 92 | a = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 93 | msg[i] = a; |
ctlee | 0:81d4b52c0083 | 94 | if (a == '$') { |
ctlee | 0:81d4b52c0083 | 95 | pc1.printf("%c",a); |
ctlee | 0:81d4b52c0083 | 96 | msg[i]=a; |
ctlee | 0:81d4b52c0083 | 97 | a = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 98 | while(a!='$'){ |
ctlee | 0:81d4b52c0083 | 99 | msg[i] = a; |
ctlee | 0:81d4b52c0083 | 100 | i++; |
ctlee | 0:81d4b52c0083 | 101 | printf("%c",a); |
ctlee | 0:81d4b52c0083 | 102 | a = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 103 | } |
ctlee | 0:81d4b52c0083 | 104 | return; |
ctlee | 0:81d4b52c0083 | 105 | // msg[i] = a; |
ctlee | 0:81d4b52c0083 | 106 | // i++; |
ctlee | 0:81d4b52c0083 | 107 | // if (a == 'G') { |
ctlee | 0:81d4b52c0083 | 108 | // pc1.printf("%c",a); |
ctlee | 0:81d4b52c0083 | 109 | // a = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 110 | // msg[i] = a; |
ctlee | 0:81d4b52c0083 | 111 | // i++; |
ctlee | 0:81d4b52c0083 | 112 | // if (a == 'P') { |
ctlee | 0:81d4b52c0083 | 113 | // pc1.printf("%c",a); |
ctlee | 0:81d4b52c0083 | 114 | // a = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 115 | // msg[i] = a; |
ctlee | 0:81d4b52c0083 | 116 | // i++; |
ctlee | 0:81d4b52c0083 | 117 | // if (a == 'R') { |
ctlee | 0:81d4b52c0083 | 118 | // pc1.printf("%c",a); |
ctlee | 0:81d4b52c0083 | 119 | // a = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 120 | // msg[i] = a; |
ctlee | 0:81d4b52c0083 | 121 | // i++; |
ctlee | 0:81d4b52c0083 | 122 | // if (a == 'M') { |
ctlee | 0:81d4b52c0083 | 123 | // pc1.printf("%c",a); |
ctlee | 0:81d4b52c0083 | 124 | // a = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 125 | // msg[i] = a; |
ctlee | 0:81d4b52c0083 | 126 | // i++; |
ctlee | 0:81d4b52c0083 | 127 | // if (a == 'C') { |
ctlee | 0:81d4b52c0083 | 128 | // pc1.printf("%c",a); |
ctlee | 0:81d4b52c0083 | 129 | // a = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 130 | // msg[i] = a; |
ctlee | 0:81d4b52c0083 | 131 | // pc1.printf(msg); |
ctlee | 0:81d4b52c0083 | 132 | // pc1.printf("\r\n"); |
ctlee | 0:81d4b52c0083 | 133 | // |
ctlee | 0:81d4b52c0083 | 134 | // for (n = 5; n < 456; n++) { |
ctlee | 0:81d4b52c0083 | 135 | // msg[n] = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 136 | // pc1.printf("%c", msg[n]); |
ctlee | 0:81d4b52c0083 | 137 | // if(msg[n] == '\r') { |
ctlee | 0:81d4b52c0083 | 138 | // msg[n] = '0'; |
ctlee | 0:81d4b52c0083 | 139 | // return; |
ctlee | 0:81d4b52c0083 | 140 | // } |
ctlee | 0:81d4b52c0083 | 141 | // } |
ctlee | 0:81d4b52c0083 | 142 | // } |
ctlee | 0:81d4b52c0083 | 143 | // } |
ctlee | 0:81d4b52c0083 | 144 | // } |
ctlee | 0:81d4b52c0083 | 145 | // } |
ctlee | 0:81d4b52c0083 | 146 | // } |
ctlee | 0:81d4b52c0083 | 147 | } |
ctlee | 0:81d4b52c0083 | 148 | // while(_gps.getc() != '$') { |
ctlee | 0:81d4b52c0083 | 149 | // //char a = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 150 | // for(i = 0; i < 256; i++) { |
ctlee | 0:81d4b52c0083 | 151 | // msg[i] = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 152 | // pc1.printf("%c", msg[i]); |
ctlee | 0:81d4b52c0083 | 153 | // if(msg[i] == '\r') { |
ctlee | 0:81d4b52c0083 | 154 | // msg[i] = 0; |
ctlee | 0:81d4b52c0083 | 155 | // return; |
ctlee | 0:81d4b52c0083 | 156 | // } |
ctlee | 0:81d4b52c0083 | 157 | // } |
ctlee | 0:81d4b52c0083 | 158 | // |
ctlee | 0:81d4b52c0083 | 159 | // |
ctlee | 0:81d4b52c0083 | 160 | // } |
ctlee | 0:81d4b52c0083 | 161 | // while(_gps.getc() != '$'); // wait for the start of a line |
ctlee | 0:81d4b52c0083 | 162 | // for(int i=0; i<256; i++) { |
ctlee | 0:81d4b52c0083 | 163 | // msg[i] = _gps.getc(); |
ctlee | 0:81d4b52c0083 | 164 | // if(msg[i] == '\r') { |
ctlee | 0:81d4b52c0083 | 165 | // msg[i] = 0; |
ctlee | 0:81d4b52c0083 | 166 | // return; |
ctlee | 0:81d4b52c0083 | 167 | // } |
ctlee | 0:81d4b52c0083 | 168 | // } |
ctlee | 0:81d4b52c0083 | 169 | // error("Overflowed message limit"); |
ctlee | 0:81d4b52c0083 | 170 | } |
ctlee | 0:81d4b52c0083 | 171 | } |