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 Servo SDFileSystem
main.cpp@14:92bacb5af01b, 2015-08-26 (annotated)
- Committer:
- dem123456789
- Date:
- Wed Aug 26 16:33:26 2015 +0000
- Revision:
- 14:92bacb5af01b
- Parent:
- 12:8644abfa86da
- Child:
- 15:dbf20c1209ae
PC part half done
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| taoqiuyang | 0:f4d390c06705 | 1 | #include "mbed.h" |
| taoqiuyang | 3:ab9f94d112c0 | 2 | #include <string> |
| dem123456789 | 9:bf5939466e86 | 3 | #include <sstream> |
| dem123456789 | 9:bf5939466e86 | 4 | #include <vector> |
| dem123456789 | 14:92bacb5af01b | 5 | #include "Get.h" |
| dem123456789 | 11:1caacb994236 | 6 | using namespace std; |
| dem123456789 | 11:1caacb994236 | 7 | |
| dem123456789 | 11:1caacb994236 | 8 | #define MAX_IMU_SIZE 29 |
| taoqiuyang | 3:ab9f94d112c0 | 9 | |
| taoqiuyang | 2:afb333543af5 | 10 | DigitalOut led1(LED1); |
| taoqiuyang | 2:afb333543af5 | 11 | DigitalOut led2(LED2); |
| taoqiuyang | 5:451b8203ef99 | 12 | DigitalOut led3(LED3); |
| dem123456789 | 8:1f5a44bade4d | 13 | DigitalOut led4(LED4); |
| taoqiuyang | 2:afb333543af5 | 14 | |
| taoqiuyang | 2:afb333543af5 | 15 | Serial pc(USBTX, USBRX); |
| taoqiuyang | 2:afb333543af5 | 16 | Serial IMU(p28, p27); // tx, rx |
| taoqiuyang | 5:451b8203ef99 | 17 | Serial GPS(p13, p14); // tx, rx |
| taoqiuyang | 2:afb333543af5 | 18 | |
| taoqiuyang | 2:afb333543af5 | 19 | char IMU_message[256]; |
| taoqiuyang | 3:ab9f94d112c0 | 20 | int IMU_message_counter=0; |
| taoqiuyang | 5:451b8203ef99 | 21 | char GPS_message[256]; |
| taoqiuyang | 5:451b8203ef99 | 22 | int GPS_message_counter=0; |
| dem123456789 | 14:92bacb5af01b | 23 | char PC_message[256]; |
| dem123456789 | 14:92bacb5af01b | 24 | int PC_message_counter=0; |
| dem123456789 | 11:1caacb994236 | 25 | |
| dem123456789 | 11:1caacb994236 | 26 | string IMU_Y="N/A"; |
| dem123456789 | 11:1caacb994236 | 27 | string IMU_P="N/A"; |
| dem123456789 | 11:1caacb994236 | 28 | string IMU_R="N/A"; |
| dem123456789 | 14:92bacb5af01b | 29 | string GPS_Quality="N/A"; |
| dem123456789 | 11:1caacb994236 | 30 | string GPS_UTC="N/A"; |
| dem123456789 | 11:1caacb994236 | 31 | string GPS_Latitude="N/A"; |
| dem123456789 | 11:1caacb994236 | 32 | string GPS_Longtitude="N/A"; |
| dem123456789 | 11:1caacb994236 | 33 | string GPS_Altitude="N/A"; |
| dem123456789 | 11:1caacb994236 | 34 | string GPS_Num_Satellite="N/A"; |
| dem123456789 | 11:1caacb994236 | 35 | string GPS_HDOP="N/A"; |
| dem123456789 | 12:8644abfa86da | 36 | string GPS_VDOP="N/A"; |
| dem123456789 | 12:8644abfa86da | 37 | string GPS_PDOP="N/A"; |
| dem123456789 | 14:92bacb5af01b | 38 | string GPS_Date="N/A"; |
| dem123456789 | 14:92bacb5af01b | 39 | string GPS_VelocityKnot="N/A"; |
| dem123456789 | 14:92bacb5af01b | 40 | string GPS_VelocityKph="N/A"; |
| dem123456789 | 14:92bacb5af01b | 41 | |
| dem123456789 | 12:8644abfa86da | 42 | int asterisk_idx; |
| dem123456789 | 9:bf5939466e86 | 43 | |
| dem123456789 | 9:bf5939466e86 | 44 | vector<string> split(const string &s, char delim) { |
| dem123456789 | 9:bf5939466e86 | 45 | stringstream ss(s); |
| dem123456789 | 9:bf5939466e86 | 46 | string item; |
| dem123456789 | 9:bf5939466e86 | 47 | vector<string> tokens; |
| dem123456789 | 9:bf5939466e86 | 48 | while (getline(ss, item, delim)) { |
| dem123456789 | 9:bf5939466e86 | 49 | tokens.push_back(item); |
| dem123456789 | 9:bf5939466e86 | 50 | } |
| dem123456789 | 9:bf5939466e86 | 51 | return tokens; |
| dem123456789 | 9:bf5939466e86 | 52 | } |
| dem123456789 | 9:bf5939466e86 | 53 | |
| dem123456789 | 9:bf5939466e86 | 54 | void updateIMU(string IMU_data) { |
| dem123456789 | 9:bf5939466e86 | 55 | string IMU_data_string(IMU_data); |
| dem123456789 | 9:bf5939466e86 | 56 | if (IMU_data_string.substr(0,4) == "#YPR" and IMU_data_string.size() <= MAX_IMU_SIZE) { |
| dem123456789 | 9:bf5939466e86 | 57 | IMU_data_string = IMU_data_string.substr(5); |
| dem123456789 | 9:bf5939466e86 | 58 | vector<string> result = split(IMU_data_string, ','); |
| dem123456789 | 9:bf5939466e86 | 59 | IMU_Y = result.at(0); |
| dem123456789 | 9:bf5939466e86 | 60 | IMU_P = result.at(1); |
| dem123456789 | 9:bf5939466e86 | 61 | IMU_R = result.at(2).substr(0, result.at(2).size()-1);; |
| dem123456789 | 9:bf5939466e86 | 62 | } |
| dem123456789 | 9:bf5939466e86 | 63 | } |
| dem123456789 | 9:bf5939466e86 | 64 | |
| dem123456789 | 10:12ba6ed2d6f0 | 65 | void updateGPS(string GPS_data) { |
| dem123456789 | 10:12ba6ed2d6f0 | 66 | string GPS_data_string(GPS_data); |
| dem123456789 | 11:1caacb994236 | 67 | if (GPS_data_string.substr(0,6) == "$GPGGA") { |
| dem123456789 | 11:1caacb994236 | 68 | GPS_data_string = GPS_data_string.substr(7); |
| dem123456789 | 10:12ba6ed2d6f0 | 69 | vector<string> result = split(GPS_data_string, ','); |
| dem123456789 | 14:92bacb5af01b | 70 | GPS_Quality = result.at(5); |
| dem123456789 | 14:92bacb5af01b | 71 | if(GPS_Quality != "0" and GPS_Quality != "6") { |
| dem123456789 | 11:1caacb994236 | 72 | GPS_UTC = result.at(0); |
| dem123456789 | 11:1caacb994236 | 73 | GPS_Latitude = result.at(2) + result.at(1); |
| dem123456789 | 11:1caacb994236 | 74 | GPS_Longtitude = result.at(4) + result.at(3); |
| dem123456789 | 11:1caacb994236 | 75 | GPS_Num_Satellite = result.at(6); |
| dem123456789 | 11:1caacb994236 | 76 | GPS_HDOP = result.at(7); |
| dem123456789 | 11:1caacb994236 | 77 | GPS_Altitude = result.at(8) + result.at(9); |
| dem123456789 | 11:1caacb994236 | 78 | } |
| dem123456789 | 14:92bacb5af01b | 79 | } else if (GPS_data_string.substr(0,6) == "$GPGSA" and GPS_Quality != "0" and GPS_Quality != "6") { |
| dem123456789 | 12:8644abfa86da | 80 | GPS_data_string = GPS_data_string.substr(7); |
| dem123456789 | 12:8644abfa86da | 81 | vector<string> result = split(GPS_data_string, ','); |
| dem123456789 | 12:8644abfa86da | 82 | GPS_PDOP = result.at(14); |
| dem123456789 | 12:8644abfa86da | 83 | asterisk_idx = result.at(16).find('*'); |
| dem123456789 | 12:8644abfa86da | 84 | GPS_VDOP = result.at(16).substr(0, asterisk_idx); |
| dem123456789 | 14:92bacb5af01b | 85 | } else if (GPS_data_string.substr(0,6) == "$GPRMC" and GPS_Quality != "0" and GPS_Quality != "6") { |
| dem123456789 | 12:8644abfa86da | 86 | GPS_data_string = GPS_data_string.substr(7); |
| dem123456789 | 12:8644abfa86da | 87 | vector<string> result = split(GPS_data_string, ','); |
| dem123456789 | 14:92bacb5af01b | 88 | GPS_Date = result.at(8); |
| dem123456789 | 14:92bacb5af01b | 89 | } else if (GPS_data_string.substr(0,6) == "$GPVTG" and GPS_Quality != "0" and GPS_Quality != "6") { |
| dem123456789 | 12:8644abfa86da | 90 | GPS_data_string = GPS_data_string.substr(7); |
| dem123456789 | 12:8644abfa86da | 91 | vector<string> result = split(GPS_data_string, ','); |
| dem123456789 | 14:92bacb5af01b | 92 | GPS_VelocityKnot = result.at(4) + result.at(5); |
| dem123456789 | 12:8644abfa86da | 93 | asterisk_idx = result.at(7).find('*'); |
| dem123456789 | 14:92bacb5af01b | 94 | GPS_VelocityKph = result.at(6) + result.at(7).substr(0, asterisk_idx); |
| dem123456789 | 10:12ba6ed2d6f0 | 95 | } |
| dem123456789 | 12:8644abfa86da | 96 | |
| dem123456789 | 10:12ba6ed2d6f0 | 97 | } |
| dem123456789 | 10:12ba6ed2d6f0 | 98 | |
| dem123456789 | 11:1caacb994236 | 99 | void printStateIMU() { |
| dem123456789 | 11:1caacb994236 | 100 | pc.printf("IMU_Y: %s, IMU_P: %s, IMU_R: %s\n",IMU_Y.c_str(), IMU_P.c_str(), IMU_R.c_str()); |
| dem123456789 | 11:1caacb994236 | 101 | } |
| dem123456789 | 11:1caacb994236 | 102 | |
| dem123456789 | 12:8644abfa86da | 103 | void printStateGPS() { |
| dem123456789 | 14:92bacb5af01b | 104 | pc.printf("GPS_Quality: %s, GPS_UTC: %s, GPS_Latitude: %s, GPS_Longtitude: %s, GPS_Altitude: %s, " |
| dem123456789 | 14:92bacb5af01b | 105 | "GPS_Num_Satellite: %s, GPS_HDOP: %s, GPS_VDOP: %s, GPS_PDOP: %s, GPS_Date: %s, GPS_VelocityKnot: %s, GPS_VelocityKph: %s\n", |
| dem123456789 | 14:92bacb5af01b | 106 | GPS_Quality.c_str(), GPS_UTC.c_str(), GPS_Latitude.c_str(), GPS_Longtitude.c_str(), GPS_Altitude.c_str(), GPS_Num_Satellite.c_str(), |
| dem123456789 | 14:92bacb5af01b | 107 | GPS_HDOP.c_str(), GPS_VDOP.c_str(), GPS_PDOP.c_str(), GPS_Date.c_str(), GPS_VelocityKnot.c_str(), GPS_VelocityKph.c_str()); |
| dem123456789 | 12:8644abfa86da | 108 | } |
| dem123456789 | 14:92bacb5af01b | 109 | |
| dem123456789 | 11:1caacb994236 | 110 | //#YPR=-183.74,-134.27,-114.39 |
| taoqiuyang | 4:37d118f2348c | 111 | void IMU_serial_ISR() { |
| taoqiuyang | 3:ab9f94d112c0 | 112 | char buf; |
| taoqiuyang | 3:ab9f94d112c0 | 113 | |
| taoqiuyang | 3:ab9f94d112c0 | 114 | while (IMU.readable()) { |
| taoqiuyang | 3:ab9f94d112c0 | 115 | buf = IMU.getc(); |
| taoqiuyang | 3:ab9f94d112c0 | 116 | |
| dem123456789 | 14:92bacb5af01b | 117 | //pc.putc(buf); |
| taoqiuyang | 3:ab9f94d112c0 | 118 | IMU_message[IMU_message_counter]=buf; |
| dem123456789 | 9:bf5939466e86 | 119 | IMU_message_counter+=1; |
| taoqiuyang | 3:ab9f94d112c0 | 120 | |
| taoqiuyang | 3:ab9f94d112c0 | 121 | if (buf=='\n'){ |
| dem123456789 | 9:bf5939466e86 | 122 | string IMU_copy(IMU_message, IMU_message_counter); |
| dem123456789 | 11:1caacb994236 | 123 | //pc.printf("%s", IMU_copy.c_str()); |
| dem123456789 | 9:bf5939466e86 | 124 | updateIMU(IMU_copy); |
| dem123456789 | 9:bf5939466e86 | 125 | IMU_message_counter=0; |
| dem123456789 | 9:bf5939466e86 | 126 | IMU_copy[0] = '\0'; |
| dem123456789 | 9:bf5939466e86 | 127 | } |
| dem123456789 | 9:bf5939466e86 | 128 | |
| taoqiuyang | 3:ab9f94d112c0 | 129 | } |
| taoqiuyang | 2:afb333543af5 | 130 | led2 = !led2; |
| taoqiuyang | 2:afb333543af5 | 131 | } |
| dem123456789 | 9:bf5939466e86 | 132 | |
| dem123456789 | 9:bf5939466e86 | 133 | |
| dem123456789 | 8:1f5a44bade4d | 134 | |
| taoqiuyang | 5:451b8203ef99 | 135 | void GPS_serial_ISR() { |
| taoqiuyang | 5:451b8203ef99 | 136 | char buf; |
| taoqiuyang | 5:451b8203ef99 | 137 | |
| taoqiuyang | 5:451b8203ef99 | 138 | while (GPS.readable()) { |
| taoqiuyang | 5:451b8203ef99 | 139 | buf = GPS.getc(); |
| dem123456789 | 14:92bacb5af01b | 140 | |
| dem123456789 | 14:92bacb5af01b | 141 | //pc.putc(buf); |
| dem123456789 | 10:12ba6ed2d6f0 | 142 | GPS_message[GPS_message_counter]=buf; |
| dem123456789 | 10:12ba6ed2d6f0 | 143 | GPS_message_counter+=1; |
| dem123456789 | 10:12ba6ed2d6f0 | 144 | |
| dem123456789 | 10:12ba6ed2d6f0 | 145 | if (buf=='\n'){ |
| dem123456789 | 10:12ba6ed2d6f0 | 146 | string GPS_copy(GPS_message, GPS_message_counter); |
| dem123456789 | 11:1caacb994236 | 147 | //pc.printf("%s", GPS_copy.c_str()); |
| dem123456789 | 10:12ba6ed2d6f0 | 148 | updateGPS(GPS_copy); |
| dem123456789 | 10:12ba6ed2d6f0 | 149 | GPS_message_counter=0; |
| dem123456789 | 10:12ba6ed2d6f0 | 150 | GPS_copy[0] = '\0'; |
| dem123456789 | 10:12ba6ed2d6f0 | 151 | } |
| taoqiuyang | 5:451b8203ef99 | 152 | } |
| taoqiuyang | 5:451b8203ef99 | 153 | |
| taoqiuyang | 5:451b8203ef99 | 154 | led3 = !led3; |
| taoqiuyang | 5:451b8203ef99 | 155 | } |
| taoqiuyang | 3:ab9f94d112c0 | 156 | |
| dem123456789 | 8:1f5a44bade4d | 157 | void PC_serial_ISR() { |
| dem123456789 | 8:1f5a44bade4d | 158 | char buf; |
| dem123456789 | 8:1f5a44bade4d | 159 | |
| dem123456789 | 8:1f5a44bade4d | 160 | while (pc.readable()) { |
| dem123456789 | 8:1f5a44bade4d | 161 | buf = pc.getc(); |
| dem123456789 | 14:92bacb5af01b | 162 | |
| dem123456789 | 14:92bacb5af01b | 163 | //pc.putc(buf); |
| dem123456789 | 14:92bacb5af01b | 164 | PC_message[PC_message_counter]=buf; |
| dem123456789 | 14:92bacb5af01b | 165 | PC_message_counter+=1; |
| dem123456789 | 14:92bacb5af01b | 166 | |
| dem123456789 | 14:92bacb5af01b | 167 | if (buf=='\n'){ |
| dem123456789 | 14:92bacb5af01b | 168 | string PC_copy(PC_message, PC_message_counter); |
| dem123456789 | 14:92bacb5af01b | 169 | //pc.printf("%s", PC_copy.c_str()); |
| dem123456789 | 14:92bacb5af01b | 170 | decodePC(PC_copy); |
| dem123456789 | 14:92bacb5af01b | 171 | PC_message_counter=0; |
| dem123456789 | 14:92bacb5af01b | 172 | PC_copy[0] = '\0'; |
| dem123456789 | 14:92bacb5af01b | 173 | } |
| dem123456789 | 8:1f5a44bade4d | 174 | } |
| dem123456789 | 8:1f5a44bade4d | 175 | |
| dem123456789 | 8:1f5a44bade4d | 176 | led4= !led4; |
| dem123456789 | 8:1f5a44bade4d | 177 | } |
| taoqiuyang | 3:ab9f94d112c0 | 178 | |
| taoqiuyang | 0:f4d390c06705 | 179 | int main() { |
| taoqiuyang | 2:afb333543af5 | 180 | IMU.baud(57600); |
| taoqiuyang | 4:37d118f2348c | 181 | IMU.attach(&IMU_serial_ISR); |
| taoqiuyang | 5:451b8203ef99 | 182 | GPS.baud(38400); |
| taoqiuyang | 5:451b8203ef99 | 183 | GPS.attach(&GPS_serial_ISR); |
| dem123456789 | 8:1f5a44bade4d | 184 | pc.baud(115200); |
| dem123456789 | 8:1f5a44bade4d | 185 | pc.attach(&PC_serial_ISR); |
| taoqiuyang | 1:e7245ffb4820 | 186 | |
| taoqiuyang | 2:afb333543af5 | 187 | while (1) { |
| taoqiuyang | 2:afb333543af5 | 188 | led1 = !led1; |
| taoqiuyang | 5:451b8203ef99 | 189 | wait(0.2); |
| dem123456789 | 12:8644abfa86da | 190 | printStateIMU(); |
| dem123456789 | 12:8644abfa86da | 191 | printStateGPS(); |
| taoqiuyang | 0:f4d390c06705 | 192 | } |
| taoqiuyang | 0:f4d390c06705 | 193 | } |