Used to determine the thermal wake left behind a weather ballon on its accent towards the edge of space.

Dependencies:   DS1820 ExtendedTimer SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001  /////////////////////////////
00002 //Main code for Thermal Wake
00003 //Chris Roberts
00004 //4-10-18
00005 ///////////////////////////// 
00006  
00007 #define MULTIPLE_PROBES
00008 #define ARM1_DATA_PIN        p25
00009 #define ARM2_DATA_PIN        p19
00010 #define TAIL_DATA_PIN        p23
00011 #define MAX_PROBES           54
00012  
00013 #include "mbed.h"
00014 #include "DS1820.h"
00015 #include "ExtendedTimer.h"
00016 #include "SDFileSystem.h"
00017  
00018 
00019 
00020 DS1820* probe1[MAX_PROBES];
00021 DS1820* probe2[MAX_PROBES];
00022 DS1820* probeTail[MAX_PROBES];
00023 Serial pc(USBTX, USBRX);
00024 SDFileSystem fs(p5, p6, p7, p8, "fs");
00025 Timer t;
00026 Ticker sampleTime;
00027 Ticker saveTime;
00028 DigitalOut led1(LED1);
00029 DigitalOut led3(p14);
00030 DigitalOut ledOn(p16);
00031 DigitalOut ledError(p15);
00032 bool timeToRead;
00033 bool timeToSave;
00034 
00035 void triggerSave()
00036     {
00037     timeToSave = true;
00038     }
00039 
00040 void triggerCollection() 
00041     {
00042     timeToRead = true; //flag to collect data
00043     }
00044     
00045 int main()
00046 {
00047     //mount filesystem
00048     bool mountFailure = fs.mount();
00049     if (mountFailure != 0)
00050     {
00051         pc.printf("Failed to mount the SD card.\r\n");
00052         ledError = 1;
00053         return -1; //end program with error status
00054         
00055     }
00056     pc.printf("Mounted SD card.\r\n");   
00057     
00058     unsigned long long fullName; //DS1820 are 64 bits (a long long)
00059     FILE* out1 = fopen("/fs/arm1.txt","w");
00060     FILE* out2 = fopen("/fs/arm2.txt","w");
00061     FILE* out3 = fopen("/fs/tail.txt","w");
00062     
00063     ledOn = 1;
00064     led3 = 0;
00065     
00066     //arm 1
00067     //initialize probe array to DS1820 objects
00068     int num_arm1 = 0;
00069     while(DS1820::unassignedProbe(ARM1_DATA_PIN))
00070     {
00071         probe1[num_arm1] = new DS1820(ARM1_DATA_PIN);
00072         num_arm1++;
00073         if(num_arm1 == MAX_PROBES)
00074         {
00075             break;
00076         }
00077     }
00078     pc.printf("found %d sensors on arm 1 \r\n", num_arm1);
00079     
00080     //arm 2
00081     //initialize probe array to DS1820 objects
00082     int num_arm2 = 0;
00083     while(DS1820::unassignedProbe(ARM2_DATA_PIN))
00084     {
00085         probe2[num_arm2] = new DS1820(ARM2_DATA_PIN);
00086         num_arm2++;
00087         if(num_arm2 == MAX_PROBES)
00088         {
00089             break;
00090         }
00091     }
00092     pc.printf("found %d sensors on arm 2\r\n", num_arm2);
00093     
00094     //tail
00095     //initialize probe array to DS1820 objects
00096     int num_tail = 0;
00097     while(DS1820::unassignedProbe(TAIL_DATA_PIN))
00098     {
00099         probeTail[num_tail] = new DS1820(TAIL_DATA_PIN);
00100         num_tail++;
00101         if(num_tail == MAX_PROBES)
00102         {
00103             break;
00104         }
00105     }
00106     pc.printf("found %d sensors on tail\r\n", num_tail);
00107     
00108     for(int i = 0; i < num_arm1; i++)
00109     {
00110         fullName = probe1[i]->whoAmI();
00111         //print 64-bit ID as a hexadecimal number
00112         fprintf(out1,"%llX\t", fullName);
00113     }
00114 
00115     for(int i = 0; i < num_arm2; i++)
00116     {
00117         fullName = probe2[i]->whoAmI();
00118         //print 64-bit ID as a hexadecimal number
00119         fprintf(out2,"%llX\t", fullName);
00120     }
00121     
00122     for(int i = 0; i < num_tail; i++)
00123     {
00124         fullName = probeTail[i]->whoAmI();
00125         //print 64-bit ID as a hexadecimal number
00126         fprintf(out3,"%llX\t", fullName);
00127     }
00128 
00129     fprintf(out1,"\r\n");
00130     fprintf(out2,"\r\n");
00131     fprintf(out3,"\r\n");
00132         
00133     //write a header row
00134     fprintf(out1, "Time (s) \t Tempurature (deg C)\r\n");
00135     fprintf(out2, "Time (s) \t Tempurature (deg C)\r\n");
00136     fprintf(out3, "Time (s) \t Tempurature (deg C)\r\n");
00137     
00138     led3 = 1;
00139 
00140     //start timer and ticker
00141     t.start();
00142     sampleTime.attach(&triggerCollection, 10); //write data every __ sec(s)
00143     timeToRead = true; //collect data at t = 0
00144     
00145     timeToSave = false;
00146     saveTime.attach(&triggerSave, 30);
00147     
00148     while (t.read() < (60 * 60 * 4))
00149     { 
00150         if(timeToRead == true)
00151         {
00152             timeToRead = false; //reset data collection flag
00153             probe1[0]-> convertTemperature(true, DS1820::all_devices);
00154             probe2[0]-> convertTemperature(true, DS1820::all_devices);
00155             probeTail[0]-> convertTemperature(true, DS1820::all_devices);
00156         
00157         //print from arm 1
00158             fprintf(out1, "%3.1f\t", t.read());
00159             //Get temp from each device
00160             for(int i = 0; i < num_arm1; i++)
00161             {
00162                 fprintf(out1, "%3.1f\t",  probe1[i]->temperature());
00163             }
00164             fprintf(out1,"\r\n"); 
00165              
00166         //print from arm 2 
00167             fprintf(out2, "%3.1f\t", t.read());
00168             //Get temp from each device
00169             for(int i = 0; i < num_arm2  ; i++)
00170             {
00171                 fprintf(out2, "%3.1f\t",  probe2[i]->temperature());
00172             }
00173             fprintf(out2,"\r\n");
00174             
00175         //print from tail
00176             fprintf(out3, "%3.1f\t", t.read());
00177             //Get temp from each device
00178             for(int i = 0; i < num_tail; i++)
00179             {
00180                 fprintf(out3, "%3.1f\t",  probeTail[i]->temperature());
00181             }
00182             fprintf(out3,"\r\n"); 
00183             
00184             
00185             if(timeToSave)
00186             {
00187                 ledError = 1;
00188                 timeToSave = false;
00189                 fclose(out1);
00190                 fclose(out2);
00191                 fclose(out3);
00192                 out1 = fopen("/fs/arm1.txt","a");
00193                 out2 = fopen("/fs/arm2.txt","a");
00194                 out3 = fopen("/fs/tail.txt","a");
00195                 ledError = 0;
00196             }       
00197             
00198         }
00199     }
00200     
00201     //close the file and unmount the file system so the SD card is happy
00202     fclose(out1);
00203     fclose(out2);
00204     fclose(out3);
00205     fs.unmount();
00206     //turn on LED to let user know it's safe to remove SD card
00207     led1 = 1;
00208 }