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.
Fork of dgps by
main.cpp@14:6be57da62283, 2014-04-10 (annotated)
- Committer:
- dylanembed123
- Date:
- Thu Apr 10 02:19:07 2014 +0000
- Revision:
- 14:6be57da62283
- Parent:
- 13:a6d3cf2b018e
- Child:
- 15:e3e03a9df89e
Update GPS;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stearnsc | 0:9c001c4e7bf4 | 1 | #include "mbed.h" |
stearnsc | 0:9c001c4e7bf4 | 2 | #include <string> |
stearnsc | 0:9c001c4e7bf4 | 3 | #include <sstream> |
dylanembed123 | 7:c75d5e5e6bfc | 4 | #include "adapt/usb.h" |
dylanembed123 | 9:da906eeac51e | 5 | #include "handle/handleCamera.h" |
dylanembed123 | 9:da906eeac51e | 6 | #include "handle/handleGPS.h" |
stearnsc | 8:28b866df62cf | 7 | |
stearnsc | 0:9c001c4e7bf4 | 8 | Serial pc(USBTX,USBRX); |
stearnsc | 0:9c001c4e7bf4 | 9 | Serial xbee(p9,p10);//tx, rx |
stearnsc | 0:9c001c4e7bf4 | 10 | Serial gps(p28,p27); |
dylanembed123 | 6:434d20e99e49 | 11 | Serial camera(p13,p14); |
stearnsc | 0:9c001c4e7bf4 | 12 | |
stearnsc | 0:9c001c4e7bf4 | 13 | typedef struct { |
stearnsc | 0:9c001c4e7bf4 | 14 | int latitude; //in .0001 minutes |
stearnsc | 0:9c001c4e7bf4 | 15 | int longitude; //in .0001 minutes |
stearnsc | 0:9c001c4e7bf4 | 16 | int altitude; //in decimeters |
stearnsc | 0:9c001c4e7bf4 | 17 | int time; //in milliseconds |
stearnsc | 0:9c001c4e7bf4 | 18 | } GpsData; |
stearnsc | 0:9c001c4e7bf4 | 19 | |
stearnsc | 8:28b866df62cf | 20 | void readSerial(Serial &s, char str[], int size) |
stearnsc | 8:28b866df62cf | 21 | { |
stearnsc | 8:28b866df62cf | 22 | for (int i = 0; i < size; i++) { |
stearnsc | 8:28b866df62cf | 23 | str[i] = s.getc(); |
stearnsc | 0:9c001c4e7bf4 | 24 | } |
stearnsc | 0:9c001c4e7bf4 | 25 | } |
stearnsc | 0:9c001c4e7bf4 | 26 | |
stearnsc | 0:9c001c4e7bf4 | 27 | //sends: "$<command>*<checksum>\r\l" |
stearnsc | 8:28b866df62cf | 28 | void sendGpsCommand(string command) |
stearnsc | 8:28b866df62cf | 29 | { |
stearnsc | 0:9c001c4e7bf4 | 30 | uint8_t checksum = 0; |
stearnsc | 0:9c001c4e7bf4 | 31 | pc.printf("Sending command to gps: "); |
stearnsc | 0:9c001c4e7bf4 | 32 | gps.putc('$'); |
stearnsc | 0:9c001c4e7bf4 | 33 | pc.putc('$'); |
stearnsc | 0:9c001c4e7bf4 | 34 | char c; |
stearnsc | 8:28b866df62cf | 35 | for (int i = 0; i < command.length(); i++) { |
stearnsc | 0:9c001c4e7bf4 | 36 | c = command[i]; |
stearnsc | 0:9c001c4e7bf4 | 37 | checksum ^= c; |
stearnsc | 0:9c001c4e7bf4 | 38 | gps.putc(c); |
stearnsc | 0:9c001c4e7bf4 | 39 | pc.putc(c); |
stearnsc | 0:9c001c4e7bf4 | 40 | } |
stearnsc | 0:9c001c4e7bf4 | 41 | gps.putc('*'); |
stearnsc | 0:9c001c4e7bf4 | 42 | pc.putc('*'); |
stearnsc | 0:9c001c4e7bf4 | 43 | string checkSumString; |
stearnsc | 8:28b866df62cf | 44 | while (checksum > 0) { |
stearnsc | 0:9c001c4e7bf4 | 45 | uint8_t checksumChar = checksum & 0x0F; |
stearnsc | 8:28b866df62cf | 46 | if (checksumChar >= 10) { |
stearnsc | 0:9c001c4e7bf4 | 47 | checksumChar -= 10; |
stearnsc | 8:28b866df62cf | 48 | checksumChar += 'A'; |
stearnsc | 0:9c001c4e7bf4 | 49 | } else { |
stearnsc | 8:28b866df62cf | 50 | checksumChar += '0'; |
stearnsc | 0:9c001c4e7bf4 | 51 | } |
stearnsc | 0:9c001c4e7bf4 | 52 | checkSumString.push_back((char) checksumChar); |
stearnsc | 8:28b866df62cf | 53 | checksum = checksum >> 4; |
stearnsc | 0:9c001c4e7bf4 | 54 | } |
stearnsc | 0:9c001c4e7bf4 | 55 | |
stearnsc | 8:28b866df62cf | 56 | for (int i = checkSumString.length() - 1; i >= 0; i--) { |
stearnsc | 0:9c001c4e7bf4 | 57 | gps.putc(checkSumString[i]); |
stearnsc | 8:28b866df62cf | 58 | pc.putc(checkSumString[i]); |
stearnsc | 8:28b866df62cf | 59 | } |
stearnsc | 0:9c001c4e7bf4 | 60 | gps.putc('\r'); |
stearnsc | 0:9c001c4e7bf4 | 61 | pc.putc('\r'); |
stearnsc | 0:9c001c4e7bf4 | 62 | gps.putc('\n'); |
stearnsc | 0:9c001c4e7bf4 | 63 | pc.putc('\n'); |
stearnsc | 0:9c001c4e7bf4 | 64 | } |
stearnsc | 8:28b866df62cf | 65 | // |
stearnsc | 8:28b866df62cf | 66 | ////cs: little endian parsing |
stearnsc | 8:28b866df62cf | 67 | //int nextInt(char *data, int i) |
stearnsc | 8:28b866df62cf | 68 | //{ |
stearnsc | 8:28b866df62cf | 69 | // i |= data[i]; |
stearnsc | 8:28b866df62cf | 70 | // i |= (data[i + 1] << 8); |
stearnsc | 8:28b866df62cf | 71 | // i |= (data[i + 2] << 16); |
stearnsc | 8:28b866df62cf | 72 | // i |= (data[i + 3] << 24); |
stearnsc | 8:28b866df62cf | 73 | // return i; |
stearnsc | 8:28b866df62cf | 74 | //} |
stearnsc | 0:9c001c4e7bf4 | 75 | |
stearnsc | 8:28b866df62cf | 76 | //void handleXbeeGps() |
stearnsc | 8:28b866df62cf | 77 | //{ |
stearnsc | 8:28b866df62cf | 78 | // static bool reading = false; |
stearnsc | 8:28b866df62cf | 79 | // static char packet[16]; |
stearnsc | 8:28b866df62cf | 80 | // static int i = 0; |
stearnsc | 8:28b866df62cf | 81 | // |
stearnsc | 8:28b866df62cf | 82 | // char c = xbee.getc(); |
stearnsc | 8:28b866df62cf | 83 | // if (reading) { |
stearnsc | 8:28b866df62cf | 84 | // packet[i] = c; |
stearnsc | 8:28b866df62cf | 85 | // i++; |
stearnsc | 8:28b866df62cf | 86 | // if (i == 16) { |
stearnsc | 8:28b866df62cf | 87 | // i = 0; |
stearnsc | 8:28b866df62cf | 88 | // otherGps.latitude = nextInt(packet, 0); |
stearnsc | 8:28b866df62cf | 89 | // otherGps.longitude = nextInt(packet, 4); |
stearnsc | 8:28b866df62cf | 90 | // otherGps.altitude = nextInt(packet, 8); |
stearnsc | 8:28b866df62cf | 91 | // otherGps.time = nextInt(packet, 12); |
stearnsc | 8:28b866df62cf | 92 | // |
stearnsc | 8:28b866df62cf | 93 | // pc.printf("His GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n", |
stearnsc | 8:28b866df62cf | 94 | // otherGps.latitude, otherGps.longitude, otherGps.altitude, otherGps.time |
stearnsc | 8:28b866df62cf | 95 | // ); |
stearnsc | 8:28b866df62cf | 96 | // reading = false; |
stearnsc | 8:28b866df62cf | 97 | // } |
stearnsc | 8:28b866df62cf | 98 | // } else if (c == 'X') { |
stearnsc | 8:28b866df62cf | 99 | // reading = true; |
stearnsc | 8:28b866df62cf | 100 | // } |
stearnsc | 8:28b866df62cf | 101 | //} |
dylanembed123 | 7:c75d5e5e6bfc | 102 | |
dylanembed123 | 7:c75d5e5e6bfc | 103 | |
stearnsc | 8:28b866df62cf | 104 | int main() |
stearnsc | 8:28b866df62cf | 105 | { |
dylanembed123 | 14:6be57da62283 | 106 | //getPS().sendPacket(0,NULL,0,PT_EMPTY);getPS().sendPacket(55,NULL,0,PT_IMAGE); |
dylanembed123 | 14:6be57da62283 | 107 | /* |
dylanembed123 | 14:6be57da62283 | 108 | char output[256];for(int i=0;i<256;i++){output[i]=i;}getPS().sendPacket(0,NULL,0,PT_IMAGE);getPS().sendPacket(55,output,256);getPS().sendPacket(55,output,5);getPS().sendPacket(55,NULL,0,PT_END); |
dylanembed123 | 14:6be57da62283 | 109 | PacketStruct* packs[4]; |
dylanembed123 | 14:6be57da62283 | 110 | for(int i=0;i<4;i){ |
dylanembed123 | 14:6be57da62283 | 111 | packs[i]=getPS().getNextPacket(); |
dylanembed123 | 14:6be57da62283 | 112 | if(packs[i]!=NULL)i++; |
dylanembed123 | 14:6be57da62283 | 113 | } |
dylanembed123 | 14:6be57da62283 | 114 | for(int i=0;i<4;i++){ |
dylanembed123 | 14:6be57da62283 | 115 | PacketStruct* pack=packs[i]; |
dylanembed123 | 14:6be57da62283 | 116 | if(pack!=NULL){ |
dylanembed123 | 14:6be57da62283 | 117 | USB::getSerial().printf("Got Packet!\n"); |
dylanembed123 | 14:6be57da62283 | 118 | USB::getSerial().printf(" > %d\n",pack->type); |
dylanembed123 | 14:6be57da62283 | 119 | for(int i=0;i<sizeof(PacketStruct);i++){ |
dylanembed123 | 14:6be57da62283 | 120 | USB::getSerial().printf("%d\n",((char*)pack)[i]); |
dylanembed123 | 14:6be57da62283 | 121 | } |
dylanembed123 | 14:6be57da62283 | 122 | USB::getSerial().printf("\n",pack->type); |
dylanembed123 | 14:6be57da62283 | 123 | }else{ |
dylanembed123 | 14:6be57da62283 | 124 | //USB::getSerial().printf("."); |
dylanembed123 | 14:6be57da62283 | 125 | } |
dylanembed123 | 14:6be57da62283 | 126 | } |
dylanembed123 | 14:6be57da62283 | 127 | while(true){} |
dylanembed123 | 14:6be57da62283 | 128 | */ |
dylanembed123 | 13:a6d3cf2b018e | 129 | //getPS().sendPacket(55,NULL,0,PT_IMAGE);char output[256];for(int i=0;i<256;i++){output[i]=i;}getPS().sendPacket(55,output,256,PT_IMAGE);getPS().sendPacket(55,output,5,PT_IMAGE);getPS().sendPacket(55,NULL,0,PT_END);while(true){} |
dylanembed123 | 7:c75d5e5e6bfc | 130 | ImageHandle imageHand; |
dylanembed123 | 9:da906eeac51e | 131 | GPSHandle gpsHand; |
dylanembed123 | 11:97625c27ab90 | 132 | /// Main Loop |
dylanembed123 | 13:a6d3cf2b018e | 133 | USB::getSerial().printf("Starting %d\n",sizeof(PacketStruct)); |
dylanembed123 | 13:a6d3cf2b018e | 134 | //while(1){ |
dylanembed123 | 13:a6d3cf2b018e | 135 | //USB::getSerial().printf("Test\n"); |
dylanembed123 | 13:a6d3cf2b018e | 136 | //XBEE::getSerial().printf("ABC\n"); |
dylanembed123 | 13:a6d3cf2b018e | 137 | //} |
dylanembed123 | 14:6be57da62283 | 138 | USB::getSerial().printf("Check GPS\n"); |
dylanembed123 | 11:97625c27ab90 | 139 | while(1){ |
dylanembed123 | 11:97625c27ab90 | 140 | // Run image handler |
dylanembed123 | 14:6be57da62283 | 141 | //imageHand.run(); |
dylanembed123 | 11:97625c27ab90 | 142 | // Run GPS handler |
dylanembed123 | 14:6be57da62283 | 143 | gpsHand.run(); |
dylanembed123 | 11:97625c27ab90 | 144 | } |
dylanembed123 | 7:c75d5e5e6bfc | 145 | /// Main Loop |
stearnsc | 8:28b866df62cf | 146 | while(true) { |
stearnsc | 8:28b866df62cf | 147 | gps.baud(57600); |
stearnsc | 8:28b866df62cf | 148 | xbee.baud(9600); |
stearnsc | 8:28b866df62cf | 149 | pc.baud(57600); |
stearnsc | 8:28b866df62cf | 150 | |
stearnsc | 8:28b866df62cf | 151 | sendGpsCommand("PMTK301,1"); |
stearnsc | 8:28b866df62cf | 152 | while(true) { |
stearnsc | 8:28b866df62cf | 153 | pc.putc(gps.getc()); |
stearnsc | 8:28b866df62cf | 154 | } |
dylanembed123 | 9:da906eeac51e | 155 | //gps.attach(&handleGpsData, Serial::RxIrq); |
dylanembed123 | 9:da906eeac51e | 156 | //xbee.attach(&handleXbeeGps, Serial::RxIrq)//; |
dylanembed123 | 6:434d20e99e49 | 157 | } |
dylanembed123 | 9:da906eeac51e | 158 | } |