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
handle/handleGPS.cpp
- Committer:
- krobertson
- Date:
- 2014-04-22
- Revision:
- 30:327191ff57e8
- Parent:
- 20:81d5655fecc2
- Child:
- 31:6f68fa0aeee5
File content as of revision 30:327191ff57e8:
#include "handleGPS.h" //Serial gps(p28,p27); GPSHandle* GPSHandle::hand = NULL; void testVar(){ } void GPSHandle::setup(){ //gpsGPS().getSerial().baud(57600); sendGpsCommand("PMTK301,1"); //GPS::getSerial().attach(&GPSHandle::handleUpdate,Serial::RxIrq); GPS::getSerial().attach(this,&GPSHandle::handleUpdate,Serial::RxIrq); //cs: Send other standard init commands? Not strictly speaking necessary, // but forces "up to date documentation" in the form of always knowing // gps config. } char GPSHandle::readWaypoints(){ USB::getSerial().printf("getting waypoitns\r\n"); PacketStruct pack; char rx_status = getPS().receivePacket(&pack); if(rx_status != 1){ return rx_status; } Point* points = (Point*)pack.data; unsigned int num_points = pack.size; for(int i=0;i<num_points;i++){ USB::getSerial().printf("Adding Waypoint: %f, %f\r\n",points[i].lat,points[i].lon); } return 1; } void GPSHandle::sendLoc(){ wait_us(100000); getPS().openConnection(); wait_us(100000); unsigned int sID=getPS().getSuperID(); getPS().sendPacket(0,NULL,0,PT_EMPTY); getPS().sendPacket(sID,NULL,0,PT_SENDLOC); getPS().sendPacket(sID,(char*)(&DH::locs().getC(LHType_locs,DH::locs().getI(LHType_locs))),sizeof(DataLocation)); getPS().sendPacket(sID,NULL,0,PT_END); wait_us(100000); getPS().closeConnection(); wait_us(100000); } bool GPSHandle::if_image_location(){ USB::getSerial().printf("Checking if at waypoint\r\n"); //DH::locs().getC(LHType_locs,DH::locs().getI(LHType_locs)){ //} return true; } static bool reading = false; //static std::stringstream line; static char line[MAXREADIN+10]; static int line_i=0; char* getNext(char*& field){ char* output=new char[MAXREADIN+1]; int i; for(i=0;i<MAXREADIN;i++){ if(field[i]=='\0'||field[i]==',')break; } for(int a=0;a<i;a++){ output[a]=field[a]; } output[i]='\0'; field=&field[i+1]; return output; } void GPSHandle::handleUpdate(){ if(GPS::getSerial().readable()<=0){return;} char c = GPS::getSerial().getc(); //USB::getSerial().printf("%c",c); if (reading) { if(line_i>=MAXREADIN){reading=false;return;} if (c == '*') { //sentence buffer complete; we're ignoring the checksum char* field=line; char* op; op=getNext(field);delete op; //GPGGA if(op[0]=='G'||op[1]=='P'||op[2]=='G'||op[3]=='G'||op[4]=='A'){ op=getNext(field);double timeS = atof(op);delete op; //time op=getNext(field);double latitude = atof(op);delete op; //latitude op=getNext(field);delete op; //N or S op=getNext(field);double longitude = atof(op);delete op; //longitude op=getNext(field);delete op; //E or W op=getNext(field);delete op; //skip op=getNext(field);delete op; //skip op=getNext(field);delete op; //skip op=getNext(field);delete op; //altitude double altitude = atof(op); if(timeS>0.5f){ //USB::getSerial().printf("\nMy GPS data: Lat: %f, Lon: %f, Alt: %f, Time:%f\r\n",latitude,longitude,altitude,timeS); DH::Locs().add(LHType_locs,DataLocation(latitude,longitude,altitude,timeS)); //USB::getSerial().printf("Current Time:%f\r\n",DH::Locs().getC().getTime()); } } //update whatever needs updating when gps updates // pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n", // gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time // ); reading = false; } else { line[line_i]=c; line_i=(line_i+1)%MAXREADIN; } } else if (c == '$') { reading = true; line_i=0; } return; } //sends: "$<command>*<checksum>\r\l" void GPSHandle::sendGpsCommand(std::string command){ uint8_t checksum = 0; //pc.printf("Sending command to gps: "); GPS::getSerial().putc('$'); //pc.putc('$'); char c; for (int i = 0; i < command.length(); i++) { c = command[i]; checksum ^= c; GPS::getSerial().putc(c); //pc.putc(c); } GPS::getSerial().putc('*'); //pc.putc('*'); string checkSumString; while (checksum > 0) { uint8_t checksumChar = checksum & 0x0F; if (checksumChar >= 10) { checksumChar -= 10; checksumChar += 'A'; } else { checksumChar += '0'; } checkSumString.push_back((char) checksumChar); checksum = checksum >> 4; } for (int i = checkSumString.length() - 1; i >= 0; i--) { GPS::getSerial().putc(checkSumString[i]); //pc.putc(checkSumString[i]); } GPS::getSerial().putc('\r'); //pc.putc('\r'); GPS::getSerial().putc('\n'); //pc.putc('\n'); } int stringToDecimal(string s) { int mult = 1; int result = 0; for (int i = s.length() - 1; i >=0; i--) { if (s[i] != '.') { result += (s[i] - '0') * mult; mult *= 10; } } return result; } bool GPSHandle::check(){ return true; } void GPSHandle::run(){ if(!initialized){initialized=true;setup();} }