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