![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
chemical dosing section of the rig
Fork of mbed_plant_rig by
main.cpp@1:d022bdae586b, 2017-07-29 (annotated)
- Committer:
- Demonthorn
- Date:
- Sat Jul 29 15:49:12 2017 +0000
- Revision:
- 1:d022bdae586b
- Parent:
- 0:790a9e0285d6
dosing rig
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Demonthorn | 0:790a9e0285d6 | 1 | //////////////////////////////////////////////////////////////// |
Demonthorn | 1:d022bdae586b | 2 | // Plant dosing Rig V1.0.06 // |
Demonthorn | 0:790a9e0285d6 | 3 | // Michael Steel // |
Demonthorn | 0:790a9e0285d6 | 4 | //////////////////////////////////////////////////////////////// |
Demonthorn | 0:790a9e0285d6 | 5 | |
Demonthorn | 0:790a9e0285d6 | 6 | // Libraries |
Demonthorn | 0:790a9e0285d6 | 7 | #include "mbed.h" |
Demonthorn | 0:790a9e0285d6 | 8 | #include "ConfigFile.h" |
Demonthorn | 0:790a9e0285d6 | 9 | // Define I/O and constants |
Demonthorn | 0:790a9e0285d6 | 10 | |
Demonthorn | 0:790a9e0285d6 | 11 | Serial pc(USBTX, USBRX); |
Demonthorn | 1:d022bdae586b | 12 | DigitalOut DOSE_PUMP(p5); |
Demonthorn | 1:d022bdae586b | 13 | DigitalOut BLED(p7); |
Demonthorn | 1:d022bdae586b | 14 | DigitalOut GLED(p8); |
Demonthorn | 1:d022bdae586b | 15 | DigitalOut RLED(p9); |
Demonthorn | 0:790a9e0285d6 | 16 | |
Demonthorn | 1:d022bdae586b | 17 | Timeout dosing; |
Demonthorn | 0:790a9e0285d6 | 18 | LocalFileSystem local("local"); |
Demonthorn | 0:790a9e0285d6 | 19 | ConfigFile cfg; |
Demonthorn | 1:d022bdae586b | 20 | float DOSEI; |
Demonthorn | 1:d022bdae586b | 21 | float DOSED; |
Demonthorn | 1:d022bdae586b | 22 | float LOG_INDEX; /*while not necesary with a single test subject |
Demonthorn | 1:d022bdae586b | 23 | this will allow for the proper selection of output message in the full version*/ |
Demonthorn | 0:790a9e0285d6 | 24 | int TCHECK; |
Demonthorn | 0:790a9e0285d6 | 25 | int YEAR; |
Demonthorn | 1:d022bdae586b | 26 | char buffer[21]; |
Demonthorn | 0:790a9e0285d6 | 27 | |
Demonthorn | 1:d022bdae586b | 28 | void DOSE(void) |
Demonthorn | 1:d022bdae586b | 29 | { |
Demonthorn | 1:d022bdae586b | 30 | DOSE_PUMP = 1; |
Demonthorn | 1:d022bdae586b | 31 | GLED = 1; |
Demonthorn | 1:d022bdae586b | 32 | time_t seconds = time(NULL); |
Demonthorn | 1:d022bdae586b | 33 | strftime(buffer, 21,"%X, %x\n", localtime(&seconds)); |
Demonthorn | 1:d022bdae586b | 34 | wait (DOSED); |
Demonthorn | 1:d022bdae586b | 35 | DOSE_PUMP = 0; |
Demonthorn | 1:d022bdae586b | 36 | GLED = 0; |
Demonthorn | 1:d022bdae586b | 37 | LOG_INDEX = 1; |
Demonthorn | 1:d022bdae586b | 38 | } |
Demonthorn | 0:790a9e0285d6 | 39 | |
Demonthorn | 0:790a9e0285d6 | 40 | int main() |
Demonthorn | 0:790a9e0285d6 | 41 | { |
Demonthorn | 1:d022bdae586b | 42 | |
Demonthorn | 0:790a9e0285d6 | 43 | // Determins Input file variables |
Demonthorn | 1:d022bdae586b | 44 | char *key1 = "DOSEI"; |
Demonthorn | 1:d022bdae586b | 45 | char *key2 = "DOSED"; |
Demonthorn | 0:790a9e0285d6 | 46 | char *key5 = "YEAR"; |
Demonthorn | 0:790a9e0285d6 | 47 | char value[BUFSIZ]; |
Demonthorn | 0:790a9e0285d6 | 48 | |
Demonthorn | 0:790a9e0285d6 | 49 | // Read the configuration file from the mbed |
Demonthorn | 0:790a9e0285d6 | 50 | cfg.read("/local/input.cfg"); |
Demonthorn | 0:790a9e0285d6 | 51 | if (cfg.getValue(key5, &value[0], sizeof(value))) { |
Demonthorn | 0:790a9e0285d6 | 52 | YEAR = atoi(value) ; |
Demonthorn | 0:790a9e0285d6 | 53 | } |
Demonthorn | 0:790a9e0285d6 | 54 | pc.printf("\r\n Plant Research Rig V1.0 \r\n Resart occured loading variables \r\n"); |
Demonthorn | 0:790a9e0285d6 | 55 | // Check correct time and get the current time from the terminal if needed |
Demonthorn | 0:790a9e0285d6 | 56 | struct tm t; |
Demonthorn | 0:790a9e0285d6 | 57 | time_t seconds = time(NULL); |
Demonthorn | 0:790a9e0285d6 | 58 | strftime(buffer, 21, "%Y\n", localtime(&seconds)); |
Demonthorn | 0:790a9e0285d6 | 59 | TCHECK = atoi(buffer) ; |
Demonthorn | 0:790a9e0285d6 | 60 | if (TCHECK != YEAR ) { |
Demonthorn | 0:790a9e0285d6 | 61 | printf("Enter current date and time:\n"); |
Demonthorn | 0:790a9e0285d6 | 62 | printf("YYYY MM DD HH MM SS[enter]\n"); |
Demonthorn | 0:790a9e0285d6 | 63 | scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday |
Demonthorn | 0:790a9e0285d6 | 64 | , &t.tm_hour, &t.tm_min, &t.tm_sec); |
Demonthorn | 0:790a9e0285d6 | 65 | |
Demonthorn | 0:790a9e0285d6 | 66 | // adjust for tm structure required values |
Demonthorn | 0:790a9e0285d6 | 67 | t.tm_year = t.tm_year - 1900; |
Demonthorn | 0:790a9e0285d6 | 68 | t.tm_mon = t.tm_mon - 1; |
Demonthorn | 0:790a9e0285d6 | 69 | |
Demonthorn | 0:790a9e0285d6 | 70 | // set the time |
Demonthorn | 0:790a9e0285d6 | 71 | set_time(mktime(&t)); |
Demonthorn | 0:790a9e0285d6 | 72 | |
Demonthorn | 0:790a9e0285d6 | 73 | // display the time |
Demonthorn | 0:790a9e0285d6 | 74 | time_t seconds = time(NULL); |
Demonthorn | 1:d022bdae586b | 75 | |
Demonthorn | 0:790a9e0285d6 | 76 | } |
Demonthorn | 0:790a9e0285d6 | 77 | wait(1); |
Demonthorn | 0:790a9e0285d6 | 78 | if (cfg.getValue(key1, &value[0], sizeof(value))) { |
Demonthorn | 0:790a9e0285d6 | 79 | pc.printf("'%s'='%s'\r\n", key1, value); |
Demonthorn | 1:d022bdae586b | 80 | DOSEI = atof(value) ; |
Demonthorn | 0:790a9e0285d6 | 81 | } else { |
Demonthorn | 0:790a9e0285d6 | 82 | pc.printf("Failure to get a configuration. Please check config file"); |
Demonthorn | 0:790a9e0285d6 | 83 | } |
Demonthorn | 0:790a9e0285d6 | 84 | if (cfg.getValue(key2, &value[0], sizeof(value))) { |
Demonthorn | 0:790a9e0285d6 | 85 | pc.printf("'%s'='%s'\r\n", key2, value); |
Demonthorn | 1:d022bdae586b | 86 | DOSED = atof(value) ; |
Demonthorn | 0:790a9e0285d6 | 87 | } |
Demonthorn | 1:d022bdae586b | 88 | Ticker dosing; |
Demonthorn | 1:d022bdae586b | 89 | dosing.attach(&DOSE, DOSEI); // setup dosing to call DOSE after DOSEI "dose interval" seconds |
Demonthorn | 1:d022bdae586b | 90 | |
Demonthorn | 0:790a9e0285d6 | 91 | pc.printf("Begin logging \r\n"); |
Demonthorn | 0:790a9e0285d6 | 92 | while(1) { |
Demonthorn | 0:790a9e0285d6 | 93 | |
Demonthorn | 1:d022bdae586b | 94 | if (LOG_INDEX == 1) { |
Demonthorn | 1:d022bdae586b | 95 | FILE *fp = fopen("/local/out.txt", "a"); // Open "out.txt" on the local file system for writing |
Demonthorn | 1:d022bdae586b | 96 | fprintf(fp, "\r\n %f s dose given at %s",DOSED ,buffer); |
Demonthorn | 1:d022bdae586b | 97 | fclose(fp); |
Demonthorn | 1:d022bdae586b | 98 | pc.printf("\r\n %f s dose given to test subject 1 at %s",DOSED ,buffer); |
Demonthorn | 1:d022bdae586b | 99 | LOG_INDEX = 0; |
Demonthorn | 1:d022bdae586b | 100 | } |
Demonthorn | 1:d022bdae586b | 101 | /*if (LOG_INDEX == 2){ |
Demonthorn | 1:d022bdae586b | 102 | FILE *fp = fopen("/local/out.txt", "a"); // Open "out.txt" on the local file system for writing |
Demonthorn | 1:d022bdae586b | 103 | fprintf(fp, "\r\n T: %f s dose given at %s,",DOSEI,buffer); |
Demonthorn | 1:d022bdae586b | 104 | fclose(fp); |
Demonthorn | 1:d022bdae586b | 105 | pc.printf("\r\n T: %f s dose given to test subject 2 at %s,",DOSEI,buffer); |
Demonthorn | 1:d022bdae586b | 106 | LOG_INDEX = 0; |
Demonthorn | 1:d022bdae586b | 107 | } |
Demonthorn | 1:d022bdae586b | 108 | // this is then repeated for each test subject with sequential numbers */ |
Demonthorn | 0:790a9e0285d6 | 109 | |
Demonthorn | 1:d022bdae586b | 110 | wait(1); |
Demonthorn | 1:d022bdae586b | 111 | |
Demonthorn | 1:d022bdae586b | 112 | |
Demonthorn | 0:790a9e0285d6 | 113 | } |
Demonthorn | 0:790a9e0285d6 | 114 | } |