added spectrometer timeout

Dependencies:   SDFileSystem mbed

Fork of All_Combined_Real2 by Colin Maxfield

Committer:
jphbergeson
Date:
Wed Apr 13 23:53:47 2016 +0000
Revision:
13:346b43aa06c0
Parent:
11:dc884cb71bfa
Added some logging, plus a variable to keep track of the value of the LED

Who changed what in which revision?

UserRevisionLine numberNew 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 13:346b43aa06c0 57 int led_val = 0;
jphbergeson 0:26713d1db198 58
jphbergeson 1:5fa445bd14a6 59 // GPS connection (through arduino)
jphbergeson 0:26713d1db198 60 Serial duino(PTC4, PTC3);
jphbergeson 1:5fa445bd14a6 61
jphbergeson 1:5fa445bd14a6 62 // USB serial to PC
jphbergeson 0:26713d1db198 63 Serial pc(USBTX, USBRX);
jphbergeson 0:26713d1db198 64
jphbergeson 0:26713d1db198 65 /**************************************************
jphbergeson 0:26713d1db198 66 ** SD FILE SYSTEM **
jphbergeson 0:26713d1db198 67 **************************************************/
jphbergeson 0:26713d1db198 68 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
jphbergeson 0:26713d1db198 69 FILE *fpData;
jphbergeson 0:26713d1db198 70
jphbergeson 2:146625d6992d 71 void read_pixels()
jphbergeson 2:146625d6992d 72 {
colingm 5:6532c5a5f576 73 char data[NUM_PIXELS];
jphbergeson 6:059312376781 74 int cycles_waited = 0;
jphbergeson 1:5fa445bd14a6 75 for (int i = 0; i < NUM_PIXELS; i++) {
jphbergeson 6:059312376781 76 while (!pixel_rdy) {
jphbergeson 6:059312376781 77 cycles_waited++;
jphbergeson 6:059312376781 78 if (cycles_waited > SPECTROMETER_TIMEOUT)
jphbergeson 6:059312376781 79 break;
jphbergeson 6:059312376781 80 }
jphbergeson 1:5fa445bd14a6 81 fifo_cs = 0;
jphbergeson 1:5fa445bd14a6 82 data[i] = spi.write(0x00); // write a dummy byte just to read the data
jphbergeson 1:5fa445bd14a6 83 fifo_cs = 1;
jphbergeson 2:146625d6992d 84
jphbergeson 6:059312376781 85 if (cycles_waited > SPECTROMETER_TIMEOUT)
jphbergeson 6:059312376781 86 pc.printf("timed out\r\n");
jphbergeson 6:059312376781 87 cycles_waited = 0;
jphbergeson 1:5fa445bd14a6 88 }
jphbergeson 2:146625d6992d 89
jphbergeson 1:5fa445bd14a6 90 // write to file and pc
jphbergeson 1:5fa445bd14a6 91 fpData = fopen(OUTPUT_FILE, "a");
jphbergeson 2:146625d6992d 92 for (int i = 0; i < NUM_PIXELS; i++) {
jphbergeson 1:5fa445bd14a6 93 pc.printf(",%d", data[i]);
jphbergeson 1:5fa445bd14a6 94 fprintf(fpData, ",%d", data[i]);
jphbergeson 1:5fa445bd14a6 95 }
jphbergeson 1:5fa445bd14a6 96 pc.printf("\r\n");
jphbergeson 1:5fa445bd14a6 97 fprintf(fpData, "\r\n");
jphbergeson 1:5fa445bd14a6 98 fclose(fpData);
colingm 8:6a694f87fb13 99
colingm 5:6532c5a5f576 100 XBeeSend((const char *)data, 2048);
jphbergeson 1:5fa445bd14a6 101 }
jphbergeson 1:5fa445bd14a6 102
jphbergeson 2:146625d6992d 103 void gps_read() {
jphbergeson 2:146625d6992d 104 char buffer[BUF_SIZE];
jphbergeson 2:146625d6992d 105
jphbergeson 2:146625d6992d 106 get_nmea(&duino, buffer, BUF_SIZE);
jphbergeson 2:146625d6992d 107
jphbergeson 2:146625d6992d 108 struct NMEA_data nmea = parse_line(buffer);
jphbergeson 2:146625d6992d 109
jphbergeson 2:146625d6992d 110 // determine whether there's a lock
jphbergeson 2:146625d6992d 111 char lock_str[BUF_SIZE];
jphbergeson 2:146625d6992d 112 if ( nmea.lock_flag == 'A' )
jphbergeson 2:146625d6992d 113 sprintf(lock_str, "Has lock");
jphbergeson 2:146625d6992d 114 else
jphbergeson 2:146625d6992d 115 sprintf(lock_str, "No lock");
jphbergeson 2:146625d6992d 116
jphbergeson 2:146625d6992d 117 // assemble data into summary string
jphbergeson 2:146625d6992d 118 char status_str[BUF_SIZE];
jphbergeson 2:146625d6992d 119 sprintf(status_str, "%02d:%02d:%02d,%d/%d/%d,%dd %lf' %c %dd %lf' %c,%s",
jphbergeson 2:146625d6992d 120 nmea.hours, nmea.minutes, nmea.seconds,
jphbergeson 2:146625d6992d 121 nmea.month, nmea.day, nmea.year,
jphbergeson 2:146625d6992d 122 nmea.latitude, nmea.latitude_minutes, nmea.latitude_direction,
jphbergeson 2:146625d6992d 123 nmea.longitude, nmea.longitude_minutes, nmea.longitude_direction, lock_str);
jphbergeson 2:146625d6992d 124
jphbergeson 2:146625d6992d 125 // print to pc, sd card
jphbergeson 2:146625d6992d 126 pc.printf("%s", status_str);
jphbergeson 2:146625d6992d 127
jphbergeson 2:146625d6992d 128 fpData = fopen(OUTPUT_FILE, "a");
jphbergeson 2:146625d6992d 129 fprintf(fpData, "%s", status_str);
jphbergeson 2:146625d6992d 130 fclose(fpData);
colingm 8:6a694f87fb13 131
colingm 8:6a694f87fb13 132 XBeeSend((const char *)status_str, strlen(status_str));
jphbergeson 2:146625d6992d 133 }
jphbergeson 2:146625d6992d 134
jphbergeson 0:26713d1db198 135 int main()
jphbergeson 0:26713d1db198 136 {
jphbergeson 13:346b43aa06c0 137 led = 1;
jphbergeson 13:346b43aa06c0 138 led_val = 1;
jphbergeson 13:346b43aa06c0 139
jphbergeson 9:924d80730ac5 140 // turn on light source & spectrometer, start with cooling unit off
jphbergeson 9:924d80730ac5 141 lightsource_ssr = 1;
jphbergeson 9:924d80730ac5 142 spectrometer_ssr = 1;
jphbergeson 9:924d80730ac5 143 cooling_ssr = 0;
jphbergeson 9:924d80730ac5 144
colingm 11:dc884cb71bfa 145 // Wait a half a second before turning the bulbs on
colingm 11:dc884cb71bfa 146 // More code be added to only turn these on if everything seems to be working
colingm 11:dc884cb71bfa 147 wait(0.5f);
colingm 11:dc884cb71bfa 148 halogen_ttl = 1;
colingm 11:dc884cb71bfa 149 deuterium_ttl = 1;
colingm 11:dc884cb71bfa 150 shutter_ttl = 1;
colingm 11:dc884cb71bfa 151
jphbergeson 0:26713d1db198 152 pc.baud(115200); // make sure to set computer TERA Term or whatever to 115200 baud!!!
jphbergeson 0:26713d1db198 153 duino.baud(9600);
jphbergeson 0:26713d1db198 154 pc.printf("Initializing ...\r\n");
jphbergeson 2:146625d6992d 155
colingm 5:6532c5a5f576 156 XBeeInit(&pc);
jphbergeson 1:5fa445bd14a6 157 fifo_cs = 1;
jphbergeson 1:5fa445bd14a6 158 trigger = 0;
jphbergeson 1:5fa445bd14a6 159 wait(0.5f);
jphbergeson 3:c611fff05072 160
jphbergeson 3:c611fff05072 161 init_temp_sensor(); // TODO: stop execution & send error message if can't init temp sensor (or other things)
jphbergeson 2:146625d6992d 162
jphbergeson 2:146625d6992d 163
jphbergeson 0:26713d1db198 164 while (true) {
jphbergeson 13:346b43aa06c0 165 pc.printf("beginning again\r\n");
jphbergeson 13:346b43aa06c0 166
jphbergeson 13:346b43aa06c0 167 // have to have this extra value b/c it sometimes has problems reading from DigitalOut
jphbergeson 13:346b43aa06c0 168 led = !led_val;
jphbergeson 13:346b43aa06c0 169 led_val = !led_val;
jphbergeson 13:346b43aa06c0 170 pc.printf("about to gps_read\r\n");
jphbergeson 7:a125a1fa4694 171 gps_read();
jphbergeson 3:c611fff05072 172 check_temp();
jphbergeson 3:c611fff05072 173 // Display result
jphbergeson 3:c611fff05072 174 pc.printf(",%s", TempCelsiusDisplay);
jphbergeson 3:c611fff05072 175 fpData = fopen(OUTPUT_FILE, "a");
jphbergeson 3:c611fff05072 176 fprintf(fpData, ",%s", TempCelsiusDisplay);
jphbergeson 3:c611fff05072 177 fclose(fpData);
jphbergeson 2:146625d6992d 178
jphbergeson 6:059312376781 179 // Trigger an acquisition from spectrometer
jphbergeson 1:5fa445bd14a6 180 trigger = 1;
jphbergeson 1:5fa445bd14a6 181 wait_ms(1);
jphbergeson 1:5fa445bd14a6 182 trigger = 0;
jphbergeson 1:5fa445bd14a6 183 read_pixels();
jphbergeson 9:924d80730ac5 184
jphbergeson 13:346b43aa06c0 185 pc.printf("%lf\r\n", tempCelsiusDouble);
jphbergeson 9:924d80730ac5 186 if (tempCelsiusDouble > OVERHEATING_TEMP) {
colingm 11:dc884cb71bfa 187 // Turn off the bulbs and the shutter
colingm 11:dc884cb71bfa 188 halogen_ttl = 0;
colingm 11:dc884cb71bfa 189 deuterium_ttl = 0;
colingm 11:dc884cb71bfa 190 shutter_ttl = 0;
jphbergeson 9:924d80730ac5 191 // turn off light source & spectrometer
jphbergeson 10:6ecdb63e6698 192 lightsource_ssr = 0;
jphbergeson 10:6ecdb63e6698 193 spectrometer_ssr = 0;
jphbergeson 9:924d80730ac5 194 cooling_ssr = 1;
jphbergeson 9:924d80730ac5 195
jphbergeson 9:924d80730ac5 196 // spin here -- just need to cool off.
jphbergeson 9:924d80730ac5 197 // Can turn back on if you want by breaking out of this loop.
jphbergeson 9:924d80730ac5 198 // For now just turn off forever.
jphbergeson 9:924d80730ac5 199 while (1);
jphbergeson 9:924d80730ac5 200 }
jphbergeson 9:924d80730ac5 201
jphbergeson 9:924d80730ac5 202 if (tempCelsiusDouble > START_COOLING_TEMP) {
jphbergeson 9:924d80730ac5 203 // turn on cooling
jphbergeson 9:924d80730ac5 204 cooling_ssr = 1;
jphbergeson 9:924d80730ac5 205 } else {
jphbergeson 9:924d80730ac5 206 // turn off cooling
jphbergeson 9:924d80730ac5 207 cooling_ssr = 0;
jphbergeson 13:346b43aa06c0 208 }
jphbergeson 13:346b43aa06c0 209 pc.printf("end of cooling\r\n");
jphbergeson 9:924d80730ac5 210
jphbergeson 0:26713d1db198 211 wait(0.5f);
jphbergeson 0:26713d1db198 212 }
jphbergeson 0:26713d1db198 213 }