added spectrometer timeout
Dependencies: SDFileSystem mbed
Fork of All_Combined_Real2 by
main.cpp@8:6a694f87fb13, 2016-04-04 (annotated)
- Committer:
- colingm
- Date:
- Mon Apr 04 18:30:02 2016 +0000
- Revision:
- 8:6a694f87fb13
- Parent:
- 7:a125a1fa4694
- Child:
- 9:924d80730ac5
Adding sending of GPS Data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jphbergeson | 0:26713d1db198 | 1 | #include "mbed.h" |
jphbergeson | 0:26713d1db198 | 2 | #include "nmea.h" |
jphbergeson | 0:26713d1db198 | 3 | #include "SDFileSystem.h" |
jphbergeson | 1:5fa445bd14a6 | 4 | #include "SPI.h" |
jphbergeson | 3:c611fff05072 | 5 | #include "temp_sensor.h" |
colingm | 5:6532c5a5f576 | 6 | #include "XBeeLib.h" |
jphbergeson | 0:26713d1db198 | 7 | |
jphbergeson | 1:5fa445bd14a6 | 8 | #define NUM_PIXELS 2048 |
jphbergeson | 0:26713d1db198 | 9 | #define BUF_SIZE 1024 |
jphbergeson | 6:059312376781 | 10 | #define SPECTROMETER_TIMEOUT 1000 |
jphbergeson | 2:146625d6992d | 11 | |
jphbergeson | 2:146625d6992d | 12 | // TODO make a new file (name=timestamp) each time we start recording data |
jphbergeson | 1:5fa445bd14a6 | 13 | #define OUTPUT_FILE "/sd/data.csv" |
jphbergeson | 0:26713d1db198 | 14 | |
jphbergeson | 1:5fa445bd14a6 | 15 | // pinout for spectrometer |
jphbergeson | 1:5fa445bd14a6 | 16 | |
jphbergeson | 1:5fa445bd14a6 | 17 | // Mosi (#8) -> PTD2 |
jphbergeson | 1:5fa445bd14a6 | 18 | // Miso (#7) -> PTD3 |
jphbergeson | 1:5fa445bd14a6 | 19 | // Sclk (#9) -> PTD1 |
jphbergeson | 1:5fa445bd14a6 | 20 | |
jphbergeson | 1:5fa445bd14a6 | 21 | // Pixel_rdy (#6) -> PTA1 |
jphbergeson | 1:5fa445bd14a6 | 22 | // Fifo_cs (#3) -> PTB23 |
jphbergeson | 1:5fa445bd14a6 | 23 | // Trigger (#13) -> PTA2 |
jphbergeson | 2:146625d6992d | 24 | |
jphbergeson | 4:70411c8dadcc | 25 | // spi_cs (#10) -> PTC2 |
jphbergeson | 4:70411c8dadcc | 26 | |
jphbergeson | 1:5fa445bd14a6 | 27 | SPI spi(PTD2, PTD3, PTD1); // mosi, miso, sclk |
jphbergeson | 1:5fa445bd14a6 | 28 | |
jphbergeson | 4:70411c8dadcc | 29 | DigitalOut spi_cs(PTC2); |
jphbergeson | 4:70411c8dadcc | 30 | |
jphbergeson | 1:5fa445bd14a6 | 31 | DigitalIn pixel_rdy(PTA1); |
jphbergeson | 1:5fa445bd14a6 | 32 | DigitalOut fifo_cs(PTB23); |
jphbergeson | 1:5fa445bd14a6 | 33 | DigitalOut trigger(PTA2); |
jphbergeson | 1:5fa445bd14a6 | 34 | |
jphbergeson | 1:5fa445bd14a6 | 35 | // Blinking red LED |
jphbergeson | 0:26713d1db198 | 36 | DigitalOut led(LED_RED); |
jphbergeson | 0:26713d1db198 | 37 | |
jphbergeson | 1:5fa445bd14a6 | 38 | // GPS connection (through arduino) |
jphbergeson | 0:26713d1db198 | 39 | Serial duino(PTC4, PTC3); |
jphbergeson | 1:5fa445bd14a6 | 40 | |
jphbergeson | 1:5fa445bd14a6 | 41 | // USB serial to PC |
jphbergeson | 0:26713d1db198 | 42 | Serial pc(USBTX, USBRX); |
jphbergeson | 0:26713d1db198 | 43 | |
jphbergeson | 0:26713d1db198 | 44 | /************************************************** |
jphbergeson | 0:26713d1db198 | 45 | ** SD FILE SYSTEM ** |
jphbergeson | 0:26713d1db198 | 46 | **************************************************/ |
jphbergeson | 0:26713d1db198 | 47 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
jphbergeson | 0:26713d1db198 | 48 | FILE *fpData; |
jphbergeson | 0:26713d1db198 | 49 | |
jphbergeson | 2:146625d6992d | 50 | void read_pixels() |
jphbergeson | 2:146625d6992d | 51 | { |
colingm | 5:6532c5a5f576 | 52 | char data[NUM_PIXELS]; |
jphbergeson | 6:059312376781 | 53 | int cycles_waited = 0; |
jphbergeson | 1:5fa445bd14a6 | 54 | for (int i = 0; i < NUM_PIXELS; i++) { |
jphbergeson | 6:059312376781 | 55 | while (!pixel_rdy) { |
jphbergeson | 6:059312376781 | 56 | cycles_waited++; |
jphbergeson | 6:059312376781 | 57 | if (cycles_waited > SPECTROMETER_TIMEOUT) |
jphbergeson | 6:059312376781 | 58 | break; |
jphbergeson | 6:059312376781 | 59 | } |
jphbergeson | 1:5fa445bd14a6 | 60 | fifo_cs = 0; |
jphbergeson | 1:5fa445bd14a6 | 61 | data[i] = spi.write(0x00); // write a dummy byte just to read the data |
jphbergeson | 1:5fa445bd14a6 | 62 | fifo_cs = 1; |
jphbergeson | 2:146625d6992d | 63 | |
jphbergeson | 6:059312376781 | 64 | if (cycles_waited > SPECTROMETER_TIMEOUT) |
jphbergeson | 6:059312376781 | 65 | pc.printf("timed out\r\n"); |
jphbergeson | 6:059312376781 | 66 | cycles_waited = 0; |
jphbergeson | 1:5fa445bd14a6 | 67 | } |
jphbergeson | 2:146625d6992d | 68 | |
jphbergeson | 1:5fa445bd14a6 | 69 | // write to file and pc |
jphbergeson | 1:5fa445bd14a6 | 70 | fpData = fopen(OUTPUT_FILE, "a"); |
jphbergeson | 2:146625d6992d | 71 | for (int i = 0; i < NUM_PIXELS; i++) { |
jphbergeson | 1:5fa445bd14a6 | 72 | pc.printf(",%d", data[i]); |
jphbergeson | 1:5fa445bd14a6 | 73 | fprintf(fpData, ",%d", data[i]); |
jphbergeson | 1:5fa445bd14a6 | 74 | } |
jphbergeson | 1:5fa445bd14a6 | 75 | pc.printf("\r\n"); |
jphbergeson | 1:5fa445bd14a6 | 76 | fprintf(fpData, "\r\n"); |
jphbergeson | 1:5fa445bd14a6 | 77 | fclose(fpData); |
colingm | 8:6a694f87fb13 | 78 | |
colingm | 5:6532c5a5f576 | 79 | XBeeSend((const char *)data, 2048); |
jphbergeson | 1:5fa445bd14a6 | 80 | } |
jphbergeson | 1:5fa445bd14a6 | 81 | |
jphbergeson | 2:146625d6992d | 82 | void gps_read() { |
jphbergeson | 2:146625d6992d | 83 | char buffer[BUF_SIZE]; |
jphbergeson | 2:146625d6992d | 84 | |
jphbergeson | 2:146625d6992d | 85 | get_nmea(&duino, buffer, BUF_SIZE); |
jphbergeson | 2:146625d6992d | 86 | |
jphbergeson | 2:146625d6992d | 87 | struct NMEA_data nmea = parse_line(buffer); |
jphbergeson | 2:146625d6992d | 88 | |
jphbergeson | 2:146625d6992d | 89 | // determine whether there's a lock |
jphbergeson | 2:146625d6992d | 90 | char lock_str[BUF_SIZE]; |
jphbergeson | 2:146625d6992d | 91 | if ( nmea.lock_flag == 'A' ) |
jphbergeson | 2:146625d6992d | 92 | sprintf(lock_str, "Has lock"); |
jphbergeson | 2:146625d6992d | 93 | else |
jphbergeson | 2:146625d6992d | 94 | sprintf(lock_str, "No lock"); |
jphbergeson | 2:146625d6992d | 95 | |
jphbergeson | 2:146625d6992d | 96 | // assemble data into summary string |
jphbergeson | 2:146625d6992d | 97 | char status_str[BUF_SIZE]; |
jphbergeson | 2:146625d6992d | 98 | sprintf(status_str, "%02d:%02d:%02d,%d/%d/%d,%dd %lf' %c %dd %lf' %c,%s", |
jphbergeson | 2:146625d6992d | 99 | nmea.hours, nmea.minutes, nmea.seconds, |
jphbergeson | 2:146625d6992d | 100 | nmea.month, nmea.day, nmea.year, |
jphbergeson | 2:146625d6992d | 101 | nmea.latitude, nmea.latitude_minutes, nmea.latitude_direction, |
jphbergeson | 2:146625d6992d | 102 | nmea.longitude, nmea.longitude_minutes, nmea.longitude_direction, lock_str); |
jphbergeson | 2:146625d6992d | 103 | |
jphbergeson | 2:146625d6992d | 104 | // print to pc, sd card |
jphbergeson | 2:146625d6992d | 105 | pc.printf("%s", status_str); |
jphbergeson | 2:146625d6992d | 106 | |
jphbergeson | 2:146625d6992d | 107 | fpData = fopen(OUTPUT_FILE, "a"); |
jphbergeson | 2:146625d6992d | 108 | fprintf(fpData, "%s", status_str); |
jphbergeson | 2:146625d6992d | 109 | fclose(fpData); |
colingm | 8:6a694f87fb13 | 110 | |
colingm | 8:6a694f87fb13 | 111 | XBeeSend((const char *)status_str, strlen(status_str)); |
jphbergeson | 2:146625d6992d | 112 | } |
jphbergeson | 2:146625d6992d | 113 | |
jphbergeson | 0:26713d1db198 | 114 | int main() |
jphbergeson | 0:26713d1db198 | 115 | { |
jphbergeson | 0:26713d1db198 | 116 | pc.baud(115200); // make sure to set computer TERA Term or whatever to 115200 baud!!! |
jphbergeson | 0:26713d1db198 | 117 | duino.baud(9600); |
jphbergeson | 0:26713d1db198 | 118 | pc.printf("Initializing ...\r\n"); |
jphbergeson | 2:146625d6992d | 119 | |
colingm | 5:6532c5a5f576 | 120 | XBeeInit(&pc); |
jphbergeson | 1:5fa445bd14a6 | 121 | fifo_cs = 1; |
jphbergeson | 1:5fa445bd14a6 | 122 | trigger = 0; |
jphbergeson | 1:5fa445bd14a6 | 123 | wait(0.5f); |
jphbergeson | 3:c611fff05072 | 124 | |
jphbergeson | 3:c611fff05072 | 125 | init_temp_sensor(); // TODO: stop execution & send error message if can't init temp sensor (or other things) |
jphbergeson | 2:146625d6992d | 126 | |
jphbergeson | 2:146625d6992d | 127 | |
jphbergeson | 0:26713d1db198 | 128 | while (true) { |
jphbergeson | 2:146625d6992d | 129 | |
jphbergeson | 0:26713d1db198 | 130 | led = !led; |
jphbergeson | 2:146625d6992d | 131 | |
jphbergeson | 7:a125a1fa4694 | 132 | gps_read(); |
jphbergeson | 3:c611fff05072 | 133 | check_temp(); |
jphbergeson | 3:c611fff05072 | 134 | // Display result |
jphbergeson | 3:c611fff05072 | 135 | pc.printf(",%s", TempCelsiusDisplay); |
jphbergeson | 3:c611fff05072 | 136 | fpData = fopen(OUTPUT_FILE, "a"); |
jphbergeson | 3:c611fff05072 | 137 | fprintf(fpData, ",%s", TempCelsiusDisplay); |
jphbergeson | 3:c611fff05072 | 138 | fclose(fpData); |
jphbergeson | 2:146625d6992d | 139 | |
jphbergeson | 6:059312376781 | 140 | // Trigger an acquisition from spectrometer |
jphbergeson | 1:5fa445bd14a6 | 141 | trigger = 1; |
jphbergeson | 1:5fa445bd14a6 | 142 | wait_ms(1); |
jphbergeson | 1:5fa445bd14a6 | 143 | trigger = 0; |
jphbergeson | 1:5fa445bd14a6 | 144 | read_pixels(); |
jphbergeson | 0:26713d1db198 | 145 | wait(0.5f); |
jphbergeson | 0:26713d1db198 | 146 | } |
jphbergeson | 0:26713d1db198 | 147 | } |