this version has all of Jim's fixes for reading the GPS and IMU data synchronously
Dependencies: MODSERIAL SDFileSystem mbed SDShell CRC CommHandler FP LinkedList LogUtil
main.cpp@10:37ff5b97cf98, 2013-05-05 (annotated)
- Committer:
- jekain314
- Date:
- Sun May 05 22:03:52 2013 +0000
- Revision:
- 10:37ff5b97cf98
- Parent:
- 9:893481b2e488
- Child:
- 12:c2f60b3ceb74
tinkered with the termination process.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jekain314 | 0:432b860b6ff7 | 1 | #include "mbed.h" |
jekain314 | 0:432b860b6ff7 | 2 | #include <string> |
jekain314 | 0:432b860b6ff7 | 3 | |
jekain314 | 0:432b860b6ff7 | 4 | //set up the message buffer to be filled by the GPS read process |
jekain314 | 0:432b860b6ff7 | 5 | #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 256 |
jekain314 | 0:432b860b6ff7 | 6 | |
jekain314 | 0:432b860b6ff7 | 7 | #include "MODSERIAL.h" |
jekain314 | 0:432b860b6ff7 | 8 | #include "SDFileSystem.h" //imported using the import utility |
jekain314 | 9:893481b2e488 | 9 | #include "SDShell.h" |
jekain314 | 0:432b860b6ff7 | 10 | |
jekain314 | 0:432b860b6ff7 | 11 | //general digital I/O specifications for this application |
jekain314 | 0:432b860b6ff7 | 12 | //SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name); |
jekain314 | 0:432b860b6ff7 | 13 | SDFileSystem sd(p11,p12,p13,p14,"sd"); |
jekain314 | 0:432b860b6ff7 | 14 | DigitalIn sd_detect(p27); |
jekain314 | 0:432b860b6ff7 | 15 | DigitalOut ppsled(LED1); //blink an LED at the 1PPS |
jekain314 | 0:432b860b6ff7 | 16 | DigitalOut trig1led(LED2); //blink an LED at the camera trigger detection |
jekain314 | 1:8e24e633f8d8 | 17 | DigitalOut rxMsg(LED3); |
jekain314 | 1:8e24e633f8d8 | 18 | DigitalOut txMsg(LED4); |
jekain314 | 1:8e24e633f8d8 | 19 | //DigitalOut recordDataled(LED4); //set the led when the record is on |
jekain314 | 1:8e24e633f8d8 | 20 | |
jekain314 | 1:8e24e633f8d8 | 21 | //hardware trigger mechanization for bulb shutter commands |
jekain314 | 1:8e24e633f8d8 | 22 | DigitalInOut fire(p29); //connected to the tip of 2.5mm connector (T2i) |
jekain314 | 1:8e24e633f8d8 | 23 | DigitalInOut pre_fire(p30); //connected to the mid-connection for 2.5mm connector (T2i) |
jekain314 | 1:8e24e633f8d8 | 24 | |
jekain314 | 1:8e24e633f8d8 | 25 | |
jekain314 | 0:432b860b6ff7 | 26 | //USB serial data stream back to the PC |
jekain314 | 0:432b860b6ff7 | 27 | Serial toPC(USBTX, USBRX); //connect the GPS TX, RX to p9 and p10 |
jekain314 | 0:432b860b6ff7 | 28 | |
jekain314 | 0:432b860b6ff7 | 29 | Timer timeFromStart; |
jekain314 | 2:7039be3daf6e | 30 | Timer timeFromPosVelMessageReceipt; |
jekain314 | 0:432b860b6ff7 | 31 | |
jekain314 | 0:432b860b6ff7 | 32 | bool detectedGPS1PPS = false; //flag set in the ISR and reset after processing the 1PPS event |
jekain314 | 0:432b860b6ff7 | 33 | int PPSCounter = 0; //counts the 1PPS occurrences |
jekain314 | 0:432b860b6ff7 | 34 | int byteCounter = 0; //byte counter -- zeroed at 1PPS |
jekain314 | 0:432b860b6ff7 | 35 | unsigned short perSecMessageCounter=0; //counts the number of messages in a sec based on the header detection |
jekain314 | 0:432b860b6ff7 | 36 | bool messageDetected = false; //have detected a message header |
jekain314 | 0:432b860b6ff7 | 37 | unsigned long IMUbytesWritten = 0; //counts the IMU bytes written by the fwrite() to the SD card |
jekain314 | 0:432b860b6ff7 | 38 | int savedByteCounter = 0; //save ByteCounter at the 1PPS for display in main |
jekain314 | 0:432b860b6ff7 | 39 | int savedPerSecMessageCounter=0; //saved PerSecMsgCounter for display in main |
jekain314 | 0:432b860b6ff7 | 40 | int savedIMUClockCounter=0; //saved at the 1PPS for later diaplay from main |
jekain314 | 0:432b860b6ff7 | 41 | bool camera1EventDetected = false; //flag from ISR indicating a clock event occurred |
jekain314 | 0:432b860b6ff7 | 42 | double camera1Time; //GPS time of the camera event |
jekain314 | 0:432b860b6ff7 | 43 | int TotalBadCRCmatches = 0; //counter for the bad CRC matches for all GPS messages |
jekain314 | 0:432b860b6ff7 | 44 | |
jekain314 | 0:432b860b6ff7 | 45 | volatile int PPSTimeOffset = 0; |
jekain314 | 0:432b860b6ff7 | 46 | |
jekain314 | 9:893481b2e488 | 47 | // stuff to send the SD file to the PC |
jekain314 | 9:893481b2e488 | 48 | void transferFile() |
jekain314 | 9:893481b2e488 | 49 | { |
jekain314 | 9:893481b2e488 | 50 | SDShell emulate; |
jekain314 | 9:893481b2e488 | 51 | emulate.init(); |
jekain314 | 10:37ff5b97cf98 | 52 | // just indicate that we're in here |
jekain314 | 9:893481b2e488 | 53 | trig1led = ppsled = rxMsg = txMsg = 1; |
jekain314 | 10:37ff5b97cf98 | 54 | |
jekain314 | 10:37ff5b97cf98 | 55 | toPC.printf("Entering Shell Emulator...\n"); |
jekain314 | 10:37ff5b97cf98 | 56 | wait(0.1f); |
jekain314 | 10:37ff5b97cf98 | 57 | emulate.shell(toPC, sd, "/sd"); |
jekain314 | 9:893481b2e488 | 58 | } |
jekain314 | 9:893481b2e488 | 59 | |
jekain314 | 0:432b860b6ff7 | 60 | ////////////////////////////////////////////////////////////////////// |
jekain314 | 0:432b860b6ff7 | 61 | // the below should become classes |
jekain314 | 0:432b860b6ff7 | 62 | ////////////////////////////////////////////////////////////////////// |
jekain314 | 0:432b860b6ff7 | 63 | #include "OEM615.h" //OEM615 GPS activities |
jekain314 | 0:432b860b6ff7 | 64 | #include "ADIS16488.h" //ADIS16488 activities |
jekain314 | 0:432b860b6ff7 | 65 | #include "PCMessaging.h" //PC messaging activities |
jekain314 | 0:432b860b6ff7 | 66 | |
jekain314 | 0:432b860b6ff7 | 67 | //ISR for detection of the GPS 1PPS |
jekain314 | 0:432b860b6ff7 | 68 | void detect1PPSISR(void) |
jekain314 | 0:432b860b6ff7 | 69 | { |
jekain314 | 0:432b860b6ff7 | 70 | timeFromPPS.reset(); //reset the 1PPS timer upon 1PPS detection |
jekain314 | 0:432b860b6ff7 | 71 | //note -- the below accounts for time information becoming available AFTER the 1PPS event |
jekain314 | 0:432b860b6ff7 | 72 | PPSTimeOffset++; //counts 1PPS events between matching POS and VEL messages |
jekain314 | 0:432b860b6ff7 | 73 | |
jekain314 | 0:432b860b6ff7 | 74 | //covers the case where the PPS ISR interrupts the IMU data ready ISR |
jekain314 | 0:432b860b6ff7 | 75 | if(IMUDataReady) IMUtimeFrom1PPS = 0; |
jekain314 | 0:432b860b6ff7 | 76 | |
jekain314 | 0:432b860b6ff7 | 77 | savedByteCounter = byteCounter; //save byteCounter for display in main |
jekain314 | 0:432b860b6ff7 | 78 | savedPerSecMessageCounter = perSecMessageCounter; //save for display un main |
jekain314 | 0:432b860b6ff7 | 79 | byteCounter = 0; //countes bytes between 1PPS events |
jekain314 | 0:432b860b6ff7 | 80 | perSecMessageCounter = 0; //counts GPS messages between 1PPS events |
jekain314 | 0:432b860b6ff7 | 81 | |
jekain314 | 0:432b860b6ff7 | 82 | GPS_COM1.rxBufferFlush(); //flush the GPS serial buffer |
jekain314 | 0:432b860b6ff7 | 83 | |
jekain314 | 0:432b860b6ff7 | 84 | detectedGPS1PPS = true; //set false in the main when 1PPS actions are complete |
jekain314 | 0:432b860b6ff7 | 85 | PPSCounter++; //count number of 1PPS epoch |
jekain314 | 0:432b860b6ff7 | 86 | |
jekain314 | 0:432b860b6ff7 | 87 | ppsled = !ppsled; //blink an LED at the 1PPS |
jekain314 | 0:432b860b6ff7 | 88 | }; |
jekain314 | 0:432b860b6ff7 | 89 | |
jekain314 | 0:432b860b6ff7 | 90 | |
jekain314 | 0:432b860b6ff7 | 91 | /////////////////////////////////////////////////////// |
jekain314 | 0:432b860b6ff7 | 92 | //set up the USB port and the GPS COM port |
jekain314 | 0:432b860b6ff7 | 93 | /////////////////////////////////////////////////////// |
jekain314 | 0:432b860b6ff7 | 94 | FILE *fpNav = NULL; //file pointer to the nav file on the SD card |
jekain314 | 0:432b860b6ff7 | 95 | void setupCOM(void) |
jekain314 | 0:432b860b6ff7 | 96 | { |
jekain314 | 0:432b860b6ff7 | 97 | //system starts with GPS in reset active |
jekain314 | 0:432b860b6ff7 | 98 | //dis-engage the reset to get the GPS started |
jekain314 | 0:432b860b6ff7 | 99 | GPS_Reset=1; wait_ms(1000); |
jekain314 | 0:432b860b6ff7 | 100 | |
jekain314 | 0:432b860b6ff7 | 101 | //establish 1PPS ISR |
jekain314 | 0:432b860b6ff7 | 102 | PPSInt.rise(&detect1PPSISR); |
jekain314 | 0:432b860b6ff7 | 103 | |
jekain314 | 0:432b860b6ff7 | 104 | //set the USB serial data rate -- rate must be matched at the PC end |
jekain314 | 0:432b860b6ff7 | 105 | //This the serial communication back to the the PC host |
jekain314 | 0:432b860b6ff7 | 106 | //Launch the C++ serial port read program there to catch the ASCII characters |
jekain314 | 0:432b860b6ff7 | 107 | //toPC.baud(9600); wait_ms(100); |
jekain314 | 0:432b860b6ff7 | 108 | toPC.baud(8*115200); wait_ms(100); |
jekain314 | 0:432b860b6ff7 | 109 | //toPC.baud(1*115200); wait_ms(100); |
jekain314 | 6:71da5b99de97 | 110 | //toPC.printf("\n\n released GPS from RESET and set to high baud rate \n\n"); |
jekain314 | 0:432b860b6ff7 | 111 | |
jekain314 | 0:432b860b6ff7 | 112 | //just wait to launch the GPS receiver |
jekain314 | 3:e1a884e5325a | 113 | for (int i=0; i<5; i++) { toPC.printf("WMsg start: %3d \n", 4-i); wait(1); } |
jekain314 | 0:432b860b6ff7 | 114 | |
jekain314 | 0:432b860b6ff7 | 115 | sd_detect.mode(PullUp); |
jekain314 | 0:432b860b6ff7 | 116 | |
jekain314 | 0:432b860b6ff7 | 117 | if (sd_detect == 0) |
jekain314 | 0:432b860b6ff7 | 118 | { |
jekain314 | 0:432b860b6ff7 | 119 | mkdir("/sd/Data", 0777); |
jekain314 | 0:432b860b6ff7 | 120 | } |
jekain314 | 0:432b860b6ff7 | 121 | else |
jekain314 | 0:432b860b6ff7 | 122 | { |
jekain314 | 6:71da5b99de97 | 123 | toPC.printf("WMsg SD card not present \n"); |
jekain314 | 0:432b860b6ff7 | 124 | } |
jekain314 | 0:432b860b6ff7 | 125 | |
jekain314 | 0:432b860b6ff7 | 126 | //NOTE: we do not assume that the GPS receiver has been pre-set up for the WALDO_FCS functionality |
jekain314 | 0:432b860b6ff7 | 127 | //we alwsys start with a reset and reprogram the receiver with our data out products |
jekain314 | 0:432b860b6ff7 | 128 | // this prevents failure because of a blown NVRAM as occurred for the older camera systems |
jekain314 | 0:432b860b6ff7 | 129 | |
jekain314 | 0:432b860b6ff7 | 130 | //this is the COM1 port from th GPS receiuver to the mbed |
jekain314 | 0:432b860b6ff7 | 131 | //it should be always started at 9600 baud because thats the default for the GPS receiver |
jekain314 | 0:432b860b6ff7 | 132 | GPS_COM1.baud(9600); wait_ms(100); |
jekain314 | 0:432b860b6ff7 | 133 | |
jekain314 | 0:432b860b6ff7 | 134 | // this ASCII command sets up the serial data from the GPS receiver on its COM1 |
jekain314 | 0:432b860b6ff7 | 135 | char ch7[] = "serialconfig COM1 9600 n 8 1 n off"; |
jekain314 | 0:432b860b6ff7 | 136 | // this is a software reset and has the same effect as a hardware reset (why do it?) |
jekain314 | 0:432b860b6ff7 | 137 | //char ch0[] = "RESET"; |
jekain314 | 0:432b860b6ff7 | 138 | //this command stops all communication from the GPS receiver on COM1 |
jekain314 | 0:432b860b6ff7 | 139 | //logs should still be presented on USB port so the Novatel CDU application can be used on the PC in parallel |
jekain314 | 0:432b860b6ff7 | 140 | char ch1[] = "unlogall COM1"; |
jekain314 | 0:432b860b6ff7 | 141 | //set the final baud rate that we will use from here |
jekain314 | 0:432b860b6ff7 | 142 | //allowable baud rate values: 9600 115200 230400 460800 921600 |
jekain314 | 0:432b860b6ff7 | 143 | //char ch2[] = "serialconfig COM1 921600 n 8 1 n off"; |
jekain314 | 0:432b860b6ff7 | 144 | char ch2[] = "serialconfig COM1 115200 n 8 1 n off"; |
jekain314 | 0:432b860b6ff7 | 145 | |
jekain314 | 0:432b860b6ff7 | 146 | //the below commands request the POS, VEL, RANGE, and TIME messages |
jekain314 | 0:432b860b6ff7 | 147 | char ch3[] = "log COM1 BESTPOSB ONTIME 1"; //messageID = 42 |
jekain314 | 0:432b860b6ff7 | 148 | char ch4[] = "log COM1 BESTVelB ONTIME 1"; //messageID = 99 |
jekain314 | 0:432b860b6ff7 | 149 | char ch5[] = "log COM1 RANGEB ONTIME 1"; //messageID = 43 |
jekain314 | 0:432b860b6ff7 | 150 | //char ch6[] = "log COM1 TIMEB ONTIME 1"; //messageID = 101 |
jekain314 | 0:432b860b6ff7 | 151 | |
jekain314 | 0:432b860b6ff7 | 152 | //set up VARF to be 100Hz with 1X10^4 * 10^-8 = 10^-4 sec (10usec) pulse width |
jekain314 | 0:432b860b6ff7 | 153 | //in fact, we do not use this output but it is available. |
jekain314 | 0:432b860b6ff7 | 154 | //originally planned to use this to command the IMU data |
jekain314 | 0:432b860b6ff7 | 155 | //char ch8[] = "FREQUENCYOUT enable 10000 1000000"; |
jekain314 | 0:432b860b6ff7 | 156 | |
jekain314 | 3:e1a884e5325a | 157 | toPC.printf("WMsg set serial config \n"); |
jekain314 | 0:432b860b6ff7 | 158 | sendASCII(ch7, sizeof(ch7)); wait_ms(500); |
jekain314 | 0:432b860b6ff7 | 159 | //sendASCII(ch0, sizeof(ch0)); |
jekain314 | 3:e1a884e5325a | 160 | toPC.printf("WMsg unlog all messages \n"); |
jekain314 | 0:432b860b6ff7 | 161 | sendASCII(ch1, sizeof(ch1)); wait_ms(500); |
jekain314 | 3:e1a884e5325a | 162 | toPC.printf("WMsg log BESTPOSB on COM1 \n"); |
jekain314 | 0:432b860b6ff7 | 163 | sendASCII(ch3, sizeof(ch3)); wait_ms(500); |
jekain314 | 3:e1a884e5325a | 164 | toPC.printf("WMsg log BESTVELB on COM1\n"); |
jekain314 | 0:432b860b6ff7 | 165 | sendASCII(ch4, sizeof(ch4)); wait_ms(500); |
jekain314 | 3:e1a884e5325a | 166 | toPC.printf("WMsg log RANGEB on COM1\n"); |
jekain314 | 0:432b860b6ff7 | 167 | sendASCII(ch5, sizeof(ch5)); wait_ms(500); |
jekain314 | 0:432b860b6ff7 | 168 | |
jekain314 | 0:432b860b6ff7 | 169 | //toPC.printf("log TIMEB om COM1 \n"); |
jekain314 | 0:432b860b6ff7 | 170 | //sendASCII(ch6, sizeof(ch6)); wait_ms(100); |
jekain314 | 0:432b860b6ff7 | 171 | |
jekain314 | 0:432b860b6ff7 | 172 | //toPC.printf("Set up th VARF signal \n"); |
jekain314 | 0:432b860b6ff7 | 173 | //sendASCII(ch8, sizeof(ch8)); wait_ms(500); |
jekain314 | 0:432b860b6ff7 | 174 | |
jekain314 | 0:432b860b6ff7 | 175 | //set GPS output COM1 to the final high rate |
jekain314 | 3:e1a884e5325a | 176 | toPC.printf("WMsg set the COM ports to high rate\n"); |
jekain314 | 0:432b860b6ff7 | 177 | sendASCII(ch2, sizeof(ch2)); wait_ms(500); |
jekain314 | 0:432b860b6ff7 | 178 | |
jekain314 | 0:432b860b6ff7 | 179 | //set the mbed COM port to match the GPS transmit rate |
jekain314 | 0:432b860b6ff7 | 180 | //the below baud rate must match the COM1 rate coming from the GPS receiver |
jekain314 | 0:432b860b6ff7 | 181 | GPS_COM1.baud(115200); wait_ms(500); //without this wait -- the baud rate is not detected when using MODSERIAL |
jekain314 | 0:432b860b6ff7 | 182 | //GPS_COM1.baud(921600); wait_ms(500); //without this wait -- the baud rate is not detected when using MODSERIAL |
jekain314 | 0:432b860b6ff7 | 183 | }; |
jekain314 | 0:432b860b6ff7 | 184 | |
jekain314 | 0:432b860b6ff7 | 185 | |
jekain314 | 0:432b860b6ff7 | 186 | ///////////////////////////////////////////////////////////////////// |
jekain314 | 0:432b860b6ff7 | 187 | // mbed main to support the Waldo_FCS |
jekain314 | 0:432b860b6ff7 | 188 | ///////////////////////////////////////////////////////////////////// |
jekain314 | 10:37ff5b97cf98 | 189 | int main() |
jekain314 | 10:37ff5b97cf98 | 190 | { |
jekain314 | 0:432b860b6ff7 | 191 | |
jekain314 | 0:432b860b6ff7 | 192 | //these are structures for the to GPS messages that must be parsed |
jekain314 | 0:432b860b6ff7 | 193 | MESSAGEHEADER msgHdr; |
jekain314 | 0:432b860b6ff7 | 194 | OEM615BESTPOS posMsg; //BESTPOS structure in OEMV615.h that has matching time to a BESTVEL message |
jekain314 | 0:432b860b6ff7 | 195 | OEM615BESTPOS curPos; //BESTPOS structure in OEMV615.h |
jekain314 | 0:432b860b6ff7 | 196 | OEM615BESTVEL velMsg; //BESTVEL structure in OEMV615.h that has matching time to a BESTPOS message |
jekain314 | 0:432b860b6ff7 | 197 | OEM615BESTVEL curVel; //BESTVEL structure in OEMV615.h |
jekain314 | 0:432b860b6ff7 | 198 | |
jekain314 | 1:8e24e633f8d8 | 199 | |
jekain314 | 1:8e24e633f8d8 | 200 | fire.output(); //set the fire pin as outoput |
jekain314 | 1:8e24e633f8d8 | 201 | pre_fire.output(); //set the pre-fire pin as output |
jekain314 | 1:8e24e633f8d8 | 202 | //fire.mode(OpenDrain); |
jekain314 | 1:8e24e633f8d8 | 203 | |
jekain314 | 1:8e24e633f8d8 | 204 | //set up for the first trigger |
jekain314 | 1:8e24e633f8d8 | 205 | fire = 1; |
jekain314 | 1:8e24e633f8d8 | 206 | pre_fire = 1; |
jekain314 | 0:432b860b6ff7 | 207 | //set up the GPS and mbed COM ports |
jekain314 | 0:432b860b6ff7 | 208 | setupCOM(); |
jekain314 | 0:432b860b6ff7 | 209 | |
jekain314 | 0:432b860b6ff7 | 210 | //set up the ADIS16488 |
jekain314 | 0:432b860b6ff7 | 211 | setupADIS(); |
jekain314 | 0:432b860b6ff7 | 212 | |
jekain314 | 0:432b860b6ff7 | 213 | setUpMessages(); //set up the expected text message commands frm the PC |
jekain314 | 0:432b860b6ff7 | 214 | |
jekain314 | 0:432b860b6ff7 | 215 | //set up the interrupt to catch the GPS receiver serial bytes as they are presented |
jekain314 | 0:432b860b6ff7 | 216 | GPS_COM1.attach(&readSerialByte, MODSERIAL::RxIrq); |
jekain314 | 0:432b860b6ff7 | 217 | |
jekain314 | 0:432b860b6ff7 | 218 | timeFromPPS.start(); //start the time for measuring time from 1PPS events |
jekain314 | 0:432b860b6ff7 | 219 | timeFromStart.start(); |
jekain314 | 0:432b860b6ff7 | 220 | |
jekain314 | 3:e1a884e5325a | 221 | //toPC.printf("\n\n top of the main loop \n\n"); |
jekain314 | 0:432b860b6ff7 | 222 | |
jekain314 | 0:432b860b6ff7 | 223 | int totalBytesWritten = 0; |
jekain314 | 0:432b860b6ff7 | 224 | |
jekain314 | 0:432b860b6ff7 | 225 | /*establish the initial value for the CRC recursion atter the header |
jekain314 | 0:432b860b6ff7 | 226 | unsigned long CRC = 0; |
jekain314 | 0:432b860b6ff7 | 227 | CRC32Value(CRC, 0xAA); |
jekain314 | 0:432b860b6ff7 | 228 | CRC32Value(CRC, 0x44); |
jekain314 | 0:432b860b6ff7 | 229 | CRC32Value(CRC, 0x12); |
jekain314 | 0:432b860b6ff7 | 230 | CRC32Value(CRC, 0x1C); |
jekain314 | 0:432b860b6ff7 | 231 | //this results in a value of: 0x39b0f0e1 |
jekain314 | 0:432b860b6ff7 | 232 | toPC.printf(" CRC after AA44121C header: %08x \n", CRC); |
jekain314 | 0:432b860b6ff7 | 233 | wait(20); |
jekain314 | 0:432b860b6ff7 | 234 | */ |
jekain314 | 1:8e24e633f8d8 | 235 | |
jekain314 | 0:432b860b6ff7 | 236 | recordData = true; |
jekain314 | 0:432b860b6ff7 | 237 | sendRecData = true; |
jekain314 | 0:432b860b6ff7 | 238 | |
jekain314 | 0:432b860b6ff7 | 239 | unsigned long cyclesPerSec = 0; |
jekain314 | 0:432b860b6ff7 | 240 | bool GPSdataWritten = false; |
jekain314 | 4:dda2ab5cc643 | 241 | bool finishTrigger = false; |
jekain314 | 4:dda2ab5cc643 | 242 | Timer triggerInterval; |
jekain314 | 0:432b860b6ff7 | 243 | |
jekain314 | 1:8e24e633f8d8 | 244 | //while(PPSCounter < 300) |
jekain314 | 0:432b860b6ff7 | 245 | /////////////////////////////////////////////////////////////////////////// |
jekain314 | 0:432b860b6ff7 | 246 | // top of the while loop |
jekain314 | 0:432b860b6ff7 | 247 | /////////////////////////////////////////////////////////////////////////// |
jekain314 | 1:8e24e633f8d8 | 248 | while(1) |
jekain314 | 0:432b860b6ff7 | 249 | { |
jekain314 | 10:37ff5b97cf98 | 250 | |
jekain314 | 0:432b860b6ff7 | 251 | //read the USB serial data from the PC to check for commands |
jekain314 | 0:432b860b6ff7 | 252 | //in the primary real-time portion, there are no bytes from the PC so this has no impact |
jekain314 | 0:432b860b6ff7 | 253 | readFromPC(); |
jekain314 | 2:7039be3daf6e | 254 | |
jekain314 | 10:37ff5b97cf98 | 255 | // |
jekain314 | 2:7039be3daf6e | 256 | //this will close the fpNav file on the SD card if the file is open |
jekain314 | 2:7039be3daf6e | 257 | //and the elapsed time from PosVel messages is > 60 secs |
jekain314 | 2:7039be3daf6e | 258 | //this prevents loosing the fpNav file if the PC goes down |
jekain314 | 10:37ff5b97cf98 | 259 | if (fpNav && (timeFromPosVelMessageReceipt.read() > 10) ) |
jekain314 | 2:7039be3daf6e | 260 | { |
jekain314 | 2:7039be3daf6e | 261 | sendRecData = true; |
jekain314 | 2:7039be3daf6e | 262 | recordData = false; |
jekain314 | 2:7039be3daf6e | 263 | } |
jekain314 | 10:37ff5b97cf98 | 264 | // |
jekain314 | 0:432b860b6ff7 | 265 | |
jekain314 | 0:432b860b6ff7 | 266 | processPCmessages(fpNav, posMsg, velMsg); |
jekain314 | 0:432b860b6ff7 | 267 | |
jekain314 | 10:37ff5b97cf98 | 268 | if (get_file_msg) break; //terminate the while loop when we receive this message from the PC |
jekain314 | 10:37ff5b97cf98 | 269 | |
jekain314 | 5:2ce1be9d4bef | 270 | if(fireTrigger) //comes from a PC request message |
jekain314 | 1:8e24e633f8d8 | 271 | { |
jekain314 | 1:8e24e633f8d8 | 272 | //pre-fire the trigger using the mid-body 2.5mm connection (T2i) |
jekain314 | 1:8e24e633f8d8 | 273 | pre_fire = 0; //pin30 (midbody of connector) set to zero |
jekain314 | 1:8e24e633f8d8 | 274 | wait(.01f); //wait for 0.25 secs |
jekain314 | 1:8e24e633f8d8 | 275 | fire = 0; //fire the trigger using the tip connection |
jekain314 | 4:dda2ab5cc643 | 276 | //wait(0.10); |
jekain314 | 4:dda2ab5cc643 | 277 | fireTrigger = false; |
jekain314 | 4:dda2ab5cc643 | 278 | finishTrigger = true; |
jekain314 | 4:dda2ab5cc643 | 279 | triggerInterval.start(); |
jekain314 | 4:dda2ab5cc643 | 280 | } |
jekain314 | 4:dda2ab5cc643 | 281 | |
jekain314 | 6:71da5b99de97 | 282 | //the trigger requires a pulse -- the above portion lowers the signal and the below raises it |
jekain314 | 6:71da5b99de97 | 283 | //this has been tested at 50 msecs and it will not fire at that pulse duration |
jekain314 | 4:dda2ab5cc643 | 284 | if(finishTrigger && triggerInterval.read_ms() > 100) |
jekain314 | 4:dda2ab5cc643 | 285 | { |
jekain314 | 1:8e24e633f8d8 | 286 | fire = 1; |
jekain314 | 1:8e24e633f8d8 | 287 | pre_fire = 1; |
jekain314 | 1:8e24e633f8d8 | 288 | unsigned long triggerTime = GPSTimemsecs + PPSTimeOffset*1000 + timeFromPPS.read_us()/1000.0; |
jekain314 | 1:8e24e633f8d8 | 289 | toPC.printf("WMsg TRIGGERTIME %10d \n", triggerTime); |
jekain314 | 4:dda2ab5cc643 | 290 | triggerInterval.reset(); |
jekain314 | 4:dda2ab5cc643 | 291 | finishTrigger = false; |
jekain314 | 1:8e24e633f8d8 | 292 | } |
jekain314 | 1:8e24e633f8d8 | 293 | |
jekain314 | 0:432b860b6ff7 | 294 | cyclesPerSec++; |
jekain314 | 0:432b860b6ff7 | 295 | |
jekain314 | 0:432b860b6ff7 | 296 | // |
jekain314 | 0:432b860b6ff7 | 297 | //////////////////////////////////////////////////////////////////////////// |
jekain314 | 0:432b860b6ff7 | 298 | //below is where we process the complete stored GPS message for the second |
jekain314 | 0:432b860b6ff7 | 299 | //The !IMUDataReady test prevents the IMU and GPS data from being written |
jekain314 | 0:432b860b6ff7 | 300 | //to disk on the same pass through this loop |
jekain314 | 0:432b860b6ff7 | 301 | ///////////////////////////////////////////////////////////////////////////// |
jekain314 | 0:432b860b6ff7 | 302 | |
jekain314 | 0:432b860b6ff7 | 303 | |
jekain314 | 0:432b860b6ff7 | 304 | if (completeMessageAvailable && !IMUDataReady) |
jekain314 | 0:432b860b6ff7 | 305 | { |
jekain314 | 0:432b860b6ff7 | 306 | |
jekain314 | 0:432b860b6ff7 | 307 | msgHdr = *((MESSAGEHEADER*)&msgBuffer[messageLocation[savedMessageCounter-1]]); |
jekain314 | 0:432b860b6ff7 | 308 | |
jekain314 | 0:432b860b6ff7 | 309 | //these times are used to tag the IMU sample time. PPSTimeOffset increments by 1 exactly at the 1PPS event (in the 1PPS ISR) |
jekain314 | 0:432b860b6ff7 | 310 | //GPSTimemsecs increments by 1 for each new GPS measurement -- note that the below computations are actually |
jekain314 | 0:432b860b6ff7 | 311 | //done at the receipt of each GPS message. This is OK because each message has the same time in its header. |
jekain314 | 0:432b860b6ff7 | 312 | //Thus GPSTimemsecs increments by 1 here while GPSTimemsecs effectively decrements by 1. |
jekain314 | 0:432b860b6ff7 | 313 | //This handles IMU time tagging between the 1PPS event and the first receipt of a new GPS time. |
jekain314 | 0:432b860b6ff7 | 314 | |
jekain314 | 0:432b860b6ff7 | 315 | GPSTimemsecs = msgHdr.GPSTime_msecs; //time in GPS message header |
jekain314 | 0:432b860b6ff7 | 316 | PPSTimeOffset = 0; //incremented by 1 in the PPS ISR |
jekain314 | 0:432b860b6ff7 | 317 | |
jekain314 | 0:432b860b6ff7 | 318 | unsigned long msgCRC = *((unsigned long*)&msgBuffer[messageLocation[savedMessageCounter-1] + 28 + msgHdr.messageLength]); |
jekain314 | 0:432b860b6ff7 | 319 | |
jekain314 | 0:432b860b6ff7 | 320 | //toPC.printf("tmeFrom1PPS= %5d ID= %3d Ln = %3d computedCRC= %08x msgCRC= %08x msgCntr = %3d CRCerr=%4d\n", |
jekain314 | 0:432b860b6ff7 | 321 | // timeFromPPS.read_us(), msgHdr.messageID, msgHdr.messageLength, computedCRC, msgCRC, savedMessageCounter, TotalBadCRCmatches); |
jekain314 | 0:432b860b6ff7 | 322 | |
jekain314 | 0:432b860b6ff7 | 323 | if ( msgCRC != computedCRC) |
jekain314 | 0:432b860b6ff7 | 324 | { |
jekain314 | 0:432b860b6ff7 | 325 | toPC.printf(" bad CRC match for messageID %3d total CRC errors = %4d \n", |
jekain314 | 0:432b860b6ff7 | 326 | msgHdr.messageLength, TotalBadCRCmatches++); |
jekain314 | 0:432b860b6ff7 | 327 | } |
jekain314 | 0:432b860b6ff7 | 328 | |
jekain314 | 0:432b860b6ff7 | 329 | if (msgHdr.messageID == 42) |
jekain314 | 0:432b860b6ff7 | 330 | { |
jekain314 | 0:432b860b6ff7 | 331 | curPos = *((OEM615BESTPOS*)&msgBuffer[messageLocation[savedMessageCounter-1]]); |
jekain314 | 0:432b860b6ff7 | 332 | posMsg = curPos; |
jekain314 | 0:432b860b6ff7 | 333 | |
jekain314 | 1:8e24e633f8d8 | 334 | if (streamPos) |
jekain314 | 0:432b860b6ff7 | 335 | { |
jekain314 | 1:8e24e633f8d8 | 336 | toPC.printf("BESTPOS %5d %1d %8.5lf %9.5lf %5.3lf %d %d\n", |
jekain314 | 0:432b860b6ff7 | 337 | curPos.msgHeader.GPSTime_msecs, curPos.solStatus, |
jekain314 | 0:432b860b6ff7 | 338 | curPos.latitude, curPos.longitude, curPos.height, |
jekain314 | 1:8e24e633f8d8 | 339 | curPos.numSV, curPos.numSolSV); |
jekain314 | 0:432b860b6ff7 | 340 | } |
jekain314 | 0:432b860b6ff7 | 341 | |
jekain314 | 0:432b860b6ff7 | 342 | } |
jekain314 | 0:432b860b6ff7 | 343 | else if (msgHdr.messageID == 99) |
jekain314 | 0:432b860b6ff7 | 344 | { |
jekain314 | 0:432b860b6ff7 | 345 | curVel = *((OEM615BESTVEL*)&msgBuffer[ messageLocation[savedMessageCounter-1] ]); |
jekain314 | 1:8e24e633f8d8 | 346 | //toPC.printf("BESTVEL vel: horizontalSpeed= %5.3f heading=%5.1f verticalSpeed=%4.2f \n", |
jekain314 | 1:8e24e633f8d8 | 347 | // curVel.horizontalSpeed, curVel.heading, curVel.verticalSpeed ); |
jekain314 | 0:432b860b6ff7 | 348 | velMsg = curVel; |
jekain314 | 0:432b860b6ff7 | 349 | } |
jekain314 | 5:2ce1be9d4bef | 350 | |
jekain314 | 0:432b860b6ff7 | 351 | completeMessageAvailable = false; |
jekain314 | 0:432b860b6ff7 | 352 | } |
jekain314 | 0:432b860b6ff7 | 353 | |
jekain314 | 1:8e24e633f8d8 | 354 | //write the GPS data to the SD card |
jekain314 | 0:432b860b6ff7 | 355 | if (!IMUDataReady && !GPSdataWritten && timeFromPPS.read_us() > 500000 && recordData && (fpNav != NULL)) |
jekain314 | 0:432b860b6ff7 | 356 | { |
jekain314 | 0:432b860b6ff7 | 357 | totalBytesWritten += fwrite(&msgBuffer, 1, byteCounter, fpNav); |
jekain314 | 0:432b860b6ff7 | 358 | GPSdataWritten = true; |
jekain314 | 0:432b860b6ff7 | 359 | } |
jekain314 | 0:432b860b6ff7 | 360 | |
jekain314 | 0:432b860b6ff7 | 361 | //the IMU data record is read from the SPI in the ISR and the IMUDataReady is set true |
jekain314 | 0:432b860b6ff7 | 362 | //we write the IMU data here |
jekain314 | 0:432b860b6ff7 | 363 | if (IMUDataReady) //IMUDataReady is true if we have a recent IMU data record |
jekain314 | 0:432b860b6ff7 | 364 | { |
jekain314 | 0:432b860b6ff7 | 365 | //write the IMU data |
jekain314 | 0:432b860b6ff7 | 366 | if ( recordData && (fpNav != NULL) ) |
jekain314 | 0:432b860b6ff7 | 367 | { |
jekain314 | 1:8e24e633f8d8 | 368 | //delTimeOfWrite = timeFromStart.read_us(); |
jekain314 | 0:432b860b6ff7 | 369 | |
jekain314 | 0:432b860b6ff7 | 370 | if (fillingPingWritingPong) fwrite(&imuPong, 1, IMUrecArraySize*sizeof(IMUREC), fpNav); |
jekain314 | 0:432b860b6ff7 | 371 | else fwrite(&imuPing, 1, IMUrecArraySize*sizeof(IMUREC), fpNav); |
jekain314 | 0:432b860b6ff7 | 372 | |
jekain314 | 1:8e24e633f8d8 | 373 | //delTimeOfWrite = (unsigned long)((unsigned long)timeFromStart.read_us() - delTimeOfWrite); |
jekain314 | 1:8e24e633f8d8 | 374 | //if (delTimeOfWrite > maxWriteTime) maxWriteTime = delTimeOfWrite; |
jekain314 | 0:432b860b6ff7 | 375 | } |
jekain314 | 0:432b860b6ff7 | 376 | IMURecordCounter+=IMUrecArraySize; |
jekain314 | 0:432b860b6ff7 | 377 | IMUDataReady = false; |
jekain314 | 0:432b860b6ff7 | 378 | } |
jekain314 | 0:432b860b6ff7 | 379 | |
jekain314 | 0:432b860b6ff7 | 380 | if (camera1EventDetected) //we have detected a camera trigger event |
jekain314 | 0:432b860b6ff7 | 381 | { |
jekain314 | 0:432b860b6ff7 | 382 | toPC.printf("WMsg TRIGGERTIME %5.3lf\n", camera1Time); |
jekain314 | 0:432b860b6ff7 | 383 | camera1EventDetected = false; |
jekain314 | 0:432b860b6ff7 | 384 | } |
jekain314 | 0:432b860b6ff7 | 385 | |
jekain314 | 0:432b860b6ff7 | 386 | if (detectedGPS1PPS) //true if we are exactly at a 1PPS event detection |
jekain314 | 0:432b860b6ff7 | 387 | { |
jekain314 | 1:8e24e633f8d8 | 388 | //toPC.printf("PPS=%4d stat=%1d bytes=%3d GPSMsgs=%2d #write=%8d cycles=%6d\n", |
jekain314 | 1:8e24e633f8d8 | 389 | // PPSCounter, posMsg.solStatus, savedByteCounter, savedPerSecMessageCounter, |
jekain314 | 1:8e24e633f8d8 | 390 | // totalBytesWritten, cyclesPerSec ); |
jekain314 | 0:432b860b6ff7 | 391 | |
jekain314 | 0:432b860b6ff7 | 392 | cyclesPerSec = 0; |
jekain314 | 0:432b860b6ff7 | 393 | totalBytesWritten = 0; |
jekain314 | 0:432b860b6ff7 | 394 | GPSdataWritten = false; |
jekain314 | 0:432b860b6ff7 | 395 | |
jekain314 | 0:432b860b6ff7 | 396 | IMURecordCounter = 0; |
jekain314 | 0:432b860b6ff7 | 397 | detectedGPS1PPS = false; |
jekain314 | 0:432b860b6ff7 | 398 | } |
jekain314 | 0:432b860b6ff7 | 399 | } |
jekain314 | 0:432b860b6ff7 | 400 | |
jekain314 | 10:37ff5b97cf98 | 401 | if (fpNav != NULL) fclose(fpNav); //insurance |
jekain314 | 0:432b860b6ff7 | 402 | toPC.printf(" normal termination \n"); |
jekain314 | 10:37ff5b97cf98 | 403 | |
jekain314 | 10:37ff5b97cf98 | 404 | /* |
jekain314 | 10:37ff5b97cf98 | 405 | //at this point we have terminated the dat collection and we need to send the stored nav file to the PC |
jekain314 | 10:37ff5b97cf98 | 406 | FILE* fpNavStored = fopen("/sd/Data/NAV.bin", "rb"); |
jekain314 | 10:37ff5b97cf98 | 407 | unsigned char tempArray[512]; |
jekain314 | 10:37ff5b97cf98 | 408 | toPC.printf(" beginning the file transfer \n"); |
jekain314 | 10:37ff5b97cf98 | 409 | int i = 0; |
jekain314 | 10:37ff5b97cf98 | 410 | while(!feof(fpNavStored) ) |
jekain314 | 10:37ff5b97cf98 | 411 | { |
jekain314 | 10:37ff5b97cf98 | 412 | |
jekain314 | 10:37ff5b97cf98 | 413 | toPC.printf(" reading the 512 bytes: %3d \n", i); |
jekain314 | 10:37ff5b97cf98 | 414 | fread(tempArray, 1, 512, fpNavStored); |
jekain314 | 10:37ff5b97cf98 | 415 | toPC.printf(" writing the 512 bytes: %3d \n", i); |
jekain314 | 10:37ff5b97cf98 | 416 | fwrite(tempArray, 1, 512, toPC); |
jekain314 | 10:37ff5b97cf98 | 417 | i++; |
jekain314 | 10:37ff5b97cf98 | 418 | } |
jekain314 | 10:37ff5b97cf98 | 419 | */ |
jekain314 | 10:37ff5b97cf98 | 420 | |
jekain314 | 10:37ff5b97cf98 | 421 | |
jekain314 | 0:432b860b6ff7 | 422 | } |