Program for the water play project for the course Software Testing Practical 2016 given at the VU University
Dependencies: mbed DRV88255 TextLCD Ping mbed-rtos
PIDController.cpp@47:a73b8640fb60, 2016-06-15 (annotated)
- Committer:
- joran
- Date:
- Wed Jun 15 11:31:18 2016 +0000
- Revision:
- 47:a73b8640fb60
- Parent:
- 46:7e4c1f2ab76c
- Child:
- 49:ca6fb19fc280
Natalia's oplossing gebruiken;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sbouber1 | 28:f4a4ee58d57e | 1 | #include "PIDController.h" |
sbouber1 | 35:c9261391a995 | 2 | #include "testing.h" |
sbouber1 | 46:7e4c1f2ab76c | 3 | #include "LCDController.h" |
sbouber1 | 35:c9261391a995 | 4 | |
sbouber1 | 35:c9261391a995 | 5 | DigitalOut heater(p18); |
sbouber1 | 28:f4a4ee58d57e | 6 | |
sbouber1 | 32:1e4919a44196 | 7 | DRV8825 mtr_fresh(p21, p27, p28, p29, p22, p23); |
sbouber1 | 32:1e4919a44196 | 8 | DRV8825 mtr_salt(p24, p27, p28, p29, p25, p26); |
sbouber1 | 32:1e4919a44196 | 9 | |
sbouber1 | 32:1e4919a44196 | 10 | |
joran | 44:7c932cc5991b | 11 | |
joran | 44:7c932cc5991b | 12 | |
sbouber1 | 28:f4a4ee58d57e | 13 | // This is called in the main loop on every iteration |
sbouber1 | 28:f4a4ee58d57e | 14 | void PIDController::update() { |
sbouber1 | 35:c9261391a995 | 15 | |
sbouber1 | 38:930469a33001 | 16 | #ifndef RUN_TESTS |
sbouber1 | 38:930469a33001 | 17 | if(this->num_iters < STARTUP_ITERATIONS) { |
sbouber1 | 38:930469a33001 | 18 | printf("PIDController: not running, startup phase"); |
sbouber1 | 38:930469a33001 | 19 | return; |
sbouber1 | 38:930469a33001 | 20 | } |
sbouber1 | 38:930469a33001 | 21 | #endif |
sbouber1 | 38:930469a33001 | 22 | |
sbouber1 | 35:c9261391a995 | 23 | // Control the heater |
sbouber1 | 35:c9261391a995 | 24 | // This could be done in the pumping function as well, if needed |
sbouber1 | 36:8aeb014bd651 | 25 | this->set_heating(this->temp->getValue() < 32.0f); |
sbouber1 | 35:c9261391a995 | 26 | |
sbouber1 | 28:f4a4ee58d57e | 27 | float s = this->salt->getValue(); |
sbouber1 | 32:1e4919a44196 | 28 | float sInGrams = this->getSaltInGrams(); |
sbouber1 | 30:cf12566013a5 | 29 | |
sbouber1 | 32:1e4919a44196 | 30 | if(s <= 6.0) { |
sbouber1 | 30:cf12566013a5 | 31 | |
sbouber1 | 32:1e4919a44196 | 32 | float ml = this->getMlSaltyWater(sInGrams, this->proximity->getValue()); |
sbouber1 | 35:c9261391a995 | 33 | int ml_int = static_cast<int>(ml); |
sbouber1 | 30:cf12566013a5 | 34 | |
sbouber1 | 35:c9261391a995 | 35 | printf("PIDCONTROLLER: need to pump %d (%.3f) ml of salty water\r\n", ml_int, ml); |
sbouber1 | 30:cf12566013a5 | 36 | |
sbouber1 | 35:c9261391a995 | 37 | //this->pump_salt_water(ml_int); |
sbouber1 | 30:cf12566013a5 | 38 | |
sbouber1 | 32:1e4919a44196 | 39 | } else if(s >= 9.0) { |
sbouber1 | 32:1e4919a44196 | 40 | |
sbouber1 | 32:1e4919a44196 | 41 | float ml = this->getMlFreshWater(s, this->proximity->getValue()); |
sbouber1 | 35:c9261391a995 | 42 | int ml_int = static_cast<int>(ml); |
sbouber1 | 32:1e4919a44196 | 43 | |
sbouber1 | 35:c9261391a995 | 44 | printf("PIDCONTROLLER: need to pump %d (%.3f) ml of fresh water\r\n", ml_int, ml); |
sbouber1 | 35:c9261391a995 | 45 | |
sbouber1 | 35:c9261391a995 | 46 | //this->pump_fresh_water(ml_int); |
sbouber1 | 32:1e4919a44196 | 47 | |
sbouber1 | 30:cf12566013a5 | 48 | } |
sbouber1 | 28:f4a4ee58d57e | 49 | } |
sbouber1 | 28:f4a4ee58d57e | 50 | |
sbouber1 | 28:f4a4ee58d57e | 51 | std::string PIDController::get_name() { |
sbouber1 | 30:cf12566013a5 | 52 | return "PIDController"; |
sbouber1 | 28:f4a4ee58d57e | 53 | } |
sbouber1 | 28:f4a4ee58d57e | 54 | |
joran | 29:2b256a7ce0ae | 55 | //get the salt in the body in grams |
joran | 29:2b256a7ce0ae | 56 | float PIDController::getSaltInGrams() |
joran | 29:2b256a7ce0ae | 57 | { |
joran | 29:2b256a7ce0ae | 58 | float currentppt = this->salt->getValue(); //in ppt |
joran | 29:2b256a7ce0ae | 59 | float currentvol = this->proximity->getValue(); //in ml |
joran | 29:2b256a7ce0ae | 60 | |
sbouber1 | 35:c9261391a995 | 61 | return currentppt * (currentvol / 1000); |
joran | 29:2b256a7ce0ae | 62 | } |
joran | 29:2b256a7ce0ae | 63 | |
joran | 29:2b256a7ce0ae | 64 | |
sbouber1 | 32:1e4919a44196 | 65 | void PIDController::pump_salt_water(int ml) { |
sbouber1 | 32:1e4919a44196 | 66 | this->pump_water(&mtr_salt, ml); |
sbouber1 | 32:1e4919a44196 | 67 | } |
sbouber1 | 32:1e4919a44196 | 68 | |
sbouber1 | 32:1e4919a44196 | 69 | |
sbouber1 | 32:1e4919a44196 | 70 | void PIDController::pump_fresh_water(int ml) { |
sbouber1 | 32:1e4919a44196 | 71 | this->pump_water(&mtr_fresh, ml); |
sbouber1 | 32:1e4919a44196 | 72 | } |
sbouber1 | 32:1e4919a44196 | 73 | |
sbouber1 | 32:1e4919a44196 | 74 | void PIDController::pump_water(DRV8825 *mtr, int ml) { |
joran | 44:7c932cc5991b | 75 | LCDController::showPumping(); |
joran | 40:1668630544c7 | 76 | this->pumping = true; |
sbouber1 | 32:1e4919a44196 | 77 | int j = 5010 * (ml - 1); |
sbouber1 | 32:1e4919a44196 | 78 | |
sbouber1 | 32:1e4919a44196 | 79 | for (int i = 500; i < MAX_SPEED; i += 5) { |
sbouber1 | 46:7e4c1f2ab76c | 80 | mtr->settings(1 / MICROSTEPS_PER_STEP, RIGHT, i); |
sbouber1 | 46:7e4c1f2ab76c | 81 | } |
sbouber1 | 46:7e4c1f2ab76c | 82 | |
sbouber1 | 46:7e4c1f2ab76c | 83 | for (int i = 0; i < 2010 + j; i += 1) { |
sbouber1 | 46:7e4c1f2ab76c | 84 | mtr->settings(1 / MICROSTEPS_PER_STEP, RIGHT, 8000); |
sbouber1 | 46:7e4c1f2ab76c | 85 | } |
sbouber1 | 46:7e4c1f2ab76c | 86 | |
sbouber1 | 46:7e4c1f2ab76c | 87 | for (int i = 8000; i > 500; i -= 5) { |
sbouber1 | 46:7e4c1f2ab76c | 88 | mtr->settings(1 / MICROSTEPS_PER_STEP, RIGHT, i); |
sbouber1 | 46:7e4c1f2ab76c | 89 | } |
sbouber1 | 46:7e4c1f2ab76c | 90 | |
sbouber1 | 46:7e4c1f2ab76c | 91 | wait(5); |
sbouber1 | 46:7e4c1f2ab76c | 92 | |
sbouber1 | 46:7e4c1f2ab76c | 93 | for (int i = 500; i < MAX_SPEED; i += 5) { |
sbouber1 | 32:1e4919a44196 | 94 | mtr->settings(1 / MICROSTEPS_PER_STEP, LEFT, i); |
sbouber1 | 32:1e4919a44196 | 95 | } |
sbouber1 | 32:1e4919a44196 | 96 | |
sbouber1 | 32:1e4919a44196 | 97 | for (int i = 0; i < 2010 + j; i += 1) { |
sbouber1 | 32:1e4919a44196 | 98 | mtr->settings(1 / MICROSTEPS_PER_STEP, LEFT, 8000); |
sbouber1 | 32:1e4919a44196 | 99 | } |
sbouber1 | 32:1e4919a44196 | 100 | |
sbouber1 | 32:1e4919a44196 | 101 | for (int i = 8000; i > 500; i -= 5) { |
sbouber1 | 32:1e4919a44196 | 102 | mtr->settings(1 / MICROSTEPS_PER_STEP, LEFT, i); |
sbouber1 | 32:1e4919a44196 | 103 | } |
sbouber1 | 32:1e4919a44196 | 104 | |
sbouber1 | 46:7e4c1f2ab76c | 105 | wait(5); |
joran | 40:1668630544c7 | 106 | this->pumping = false; |
sbouber1 | 32:1e4919a44196 | 107 | } |
sbouber1 | 32:1e4919a44196 | 108 | |
sbouber1 | 32:1e4919a44196 | 109 | |
sbouber1 | 32:1e4919a44196 | 110 | float PIDController::getMlSaltyWater(float gramsinbody, float volumeinbody) { |
sbouber1 | 32:1e4919a44196 | 111 | |
joran | 47:a73b8640fb60 | 112 | float solutiongramperten = 1.16f; // 1.0f for our solution |
sbouber1 | 32:1e4919a44196 | 113 | float solvolume = 10; // 10ml |
sbouber1 | 32:1e4919a44196 | 114 | |
sbouber1 | 32:1e4919a44196 | 115 | float idealpptconstant = 0.007f; //7 ppt / 1000 |
sbouber1 | 32:1e4919a44196 | 116 | float x1 = volumeinbody * idealpptconstant; |
sbouber1 | 32:1e4919a44196 | 117 | float x2 = solvolume * idealpptconstant; |
sbouber1 | 32:1e4919a44196 | 118 | |
sbouber1 | 32:1e4919a44196 | 119 | x1 = x1 - gramsinbody; |
joran | 47:a73b8640fb60 | 120 | x2 = solutiongramperten - x2; |
sbouber1 | 32:1e4919a44196 | 121 | |
sbouber1 | 32:1e4919a44196 | 122 | float outputml = (x1 / x2 * solvolume); |
sbouber1 | 32:1e4919a44196 | 123 | |
sbouber1 | 32:1e4919a44196 | 124 | return outputml; // amount in ml to get 7 ppt. |
sbouber1 | 35:c9261391a995 | 125 | } |
sbouber1 | 35:c9261391a995 | 126 | |
sbouber1 | 35:c9261391a995 | 127 | bool PIDController::is_heating() { |
sbouber1 | 35:c9261391a995 | 128 | return this->heating; |
sbouber1 | 35:c9261391a995 | 129 | } |
sbouber1 | 35:c9261391a995 | 130 | |
joran | 40:1668630544c7 | 131 | bool PIDController::is_pumping() { |
joran | 40:1668630544c7 | 132 | return this->pumping; |
joran | 40:1668630544c7 | 133 | } |
joran | 40:1668630544c7 | 134 | |
sbouber1 | 35:c9261391a995 | 135 | void PIDController::set_heating(bool enabled) { |
sbouber1 | 35:c9261391a995 | 136 | if(enabled == this->heating) return; |
sbouber1 | 35:c9261391a995 | 137 | |
sbouber1 | 35:c9261391a995 | 138 | this->heating = enabled; |
sbouber1 | 35:c9261391a995 | 139 | if(enabled) { |
sbouber1 | 35:c9261391a995 | 140 | #ifdef RUN_TESTS |
sbouber1 | 35:c9261391a995 | 141 | printf("Should set heater to 1\r\n"); |
sbouber1 | 35:c9261391a995 | 142 | #else |
sbouber1 | 35:c9261391a995 | 143 | heater = 1; |
sbouber1 | 35:c9261391a995 | 144 | #endif |
sbouber1 | 35:c9261391a995 | 145 | } else { |
sbouber1 | 35:c9261391a995 | 146 | #ifdef RUN_TESTS |
sbouber1 | 35:c9261391a995 | 147 | printf("Should set heater to 0\r\n"); |
sbouber1 | 35:c9261391a995 | 148 | #else |
sbouber1 | 35:c9261391a995 | 149 | heater = 0; |
sbouber1 | 35:c9261391a995 | 150 | #endif |
sbouber1 | 32:1e4919a44196 | 151 | } |
sbouber1 | 35:c9261391a995 | 152 | } |
sbouber1 | 32:1e4919a44196 | 153 | |
sbouber1 | 32:1e4919a44196 | 154 | float PIDController::getMlFreshWater(float ppt, float volume) { |
sbouber1 | 32:1e4919a44196 | 155 | return (volume * (ppt / 7.0)) - volume; |
sbouber1 | 32:1e4919a44196 | 156 | } |
sbouber1 | 32:1e4919a44196 | 157 | |
sbouber1 | 46:7e4c1f2ab76c | 158 | void PIDController::doTestingStuff(int ml) { |
sbouber1 | 46:7e4c1f2ab76c | 159 | while(1) |
sbouber1 | 46:7e4c1f2ab76c | 160 | this->pump_salt_water(ml); |
sbouber1 | 46:7e4c1f2ab76c | 161 | } |
sbouber1 | 46:7e4c1f2ab76c | 162 |