added spectrometer timeout
Dependencies: SDFileSystem mbed
Fork of All_Combined_Real2 by
main.cpp@10:6ecdb63e6698, 2016-04-11 (annotated)
- Committer:
- jphbergeson
- Date:
- Mon Apr 11 19:01:23 2016 +0000
- Revision:
- 10:6ecdb63e6698
- Parent:
- 9:924d80730ac5
- Child:
- 11:dc884cb71bfa
fixed a typo;
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 | 9:924d80730ac5 | 12 | // Temperatures when to turn on the cooling device, and when to shut off the light source |
jphbergeson | 9:924d80730ac5 | 13 | #define OVERHEATING_TEMP 33.0 |
jphbergeson | 9:924d80730ac5 | 14 | #define START_COOLING_TEMP 28.0 |
jphbergeson | 9:924d80730ac5 | 15 | |
jphbergeson | 9:924d80730ac5 | 16 | // pins for SSR's: |
jphbergeson | 9:924d80730ac5 | 17 | // PTB9 > Spectrometer |
jphbergeson | 9:924d80730ac5 | 18 | // PTA0 > Light Source |
jphbergeson | 9:924d80730ac5 | 19 | // PTD0 > Cooling Unit |
jphbergeson | 9:924d80730ac5 | 20 | DigitalOut spectrometer_ssr(PTB9); |
jphbergeson | 9:924d80730ac5 | 21 | DigitalOut lightsource_ssr(PTA0); |
jphbergeson | 9:924d80730ac5 | 22 | DigitalOut cooling_ssr(PTD0); |
jphbergeson | 9:924d80730ac5 | 23 | |
jphbergeson | 2:146625d6992d | 24 | // TODO make a new file (name=timestamp) each time we start recording data |
jphbergeson | 1:5fa445bd14a6 | 25 | #define OUTPUT_FILE "/sd/data.csv" |
jphbergeson | 0:26713d1db198 | 26 | |
jphbergeson | 1:5fa445bd14a6 | 27 | // pinout for spectrometer |
jphbergeson | 1:5fa445bd14a6 | 28 | |
jphbergeson | 1:5fa445bd14a6 | 29 | // Mosi (#8) -> PTD2 |
jphbergeson | 1:5fa445bd14a6 | 30 | // Miso (#7) -> PTD3 |
jphbergeson | 1:5fa445bd14a6 | 31 | // Sclk (#9) -> PTD1 |
jphbergeson | 1:5fa445bd14a6 | 32 | |
jphbergeson | 1:5fa445bd14a6 | 33 | // Pixel_rdy (#6) -> PTA1 |
jphbergeson | 1:5fa445bd14a6 | 34 | // Fifo_cs (#3) -> PTB23 |
jphbergeson | 1:5fa445bd14a6 | 35 | // Trigger (#13) -> PTA2 |
jphbergeson | 2:146625d6992d | 36 | |
jphbergeson | 4:70411c8dadcc | 37 | // spi_cs (#10) -> PTC2 |
jphbergeson | 4:70411c8dadcc | 38 | |
jphbergeson | 1:5fa445bd14a6 | 39 | SPI spi(PTD2, PTD3, PTD1); // mosi, miso, sclk |
jphbergeson | 1:5fa445bd14a6 | 40 | |
jphbergeson | 4:70411c8dadcc | 41 | DigitalOut spi_cs(PTC2); |
jphbergeson | 4:70411c8dadcc | 42 | |
jphbergeson | 1:5fa445bd14a6 | 43 | DigitalIn pixel_rdy(PTA1); |
jphbergeson | 1:5fa445bd14a6 | 44 | DigitalOut fifo_cs(PTB23); |
jphbergeson | 1:5fa445bd14a6 | 45 | DigitalOut trigger(PTA2); |
jphbergeson | 1:5fa445bd14a6 | 46 | |
jphbergeson | 1:5fa445bd14a6 | 47 | // Blinking red LED |
jphbergeson | 0:26713d1db198 | 48 | DigitalOut led(LED_RED); |
jphbergeson | 0:26713d1db198 | 49 | |
jphbergeson | 1:5fa445bd14a6 | 50 | // GPS connection (through arduino) |
jphbergeson | 0:26713d1db198 | 51 | Serial duino(PTC4, PTC3); |
jphbergeson | 1:5fa445bd14a6 | 52 | |
jphbergeson | 1:5fa445bd14a6 | 53 | // USB serial to PC |
jphbergeson | 0:26713d1db198 | 54 | Serial pc(USBTX, USBRX); |
jphbergeson | 0:26713d1db198 | 55 | |
jphbergeson | 0:26713d1db198 | 56 | /************************************************** |
jphbergeson | 0:26713d1db198 | 57 | ** SD FILE SYSTEM ** |
jphbergeson | 0:26713d1db198 | 58 | **************************************************/ |
jphbergeson | 0:26713d1db198 | 59 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
jphbergeson | 0:26713d1db198 | 60 | FILE *fpData; |
jphbergeson | 0:26713d1db198 | 61 | |
jphbergeson | 2:146625d6992d | 62 | void read_pixels() |
jphbergeson | 2:146625d6992d | 63 | { |
colingm | 5:6532c5a5f576 | 64 | char data[NUM_PIXELS]; |
jphbergeson | 6:059312376781 | 65 | int cycles_waited = 0; |
jphbergeson | 1:5fa445bd14a6 | 66 | for (int i = 0; i < NUM_PIXELS; i++) { |
jphbergeson | 6:059312376781 | 67 | while (!pixel_rdy) { |
jphbergeson | 6:059312376781 | 68 | cycles_waited++; |
jphbergeson | 6:059312376781 | 69 | if (cycles_waited > SPECTROMETER_TIMEOUT) |
jphbergeson | 6:059312376781 | 70 | break; |
jphbergeson | 6:059312376781 | 71 | } |
jphbergeson | 1:5fa445bd14a6 | 72 | fifo_cs = 0; |
jphbergeson | 1:5fa445bd14a6 | 73 | data[i] = spi.write(0x00); // write a dummy byte just to read the data |
jphbergeson | 1:5fa445bd14a6 | 74 | fifo_cs = 1; |
jphbergeson | 2:146625d6992d | 75 | |
jphbergeson | 6:059312376781 | 76 | if (cycles_waited > SPECTROMETER_TIMEOUT) |
jphbergeson | 6:059312376781 | 77 | pc.printf("timed out\r\n"); |
jphbergeson | 6:059312376781 | 78 | cycles_waited = 0; |
jphbergeson | 1:5fa445bd14a6 | 79 | } |
jphbergeson | 2:146625d6992d | 80 | |
jphbergeson | 1:5fa445bd14a6 | 81 | // write to file and pc |
jphbergeson | 1:5fa445bd14a6 | 82 | fpData = fopen(OUTPUT_FILE, "a"); |
jphbergeson | 2:146625d6992d | 83 | for (int i = 0; i < NUM_PIXELS; i++) { |
jphbergeson | 1:5fa445bd14a6 | 84 | pc.printf(",%d", data[i]); |
jphbergeson | 1:5fa445bd14a6 | 85 | fprintf(fpData, ",%d", data[i]); |
jphbergeson | 1:5fa445bd14a6 | 86 | } |
jphbergeson | 1:5fa445bd14a6 | 87 | pc.printf("\r\n"); |
jphbergeson | 1:5fa445bd14a6 | 88 | fprintf(fpData, "\r\n"); |
jphbergeson | 1:5fa445bd14a6 | 89 | fclose(fpData); |
colingm | 8:6a694f87fb13 | 90 | |
colingm | 5:6532c5a5f576 | 91 | XBeeSend((const char *)data, 2048); |
jphbergeson | 1:5fa445bd14a6 | 92 | } |
jphbergeson | 1:5fa445bd14a6 | 93 | |
jphbergeson | 2:146625d6992d | 94 | void gps_read() { |
jphbergeson | 2:146625d6992d | 95 | char buffer[BUF_SIZE]; |
jphbergeson | 2:146625d6992d | 96 | |
jphbergeson | 2:146625d6992d | 97 | get_nmea(&duino, buffer, BUF_SIZE); |
jphbergeson | 2:146625d6992d | 98 | |
jphbergeson | 2:146625d6992d | 99 | struct NMEA_data nmea = parse_line(buffer); |
jphbergeson | 2:146625d6992d | 100 | |
jphbergeson | 2:146625d6992d | 101 | // determine whether there's a lock |
jphbergeson | 2:146625d6992d | 102 | char lock_str[BUF_SIZE]; |
jphbergeson | 2:146625d6992d | 103 | if ( nmea.lock_flag == 'A' ) |
jphbergeson | 2:146625d6992d | 104 | sprintf(lock_str, "Has lock"); |
jphbergeson | 2:146625d6992d | 105 | else |
jphbergeson | 2:146625d6992d | 106 | sprintf(lock_str, "No lock"); |
jphbergeson | 2:146625d6992d | 107 | |
jphbergeson | 2:146625d6992d | 108 | // assemble data into summary string |
jphbergeson | 2:146625d6992d | 109 | char status_str[BUF_SIZE]; |
jphbergeson | 2:146625d6992d | 110 | sprintf(status_str, "%02d:%02d:%02d,%d/%d/%d,%dd %lf' %c %dd %lf' %c,%s", |
jphbergeson | 2:146625d6992d | 111 | nmea.hours, nmea.minutes, nmea.seconds, |
jphbergeson | 2:146625d6992d | 112 | nmea.month, nmea.day, nmea.year, |
jphbergeson | 2:146625d6992d | 113 | nmea.latitude, nmea.latitude_minutes, nmea.latitude_direction, |
jphbergeson | 2:146625d6992d | 114 | nmea.longitude, nmea.longitude_minutes, nmea.longitude_direction, lock_str); |
jphbergeson | 2:146625d6992d | 115 | |
jphbergeson | 2:146625d6992d | 116 | // print to pc, sd card |
jphbergeson | 2:146625d6992d | 117 | pc.printf("%s", status_str); |
jphbergeson | 2:146625d6992d | 118 | |
jphbergeson | 2:146625d6992d | 119 | fpData = fopen(OUTPUT_FILE, "a"); |
jphbergeson | 2:146625d6992d | 120 | fprintf(fpData, "%s", status_str); |
jphbergeson | 2:146625d6992d | 121 | fclose(fpData); |
colingm | 8:6a694f87fb13 | 122 | |
colingm | 8:6a694f87fb13 | 123 | XBeeSend((const char *)status_str, strlen(status_str)); |
jphbergeson | 2:146625d6992d | 124 | } |
jphbergeson | 2:146625d6992d | 125 | |
jphbergeson | 0:26713d1db198 | 126 | int main() |
jphbergeson | 0:26713d1db198 | 127 | { |
jphbergeson | 9:924d80730ac5 | 128 | // turn on light source & spectrometer, start with cooling unit off |
jphbergeson | 9:924d80730ac5 | 129 | lightsource_ssr = 1; |
jphbergeson | 9:924d80730ac5 | 130 | spectrometer_ssr = 1; |
jphbergeson | 9:924d80730ac5 | 131 | cooling_ssr = 0; |
jphbergeson | 9:924d80730ac5 | 132 | |
jphbergeson | 0:26713d1db198 | 133 | pc.baud(115200); // make sure to set computer TERA Term or whatever to 115200 baud!!! |
jphbergeson | 0:26713d1db198 | 134 | duino.baud(9600); |
jphbergeson | 0:26713d1db198 | 135 | pc.printf("Initializing ...\r\n"); |
jphbergeson | 2:146625d6992d | 136 | |
colingm | 5:6532c5a5f576 | 137 | XBeeInit(&pc); |
jphbergeson | 1:5fa445bd14a6 | 138 | fifo_cs = 1; |
jphbergeson | 1:5fa445bd14a6 | 139 | trigger = 0; |
jphbergeson | 1:5fa445bd14a6 | 140 | wait(0.5f); |
jphbergeson | 3:c611fff05072 | 141 | |
jphbergeson | 3:c611fff05072 | 142 | init_temp_sensor(); // TODO: stop execution & send error message if can't init temp sensor (or other things) |
jphbergeson | 2:146625d6992d | 143 | |
jphbergeson | 2:146625d6992d | 144 | |
jphbergeson | 0:26713d1db198 | 145 | while (true) { |
jphbergeson | 2:146625d6992d | 146 | |
jphbergeson | 0:26713d1db198 | 147 | led = !led; |
jphbergeson | 2:146625d6992d | 148 | |
jphbergeson | 7:a125a1fa4694 | 149 | gps_read(); |
jphbergeson | 3:c611fff05072 | 150 | check_temp(); |
jphbergeson | 3:c611fff05072 | 151 | // Display result |
jphbergeson | 3:c611fff05072 | 152 | pc.printf(",%s", TempCelsiusDisplay); |
jphbergeson | 3:c611fff05072 | 153 | fpData = fopen(OUTPUT_FILE, "a"); |
jphbergeson | 3:c611fff05072 | 154 | fprintf(fpData, ",%s", TempCelsiusDisplay); |
jphbergeson | 3:c611fff05072 | 155 | fclose(fpData); |
jphbergeson | 2:146625d6992d | 156 | |
jphbergeson | 6:059312376781 | 157 | // Trigger an acquisition from spectrometer |
jphbergeson | 1:5fa445bd14a6 | 158 | trigger = 1; |
jphbergeson | 1:5fa445bd14a6 | 159 | wait_ms(1); |
jphbergeson | 1:5fa445bd14a6 | 160 | trigger = 0; |
jphbergeson | 1:5fa445bd14a6 | 161 | read_pixels(); |
jphbergeson | 9:924d80730ac5 | 162 | |
jphbergeson | 9:924d80730ac5 | 163 | if (tempCelsiusDouble > OVERHEATING_TEMP) { |
jphbergeson | 9:924d80730ac5 | 164 | // turn off light source & spectrometer |
jphbergeson | 10:6ecdb63e6698 | 165 | lightsource_ssr = 0; |
jphbergeson | 10:6ecdb63e6698 | 166 | spectrometer_ssr = 0; |
jphbergeson | 9:924d80730ac5 | 167 | cooling_ssr = 1; |
jphbergeson | 9:924d80730ac5 | 168 | |
jphbergeson | 9:924d80730ac5 | 169 | // spin here -- just need to cool off. |
jphbergeson | 9:924d80730ac5 | 170 | // Can turn back on if you want by breaking out of this loop. |
jphbergeson | 9:924d80730ac5 | 171 | // For now just turn off forever. |
jphbergeson | 9:924d80730ac5 | 172 | while (1); |
jphbergeson | 9:924d80730ac5 | 173 | } |
jphbergeson | 9:924d80730ac5 | 174 | |
jphbergeson | 9:924d80730ac5 | 175 | if (tempCelsiusDouble > START_COOLING_TEMP) { |
jphbergeson | 9:924d80730ac5 | 176 | // turn on cooling |
jphbergeson | 9:924d80730ac5 | 177 | cooling_ssr = 1; |
jphbergeson | 9:924d80730ac5 | 178 | } else { |
jphbergeson | 9:924d80730ac5 | 179 | // turn off cooling |
jphbergeson | 9:924d80730ac5 | 180 | cooling_ssr = 0; |
jphbergeson | 9:924d80730ac5 | 181 | } |
jphbergeson | 9:924d80730ac5 | 182 | |
jphbergeson | 0:26713d1db198 | 183 | wait(0.5f); |
jphbergeson | 0:26713d1db198 | 184 | } |
jphbergeson | 0:26713d1db198 | 185 | } |