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 GPS_Incremental by
main.cpp@9:b45feb91ba38, 2013-04-19 (annotated)
- Committer:
- jekain314
- Date:
- Fri Apr 19 16:21:27 2013 +0000
- Revision:
- 9:b45feb91ba38
- Parent:
- 8:13724ed3f825
update to allow better imu gps data collection
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| dannyman939 | 0:c746ee34feae | 1 | #include "mbed.h" |
| jekain314 | 6:2a8486283198 | 2 | #include <string> |
| dannyman939 | 0:c746ee34feae | 3 | |
| dannyman939 | 0:c746ee34feae | 4 | //set up the message buffer to be filled by the GPS read process |
| dannyman939 | 0:c746ee34feae | 5 | #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 256 |
| dannyman939 | 0:c746ee34feae | 6 | |
| dannyman939 | 0:c746ee34feae | 7 | #include "MODSERIAL.h" |
| dannyman939 | 0:c746ee34feae | 8 | #include "SDFileSystem.h" //imported using the import utility |
| dannyman939 | 0:c746ee34feae | 9 | |
| jekain314 | 4:68268737ff89 | 10 | //general digital I/O specifications for this application |
| dannyman939 | 0:c746ee34feae | 11 | //SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name); |
| dannyman939 | 0:c746ee34feae | 12 | SDFileSystem sd(p11,p12,p13,p14,"sd"); |
| jekain314 | 6:2a8486283198 | 13 | DigitalIn sd_detect(p27); |
| jekain314 | 6:2a8486283198 | 14 | DigitalOut ppsled(LED1); //blink an LED at the 1PPS |
| jekain314 | 6:2a8486283198 | 15 | DigitalOut trig1led(LED2); //blink an LED at the camera trigger detection |
| jekain314 | 6:2a8486283198 | 16 | DigitalOut recordDataled(LED4); //set the led when the record is on |
| jekain314 | 6:2a8486283198 | 17 | InterruptIn camera1Int(p30); // camera interrupt in |
| jekain314 | 6:2a8486283198 | 18 | DigitalOut camera2Pin(p29); // We dont use the second camera interrupt |
| dannyman939 | 0:c746ee34feae | 19 | //USB serial data stream back to the PC |
| jekain314 | 6:2a8486283198 | 20 | Serial toPC(USBTX, USBRX); //connect the GPS TX, RX to p9 and p10 |
| dannyman939 | 0:c746ee34feae | 21 | |
| jekain314 | 6:2a8486283198 | 22 | bool detectedGPS1PPS = false; //flag set in the ISR and reset after processing the 1PPS event |
| jekain314 | 6:2a8486283198 | 23 | int PPSCounter = 0; //counts the 1PPS occurrences |
| jekain314 | 6:2a8486283198 | 24 | int byteCounter = 0; //byte counter -- zeroed at 1PPS |
| jekain314 | 4:68268737ff89 | 25 | unsigned short perSecMessageCounter=0; //counts the number of messages in a sec based on the header detection |
| jekain314 | 6:2a8486283198 | 26 | bool lookingForMessages = true; //set in the PPS ISR and set false after the message processing in the main |
| jekain314 | 6:2a8486283198 | 27 | bool messageDetected = false; //have detected a message header |
| jekain314 | 6:2a8486283198 | 28 | unsigned long IMUbytesWritten = 0; //counts the IMU bytes written by the fwrite() to the SD card |
| jekain314 | 6:2a8486283198 | 29 | int savedByteCounter = 0; //save ByteCounter at the 1PPS for display in main |
| jekain314 | 6:2a8486283198 | 30 | int savedPerSecMessageCounter=0; //saved PerSecMsgCounter for display in main |
| jekain314 | 6:2a8486283198 | 31 | int IMUClockCounter = 0; //counter for IMU samples per sec |
| jekain314 | 6:2a8486283198 | 32 | int savedIMUClockCounter=0; //saved at the 1PPS for later diaplay from main |
| jekain314 | 6:2a8486283198 | 33 | bool camera1EventDetected = false; //flag from ISR indicating a clock event occurred |
| jekain314 | 6:2a8486283198 | 34 | double camera1Time; //GPS time of the camera event |
| jekain314 | 6:2a8486283198 | 35 | int TotalBadCRCmatches = 0; //counter for the bad CRC matches for all GPS messages |
| jekain314 | 4:68268737ff89 | 36 | |
| jekain314 | 6:2a8486283198 | 37 | ////////////////////////////////////////////////////////////////////// |
| jekain314 | 6:2a8486283198 | 38 | // the below should become classes |
| jekain314 | 6:2a8486283198 | 39 | ////////////////////////////////////////////////////////////////////// |
| jekain314 | 6:2a8486283198 | 40 | #include "OEM615.h" //OEM615 GPS activities |
| jekain314 | 6:2a8486283198 | 41 | #include "ADIS16488.h" //ADIS16488 activities |
| jekain314 | 6:2a8486283198 | 42 | #include "PCMessaging.h" //PC messaging activities |
| dannyman939 | 0:c746ee34feae | 43 | |
| dannyman939 | 0:c746ee34feae | 44 | //ISR for detection of the GPS 1PPS |
| dannyman939 | 0:c746ee34feae | 45 | void detect1PPSISR(void) |
| dannyman939 | 0:c746ee34feae | 46 | { |
| jekain314 | 6:2a8486283198 | 47 | timeFromPPS.reset(); //reset the 1PPS timer upon 1PPS detection |
| jekain314 | 4:68268737ff89 | 48 | savedIMUClockCounter = IMUClockCounter; //number of IMU clocks received since last 1PPS |
| jekain314 | 6:2a8486283198 | 49 | savedByteCounter = byteCounter; //save byteCounter for display in main |
| jekain314 | 6:2a8486283198 | 50 | savedPerSecMessageCounter = perSecMessageCounter; //save for display un main |
| jekain314 | 6:2a8486283198 | 51 | IMUClockCounter = 0; //counts per-sec IMU samples (between 1PPS events) |
| jekain314 | 6:2a8486283198 | 52 | byteCounter = 0; //countes bytes between 1PPS events |
| jekain314 | 6:2a8486283198 | 53 | perSecMessageCounter = 0; //counts GPS messages between 1PPS events |
| jekain314 | 6:2a8486283198 | 54 | |
| jekain314 | 6:2a8486283198 | 55 | GPS_COM1.rxBufferFlush(); //flush the GPS serial buffer |
| dannyman939 | 0:c746ee34feae | 56 | |
| jekain314 | 6:2a8486283198 | 57 | detectedGPS1PPS = true; //set false in the main when 1PPS actions are complete |
| jekain314 | 6:2a8486283198 | 58 | lookingForMessages = true; //set false in main after processing messages |
| jekain314 | 6:2a8486283198 | 59 | PPSCounter++; //count number of 1PPS epoch |
| jekain314 | 6:2a8486283198 | 60 | |
| jekain314 | 6:2a8486283198 | 61 | //note -- the below accounts for time information becoming available AFTER the 1PPS event |
| jekain314 | 6:2a8486283198 | 62 | PPSTimeOffset++; //counts 1PPS events between matching POS and VEL messages |
| jekain314 | 6:2a8486283198 | 63 | |
| jekain314 | 6:2a8486283198 | 64 | ppsled = !ppsled; //blink an LED at the 1PPS |
| dannyman939 | 0:c746ee34feae | 65 | }; |
| dannyman939 | 0:c746ee34feae | 66 | |
| dannyman939 | 0:c746ee34feae | 67 | //ISR for detection of the hotshoe trigger 1 |
| dannyman939 | 1:cbb9104d826f | 68 | void camera1ISR(void) |
| dannyman939 | 0:c746ee34feae | 69 | { |
| jekain314 | 6:2a8486283198 | 70 | //GPSTime is from POS message header |
| jekain314 | 6:2a8486283198 | 71 | //PPSTimeOffset is an even sec to account for Time becoming known AFTER the 1PPS |
| jekain314 | 6:2a8486283198 | 72 | //PPSTimeOffset + timeFromPPS.read() can be as large as 1.02 secs |
| jekain314 | 6:2a8486283198 | 73 | camera1Time = GPSTime + PPSTimeOffset + timeFromPPS.read(); |
| jekain314 | 6:2a8486283198 | 74 | camera1EventDetected = true; //reset to false in main after processing the image detection |
| jekain314 | 4:68268737ff89 | 75 | trig1led = !trig1led; //blink an LEWD at the camera event detection |
| dannyman939 | 0:c746ee34feae | 76 | }; |
| dannyman939 | 0:c746ee34feae | 77 | |
| jekain314 | 6:2a8486283198 | 78 | /////////////////////////////////////////////////////// |
| jekain314 | 6:2a8486283198 | 79 | //set up the USB port and the GPS COM port |
| jekain314 | 6:2a8486283198 | 80 | /////////////////////////////////////////////////////// |
| jekain314 | 6:2a8486283198 | 81 | FILE *fpNav = NULL; //file pointer to the nav file on the SD card |
| dannyman939 | 0:c746ee34feae | 82 | void setupCOM(void) |
| dannyman939 | 0:c746ee34feae | 83 | { |
| dannyman939 | 0:c746ee34feae | 84 | //system starts with GPS in reset active |
| dannyman939 | 0:c746ee34feae | 85 | //dis-engage the reset to get the GPS started |
| dannyman939 | 0:c746ee34feae | 86 | GPS_Reset=1; wait_ms(1000); |
| dannyman939 | 0:c746ee34feae | 87 | |
| dannyman939 | 0:c746ee34feae | 88 | //establish 1PPS ISR |
| dannyman939 | 0:c746ee34feae | 89 | PPSInt.rise(&detect1PPSISR); |
| dannyman939 | 0:c746ee34feae | 90 | |
| dannyman939 | 0:c746ee34feae | 91 | //set the USB serial data rate -- rate must be matched at the PC end |
| dannyman939 | 0:c746ee34feae | 92 | //This the serial communication back to the the PC host |
| dannyman939 | 0:c746ee34feae | 93 | //Launch the C++ serial port read program there to catch the ASCII characters |
| dannyman939 | 0:c746ee34feae | 94 | //toPC.baud(9600); wait_ms(100); |
| dannyman939 | 0:c746ee34feae | 95 | toPC.baud(8*115200); wait_ms(100); |
| dannyman939 | 2:7301e63832ee | 96 | //toPC.baud(1*115200); wait_ms(100); |
| dannyman939 | 0:c746ee34feae | 97 | toPC.printf("\n\n released GPS from RESET and set to high baud rate \n\n"); |
| dannyman939 | 0:c746ee34feae | 98 | |
| jekain314 | 4:68268737ff89 | 99 | //just wait to launch the GPS receiver |
| dannyman939 | 0:c746ee34feae | 100 | for (int i=0; i<5; i++) { toPC.printf(" to start: %3d \n", 4-i); wait(1); } |
| dannyman939 | 0:c746ee34feae | 101 | |
| jekain314 | 6:2a8486283198 | 102 | sd_detect.mode(PullUp); |
| dannyman939 | 0:c746ee34feae | 103 | |
| jekain314 | 6:2a8486283198 | 104 | if (sd_detect == 0) |
| jekain314 | 6:2a8486283198 | 105 | { |
| jekain314 | 6:2a8486283198 | 106 | mkdir("/sd/Data", 0777); |
| jekain314 | 6:2a8486283198 | 107 | } |
| jekain314 | 6:2a8486283198 | 108 | else |
| jekain314 | 6:2a8486283198 | 109 | { |
| jekain314 | 6:2a8486283198 | 110 | toPC.printf(" SD card not present \n"); |
| jekain314 | 6:2a8486283198 | 111 | } |
| jekain314 | 6:2a8486283198 | 112 | |
| jekain314 | 4:68268737ff89 | 113 | //NOTE: we do not assume that the GPS receiver has been pre-set up for the WALDO_FCS functionality |
| jekain314 | 4:68268737ff89 | 114 | //we alwsys start with a reset and reprogram the receiver with our data out products |
| jekain314 | 4:68268737ff89 | 115 | // this prevents failure because of a blown NVRAM as occurred for the older camera systems |
| jekain314 | 4:68268737ff89 | 116 | |
| dannyman939 | 0:c746ee34feae | 117 | //this is the COM1 port from th GPS receiuver to the mbed |
| dannyman939 | 0:c746ee34feae | 118 | //it should be always started at 9600 baud because thats the default for the GPS receiver |
| dannyman939 | 0:c746ee34feae | 119 | GPS_COM1.baud(9600); wait_ms(100); |
| dannyman939 | 0:c746ee34feae | 120 | |
| dannyman939 | 0:c746ee34feae | 121 | // this ASCII command sets up the serial data from the GPS receiver on its COM1 |
| dannyman939 | 0:c746ee34feae | 122 | char ch7[] = "serialconfig COM1 9600 n 8 1 n off"; |
| jekain314 | 4:68268737ff89 | 123 | // this is a software reset and has the same effect as a hardware reset (why do it?) |
| jekain314 | 4:68268737ff89 | 124 | //char ch0[] = "RESET"; |
| dannyman939 | 0:c746ee34feae | 125 | //this command stops all communication from the GPS receiver on COM1 |
| jekain314 | 4:68268737ff89 | 126 | //logs should still be presented on USB port so the Novatel CDU application can be used on the PC in parallel |
| dannyman939 | 0:c746ee34feae | 127 | char ch1[] = "unlogall COM1"; |
| dannyman939 | 0:c746ee34feae | 128 | //set the final baud rate that we will use from here |
| dannyman939 | 0:c746ee34feae | 129 | //allowable baud rate values: 9600 115200 230400 460800 921600 |
| jekain314 | 4:68268737ff89 | 130 | //char ch2[] = "serialconfig COM1 921600 n 8 1 n off"; |
| jekain314 | 9:b45feb91ba38 | 131 | char ch2[] = "serialconfig COM1 115200 n 8 1 n off"; |
| dannyman939 | 0:c746ee34feae | 132 | |
| dannyman939 | 0:c746ee34feae | 133 | //the below commands request the POS, VEL, RANGE, and TIME messages |
| dannyman939 | 0:c746ee34feae | 134 | char ch3[] = "log COM1 BESTPOSB ONTIME 1"; //messageID = 42 |
| dannyman939 | 0:c746ee34feae | 135 | char ch4[] = "log COM1 BESTVelB ONTIME 1"; //messageID = 99 |
| dannyman939 | 0:c746ee34feae | 136 | char ch5[] = "log COM1 RANGEB ONTIME 1"; //messageID = 43 |
| jekain314 | 4:68268737ff89 | 137 | //char ch6[] = "log COM1 TIMEB ONTIME 1"; //messageID = 101 |
| dannyman939 | 0:c746ee34feae | 138 | |
| dannyman939 | 0:c746ee34feae | 139 | //set up VARF to be 100Hz with 1X10^4 * 10^-8 = 10^-4 sec (10usec) pulse width |
| jekain314 | 4:68268737ff89 | 140 | //in fact, we do not use this output but it is available. |
| jekain314 | 4:68268737ff89 | 141 | //originally planned to use this to command the IMU data |
| jekain314 | 6:2a8486283198 | 142 | //char ch8[] = "FREQUENCYOUT enable 10000 1000000"; |
| dannyman939 | 0:c746ee34feae | 143 | |
| dannyman939 | 0:c746ee34feae | 144 | toPC.printf("set serial config \n"); |
| dannyman939 | 0:c746ee34feae | 145 | sendASCII(ch7, sizeof(ch7)); wait_ms(500); |
| dannyman939 | 0:c746ee34feae | 146 | //sendASCII(ch0, sizeof(ch0)); |
| dannyman939 | 0:c746ee34feae | 147 | toPC.printf("unlog all messages \n"); |
| dannyman939 | 0:c746ee34feae | 148 | sendASCII(ch1, sizeof(ch1)); wait_ms(500); |
| dannyman939 | 0:c746ee34feae | 149 | toPC.printf("log BESTPOSB on COM1 \n"); |
| dannyman939 | 0:c746ee34feae | 150 | sendASCII(ch3, sizeof(ch3)); wait_ms(500); |
| dannyman939 | 0:c746ee34feae | 151 | toPC.printf("log BESTVELB on COM1\n"); |
| dannyman939 | 0:c746ee34feae | 152 | sendASCII(ch4, sizeof(ch4)); wait_ms(500); |
| dannyman939 | 0:c746ee34feae | 153 | toPC.printf("log RANGEB on COM1\n"); |
| dannyman939 | 0:c746ee34feae | 154 | sendASCII(ch5, sizeof(ch5)); wait_ms(500); |
| dannyman939 | 0:c746ee34feae | 155 | |
| dannyman939 | 0:c746ee34feae | 156 | //toPC.printf("log TIMEB om COM1 \n"); |
| dannyman939 | 0:c746ee34feae | 157 | //sendASCII(ch6, sizeof(ch6)); wait_ms(100); |
| dannyman939 | 0:c746ee34feae | 158 | |
| jekain314 | 6:2a8486283198 | 159 | //toPC.printf("Set up th VARF signal \n"); |
| jekain314 | 6:2a8486283198 | 160 | //sendASCII(ch8, sizeof(ch8)); wait_ms(500); |
| dannyman939 | 0:c746ee34feae | 161 | |
| dannyman939 | 0:c746ee34feae | 162 | //set GPS output COM1 to the final high rate |
| dannyman939 | 0:c746ee34feae | 163 | toPC.printf("set the COM ports to high rate\n"); |
| dannyman939 | 0:c746ee34feae | 164 | sendASCII(ch2, sizeof(ch2)); wait_ms(500); |
| dannyman939 | 0:c746ee34feae | 165 | |
| dannyman939 | 0:c746ee34feae | 166 | //set the mbed COM port to match the GPS transmit rate |
| dannyman939 | 0:c746ee34feae | 167 | //the below baud rate must match the COM1 rate coming from the GPS receiver |
| jekain314 | 9:b45feb91ba38 | 168 | GPS_COM1.baud(115200); wait_ms(500); //without this wait -- the baud rate is not detected when using MODSERIAL |
| jekain314 | 4:68268737ff89 | 169 | //GPS_COM1.baud(921600); wait_ms(500); //without this wait -- the baud rate is not detected when using MODSERIAL |
| dannyman939 | 0:c746ee34feae | 170 | }; |
| dannyman939 | 0:c746ee34feae | 171 | |
| dannyman939 | 0:c746ee34feae | 172 | void setupTriggers() |
| dannyman939 | 0:c746ee34feae | 173 | { |
| dannyman939 | 1:cbb9104d826f | 174 | camera1Int.mode(PullUp); |
| dannyman939 | 1:cbb9104d826f | 175 | camera2Pin = 1; |
| dannyman939 | 0:c746ee34feae | 176 | //establish Trigger ISR |
| dannyman939 | 1:cbb9104d826f | 177 | camera1Int.rise(&camera1ISR); |
| dannyman939 | 0:c746ee34feae | 178 | |
| dannyman939 | 3:5913df46f94a | 179 | }; |
| dannyman939 | 0:c746ee34feae | 180 | |
| jekain314 | 6:2a8486283198 | 181 | ///////////////////////////////////////////////////////////////////// |
| jekain314 | 6:2a8486283198 | 182 | // mbed main to support the Waldo_FCS |
| jekain314 | 6:2a8486283198 | 183 | ///////////////////////////////////////////////////////////////////// |
| jekain314 | 6:2a8486283198 | 184 | int main() { |
| jekain314 | 4:68268737ff89 | 185 | |
| jekain314 | 6:2a8486283198 | 186 | //these are structures for the to GPS messages that must be parsed |
| jekain314 | 9:b45feb91ba38 | 187 | MESSAGEHEADER msgHdr; |
| jekain314 | 6:2a8486283198 | 188 | OEM615BESTPOS posMsg; //BESTPOS structure in OEMV615.h that has matching time to a BESTVEL message |
| jekain314 | 4:68268737ff89 | 189 | OEM615BESTPOS curPos; //BESTPOS structure in OEMV615.h |
| jekain314 | 6:2a8486283198 | 190 | OEM615BESTVEL velMsg; //BESTVEL structure in OEMV615.h that has matching time to a BESTPOS message |
| jekain314 | 4:68268737ff89 | 191 | OEM615BESTVEL curVel; //BESTVEL structure in OEMV615.h |
| dannyman939 | 0:c746ee34feae | 192 | |
| dannyman939 | 2:7301e63832ee | 193 | //set up the GPS and mbed COM ports |
| dannyman939 | 0:c746ee34feae | 194 | setupCOM(); |
| dannyman939 | 0:c746ee34feae | 195 | |
| dannyman939 | 0:c746ee34feae | 196 | //set up the ADIS16488 |
| dannyman939 | 0:c746ee34feae | 197 | setupADIS(); |
| dannyman939 | 0:c746ee34feae | 198 | |
| dannyman939 | 0:c746ee34feae | 199 | //setup Hotshoe |
| dannyman939 | 0:c746ee34feae | 200 | setupTriggers(); |
| jekain314 | 6:2a8486283198 | 201 | |
| jekain314 | 6:2a8486283198 | 202 | setUpMessages(); //set up the expected text message commands frm the PC |
| dannyman939 | 0:c746ee34feae | 203 | |
| jekain314 | 4:68268737ff89 | 204 | //set up the interrupt to catch the GPS receiver serial bytes as they are presented |
| dannyman939 | 0:c746ee34feae | 205 | GPS_COM1.attach(&readSerialByte, MODSERIAL::RxIrq); |
| dannyman939 | 0:c746ee34feae | 206 | |
| jekain314 | 4:68268737ff89 | 207 | timeFromPPS.start(); //start the time for measuring time from 1PPS events |
| jekain314 | 6:2a8486283198 | 208 | |
| jekain314 | 6:2a8486283198 | 209 | toPC.printf("\n\n top of the main loop \n\n"); |
| jekain314 | 6:2a8486283198 | 210 | |
| jekain314 | 6:2a8486283198 | 211 | int totalBytesWritten = 0; |
| jekain314 | 6:2a8486283198 | 212 | |
| jekain314 | 9:b45feb91ba38 | 213 | /* |
| jekain314 | 9:b45feb91ba38 | 214 | unsigned long CRC = 0; |
| jekain314 | 9:b45feb91ba38 | 215 | CRC32Value(CRC, 0xAA); |
| jekain314 | 9:b45feb91ba38 | 216 | CRC32Value(CRC, 0x44); |
| jekain314 | 9:b45feb91ba38 | 217 | CRC32Value(CRC, 0x12); |
| jekain314 | 9:b45feb91ba38 | 218 | CRC32Value(CRC, 0x1C); |
| jekain314 | 9:b45feb91ba38 | 219 | toPC.printf(" CRC after AA44121C header: %08x \n", CRC); |
| jekain314 | 9:b45feb91ba38 | 220 | wait(20); |
| jekain314 | 9:b45feb91ba38 | 221 | */ |
| jekain314 | 9:b45feb91ba38 | 222 | |
| jekain314 | 9:b45feb91ba38 | 223 | int CRCerrors = 0; |
| jekain314 | 9:b45feb91ba38 | 224 | |
| jekain314 | 9:b45feb91ba38 | 225 | recordData = true; |
| jekain314 | 9:b45feb91ba38 | 226 | sendRecData = true; |
| jekain314 | 9:b45feb91ba38 | 227 | |
| jekain314 | 9:b45feb91ba38 | 228 | while(PPSCounter < 300) |
| jekain314 | 4:68268737ff89 | 229 | /////////////////////////////////////////////////////////////////////////// |
| jekain314 | 4:68268737ff89 | 230 | // top of the while loop |
| jekain314 | 4:68268737ff89 | 231 | /////////////////////////////////////////////////////////////////////////// |
| jekain314 | 9:b45feb91ba38 | 232 | //while(1) |
| dannyman939 | 0:c746ee34feae | 233 | { |
| dannyman939 | 0:c746ee34feae | 234 | //read the USB serial data from the PC to check for commands |
| jekain314 | 4:68268737ff89 | 235 | //in the primary real-time portion, there are no bytes from the PC so this has no impact |
| dannyman939 | 0:c746ee34feae | 236 | readFromPC(); |
| jekain314 | 7:2e20e4cf53e6 | 237 | |
| jekain314 | 6:2a8486283198 | 238 | processPCmessages(fpNav, posMsg, velMsg); |
| dannyman939 | 0:c746ee34feae | 239 | |
| jekain314 | 6:2a8486283198 | 240 | // |
| jekain314 | 4:68268737ff89 | 241 | //////////////////////////////////////////////////////////////////////////// |
| jekain314 | 4:68268737ff89 | 242 | //below is where we process the complete stored GPS message for the second |
| jekain314 | 4:68268737ff89 | 243 | //The !IMUDataReady test prevents the IMU and GPS data from being written |
| jekain314 | 9:b45feb91ba38 | 244 | //to disk on the same pass through this loop |
| jekain314 | 4:68268737ff89 | 245 | ///////////////////////////////////////////////////////////////////////////// |
| jekain314 | 9:b45feb91ba38 | 246 | |
| jekain314 | 9:b45feb91ba38 | 247 | |
| jekain314 | 9:b45feb91ba38 | 248 | if (completeMessageAvailable && !IMUDataReady) |
| jekain314 | 9:b45feb91ba38 | 249 | { |
| jekain314 | 9:b45feb91ba38 | 250 | |
| jekain314 | 9:b45feb91ba38 | 251 | msgHdr = *((MESSAGEHEADER*)&msgBuffer[messageLocation[savedMessageCounter-1]]); |
| jekain314 | 4:68268737ff89 | 252 | |
| jekain314 | 9:b45feb91ba38 | 253 | GPSTimemsecs = msgHdr.GPSTime_msecs; |
| jekain314 | 9:b45feb91ba38 | 254 | ///////////////////////////////////////////////////////////////////////////////////////// |
| jekain314 | 9:b45feb91ba38 | 255 | //IMPORTANT: we reset the PPSTimeOffset when we have a matching position and velocity |
| jekain314 | 9:b45feb91ba38 | 256 | PPSTimeOffset = 0; |
| jekain314 | 9:b45feb91ba38 | 257 | ///////////////////////////////////////////////////////////////////////////////////////// |
| jekain314 | 9:b45feb91ba38 | 258 | |
| jekain314 | 9:b45feb91ba38 | 259 | unsigned long msgCRC = *((unsigned long*)&msgBuffer[messageLocation[savedMessageCounter-1] + 28 + msgHdr.messageLength]); |
| jekain314 | 9:b45feb91ba38 | 260 | |
| jekain314 | 9:b45feb91ba38 | 261 | //toPC.printf("tmeFrom1PPS= %5d ID= %3d Ln = %3d computedCRC= %08x msgCRC= %08x msgCntr = %3d CRCerr=%4d\n", |
| jekain314 | 9:b45feb91ba38 | 262 | // timeFromPPS.read_us(), msgHdr.messageID, msgHdr.messageLength, computedCRC, msgCRC, savedMessageCounter, TotalBadCRCmatches); |
| jekain314 | 9:b45feb91ba38 | 263 | |
| jekain314 | 9:b45feb91ba38 | 264 | if ( msgCRC != computedCRC) |
| dannyman939 | 0:c746ee34feae | 265 | { |
| jekain314 | 9:b45feb91ba38 | 266 | toPC.printf(" bad CRC match for messageID %3d total CRC errors = %4d \n", |
| jekain314 | 9:b45feb91ba38 | 267 | msgHdr.messageLength, TotalBadCRCmatches++); |
| jekain314 | 9:b45feb91ba38 | 268 | } |
| jekain314 | 9:b45feb91ba38 | 269 | |
| jekain314 | 9:b45feb91ba38 | 270 | if (msgHdr.messageID == 42) |
| jekain314 | 9:b45feb91ba38 | 271 | { |
| jekain314 | 9:b45feb91ba38 | 272 | curPos = *((OEM615BESTPOS*)&msgBuffer[messageLocation[savedMessageCounter-1]]); |
| jekain314 | 9:b45feb91ba38 | 273 | posMsg = curPos; |
| jekain314 | 9:b45feb91ba38 | 274 | |
| jekain314 | 9:b45feb91ba38 | 275 | if (streamPos) |
| dannyman939 | 2:7301e63832ee | 276 | { |
| jekain314 | 9:b45feb91ba38 | 277 | toPC.printf("WMsg BESTPOS %5d %1d %8.5lf %9.5lf %5.3lf %d %d %d\n", |
| jekain314 | 9:b45feb91ba38 | 278 | curPos.msgHeader.GPSTime_msecs, curPos.solStatus, |
| jekain314 | 9:b45feb91ba38 | 279 | curPos.latitude, curPos.longitude, curPos.height, |
| jekain314 | 9:b45feb91ba38 | 280 | curPos.numSV, curPos.numSolSV, curPos.numGGL1); |
| jekain314 | 4:68268737ff89 | 281 | } |
| jekain314 | 4:68268737ff89 | 282 | |
| jekain314 | 9:b45feb91ba38 | 283 | ///////////////////////////////////////////////////////////////////////////////////////// |
| jekain314 | 9:b45feb91ba38 | 284 | //IMPORTANT: we reset the PPSTimeOffset when we have a matching position and velocity |
| jekain314 | 9:b45feb91ba38 | 285 | PPSTimeOffset = 0; |
| jekain314 | 9:b45feb91ba38 | 286 | ///////////////////////////////////////////////////////////////////////////////////////// |
| jekain314 | 9:b45feb91ba38 | 287 | } |
| jekain314 | 9:b45feb91ba38 | 288 | else if (msgHdr.messageID == 99) |
| jekain314 | 9:b45feb91ba38 | 289 | { |
| jekain314 | 9:b45feb91ba38 | 290 | curVel = *((OEM615BESTVEL*)&msgBuffer[ messageLocation[savedMessageCounter-1] ]); |
| jekain314 | 9:b45feb91ba38 | 291 | velMsg = curVel; |
| jekain314 | 9:b45feb91ba38 | 292 | } |
| jekain314 | 4:68268737ff89 | 293 | |
| jekain314 | 6:2a8486283198 | 294 | if (recordData && (fpNav != NULL) && (byteCounter > 0)) |
| jekain314 | 6:2a8486283198 | 295 | { |
| jekain314 | 9:b45feb91ba38 | 296 | //wait_us(10); |
| jekain314 | 9:b45feb91ba38 | 297 | int totalMessageLength = 28 + msgHdr.messageLength + 4; //header length + message Length + CRC word size |
| jekain314 | 9:b45feb91ba38 | 298 | totalBytesWritten += fwrite(&msgBuffer[messageLocation[savedMessageCounter-1]], 1, totalMessageLength, fpNav); // this writes out a complete set of messages for this sec |
| jekain314 | 9:b45feb91ba38 | 299 | //wait_us(10); |
| jekain314 | 6:2a8486283198 | 300 | } |
| jekain314 | 9:b45feb91ba38 | 301 | |
| jekain314 | 9:b45feb91ba38 | 302 | completeMessageAvailable = false; |
| jekain314 | 9:b45feb91ba38 | 303 | } |
| jekain314 | 6:2a8486283198 | 304 | |
| jekain314 | 4:68268737ff89 | 305 | //the IMU data record is read from the SPI in the ISR and the IMUDataReady is set true |
| jekain314 | 4:68268737ff89 | 306 | //we write the IMU data here |
| jekain314 | 4:68268737ff89 | 307 | if (IMUDataReady) //IMUDataReady is true if we have a recent IMU data record |
| jekain314 | 4:68268737ff89 | 308 | { |
| jekain314 | 6:2a8486283198 | 309 | //GPSTime (secs from midnight) is from the header of the position message |
| jekain314 | 6:2a8486283198 | 310 | //PPSTimeOffset accounts for time becoming known ~20msec AFTER the 1PPS |
| jekain314 | 9:b45feb91ba38 | 311 | IMUtimeFrom1PPS = timeFromPPS.read_us(); |
| jekain314 | 9:b45feb91ba38 | 312 | imuRec.GPSTime = GPSTimemsecs + PPSTimeOffset*1000 + IMUtimeFrom1PPS/1000.0; |
| jekain314 | 9:b45feb91ba38 | 313 | //wait_us(1); |
| jekain314 | 9:b45feb91ba38 | 314 | spi.write((int) HIGH_REGISTER[0]); //wait_us(1); // next read will return results from HIGH_REGITER[0] |
| jekain314 | 4:68268737ff89 | 315 | for (int i=0; i<6; i++) //read the 6 rate and accel variables |
| jekain314 | 4:68268737ff89 | 316 | { |
| jekain314 | 9:b45feb91ba38 | 317 | wd.pt[1] = (unsigned short)spi.write((int) LOW_REGISTER[i]); //wait_us(1) ; |
| jekain314 | 4:68268737ff89 | 318 | if (i<5) // dont this on the last because this was pre-called |
| jekain314 | 9:b45feb91ba38 | 319 | { wd.pt[0] = (unsigned short)spi.write((int) HIGH_REGISTER[i+1]); } |
| jekain314 | 4:68268737ff89 | 320 | imuRec.dataWord[i] = wd.dataWord; //data word is a signed long |
| jekain314 | 4:68268737ff89 | 321 | } |
| jekain314 | 4:68268737ff89 | 322 | IMURecordCounter++; |
| jekain314 | 4:68268737ff89 | 323 | //write the IMU data |
| dannyman939 | 0:c746ee34feae | 324 | if (recordData && (fpNav != NULL)) |
| jekain314 | 6:2a8486283198 | 325 | { |
| jekain314 | 6:2a8486283198 | 326 | totalBytesWritten += fwrite(&imuRec, 1, sizeof(IMUREC), fpNav); |
| dannyman939 | 0:c746ee34feae | 327 | } |
| jekain314 | 4:68268737ff89 | 328 | IMUDataReady = false; |
| dannyman939 | 0:c746ee34feae | 329 | } |
| jekain314 | 4:68268737ff89 | 330 | |
| jekain314 | 4:68268737ff89 | 331 | if (camera1EventDetected) //we have detected a camera trigger event |
| dannyman939 | 0:c746ee34feae | 332 | { |
| dannyman939 | 2:7301e63832ee | 333 | toPC.printf("WMsg TRIGGERTIME %5.3lf\n", camera1Time); |
| dannyman939 | 0:c746ee34feae | 334 | camera1EventDetected = false; |
| dannyman939 | 0:c746ee34feae | 335 | } |
| jekain314 | 4:68268737ff89 | 336 | |
| jekain314 | 4:68268737ff89 | 337 | if (detectedGPS1PPS) //true if we are exactly at a 1PPS event detection |
| dannyman939 | 0:c746ee34feae | 338 | { |
| jekain314 | 9:b45feb91ba38 | 339 | toPC.printf("\n PPSCounter=%4d byteCounter=%10d Msgs Received=%3d IMUClock=%4d IMURec = %3d bytesWritten=%8d\n", |
| jekain314 | 9:b45feb91ba38 | 340 | PPSCounter, savedByteCounter, savedPerSecMessageCounter, savedIMUClockCounter, IMURecordCounter, totalBytesWritten); |
| jekain314 | 9:b45feb91ba38 | 341 | |
| jekain314 | 9:b45feb91ba38 | 342 | IMURecordCounter = 0; |
| dannyman939 | 0:c746ee34feae | 343 | detectedGPS1PPS = false; |
| dannyman939 | 0:c746ee34feae | 344 | } |
| dannyman939 | 0:c746ee34feae | 345 | } |
| jekain314 | 9:b45feb91ba38 | 346 | |
| jekain314 | 4:68268737ff89 | 347 | fclose(fpNav); |
| dannyman939 | 0:c746ee34feae | 348 | toPC.printf(" normal termination \n"); |
| dannyman939 | 0:c746ee34feae | 349 | } |
