starting integration with XBee

Dependencies:   SDFileSystem mbed

Committer:
jphbergeson
Date:
Tue Mar 15 17:52:05 2016 +0000
Revision:
3:c611fff05072
Parent:
2:146625d6992d
Child:
4:70411c8dadcc
Working with temp sensor

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