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

Dependencies:   DS1820 ExtendedTimer SDFileSystem mbed

Committer:
croberts21
Date:
Thu Apr 19 05:39:33 2018 +0000
Revision:
2:1136b2fda54b
Parent:
1:abe448f0aae0
Final code for Thermal Wake Near Space project.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
croberts21 0:2e66ec9086f9 1 /////////////////////////////
croberts21 0:2e66ec9086f9 2 //Main code for Thermal Wake
croberts21 0:2e66ec9086f9 3 //Chris Roberts
croberts21 0:2e66ec9086f9 4 //4-10-18
croberts21 0:2e66ec9086f9 5 /////////////////////////////
croberts21 0:2e66ec9086f9 6
croberts21 0:2e66ec9086f9 7 #define MULTIPLE_PROBES
croberts21 0:2e66ec9086f9 8 #define ARM1_DATA_PIN p25
croberts21 0:2e66ec9086f9 9 #define ARM2_DATA_PIN p19
croberts21 0:2e66ec9086f9 10 #define TAIL_DATA_PIN p23
croberts21 0:2e66ec9086f9 11 #define MAX_PROBES 54
croberts21 0:2e66ec9086f9 12
croberts21 0:2e66ec9086f9 13 #include "mbed.h"
croberts21 0:2e66ec9086f9 14 #include "DS1820.h"
croberts21 0:2e66ec9086f9 15 #include "ExtendedTimer.h"
croberts21 0:2e66ec9086f9 16 #include "SDFileSystem.h"
croberts21 0:2e66ec9086f9 17
croberts21 0:2e66ec9086f9 18
croberts21 0:2e66ec9086f9 19
croberts21 1:abe448f0aae0 20 DS1820* probe1[MAX_PROBES];
croberts21 1:abe448f0aae0 21 DS1820* probe2[MAX_PROBES];
croberts21 1:abe448f0aae0 22 DS1820* probeTail[MAX_PROBES];
croberts21 0:2e66ec9086f9 23 Serial pc(USBTX, USBRX);
croberts21 0:2e66ec9086f9 24 SDFileSystem fs(p5, p6, p7, p8, "fs");
croberts21 0:2e66ec9086f9 25 Timer t;
croberts21 0:2e66ec9086f9 26 Ticker sampleTime;
croberts21 0:2e66ec9086f9 27 Ticker saveTime;
croberts21 0:2e66ec9086f9 28 DigitalOut led1(LED1);
croberts21 0:2e66ec9086f9 29 DigitalOut led3(p14);
croberts21 0:2e66ec9086f9 30 DigitalOut ledOn(p16);
croberts21 0:2e66ec9086f9 31 DigitalOut ledError(p15);
croberts21 0:2e66ec9086f9 32 bool timeToRead;
croberts21 0:2e66ec9086f9 33 bool timeToSave;
croberts21 0:2e66ec9086f9 34
croberts21 0:2e66ec9086f9 35 void triggerSave()
croberts21 0:2e66ec9086f9 36 {
croberts21 0:2e66ec9086f9 37 timeToSave = true;
croberts21 0:2e66ec9086f9 38 }
croberts21 0:2e66ec9086f9 39
croberts21 0:2e66ec9086f9 40 void triggerCollection()
croberts21 0:2e66ec9086f9 41 {
croberts21 0:2e66ec9086f9 42 timeToRead = true; //flag to collect data
croberts21 0:2e66ec9086f9 43 }
croberts21 0:2e66ec9086f9 44
croberts21 0:2e66ec9086f9 45 int main()
croberts21 0:2e66ec9086f9 46 {
croberts21 0:2e66ec9086f9 47 //mount filesystem
croberts21 0:2e66ec9086f9 48 bool mountFailure = fs.mount();
croberts21 0:2e66ec9086f9 49 if (mountFailure != 0)
croberts21 0:2e66ec9086f9 50 {
croberts21 0:2e66ec9086f9 51 pc.printf("Failed to mount the SD card.\r\n");
croberts21 0:2e66ec9086f9 52 ledError = 1;
croberts21 0:2e66ec9086f9 53 return -1; //end program with error status
croberts21 0:2e66ec9086f9 54
croberts21 0:2e66ec9086f9 55 }
croberts21 0:2e66ec9086f9 56 pc.printf("Mounted SD card.\r\n");
croberts21 0:2e66ec9086f9 57
croberts21 0:2e66ec9086f9 58 unsigned long long fullName; //DS1820 are 64 bits (a long long)
croberts21 0:2e66ec9086f9 59 FILE* out1 = fopen("/fs/arm1.txt","w");
croberts21 0:2e66ec9086f9 60 FILE* out2 = fopen("/fs/arm2.txt","w");
croberts21 0:2e66ec9086f9 61 FILE* out3 = fopen("/fs/tail.txt","w");
croberts21 0:2e66ec9086f9 62
croberts21 0:2e66ec9086f9 63 ledOn = 1;
croberts21 0:2e66ec9086f9 64 led3 = 0;
croberts21 0:2e66ec9086f9 65
croberts21 0:2e66ec9086f9 66 //arm 1
croberts21 0:2e66ec9086f9 67 //initialize probe array to DS1820 objects
croberts21 0:2e66ec9086f9 68 int num_arm1 = 0;
croberts21 0:2e66ec9086f9 69 while(DS1820::unassignedProbe(ARM1_DATA_PIN))
croberts21 0:2e66ec9086f9 70 {
croberts21 1:abe448f0aae0 71 probe1[num_arm1] = new DS1820(ARM1_DATA_PIN);
croberts21 0:2e66ec9086f9 72 num_arm1++;
croberts21 0:2e66ec9086f9 73 if(num_arm1 == MAX_PROBES)
croberts21 0:2e66ec9086f9 74 {
croberts21 0:2e66ec9086f9 75 break;
croberts21 0:2e66ec9086f9 76 }
croberts21 0:2e66ec9086f9 77 }
croberts21 0:2e66ec9086f9 78 pc.printf("found %d sensors on arm 1 \r\n", num_arm1);
croberts21 0:2e66ec9086f9 79
croberts21 0:2e66ec9086f9 80 //arm 2
croberts21 0:2e66ec9086f9 81 //initialize probe array to DS1820 objects
croberts21 0:2e66ec9086f9 82 int num_arm2 = 0;
croberts21 0:2e66ec9086f9 83 while(DS1820::unassignedProbe(ARM2_DATA_PIN))
croberts21 0:2e66ec9086f9 84 {
croberts21 1:abe448f0aae0 85 probe2[num_arm2] = new DS1820(ARM2_DATA_PIN);
croberts21 0:2e66ec9086f9 86 num_arm2++;
croberts21 0:2e66ec9086f9 87 if(num_arm2 == MAX_PROBES)
croberts21 0:2e66ec9086f9 88 {
croberts21 0:2e66ec9086f9 89 break;
croberts21 0:2e66ec9086f9 90 }
croberts21 0:2e66ec9086f9 91 }
croberts21 0:2e66ec9086f9 92 pc.printf("found %d sensors on arm 2\r\n", num_arm2);
croberts21 0:2e66ec9086f9 93
croberts21 0:2e66ec9086f9 94 //tail
croberts21 0:2e66ec9086f9 95 //initialize probe array to DS1820 objects
croberts21 0:2e66ec9086f9 96 int num_tail = 0;
croberts21 0:2e66ec9086f9 97 while(DS1820::unassignedProbe(TAIL_DATA_PIN))
croberts21 0:2e66ec9086f9 98 {
croberts21 1:abe448f0aae0 99 probeTail[num_tail] = new DS1820(TAIL_DATA_PIN);
croberts21 0:2e66ec9086f9 100 num_tail++;
croberts21 0:2e66ec9086f9 101 if(num_tail == MAX_PROBES)
croberts21 0:2e66ec9086f9 102 {
croberts21 0:2e66ec9086f9 103 break;
croberts21 0:2e66ec9086f9 104 }
croberts21 0:2e66ec9086f9 105 }
croberts21 0:2e66ec9086f9 106 pc.printf("found %d sensors on tail\r\n", num_tail);
croberts21 0:2e66ec9086f9 107
croberts21 0:2e66ec9086f9 108 for(int i = 0; i < num_arm1; i++)
croberts21 0:2e66ec9086f9 109 {
croberts21 1:abe448f0aae0 110 fullName = probe1[i]->whoAmI();
croberts21 0:2e66ec9086f9 111 //print 64-bit ID as a hexadecimal number
croberts21 0:2e66ec9086f9 112 fprintf(out1,"%llX\t", fullName);
croberts21 0:2e66ec9086f9 113 }
croberts21 0:2e66ec9086f9 114
croberts21 0:2e66ec9086f9 115 for(int i = 0; i < num_arm2; i++)
croberts21 0:2e66ec9086f9 116 {
croberts21 1:abe448f0aae0 117 fullName = probe2[i]->whoAmI();
croberts21 0:2e66ec9086f9 118 //print 64-bit ID as a hexadecimal number
croberts21 0:2e66ec9086f9 119 fprintf(out2,"%llX\t", fullName);
croberts21 0:2e66ec9086f9 120 }
croberts21 0:2e66ec9086f9 121
croberts21 0:2e66ec9086f9 122 for(int i = 0; i < num_tail; i++)
croberts21 0:2e66ec9086f9 123 {
croberts21 1:abe448f0aae0 124 fullName = probeTail[i]->whoAmI();
croberts21 0:2e66ec9086f9 125 //print 64-bit ID as a hexadecimal number
croberts21 0:2e66ec9086f9 126 fprintf(out3,"%llX\t", fullName);
croberts21 0:2e66ec9086f9 127 }
croberts21 0:2e66ec9086f9 128
croberts21 0:2e66ec9086f9 129 fprintf(out1,"\r\n");
croberts21 0:2e66ec9086f9 130 fprintf(out2,"\r\n");
croberts21 0:2e66ec9086f9 131 fprintf(out3,"\r\n");
croberts21 0:2e66ec9086f9 132
croberts21 0:2e66ec9086f9 133 //write a header row
croberts21 0:2e66ec9086f9 134 fprintf(out1, "Time (s) \t Tempurature (deg C)\r\n");
croberts21 0:2e66ec9086f9 135 fprintf(out2, "Time (s) \t Tempurature (deg C)\r\n");
croberts21 0:2e66ec9086f9 136 fprintf(out3, "Time (s) \t Tempurature (deg C)\r\n");
croberts21 0:2e66ec9086f9 137
croberts21 0:2e66ec9086f9 138 led3 = 1;
croberts21 0:2e66ec9086f9 139
croberts21 0:2e66ec9086f9 140 //start timer and ticker
croberts21 0:2e66ec9086f9 141 t.start();
croberts21 2:1136b2fda54b 142 sampleTime.attach(&triggerCollection, 10); //write data every __ sec(s)
croberts21 0:2e66ec9086f9 143 timeToRead = true; //collect data at t = 0
croberts21 0:2e66ec9086f9 144
croberts21 0:2e66ec9086f9 145 timeToSave = false;
croberts21 2:1136b2fda54b 146 saveTime.attach(&triggerSave, 30);
croberts21 0:2e66ec9086f9 147
croberts21 0:2e66ec9086f9 148 while (t.read() < (60 * 60 * 4))
croberts21 0:2e66ec9086f9 149 {
croberts21 0:2e66ec9086f9 150 if(timeToRead == true)
croberts21 0:2e66ec9086f9 151 {
croberts21 0:2e66ec9086f9 152 timeToRead = false; //reset data collection flag
croberts21 1:abe448f0aae0 153 probe1[0]-> convertTemperature(true, DS1820::all_devices);
croberts21 1:abe448f0aae0 154 probe2[0]-> convertTemperature(true, DS1820::all_devices);
croberts21 1:abe448f0aae0 155 probeTail[0]-> convertTemperature(true, DS1820::all_devices);
croberts21 0:2e66ec9086f9 156
croberts21 0:2e66ec9086f9 157 //print from arm 1
croberts21 0:2e66ec9086f9 158 fprintf(out1, "%3.1f\t", t.read());
croberts21 0:2e66ec9086f9 159 //Get temp from each device
croberts21 0:2e66ec9086f9 160 for(int i = 0; i < num_arm1; i++)
croberts21 0:2e66ec9086f9 161 {
croberts21 1:abe448f0aae0 162 fprintf(out1, "%3.1f\t", probe1[i]->temperature());
croberts21 0:2e66ec9086f9 163 }
croberts21 0:2e66ec9086f9 164 fprintf(out1,"\r\n");
croberts21 0:2e66ec9086f9 165
croberts21 0:2e66ec9086f9 166 //print from arm 2
croberts21 0:2e66ec9086f9 167 fprintf(out2, "%3.1f\t", t.read());
croberts21 0:2e66ec9086f9 168 //Get temp from each device
croberts21 0:2e66ec9086f9 169 for(int i = 0; i < num_arm2 ; i++)
croberts21 0:2e66ec9086f9 170 {
croberts21 1:abe448f0aae0 171 fprintf(out2, "%3.1f\t", probe2[i]->temperature());
croberts21 0:2e66ec9086f9 172 }
croberts21 0:2e66ec9086f9 173 fprintf(out2,"\r\n");
croberts21 0:2e66ec9086f9 174
croberts21 0:2e66ec9086f9 175 //print from tail
croberts21 0:2e66ec9086f9 176 fprintf(out3, "%3.1f\t", t.read());
croberts21 0:2e66ec9086f9 177 //Get temp from each device
croberts21 0:2e66ec9086f9 178 for(int i = 0; i < num_tail; i++)
croberts21 0:2e66ec9086f9 179 {
croberts21 1:abe448f0aae0 180 fprintf(out3, "%3.1f\t", probeTail[i]->temperature());
croberts21 0:2e66ec9086f9 181 }
croberts21 0:2e66ec9086f9 182 fprintf(out3,"\r\n");
croberts21 0:2e66ec9086f9 183
croberts21 0:2e66ec9086f9 184
croberts21 0:2e66ec9086f9 185 if(timeToSave)
croberts21 0:2e66ec9086f9 186 {
croberts21 2:1136b2fda54b 187 ledError = 1;
croberts21 0:2e66ec9086f9 188 timeToSave = false;
croberts21 0:2e66ec9086f9 189 fclose(out1);
croberts21 0:2e66ec9086f9 190 fclose(out2);
croberts21 0:2e66ec9086f9 191 fclose(out3);
croberts21 0:2e66ec9086f9 192 out1 = fopen("/fs/arm1.txt","a");
croberts21 0:2e66ec9086f9 193 out2 = fopen("/fs/arm2.txt","a");
croberts21 0:2e66ec9086f9 194 out3 = fopen("/fs/tail.txt","a");
croberts21 2:1136b2fda54b 195 ledError = 0;
croberts21 0:2e66ec9086f9 196 }
croberts21 0:2e66ec9086f9 197
croberts21 0:2e66ec9086f9 198 }
croberts21 0:2e66ec9086f9 199 }
croberts21 0:2e66ec9086f9 200
croberts21 0:2e66ec9086f9 201 //close the file and unmount the file system so the SD card is happy
croberts21 0:2e66ec9086f9 202 fclose(out1);
croberts21 0:2e66ec9086f9 203 fclose(out2);
croberts21 0:2e66ec9086f9 204 fclose(out3);
croberts21 0:2e66ec9086f9 205 fs.unmount();
croberts21 0:2e66ec9086f9 206 //turn on LED to let user know it's safe to remove SD card
croberts21 0:2e66ec9086f9 207 led1 = 1;
croberts21 0:2e66ec9086f9 208 }