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

Committer:
sam_grove
Date:
Fri Jun 21 05:48:02 2013 +0000
Revision:
27:94a6f0589993
Parent:
26:c2208b0ff78b
Child:
29:dead10cce6e9
added comments to main.cpp

Who changed what in which revision?

UserRevisionLine numberNew 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 0:432b860b6ff7 9
jekain314 0:432b860b6ff7 10 //general digital I/O specifications for this application
jekain314 0:432b860b6ff7 11 //SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);
jekain314 0:432b860b6ff7 12 SDFileSystem sd(p11,p12,p13,p14,"sd");
jekain314 0:432b860b6ff7 13 DigitalIn sd_detect(p27);
jekain314 0:432b860b6ff7 14 DigitalOut ppsled(LED1); //blink an LED at the 1PPS
jekain314 0:432b860b6ff7 15 DigitalOut trig1led(LED2); //blink an LED at the camera trigger detection
jekain314 1:8e24e633f8d8 16 DigitalOut rxMsg(LED3);
jekain314 1:8e24e633f8d8 17 DigitalOut txMsg(LED4);
jekain314 1:8e24e633f8d8 18 //DigitalOut recordDataled(LED4); //set the led when the record is on
jekain314 1:8e24e633f8d8 19
jekain314 1:8e24e633f8d8 20 //hardware trigger mechanization for bulb shutter commands
jekain314 1:8e24e633f8d8 21 DigitalInOut fire(p29); //connected to the tip of 2.5mm connector (T2i)
jekain314 1:8e24e633f8d8 22 DigitalInOut pre_fire(p30); //connected to the mid-connection for 2.5mm connector (T2i)
jekain314 1:8e24e633f8d8 23
jekain314 1:8e24e633f8d8 24
sam_grove 27:94a6f0589993 25
sam_grove 27:94a6f0589993 26 /* Look at https://mbed.org/users/sam_grove/code/BufferedSerial/ */
sam_grove 27:94a6f0589993 27 /* I wrote this and it works quite well. IRQ driven UART with software buffers for TX and RX */
sam_grove 27:94a6f0589993 28 /* Same API as Serial with some enhancements */
sam_grove 27:94a6f0589993 29 /* This will reduce the loop around main time. Right now the problem is that the delay */
sam_grove 27:94a6f0589993 30 /* between events changes based on the UART speed */
sam_grove 27:94a6f0589993 31
jekain314 0:432b860b6ff7 32 //USB serial data stream back to the PC
jekain314 0:432b860b6ff7 33 Serial toPC(USBTX, USBRX); //connect the GPS TX, RX to p9 and p10
jekain314 0:432b860b6ff7 34
jekain314 0:432b860b6ff7 35 Timer timeFromStart;
jekain314 2:7039be3daf6e 36 Timer timeFromPosVelMessageReceipt;
jekain314 0:432b860b6ff7 37
sam_grove 27:94a6f0589993 38 /* should make all flags uint32_t for fastest access */
sam_grove 27:94a6f0589993 39 /* encapsulate all this into a structure */
sam_grove 27:94a6f0589993 40 /* or several based on what peripheral they belong to */
sam_grove 27:94a6f0589993 41
jekain314 0:432b860b6ff7 42 bool detectedGPS1PPS = false; //flag set in the ISR and reset after processing the 1PPS event
jekain314 0:432b860b6ff7 43 int PPSCounter = 0; //counts the 1PPS occurrences
jekain314 0:432b860b6ff7 44 int byteCounter = 0; //byte counter -- zeroed at 1PPS
jekain314 0:432b860b6ff7 45 unsigned short perSecMessageCounter=0; //counts the number of messages in a sec based on the header detection
jekain314 0:432b860b6ff7 46 bool messageDetected = false; //have detected a message header
jekain314 0:432b860b6ff7 47 unsigned long IMUbytesWritten = 0; //counts the IMU bytes written by the fwrite() to the SD card
jekain314 0:432b860b6ff7 48 int savedByteCounter = 0; //save ByteCounter at the 1PPS for display in main
jekain314 0:432b860b6ff7 49 int savedPerSecMessageCounter=0; //saved PerSecMsgCounter for display in main
jekain314 0:432b860b6ff7 50 int savedIMUClockCounter=0; //saved at the 1PPS for later diaplay from main
jekain314 0:432b860b6ff7 51 bool camera1EventDetected = false; //flag from ISR indicating a clock event occurred
jekain314 0:432b860b6ff7 52 double camera1Time; //GPS time of the camera event
jekain314 0:432b860b6ff7 53 int TotalBadCRCmatches = 0; //counter for the bad CRC matches for all GPS messages
sam_grove 27:94a6f0589993 54 volatile int PPSTimeOffset = 0;
jekain314 0:432b860b6ff7 55
sam_grove 27:94a6f0589993 56 /* Ive done some of this. Look at: */
sam_grove 27:94a6f0589993 57 /* https://mbed.org/users/sam_grove/code/OEM615/ */
sam_grove 27:94a6f0589993 58 /* https://mbed.org/users/sam_grove/code/ADIS16488/ */
sam_grove 27:94a6f0589993 59 /* a starting point that i'd done back during the hardware tester */
jekain314 0:432b860b6ff7 60
jekain314 0:432b860b6ff7 61 //////////////////////////////////////////////////////////////////////
jekain314 0:432b860b6ff7 62 // the below should become classes
jekain314 0:432b860b6ff7 63 //////////////////////////////////////////////////////////////////////
jekain314 0:432b860b6ff7 64 #include "OEM615.h" //OEM615 GPS activities
jekain314 0:432b860b6ff7 65 #include "ADIS16488.h" //ADIS16488 activities
jekain314 0:432b860b6ff7 66 #include "PCMessaging.h" //PC messaging activities
jekain314 0:432b860b6ff7 67
jekain314 16:2aea22130ba1 68
sam_grove 27:94a6f0589993 69 /* This should be called from a PC message handler when found. */
sam_grove 27:94a6f0589993 70
jekain314 16:2aea22130ba1 71 // stuff to send the SD file to the PC
jekain314 16:2aea22130ba1 72 #include "SDShell.h"
jekain314 16:2aea22130ba1 73 void transferFile()
jekain314 16:2aea22130ba1 74 {
jekain314 16:2aea22130ba1 75 SDShell emulate; // create the object
jekain314 16:2aea22130ba1 76 emulate.init(); // init the params inside
jekain314 16:2aea22130ba1 77 GPS_Reset = 0; // low power PCB mode
jekain314 16:2aea22130ba1 78 ADIS_RST = 0; // same here
jekain314 16:2aea22130ba1 79 wait(0.01f); // just make sure that the hardware has time to stop
jekain314 16:2aea22130ba1 80 fflush(stdout); // and clear any TX reminants
jekain314 16:2aea22130ba1 81 toPC.printf("Entering Shell Emulator...\n"); // just for fluf
jekain314 16:2aea22130ba1 82 wait(0.1f); // no reason for this either
jekain314 16:2aea22130ba1 83 emulate.shell(toPC, sd, "/sd"); // now the SDShell object will serve SD files via UNIX commands
jekain314 16:2aea22130ba1 84 }
jekain314 16:2aea22130ba1 85
sam_grove 27:94a6f0589993 86 /* Move into the GPS class */
sam_grove 27:94a6f0589993 87
jekain314 0:432b860b6ff7 88 //ISR for detection of the GPS 1PPS
jekain314 0:432b860b6ff7 89 void detect1PPSISR(void)
jekain314 0:432b860b6ff7 90 {
jekain314 0:432b860b6ff7 91 timeFromPPS.reset(); //reset the 1PPS timer upon 1PPS detection
jekain314 0:432b860b6ff7 92 //note -- the below accounts for time information becoming available AFTER the 1PPS event
jekain314 0:432b860b6ff7 93 PPSTimeOffset++; //counts 1PPS events between matching POS and VEL messages
jekain314 0:432b860b6ff7 94
jekain314 0:432b860b6ff7 95 //covers the case where the PPS ISR interrupts the IMU data ready ISR
jekain314 0:432b860b6ff7 96 if(IMUDataReady) IMUtimeFrom1PPS = 0;
jekain314 0:432b860b6ff7 97
jekain314 0:432b860b6ff7 98 savedByteCounter = byteCounter; //save byteCounter for display in main
jekain314 0:432b860b6ff7 99 savedPerSecMessageCounter = perSecMessageCounter; //save for display un main
jekain314 0:432b860b6ff7 100 byteCounter = 0; //countes bytes between 1PPS events
jekain314 0:432b860b6ff7 101 perSecMessageCounter = 0; //counts GPS messages between 1PPS events
jekain314 0:432b860b6ff7 102
jekain314 0:432b860b6ff7 103 GPS_COM1.rxBufferFlush(); //flush the GPS serial buffer
jekain314 0:432b860b6ff7 104
jekain314 0:432b860b6ff7 105 detectedGPS1PPS = true; //set false in the main when 1PPS actions are complete
jekain314 0:432b860b6ff7 106 PPSCounter++; //count number of 1PPS epoch
jekain314 0:432b860b6ff7 107
jekain314 0:432b860b6ff7 108 ppsled = !ppsled; //blink an LED at the 1PPS
jekain314 0:432b860b6ff7 109 };
jekain314 0:432b860b6ff7 110
jekain314 0:432b860b6ff7 111
sam_grove 27:94a6f0589993 112
sam_grove 27:94a6f0589993 113 /* Break up into GPS class and PC message class */
sam_grove 27:94a6f0589993 114 /* GPS boot up messages should be checking for a response rather than waiting a predefined time */
sam_grove 27:94a6f0589993 115 /* SD card stuff should also be a seperate class. Allow for dynamic directory and file awareness */
jekain314 0:432b860b6ff7 116 ///////////////////////////////////////////////////////
jekain314 0:432b860b6ff7 117 //set up the USB port and the GPS COM port
jekain314 0:432b860b6ff7 118 ///////////////////////////////////////////////////////
jekain314 0:432b860b6ff7 119 FILE *fpNav = NULL; //file pointer to the nav file on the SD card
jekain314 0:432b860b6ff7 120 void setupCOM(void)
jekain314 0:432b860b6ff7 121 {
jekain314 0:432b860b6ff7 122 //system starts with GPS in reset active
jekain314 0:432b860b6ff7 123 //dis-engage the reset to get the GPS started
jekain314 0:432b860b6ff7 124 GPS_Reset=1; wait_ms(1000);
jekain314 0:432b860b6ff7 125
jekain314 0:432b860b6ff7 126 //establish 1PPS ISR
jekain314 0:432b860b6ff7 127 PPSInt.rise(&detect1PPSISR);
jekain314 0:432b860b6ff7 128
jekain314 0:432b860b6ff7 129 //set the USB serial data rate -- rate must be matched at the PC end
jekain314 0:432b860b6ff7 130 //This the serial communication back to the the PC host
jekain314 0:432b860b6ff7 131 //Launch the C++ serial port read program there to catch the ASCII characters
jekain314 0:432b860b6ff7 132 //toPC.baud(9600); wait_ms(100);
jekain314 25:2287bd8c9877 133 toPC.baud(8*115200); wait_ms(100);
jekain314 0:432b860b6ff7 134 //toPC.baud(1*115200); wait_ms(100);
jekain314 6:71da5b99de97 135 //toPC.printf("\n\n released GPS from RESET and set to high baud rate \n\n");
jekain314 0:432b860b6ff7 136
jekain314 0:432b860b6ff7 137 //just wait to launch the GPS receiver
jekain314 3:e1a884e5325a 138 for (int i=0; i<5; i++) { toPC.printf("WMsg start: %3d \n", 4-i); wait(1); }
jekain314 0:432b860b6ff7 139
jekain314 0:432b860b6ff7 140 sd_detect.mode(PullUp);
jekain314 0:432b860b6ff7 141
jekain314 0:432b860b6ff7 142 if (sd_detect == 0)
jekain314 0:432b860b6ff7 143 {
jekain314 0:432b860b6ff7 144 mkdir("/sd/Data", 0777);
jekain314 0:432b860b6ff7 145 }
jekain314 0:432b860b6ff7 146 else
jekain314 0:432b860b6ff7 147 {
jekain314 6:71da5b99de97 148 toPC.printf("WMsg SD card not present \n");
jekain314 0:432b860b6ff7 149 }
jekain314 0:432b860b6ff7 150
jekain314 0:432b860b6ff7 151 //NOTE: we do not assume that the GPS receiver has been pre-set up for the WALDO_FCS functionality
jekain314 0:432b860b6ff7 152 //we alwsys start with a reset and reprogram the receiver with our data out products
jekain314 0:432b860b6ff7 153 // this prevents failure because of a blown NVRAM as occurred for the older camera systems
jekain314 0:432b860b6ff7 154
jekain314 0:432b860b6ff7 155 //this is the COM1 port from th GPS receiuver to the mbed
jekain314 0:432b860b6ff7 156 //it should be always started at 9600 baud because thats the default for the GPS receiver
jekain314 0:432b860b6ff7 157 GPS_COM1.baud(9600); wait_ms(100);
jekain314 0:432b860b6ff7 158
jekain314 0:432b860b6ff7 159 // this ASCII command sets up the serial data from the GPS receiver on its COM1
jekain314 0:432b860b6ff7 160 char ch7[] = "serialconfig COM1 9600 n 8 1 n off";
jekain314 0:432b860b6ff7 161 // this is a software reset and has the same effect as a hardware reset (why do it?)
jekain314 0:432b860b6ff7 162 //char ch0[] = "RESET";
jekain314 0:432b860b6ff7 163 //this command stops all communication from the GPS receiver on COM1
jekain314 0:432b860b6ff7 164 //logs should still be presented on USB port so the Novatel CDU application can be used on the PC in parallel
jekain314 0:432b860b6ff7 165 char ch1[] = "unlogall COM1";
jekain314 0:432b860b6ff7 166 //set the final baud rate that we will use from here
jekain314 0:432b860b6ff7 167 //allowable baud rate values: 9600 115200 230400 460800 921600
jekain314 0:432b860b6ff7 168 //char ch2[] = "serialconfig COM1 921600 n 8 1 n off";
jekain314 0:432b860b6ff7 169 char ch2[] = "serialconfig COM1 115200 n 8 1 n off";
jekain314 0:432b860b6ff7 170
jekain314 0:432b860b6ff7 171 //the below commands request the POS, VEL, RANGE, and TIME messages
jekain314 0:432b860b6ff7 172 char ch3[] = "log COM1 BESTPOSB ONTIME 1"; //messageID = 42
jekain314 0:432b860b6ff7 173 char ch4[] = "log COM1 BESTVelB ONTIME 1"; //messageID = 99
jekain314 0:432b860b6ff7 174 char ch5[] = "log COM1 RANGEB ONTIME 1"; //messageID = 43
jekain314 0:432b860b6ff7 175 //char ch6[] = "log COM1 TIMEB ONTIME 1"; //messageID = 101
jekain314 0:432b860b6ff7 176
jekain314 0:432b860b6ff7 177 //set up VARF to be 100Hz with 1X10^4 * 10^-8 = 10^-4 sec (10usec) pulse width
jekain314 0:432b860b6ff7 178 //in fact, we do not use this output but it is available.
jekain314 0:432b860b6ff7 179 //originally planned to use this to command the IMU data
jekain314 0:432b860b6ff7 180 //char ch8[] = "FREQUENCYOUT enable 10000 1000000";
jekain314 0:432b860b6ff7 181
jekain314 15:f3b92958cf5a 182 //toPC.printf("WMsg set serial config \n");
jekain314 0:432b860b6ff7 183 sendASCII(ch7, sizeof(ch7)); wait_ms(500);
jekain314 0:432b860b6ff7 184 //sendASCII(ch0, sizeof(ch0));
jekain314 15:f3b92958cf5a 185 //toPC.printf("WMsg unlog all messages \n");
jekain314 0:432b860b6ff7 186 sendASCII(ch1, sizeof(ch1)); wait_ms(500);
jekain314 15:f3b92958cf5a 187 //toPC.printf("WMsg log BESTPOSB on COM1 \n");
jekain314 0:432b860b6ff7 188 sendASCII(ch3, sizeof(ch3)); wait_ms(500);
jekain314 15:f3b92958cf5a 189 //toPC.printf("WMsg log BESTVELB on COM1\n");
jekain314 0:432b860b6ff7 190 sendASCII(ch4, sizeof(ch4)); wait_ms(500);
jekain314 15:f3b92958cf5a 191 //toPC.printf("WMsg log RANGEB on COM1\n");
jekain314 0:432b860b6ff7 192 sendASCII(ch5, sizeof(ch5)); wait_ms(500);
jekain314 0:432b860b6ff7 193
jekain314 0:432b860b6ff7 194 //toPC.printf("log TIMEB om COM1 \n");
jekain314 0:432b860b6ff7 195 //sendASCII(ch6, sizeof(ch6)); wait_ms(100);
jekain314 0:432b860b6ff7 196
jekain314 0:432b860b6ff7 197 //toPC.printf("Set up th VARF signal \n");
jekain314 0:432b860b6ff7 198 //sendASCII(ch8, sizeof(ch8)); wait_ms(500);
jekain314 0:432b860b6ff7 199
jekain314 0:432b860b6ff7 200 //set GPS output COM1 to the final high rate
jekain314 15:f3b92958cf5a 201 //toPC.printf("WMsg set the COM ports to high rate\n");
jekain314 0:432b860b6ff7 202 sendASCII(ch2, sizeof(ch2)); wait_ms(500);
jekain314 0:432b860b6ff7 203
jekain314 0:432b860b6ff7 204 //set the mbed COM port to match the GPS transmit rate
jekain314 0:432b860b6ff7 205 //the below baud rate must match the COM1 rate coming from the GPS receiver
jekain314 0:432b860b6ff7 206 GPS_COM1.baud(115200); wait_ms(500); //without this wait -- the baud rate is not detected when using MODSERIAL
jekain314 0:432b860b6ff7 207 //GPS_COM1.baud(921600); wait_ms(500); //without this wait -- the baud rate is not detected when using MODSERIAL
jekain314 0:432b860b6ff7 208 };
jekain314 0:432b860b6ff7 209
jekain314 0:432b860b6ff7 210
jekain314 0:432b860b6ff7 211 /////////////////////////////////////////////////////////////////////
jekain314 0:432b860b6ff7 212 // mbed main to support the Waldo_FCS
jekain314 0:432b860b6ff7 213 /////////////////////////////////////////////////////////////////////
jekain314 14:009d4671f0e3 214 int main()
jekain314 14:009d4671f0e3 215 {
jekain314 0:432b860b6ff7 216
sam_grove 27:94a6f0589993 217 /* IMO this should all be ASCII (GPS data). Too much processing on the mbed side for messages that arent used */
sam_grove 27:94a6f0589993 218 /* the parsing and error checking + printf with floating point are EXPENSIVE CPU instructions */
sam_grove 27:94a6f0589993 219
jekain314 0:432b860b6ff7 220 //these are structures for the to GPS messages that must be parsed
jekain314 0:432b860b6ff7 221 MESSAGEHEADER msgHdr;
jekain314 0:432b860b6ff7 222 OEM615BESTPOS posMsg; //BESTPOS structure in OEMV615.h that has matching time to a BESTVEL message
jekain314 0:432b860b6ff7 223 OEM615BESTPOS curPos; //BESTPOS structure in OEMV615.h
jekain314 0:432b860b6ff7 224 OEM615BESTVEL velMsg; //BESTVEL structure in OEMV615.h that has matching time to a BESTPOS message
jekain314 0:432b860b6ff7 225 OEM615BESTVEL curVel; //BESTVEL structure in OEMV615.h
jekain314 0:432b860b6ff7 226
jekain314 1:8e24e633f8d8 227
sam_grove 27:94a6f0589993 228 /* Self explanitory */
sam_grove 27:94a6f0589993 229
jekain314 1:8e24e633f8d8 230 fire.output(); //set the fire pin as outoput
jekain314 1:8e24e633f8d8 231 pre_fire.output(); //set the pre-fire pin as output
sam_grove 27:94a6f0589993 232
sam_grove 27:94a6f0589993 233 /* both should be open drain */
sam_grove 27:94a6f0589993 234
jekain314 1:8e24e633f8d8 235 //fire.mode(OpenDrain);
jekain314 1:8e24e633f8d8 236
sam_grove 27:94a6f0589993 237 /* not necessary if open drain outputs */
sam_grove 27:94a6f0589993 238
jekain314 1:8e24e633f8d8 239 //set up for the first trigger
jekain314 1:8e24e633f8d8 240 fire = 1;
jekain314 1:8e24e633f8d8 241 pre_fire = 1;
sam_grove 27:94a6f0589993 242
sam_grove 27:94a6f0589993 243 /* for lower power modes of operation all this should be different classes and called when the PC requests it */
sam_grove 27:94a6f0589993 244 /* always on and idle is a waste of battery power. Could probabally add 30-50% life by leaving things off until needed */
sam_grove 27:94a6f0589993 245
jekain314 0:432b860b6ff7 246 //set up the GPS and mbed COM ports
jekain314 0:432b860b6ff7 247 setupCOM();
jekain314 0:432b860b6ff7 248
sam_grove 27:94a6f0589993 249 /* same as above */
sam_grove 27:94a6f0589993 250
jekain314 0:432b860b6ff7 251 //set up the ADIS16488
jekain314 0:432b860b6ff7 252 setupADIS();
jekain314 0:432b860b6ff7 253
jekain314 25:2287bd8c9877 254 setUpMessages(); //set up the expected text message commands from the PC
jekain314 0:432b860b6ff7 255
sam_grove 27:94a6f0589993 256 /* same as above */
sam_grove 27:94a6f0589993 257
jekain314 25:2287bd8c9877 258 //initiate the interrupt to catch the GPS receiver serial bytes as they are presented
jekain314 0:432b860b6ff7 259 GPS_COM1.attach(&readSerialByte, MODSERIAL::RxIrq);
jekain314 0:432b860b6ff7 260
sam_grove 27:94a6f0589993 261 /* same as above */
sam_grove 27:94a6f0589993 262
jekain314 0:432b860b6ff7 263 timeFromPPS.start(); //start the time for measuring time from 1PPS events
jekain314 0:432b860b6ff7 264 timeFromStart.start();
jekain314 0:432b860b6ff7 265
jekain314 3:e1a884e5325a 266 //toPC.printf("\n\n top of the main loop \n\n");
jekain314 0:432b860b6ff7 267
jekain314 0:432b860b6ff7 268 int totalBytesWritten = 0;
jekain314 0:432b860b6ff7 269
jekain314 25:2287bd8c9877 270 /*establish the initial value for the CRC recursion after the header signature bytes
jekain314 0:432b860b6ff7 271 unsigned long CRC = 0;
jekain314 0:432b860b6ff7 272 CRC32Value(CRC, 0xAA);
jekain314 0:432b860b6ff7 273 CRC32Value(CRC, 0x44);
jekain314 0:432b860b6ff7 274 CRC32Value(CRC, 0x12);
jekain314 0:432b860b6ff7 275 CRC32Value(CRC, 0x1C);
jekain314 0:432b860b6ff7 276 //this results in a value of: 0x39b0f0e1
jekain314 0:432b860b6ff7 277 toPC.printf(" CRC after AA44121C header: %08x \n", CRC);
jekain314 0:432b860b6ff7 278 wait(20);
jekain314 0:432b860b6ff7 279 */
jekain314 25:2287bd8c9877 280
jekain314 25:2287bd8c9877 281 //at the start we do not record the data
jekain314 24:353322495742 282 recordData = false;
jekain314 24:353322495742 283 sendRecData = false;
jekain314 0:432b860b6ff7 284
sam_grove 27:94a6f0589993 285 /* I'd get these off the stack and into a global structure */
sam_grove 27:94a6f0589993 286 /* If you did have a stack error condition (buffer overwrite) all this could be clobbered */
sam_grove 27:94a6f0589993 287
jekain314 25:2287bd8c9877 288 unsigned long cyclesPerSec = 0; //main while() loop cycles per GPS sec
jekain314 0:432b860b6ff7 289 bool GPSdataWritten = false;
jekain314 4:dda2ab5cc643 290 bool finishTrigger = false;
jekain314 4:dda2ab5cc643 291 Timer triggerInterval;
jekain314 0:432b860b6ff7 292
jekain314 1:8e24e633f8d8 293 //while(PPSCounter < 300)
jekain314 17:71900da6ced6 294
sam_grove 27:94a6f0589993 295 /* New command logic will keep this loop from ever exiting */
sam_grove 27:94a6f0589993 296
jekain314 17:71900da6ced6 297 bool newMission = true;
jekain314 17:71900da6ced6 298
jekain314 0:432b860b6ff7 299 ///////////////////////////////////////////////////////////////////////////
jekain314 17:71900da6ced6 300 // top of the mission while loop
jekain314 0:432b860b6ff7 301 ///////////////////////////////////////////////////////////////////////////
jekain314 17:71900da6ced6 302 while(newMission)
jekain314 0:432b860b6ff7 303 {
jekain314 14:009d4671f0e3 304
sam_grove 27:94a6f0589993 305 /* right idea. Build messages and parse */
sam_grove 27:94a6f0589993 306
jekain314 0:432b860b6ff7 307 //read the USB serial data from the PC to check for commands
jekain314 0:432b860b6ff7 308 readFromPC();
jekain314 2:7039be3daf6e 309
sam_grove 27:94a6f0589993 310 /* file should only be open when writing. If you crash and the file is open */
sam_grove 27:94a6f0589993 311 /* the data will be lost. Should be in a class that takes structures or strings to write to the file */
sam_grove 27:94a6f0589993 312
jekain314 2:7039be3daf6e 313 //this will close the fpNav file on the SD card if the file is open
jekain314 2:7039be3daf6e 314 //and the elapsed time from PosVel messages is > 60 secs
jekain314 2:7039be3daf6e 315 //this prevents loosing the fpNav file if the PC goes down
jekain314 25:2287bd8c9877 316 // !!!! timeFromPosVelMessageReceipt !!! was never started
jekain314 14:009d4671f0e3 317 if (fpNav && (timeFromPosVelMessageReceipt.read() > 10) )
jekain314 2:7039be3daf6e 318 {
jekain314 2:7039be3daf6e 319 sendRecData = true;
jekain314 2:7039be3daf6e 320 recordData = false;
jekain314 2:7039be3daf6e 321 }
jekain314 0:432b860b6ff7 322
sam_grove 27:94a6f0589993 323 /* Should be done when reading and building the message */
sam_grove 27:94a6f0589993 324
jekain314 25:2287bd8c9877 325 // for any received PC message, take the appropriate action
jekain314 0:432b860b6ff7 326 processPCmessages(fpNav, posMsg, velMsg);
jekain314 0:432b860b6ff7 327
sam_grove 27:94a6f0589993 328 /* file should always be closed until needed */
sam_grove 27:94a6f0589993 329
jekain314 25:2287bd8c9877 330 //if we receive a "GETFILE" message from the PC -- close the fpNavFile and break from the while() loop
jekain314 25:2287bd8c9877 331 if (get_file_msg)
jekain314 20:3f04a0bde484 332 {
jekain314 20:3f04a0bde484 333 if (fpNav != NULL) fclose(fpNav);
jekain314 20:3f04a0bde484 334 break; //terminate the while loop when we receive this message from the PC
jekain314 20:3f04a0bde484 335 }
jekain314 14:009d4671f0e3 336
sam_grove 27:94a6f0589993 337 /* wait looks unnecessary (and bad during runtime). During all tests with the camera the pre-fire is */
sam_grove 27:94a6f0589993 338 /* 250mS before the fire. Not sure if this changes the time til shutter open */
sam_grove 27:94a6f0589993 339 /* Look at the Timeout class */
sam_grove 27:94a6f0589993 340
jekain314 5:2ce1be9d4bef 341 if(fireTrigger) //comes from a PC request message
jekain314 1:8e24e633f8d8 342 {
jekain314 26:c2208b0ff78b 343 unsigned long triggerTime = GPSTimemsecs + PPSTimeOffset*1000.0 + timeFromPPS.read_us()/1000.0;
jekain314 14:009d4671f0e3 344 toPC.printf("WMsg TRIGGERTIME %10d \n", triggerTime);
jekain314 1:8e24e633f8d8 345 //pre-fire the trigger using the mid-body 2.5mm connection (T2i)
jekain314 1:8e24e633f8d8 346 pre_fire = 0; //pin30 (midbody of connector) set to zero
jekain314 25:2287bd8c9877 347 wait(.01f); // not sure what this does
jekain314 1:8e24e633f8d8 348 fire = 0; //fire the trigger using the tip connection
jekain314 25:2287bd8c9877 349 fireTrigger = false; //finished the setup -- but wait to do the actual fire
jekain314 25:2287bd8c9877 350 finishTrigger = true; //set to false after firing the trigger
jekain314 4:dda2ab5cc643 351 triggerInterval.start();
jekain314 4:dda2ab5cc643 352 }
jekain314 4:dda2ab5cc643 353
sam_grove 27:94a6f0589993 354 /* see above */
sam_grove 27:94a6f0589993 355
jekain314 6:71da5b99de97 356 //the trigger requires a pulse -- the above portion lowers the signal and the below raises it
jekain314 6:71da5b99de97 357 //this has been tested at 50 msecs and it will not fire at that pulse duration
jekain314 4:dda2ab5cc643 358 if(finishTrigger && triggerInterval.read_ms() > 100)
jekain314 4:dda2ab5cc643 359 {
jekain314 1:8e24e633f8d8 360 fire = 1;
jekain314 1:8e24e633f8d8 361 pre_fire = 1;
jekain314 4:dda2ab5cc643 362 triggerInterval.reset();
jekain314 25:2287bd8c9877 363 finishTrigger = false; //completes the trigger firing pulse definition
jekain314 1:8e24e633f8d8 364 }
jekain314 1:8e24e633f8d8 365
sam_grove 27:94a6f0589993 366 /* who clears this, PPS? */
sam_grove 27:94a6f0589993 367
jekain314 0:432b860b6ff7 368 cyclesPerSec++;
jekain314 0:432b860b6ff7 369
jekain314 0:432b860b6ff7 370 ////////////////////////////////////////////////////////////////////////////
jekain314 0:432b860b6ff7 371 //below is where we process the complete stored GPS message for the second
jekain314 0:432b860b6ff7 372 //The !IMUDataReady test prevents the IMU and GPS data from being written
jekain314 0:432b860b6ff7 373 //to disk on the same pass through this loop
jekain314 25:2287bd8c9877 374 /////////////////////////////////////////////////////////////////////////////
jekain314 0:432b860b6ff7 375
jekain314 25:2287bd8c9877 376 //there are three potential messages and all messages have a header
jekain314 0:432b860b6ff7 377 if (completeMessageAvailable && !IMUDataReady)
jekain314 0:432b860b6ff7 378 {
jekain314 26:c2208b0ff78b 379 //must unpack header first to get the message length
jekain314 0:432b860b6ff7 380 msgHdr = *((MESSAGEHEADER*)&msgBuffer[messageLocation[savedMessageCounter-1]]);
jekain314 0:432b860b6ff7 381
jekain314 0:432b860b6ff7 382 //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 383 //GPSTimemsecs increments by 1 for each new GPS measurement -- note that the below computations are actually
jekain314 0:432b860b6ff7 384 //done at the receipt of each GPS message. This is OK because each message has the same time in its header.
jekain314 0:432b860b6ff7 385 //Thus GPSTimemsecs increments by 1 here while GPSTimemsecs effectively decrements by 1.
jekain314 0:432b860b6ff7 386 //This handles IMU time tagging between the 1PPS event and the first receipt of a new GPS time.
jekain314 0:432b860b6ff7 387
jekain314 25:2287bd8c9877 388 //message header length is 28 -- right side is pointer to the receiver-computed CRC for this record
jekain314 26:c2208b0ff78b 389 //CRC is computed while reading in the GPS bytes
jekain314 0:432b860b6ff7 390 unsigned long msgCRC = *((unsigned long*)&msgBuffer[messageLocation[savedMessageCounter-1] + 28 + msgHdr.messageLength]);
jekain314 0:432b860b6ff7 391
jekain314 0:432b860b6ff7 392 //toPC.printf("tmeFrom1PPS= %5d ID= %3d Ln = %3d computedCRC= %08x msgCRC= %08x msgCntr = %3d CRCerr=%4d\n",
jekain314 0:432b860b6ff7 393 // timeFromPPS.read_us(), msgHdr.messageID, msgHdr.messageLength, computedCRC, msgCRC, savedMessageCounter, TotalBadCRCmatches);
jekain314 0:432b860b6ff7 394
sam_grove 27:94a6f0589993 395 /* is this really necessary? Seems like keeping it in ascii and sending in ascii would make more sense */
sam_grove 27:94a6f0589993 396
jekain314 26:c2208b0ff78b 397 if ( msgCRC == computedCRC) //computedCRC is performed as we read each bvyte
jekain314 25:2287bd8c9877 398 {
jekain314 25:2287bd8c9877 399 //if the CRC check is valid -- then get the time from the header
jekain314 25:2287bd8c9877 400 //we get three messages each sec -- does it matter that we do this three times?
jekain314 25:2287bd8c9877 401 GPSTimemsecs = msgHdr.GPSTime_msecs; //time in GPS message header
jekain314 25:2287bd8c9877 402
jekain314 25:2287bd8c9877 403 //the PPSTimeOffset accounts for the occurrence where we do not get any GPS messages over a sec -- but PPS is still operative
jekain314 25:2287bd8c9877 404 PPSTimeOffset = 0; //incremented by 1 in the PPS ISR
jekain314 26:c2208b0ff78b 405
jekain314 26:c2208b0ff78b 406 //We need the pos and vel messages to pass back data to the PC -- error cases that can occur:
jekain314 26:c2208b0ff78b 407 // (1) missed 42 (POS) -- use last good pos and extrapolate using last good vel
jekain314 26:c2208b0ff78b 408 // (2) missed 99 (VEL) -- use the last good vel since likely not changed much
jekain314 26:c2208b0ff78b 409 // (3) missed both 42 and 99 -- must use last good position and extrapolae using last good velocity
jekain314 26:c2208b0ff78b 410 // GPS time used to time-tag the IMU data and to do the extrapolttion from last good position to send to PC
jekain314 26:c2208b0ff78b 411 // in the position extrapolation, we will use the GPS time that is kept in the header of the POS msg (42)
jekain314 26:c2208b0ff78b 412 // see the procedure: sendPosVelMessageToPC()
jekain314 0:432b860b6ff7 413
jekain314 26:c2208b0ff78b 414 if (msgHdr.messageID == 42) //this is the position message (lat, lon, alt)
jekain314 0:432b860b6ff7 415 {
jekain314 26:c2208b0ff78b 416 //map the starting record byte index to the record structure
jekain314 26:c2208b0ff78b 417 curPos = *((OEM615BESTPOS*)&msgBuffer[messageLocation[savedMessageCounter-1]]);
jekain314 26:c2208b0ff78b 418 posMsg = curPos;
jekain314 26:c2208b0ff78b 419
jekain314 26:c2208b0ff78b 420 if (streamPos) // we no longer use this functionality
jekain314 26:c2208b0ff78b 421 {
jekain314 26:c2208b0ff78b 422 toPC.printf("BESTPOS %5d %1d %8.6lf %9.6lf %5.3lf %d %d\n",
jekain314 26:c2208b0ff78b 423 curPos.msgHeader.GPSTime_msecs, curPos.solStatus,
jekain314 26:c2208b0ff78b 424 curPos.latitude, curPos.longitude, curPos.height,
jekain314 26:c2208b0ff78b 425 curPos.numSV, curPos.numSolSV);
jekain314 26:c2208b0ff78b 426 }
jekain314 26:c2208b0ff78b 427
jekain314 26:c2208b0ff78b 428 }
jekain314 26:c2208b0ff78b 429 else if (msgHdr.messageID == 99) //this is the velocity message
jekain314 26:c2208b0ff78b 430 {
jekain314 26:c2208b0ff78b 431 curVel = *((OEM615BESTVEL*)&msgBuffer[ messageLocation[savedMessageCounter-1] ]);
jekain314 26:c2208b0ff78b 432 //toPC.printf("BESTVEL vel: horizontalSpeed= %5.3f heading=%5.1f verticalSpeed=%4.2f \n",
jekain314 26:c2208b0ff78b 433 // curVel.horizontalSpeed, curVel.heading, curVel.verticalSpeed );
jekain314 26:c2208b0ff78b 434 velMsg = curVel;
jekain314 0:432b860b6ff7 435 }
jekain314 26:c2208b0ff78b 436
jekain314 26:c2208b0ff78b 437 //below is set to true when we detect that we have received a complete GPS message
jekain314 26:c2208b0ff78b 438 completeMessageAvailable = false;
jekain314 26:c2208b0ff78b 439 }
jekain314 26:c2208b0ff78b 440 else // do this if we do not pass the CRC
jekain314 0:432b860b6ff7 441 {
jekain314 26:c2208b0ff78b 442 toPC.printf("WMsg bad CRC match for messageID %3d total CRC errors = %4d \n",
jekain314 26:c2208b0ff78b 443 msgHdr.messageLength, TotalBadCRCmatches++);
jekain314 0:432b860b6ff7 444 }
jekain314 0:432b860b6ff7 445 }
jekain314 26:c2208b0ff78b 446
jekain314 0:432b860b6ff7 447
sam_grove 27:94a6f0589993 448 /* should move into a file management class for protection and error checking */
sam_grove 27:94a6f0589993 449
jekain314 1:8e24e633f8d8 450 //write the GPS data to the SD card
jekain314 25:2287bd8c9877 451 //NOTE: this is valid only for a once-per-sec GPS message
jekain314 25:2287bd8c9877 452 //for this case, all messages come out well prior to 0.5 secs after the 1PPS
jekain314 0:432b860b6ff7 453 if (!IMUDataReady && !GPSdataWritten && timeFromPPS.read_us() > 500000 && recordData && (fpNav != NULL))
jekain314 0:432b860b6ff7 454 {
jekain314 0:432b860b6ff7 455 totalBytesWritten += fwrite(&msgBuffer, 1, byteCounter, fpNav);
jekain314 0:432b860b6ff7 456 GPSdataWritten = true;
jekain314 0:432b860b6ff7 457 }
jekain314 0:432b860b6ff7 458
sam_grove 27:94a6f0589993 459 /* would have 2 arrays of structures and a pointer in the class than changes between the buffers. */
sam_grove 27:94a6f0589993 460 /* This way there is 1 write and management in 1 place */
sam_grove 27:94a6f0589993 461
jekain314 0:432b860b6ff7 462 //the IMU data record is read from the SPI in the ISR and the IMUDataReady is set true
jekain314 0:432b860b6ff7 463 //we write the IMU data here
jekain314 0:432b860b6ff7 464 if (IMUDataReady) //IMUDataReady is true if we have a recent IMU data record
jekain314 0:432b860b6ff7 465 {
jekain314 0:432b860b6ff7 466 //write the IMU data
jekain314 0:432b860b6ff7 467 if ( recordData && (fpNav != NULL) )
jekain314 0:432b860b6ff7 468 {
jekain314 1:8e24e633f8d8 469 //delTimeOfWrite = timeFromStart.read_us();
jekain314 0:432b860b6ff7 470
jekain314 20:3f04a0bde484 471 if (fillingPingWritingPong) totalBytesWritten += fwrite(&imuPong, 1, IMUrecArraySize*sizeof(IMUREC), fpNav);
jekain314 20:3f04a0bde484 472 else totalBytesWritten += fwrite(&imuPing, 1, IMUrecArraySize*sizeof(IMUREC), fpNav);
jekain314 0:432b860b6ff7 473
jekain314 1:8e24e633f8d8 474 //delTimeOfWrite = (unsigned long)((unsigned long)timeFromStart.read_us() - delTimeOfWrite);
jekain314 1:8e24e633f8d8 475 //if (delTimeOfWrite > maxWriteTime) maxWriteTime = delTimeOfWrite;
jekain314 0:432b860b6ff7 476 }
jekain314 0:432b860b6ff7 477 IMURecordCounter+=IMUrecArraySize;
jekain314 0:432b860b6ff7 478 IMUDataReady = false;
jekain314 0:432b860b6ff7 479 }
jekain314 0:432b860b6ff7 480
sam_grove 27:94a6f0589993 481 /* should remove floating point if possible and send as milli-sec integers */
sam_grove 27:94a6f0589993 482
jekain314 25:2287bd8c9877 483 //this is a command from the PC to fire a trigger
jekain314 0:432b860b6ff7 484 if (camera1EventDetected) //we have detected a camera trigger event
jekain314 0:432b860b6ff7 485 {
jekain314 0:432b860b6ff7 486 toPC.printf("WMsg TRIGGERTIME %5.3lf\n", camera1Time);
jekain314 0:432b860b6ff7 487 camera1EventDetected = false;
jekain314 0:432b860b6ff7 488 }
jekain314 0:432b860b6ff7 489
sam_grove 27:94a6f0589993 490 /* managed in GPS class */
sam_grove 27:94a6f0589993 491
jekain314 0:432b860b6ff7 492 if (detectedGPS1PPS) //true if we are exactly at a 1PPS event detection
jekain314 0:432b860b6ff7 493 {
jekain314 1:8e24e633f8d8 494 //toPC.printf("PPS=%4d stat=%1d bytes=%3d GPSMsgs=%2d #write=%8d cycles=%6d\n",
jekain314 1:8e24e633f8d8 495 // PPSCounter, posMsg.solStatus, savedByteCounter, savedPerSecMessageCounter,
jekain314 1:8e24e633f8d8 496 // totalBytesWritten, cyclesPerSec );
jekain314 0:432b860b6ff7 497
jekain314 0:432b860b6ff7 498 cyclesPerSec = 0;
jekain314 20:3f04a0bde484 499 //totalBytesWritten = 0;
jekain314 0:432b860b6ff7 500 GPSdataWritten = false;
jekain314 22:1cbdbc856660 501 //toPC.printf(" bytesWritten = %5d \n", totalBytesWritten);
jekain314 0:432b860b6ff7 502
jekain314 0:432b860b6ff7 503 IMURecordCounter = 0;
jekain314 0:432b860b6ff7 504 detectedGPS1PPS = false;
jekain314 22:1cbdbc856660 505
jekain314 25:2287bd8c9877 506 rxMsg = !rxMsg; //flash the lights to make sure the mbed loop is operative
jekain314 22:1cbdbc856660 507 txMsg = !txMsg;
jekain314 0:432b860b6ff7 508 }
jekain314 25:2287bd8c9877 509 ///////////////////////////////////////////
jekain314 25:2287bd8c9877 510 } //end of the major while() loop
jekain314 25:2287bd8c9877 511 ///////////////////////////////////////////
jekain314 0:432b860b6ff7 512
jekain314 17:71900da6ced6 513
sam_grove 27:94a6f0589993 514 /* should already be closed by file management class */
sam_grove 27:94a6f0589993 515
jekain314 20:3f04a0bde484 516 if (fpNav != NULL)
jekain314 20:3f04a0bde484 517 {
jekain314 20:3f04a0bde484 518 fclose(fpNav); //insurance
jekain314 22:1cbdbc856660 519 toPC.printf("WMsg closeFPNav \n");
jekain314 20:3f04a0bde484 520 }
jekain314 20:3f04a0bde484 521
sam_grove 27:94a6f0589993 522 /* accessable by SDShell class */
sam_grove 27:94a6f0589993 523 /* see: https://mbed.org/users/sam_grove/code/SDShell/ */
sam_grove 27:94a6f0589993 524
jekain314 22:1cbdbc856660 525 toPC.printf("WMsg totalBytesWritten %5d \n", totalBytesWritten);
jekain314 20:3f04a0bde484 526 wait_ms(100);
jekain314 20:3f04a0bde484 527
sam_grove 27:94a6f0589993 528 /* just a state of the communication management class */
sam_grove 27:94a6f0589993 529
jekain314 26:c2208b0ff78b 530 //send the nav file to the PC
jekain314 17:71900da6ced6 531 transferFile();
jekain314 19:26c5298a7138 532 //rxMsg = txMsg = 0; // just indicate that we're in here
jekain314 17:71900da6ced6 533 // to exit this function the HOST (ie: computer or PC app) must send "exit" otherwise the mbed will act
jekain314 17:71900da6ced6 534 // like a terminal and serve SD file data forever
jekain314 16:2aea22130ba1 535
sam_grove 27:94a6f0589993 536 /* no longer needed */
sam_grove 27:94a6f0589993 537
jekain314 22:1cbdbc856660 538 toPC.printf("WMsg normalTermination \n");
jekain314 20:3f04a0bde484 539 wait_ms(100);
jekain314 17:71900da6ced6 540
jekain314 17:71900da6ced6 541 NVIC_SystemReset();
jekain314 14:009d4671f0e3 542
jekain314 0:432b860b6ff7 543 }