final
Dependencies: mbed mbed-rtos SDFileSystem TB6612FNG
main.cpp@0:3356e3f538d9, 2019-12-05 (annotated)
- Committer:
- avadivel3
- Date:
- Thu Dec 05 22:04:22 2019 +0000
- Revision:
- 0:3356e3f538d9
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
avadivel3 | 0:3356e3f538d9 | 1 | #include "mbed.h" |
avadivel3 | 0:3356e3f538d9 | 2 | #include "TB6612FNG.h" |
avadivel3 | 0:3356e3f538d9 | 3 | #include "SDFileSystem.h" |
avadivel3 | 0:3356e3f538d9 | 4 | #include "rtos.h" |
avadivel3 | 0:3356e3f538d9 | 5 | |
avadivel3 | 0:3356e3f538d9 | 6 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
avadivel3 | 0:3356e3f538d9 | 7 | Thread thread; |
avadivel3 | 0:3356e3f538d9 | 8 | // -------------------taken pins-------------------------------- |
avadivel3 | 0:3356e3f538d9 | 9 | //5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,_,27,28,29,30 |
avadivel3 | 0:3356e3f538d9 | 10 | // ------------------------------------------------------------- |
avadivel3 | 0:3356e3f538d9 | 11 | |
avadivel3 | 0:3356e3f538d9 | 12 | //direction pins |
avadivel3 | 0:3356e3f538d9 | 13 | DigitalOut idir1(p9); |
avadivel3 | 0:3356e3f538d9 | 14 | DigitalOut idir2(p10); |
avadivel3 | 0:3356e3f538d9 | 15 | DigitalOut i2_dir1(p27); |
avadivel3 | 0:3356e3f538d9 | 16 | DigitalOut i2_dir2(p28); |
avadivel3 | 0:3356e3f538d9 | 17 | DigitalOut i3_dir1(p30); |
avadivel3 | 0:3356e3f538d9 | 18 | DigitalOut i3_dir2(p17); |
avadivel3 | 0:3356e3f538d9 | 19 | |
avadivel3 | 0:3356e3f538d9 | 20 | //in Thread thread; |
avadivel3 | 0:3356e3f538d9 | 21 | DigitalOut wc_dir1(p29); //SOLDER |
avadivel3 | 0:3356e3f538d9 | 22 | DigitalOut wc_dir2(p20); //SOLDER |
avadivel3 | 0:3356e3f538d9 | 23 | |
avadivel3 | 0:3356e3f538d9 | 24 | DigitalOut wc1_dir1(p12); //SOLDER |
avadivel3 | 0:3356e3f538d9 | 25 | DigitalOut wc1_dir2(p13); //SOLDER |
avadivel3 | 0:3356e3f538d9 | 26 | |
avadivel3 | 0:3356e3f538d9 | 27 | //pushbutton to stop program execution |
avadivel3 | 0:3356e3f538d9 | 28 | DigitalIn pb(p29); |
avadivel3 | 0:3356e3f538d9 | 29 | |
avadivel3 | 0:3356e3f538d9 | 30 | //pwm out to motors |
avadivel3 | 0:3356e3f538d9 | 31 | PwmOut ipwm(p21); |
avadivel3 | 0:3356e3f538d9 | 32 | PwmOut i2_pwm(p22); |
avadivel3 | 0:3356e3f538d9 | 33 | PwmOut i3_pwm(p23); |
avadivel3 | 0:3356e3f538d9 | 34 | |
avadivel3 | 0:3356e3f538d9 | 35 | //in Thread thread; |
avadivel3 | 0:3356e3f538d9 | 36 | PwmOut wc_in_pwm(p24); |
avadivel3 | 0:3356e3f538d9 | 37 | PwmOut wc_out_pwm(p25); |
avadivel3 | 0:3356e3f538d9 | 38 | |
avadivel3 | 0:3356e3f538d9 | 39 | //standby pins |
avadivel3 | 0:3356e3f538d9 | 40 | DigitalOut istdby(p11); |
avadivel3 | 0:3356e3f538d9 | 41 | DigitalOut set2_istdby(p18); |
avadivel3 | 0:3356e3f538d9 | 42 | DigitalOut wc_istdby(p14); |
avadivel3 | 0:3356e3f538d9 | 43 | |
avadivel3 | 0:3356e3f538d9 | 44 | //water level sensors |
avadivel3 | 0:3356e3f538d9 | 45 | DigitalIn water_level_fw_reservoir(p15); |
avadivel3 | 0:3356e3f538d9 | 46 | DigitalIn water_level_display_tank(p16); |
avadivel3 | 0:3356e3f538d9 | 47 | DigitalIn salt_mix_reservoir(p19); |
avadivel3 | 0:3356e3f538d9 | 48 | |
avadivel3 | 0:3356e3f538d9 | 49 | //flow rate (L/ # of days) |
avadivel3 | 0:3356e3f538d9 | 50 | float flow_rate_L_num_days = 40; |
avadivel3 | 0:3356e3f538d9 | 51 | |
avadivel3 | 0:3356e3f538d9 | 52 | //timespan (# of days) |
avadivel3 | 0:3356e3f538d9 | 53 | int days = 1; |
avadivel3 | 0:3356e3f538d9 | 54 | int sec_days = days * 86400; |
avadivel3 | 0:3356e3f538d9 | 55 | |
avadivel3 | 0:3356e3f538d9 | 56 | //vol of 2s pump (L) |
avadivel3 | 0:3356e3f538d9 | 57 | float two_s_pump_volume_L = 0.05; |
avadivel3 | 0:3356e3f538d9 | 58 | |
avadivel3 | 0:3356e3f538d9 | 59 | //pc serial connection |
avadivel3 | 0:3356e3f538d9 | 60 | Serial pc(USBTX, USBRX); // tx, rx |
avadivel3 | 0:3356e3f538d9 | 61 | |
avadivel3 | 0:3356e3f538d9 | 62 | //motors in main thread |
avadivel3 | 0:3356e3f538d9 | 63 | TB6612FNG fw_reservoir_to_display_tank(p27, p28, p22, p11); |
avadivel3 | 0:3356e3f538d9 | 64 | TB6612FNG source_to_fw_reservoir(p9, p10, p21, p11); |
avadivel3 | 0:3356e3f538d9 | 65 | TB6612FNG fw_reservoir_to_salt_mix_reservoir(p30, p17, p23, p18); |
avadivel3 | 0:3356e3f538d9 | 66 | |
avadivel3 | 0:3356e3f538d9 | 67 | //motors in Thread thread; |
avadivel3 | 0:3356e3f538d9 | 68 | TB6612FNG water_change_in(p29, p20, p24, p18); |
avadivel3 | 0:3356e3f538d9 | 69 | TB6612FNG water_change_out(p12, p13, p25, p14); |
avadivel3 | 0:3356e3f538d9 | 70 | |
avadivel3 | 0:3356e3f538d9 | 71 | //Motor Pins |
avadivel3 | 0:3356e3f538d9 | 72 | //TB6612FNG motor(PinName idir1, PinName idir2, PinName ipwm, PinName istby); |
avadivel3 | 0:3356e3f538d9 | 73 | |
avadivel3 | 0:3356e3f538d9 | 74 | |
avadivel3 | 0:3356e3f538d9 | 75 | |
avadivel3 | 0:3356e3f538d9 | 76 | void water_change(){ |
avadivel3 | 0:3356e3f538d9 | 77 | |
avadivel3 | 0:3356e3f538d9 | 78 | time_t start = time(NULL); |
avadivel3 | 0:3356e3f538d9 | 79 | |
avadivel3 | 0:3356e3f538d9 | 80 | // float flow_rate_L_num_days = 40; |
avadivel3 | 0:3356e3f538d9 | 81 | // float two_s_pump_volume_L = 0.05; |
avadivel3 | 0:3356e3f538d9 | 82 | int pumps_per_num_days = flow_rate_L_num_days / two_s_pump_volume_L; |
avadivel3 | 0:3356e3f538d9 | 83 | int total_wait_time = sec_days - (pumps_per_num_days * 2); |
avadivel3 | 0:3356e3f538d9 | 84 | int wait_bw_pump = total_wait_time / pumps_per_num_days; |
avadivel3 | 0:3356e3f538d9 | 85 | |
avadivel3 | 0:3356e3f538d9 | 86 | pc.printf("delay bw pumps in seconds: %d\n", wait_bw_pump); |
avadivel3 | 0:3356e3f538d9 | 87 | |
avadivel3 | 0:3356e3f538d9 | 88 | source_to_fw_reservoir.start(); |
avadivel3 | 0:3356e3f538d9 | 89 | water_change_in.start(); |
avadivel3 | 0:3356e3f538d9 | 90 | water_change_out.start(); |
avadivel3 | 0:3356e3f538d9 | 91 | |
avadivel3 | 0:3356e3f538d9 | 92 | while(time(NULL) - start < sec_days) { |
avadivel3 | 0:3356e3f538d9 | 93 | |
avadivel3 | 0:3356e3f538d9 | 94 | water_change_in.fwd(1); |
avadivel3 | 0:3356e3f538d9 | 95 | water_change_out.fwd(1); |
avadivel3 | 0:3356e3f538d9 | 96 | |
avadivel3 | 0:3356e3f538d9 | 97 | wait(wait_bw_pump); |
avadivel3 | 0:3356e3f538d9 | 98 | |
avadivel3 | 0:3356e3f538d9 | 99 | } |
avadivel3 | 0:3356e3f538d9 | 100 | |
avadivel3 | 0:3356e3f538d9 | 101 | water_change_in.stop(); |
avadivel3 | 0:3356e3f538d9 | 102 | water_change_out.stop(); |
avadivel3 | 0:3356e3f538d9 | 103 | |
avadivel3 | 0:3356e3f538d9 | 104 | } |
avadivel3 | 0:3356e3f538d9 | 105 | |
avadivel3 | 0:3356e3f538d9 | 106 | |
avadivel3 | 0:3356e3f538d9 | 107 | int main() { |
avadivel3 | 0:3356e3f538d9 | 108 | |
avadivel3 | 0:3356e3f538d9 | 109 | thread.start(water_change); |
avadivel3 | 0:3356e3f538d9 | 110 | |
avadivel3 | 0:3356e3f538d9 | 111 | pb.mode(PullUp); |
avadivel3 | 0:3356e3f538d9 | 112 | int count = 0; |
avadivel3 | 0:3356e3f538d9 | 113 | int count2 = 0; |
avadivel3 | 0:3356e3f538d9 | 114 | int count3 = 0; |
avadivel3 | 0:3356e3f538d9 | 115 | |
avadivel3 | 0:3356e3f538d9 | 116 | mkdir("/sd/mydir", 0777); |
avadivel3 | 0:3356e3f538d9 | 117 | |
avadivel3 | 0:3356e3f538d9 | 118 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); |
avadivel3 | 0:3356e3f538d9 | 119 | if(fp == NULL) { |
avadivel3 | 0:3356e3f538d9 | 120 | error("Could not open file for write\n"); |
avadivel3 | 0:3356e3f538d9 | 121 | } |
avadivel3 | 0:3356e3f538d9 | 122 | |
avadivel3 | 0:3356e3f538d9 | 123 | time_t start = time(NULL); |
avadivel3 | 0:3356e3f538d9 | 124 | fprintf(fp, "Start Time: %d\n", start); |
avadivel3 | 0:3356e3f538d9 | 125 | |
avadivel3 | 0:3356e3f538d9 | 126 | |
avadivel3 | 0:3356e3f538d9 | 127 | while(pb != 0) { //push button not pressed. pb != 0 |
avadivel3 | 0:3356e3f538d9 | 128 | //pc.printf("%d \n", test); |
avadivel3 | 0:3356e3f538d9 | 129 | ////////////////////////////////////////////// |
avadivel3 | 0:3356e3f538d9 | 130 | int val = water_level_fw_reservoir.read(); |
avadivel3 | 0:3356e3f538d9 | 131 | |
avadivel3 | 0:3356e3f538d9 | 132 | source_to_fw_reservoir.start(); |
avadivel3 | 0:3356e3f538d9 | 133 | |
avadivel3 | 0:3356e3f538d9 | 134 | |
avadivel3 | 0:3356e3f538d9 | 135 | while(val == 1) { //air detected |
avadivel3 | 0:3356e3f538d9 | 136 | val = water_level_fw_reservoir.read(); |
avadivel3 | 0:3356e3f538d9 | 137 | source_to_fw_reservoir.fwd(1); |
avadivel3 | 0:3356e3f538d9 | 138 | fprintf(fp, "SOURCE TO FWR #%d\n", count); |
avadivel3 | 0:3356e3f538d9 | 139 | time_t program_seconds = time(NULL); |
avadivel3 | 0:3356e3f538d9 | 140 | fprintf(fp, "Latest measurement time: %d\n", program_seconds); |
avadivel3 | 0:3356e3f538d9 | 141 | wait(2); |
avadivel3 | 0:3356e3f538d9 | 142 | count++; |
avadivel3 | 0:3356e3f538d9 | 143 | } |
avadivel3 | 0:3356e3f538d9 | 144 | |
avadivel3 | 0:3356e3f538d9 | 145 | source_to_fw_reservoir.stop(); |
avadivel3 | 0:3356e3f538d9 | 146 | /////////////////////////////////////////////////////// |
avadivel3 | 0:3356e3f538d9 | 147 | int val2 = water_level_display_tank.read(); //read display tank water level |
avadivel3 | 0:3356e3f538d9 | 148 | |
avadivel3 | 0:3356e3f538d9 | 149 | |
avadivel3 | 0:3356e3f538d9 | 150 | |
avadivel3 | 0:3356e3f538d9 | 151 | fw_reservoir_to_display_tank.start(); //pump start |
avadivel3 | 0:3356e3f538d9 | 152 | |
avadivel3 | 0:3356e3f538d9 | 153 | |
avadivel3 | 0:3356e3f538d9 | 154 | if(val2 == 1) { //air detected (needs water) |
avadivel3 | 0:3356e3f538d9 | 155 | fprintf(fp, "FWR TO DSPLY TANK #%d\n", count2); |
avadivel3 | 0:3356e3f538d9 | 156 | time_t program_seconds = time(NULL); |
avadivel3 | 0:3356e3f538d9 | 157 | fprintf(fp, "Latest measurement time: %d\n", program_seconds); |
avadivel3 | 0:3356e3f538d9 | 158 | fw_reservoir_to_display_tank.fwd(1); //power pump forward |
avadivel3 | 0:3356e3f538d9 | 159 | count2++; |
avadivel3 | 0:3356e3f538d9 | 160 | wait(2); |
avadivel3 | 0:3356e3f538d9 | 161 | } |
avadivel3 | 0:3356e3f538d9 | 162 | |
avadivel3 | 0:3356e3f538d9 | 163 | fw_reservoir_to_display_tank.stop(); |
avadivel3 | 0:3356e3f538d9 | 164 | |
avadivel3 | 0:3356e3f538d9 | 165 | /////////////////////////////////////////////////////// |
avadivel3 | 0:3356e3f538d9 | 166 | int val3 = salt_mix_reservoir.read(); //read display tank water level |
avadivel3 | 0:3356e3f538d9 | 167 | |
avadivel3 | 0:3356e3f538d9 | 168 | |
avadivel3 | 0:3356e3f538d9 | 169 | |
avadivel3 | 0:3356e3f538d9 | 170 | fw_reservoir_to_salt_mix_reservoir.start(); //pump start |
avadivel3 | 0:3356e3f538d9 | 171 | |
avadivel3 | 0:3356e3f538d9 | 172 | |
avadivel3 | 0:3356e3f538d9 | 173 | if(val3 == 1) { //air detected (needs water) |
avadivel3 | 0:3356e3f538d9 | 174 | fprintf(fp, "FWR TO SLT MX RVR #%d\n", count3); |
avadivel3 | 0:3356e3f538d9 | 175 | time_t program_seconds = time(NULL); |
avadivel3 | 0:3356e3f538d9 | 176 | fprintf(fp, "Latest measurement time: %d\n", program_seconds); |
avadivel3 | 0:3356e3f538d9 | 177 | fw_reservoir_to_salt_mix_reservoir.fwd(1); //power pump forward |
avadivel3 | 0:3356e3f538d9 | 178 | count3++; |
avadivel3 | 0:3356e3f538d9 | 179 | wait(2); |
avadivel3 | 0:3356e3f538d9 | 180 | } |
avadivel3 | 0:3356e3f538d9 | 181 | |
avadivel3 | 0:3356e3f538d9 | 182 | fw_reservoir_to_salt_mix_reservoir.stop(); |
avadivel3 | 0:3356e3f538d9 | 183 | |
avadivel3 | 0:3356e3f538d9 | 184 | |
avadivel3 | 0:3356e3f538d9 | 185 | } |
avadivel3 | 0:3356e3f538d9 | 186 | |
avadivel3 | 0:3356e3f538d9 | 187 | fclose(fp); //close sd filesystem |
avadivel3 | 0:3356e3f538d9 | 188 | } |