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@36:8aeb014bd651, 2016-06-14 (annotated)
- Committer:
- sbouber1
- Date:
- Tue Jun 14 15:54:12 2016 +0000
- Revision:
- 36:8aeb014bd651
- Parent:
- 35:c9261391a995
- Child:
- 38:930469a33001
heater test
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 | 35:c9261391a995 | 3 | |
sbouber1 | 35:c9261391a995 | 4 | DigitalOut heater(p18); |
sbouber1 | 28:f4a4ee58d57e | 5 | |
sbouber1 | 32:1e4919a44196 | 6 | DRV8825 mtr_fresh(p21, p27, p28, p29, p22, p23); |
sbouber1 | 32:1e4919a44196 | 7 | DRV8825 mtr_salt(p24, p27, p28, p29, p25, p26); |
sbouber1 | 32:1e4919a44196 | 8 | |
sbouber1 | 32:1e4919a44196 | 9 | |
sbouber1 | 28:f4a4ee58d57e | 10 | // This is called in the main loop on every iteration |
sbouber1 | 28:f4a4ee58d57e | 11 | void PIDController::update() { |
sbouber1 | 35:c9261391a995 | 12 | |
sbouber1 | 35:c9261391a995 | 13 | // Control the heater |
sbouber1 | 35:c9261391a995 | 14 | // This could be done in the pumping function as well, if needed |
sbouber1 | 36:8aeb014bd651 | 15 | this->set_heating(this->temp->getValue() < 32.0f); |
sbouber1 | 35:c9261391a995 | 16 | |
sbouber1 | 28:f4a4ee58d57e | 17 | float s = this->salt->getValue(); |
sbouber1 | 32:1e4919a44196 | 18 | float sInGrams = this->getSaltInGrams(); |
sbouber1 | 30:cf12566013a5 | 19 | |
sbouber1 | 32:1e4919a44196 | 20 | if(s <= 6.0) { |
sbouber1 | 30:cf12566013a5 | 21 | |
sbouber1 | 32:1e4919a44196 | 22 | float ml = this->getMlSaltyWater(sInGrams, this->proximity->getValue()); |
sbouber1 | 35:c9261391a995 | 23 | int ml_int = static_cast<int>(ml); |
sbouber1 | 30:cf12566013a5 | 24 | |
sbouber1 | 35:c9261391a995 | 25 | printf("PIDCONTROLLER: need to pump %d (%.3f) ml of salty water\r\n", ml_int, ml); |
sbouber1 | 30:cf12566013a5 | 26 | |
sbouber1 | 35:c9261391a995 | 27 | //this->pump_salt_water(ml_int); |
sbouber1 | 30:cf12566013a5 | 28 | |
sbouber1 | 32:1e4919a44196 | 29 | } else if(s >= 9.0) { |
sbouber1 | 32:1e4919a44196 | 30 | |
sbouber1 | 32:1e4919a44196 | 31 | float ml = this->getMlFreshWater(s, this->proximity->getValue()); |
sbouber1 | 35:c9261391a995 | 32 | int ml_int = static_cast<int>(ml); |
sbouber1 | 32:1e4919a44196 | 33 | |
sbouber1 | 35:c9261391a995 | 34 | printf("PIDCONTROLLER: need to pump %d (%.3f) ml of fresh water\r\n", ml_int, ml); |
sbouber1 | 35:c9261391a995 | 35 | |
sbouber1 | 35:c9261391a995 | 36 | //this->pump_fresh_water(ml_int); |
sbouber1 | 32:1e4919a44196 | 37 | |
sbouber1 | 30:cf12566013a5 | 38 | } |
sbouber1 | 28:f4a4ee58d57e | 39 | } |
sbouber1 | 28:f4a4ee58d57e | 40 | |
sbouber1 | 28:f4a4ee58d57e | 41 | std::string PIDController::get_name() { |
sbouber1 | 30:cf12566013a5 | 42 | return "PIDController"; |
sbouber1 | 28:f4a4ee58d57e | 43 | } |
sbouber1 | 28:f4a4ee58d57e | 44 | |
joran | 29:2b256a7ce0ae | 45 | //get the salt in the body in grams |
joran | 29:2b256a7ce0ae | 46 | float PIDController::getSaltInGrams() |
joran | 29:2b256a7ce0ae | 47 | { |
joran | 29:2b256a7ce0ae | 48 | float currentppt = this->salt->getValue(); //in ppt |
joran | 29:2b256a7ce0ae | 49 | float currentvol = this->proximity->getValue(); //in ml |
joran | 29:2b256a7ce0ae | 50 | |
sbouber1 | 35:c9261391a995 | 51 | return currentppt * (currentvol / 1000); |
joran | 29:2b256a7ce0ae | 52 | } |
joran | 29:2b256a7ce0ae | 53 | |
joran | 29:2b256a7ce0ae | 54 | |
sbouber1 | 32:1e4919a44196 | 55 | void PIDController::pump_salt_water(int ml) { |
sbouber1 | 32:1e4919a44196 | 56 | this->pump_water(&mtr_salt, ml); |
sbouber1 | 32:1e4919a44196 | 57 | } |
sbouber1 | 32:1e4919a44196 | 58 | |
sbouber1 | 32:1e4919a44196 | 59 | |
sbouber1 | 32:1e4919a44196 | 60 | void PIDController::pump_fresh_water(int ml) { |
sbouber1 | 32:1e4919a44196 | 61 | this->pump_water(&mtr_fresh, ml); |
sbouber1 | 32:1e4919a44196 | 62 | } |
sbouber1 | 32:1e4919a44196 | 63 | |
sbouber1 | 32:1e4919a44196 | 64 | void PIDController::pump_water(DRV8825 *mtr, int ml) { |
sbouber1 | 35:c9261391a995 | 65 | |
sbouber1 | 32:1e4919a44196 | 66 | int j = 5010 * (ml - 1); |
sbouber1 | 32:1e4919a44196 | 67 | |
sbouber1 | 32:1e4919a44196 | 68 | for (int i = 500; i < MAX_SPEED; i += 5) { |
sbouber1 | 32:1e4919a44196 | 69 | mtr->settings(1 / MICROSTEPS_PER_STEP, LEFT, i); |
sbouber1 | 32:1e4919a44196 | 70 | } |
sbouber1 | 32:1e4919a44196 | 71 | |
sbouber1 | 32:1e4919a44196 | 72 | for (int i = 0; i < 2010 + j; i += 1) { |
sbouber1 | 32:1e4919a44196 | 73 | mtr->settings(1 / MICROSTEPS_PER_STEP, LEFT, 8000); |
sbouber1 | 32:1e4919a44196 | 74 | } |
sbouber1 | 32:1e4919a44196 | 75 | |
sbouber1 | 32:1e4919a44196 | 76 | for (int i = 8000; i > 500; i -= 5) { |
sbouber1 | 32:1e4919a44196 | 77 | mtr->settings(1 / MICROSTEPS_PER_STEP, LEFT, i); |
sbouber1 | 32:1e4919a44196 | 78 | } |
sbouber1 | 32:1e4919a44196 | 79 | |
sbouber1 | 32:1e4919a44196 | 80 | wait(3); |
sbouber1 | 32:1e4919a44196 | 81 | |
sbouber1 | 32:1e4919a44196 | 82 | for (int i = 500; i < MAX_SPEED; i += 5) { |
sbouber1 | 32:1e4919a44196 | 83 | mtr->settings(1 / MICROSTEPS_PER_STEP, RIGHT, i); |
sbouber1 | 32:1e4919a44196 | 84 | } |
sbouber1 | 32:1e4919a44196 | 85 | |
sbouber1 | 32:1e4919a44196 | 86 | for (int i = 0; i < 2010 + j; i += 1) { |
sbouber1 | 32:1e4919a44196 | 87 | mtr->settings(1 / MICROSTEPS_PER_STEP, RIGHT, 8000); |
sbouber1 | 32:1e4919a44196 | 88 | } |
sbouber1 | 32:1e4919a44196 | 89 | |
sbouber1 | 32:1e4919a44196 | 90 | for (int i = 8000; i > 500; i -= 5) { |
sbouber1 | 32:1e4919a44196 | 91 | mtr->settings(1 / MICROSTEPS_PER_STEP, RIGHT, i); |
sbouber1 | 32:1e4919a44196 | 92 | } |
sbouber1 | 32:1e4919a44196 | 93 | |
sbouber1 | 35:c9261391a995 | 94 | wait(3); |
sbouber1 | 32:1e4919a44196 | 95 | } |
sbouber1 | 32:1e4919a44196 | 96 | |
sbouber1 | 32:1e4919a44196 | 97 | |
sbouber1 | 32:1e4919a44196 | 98 | float PIDController::getMlSaltyWater(float gramsinbody, float volumeinbody) { |
sbouber1 | 32:1e4919a44196 | 99 | |
sbouber1 | 32:1e4919a44196 | 100 | float solvolume = 10; // 10ml |
sbouber1 | 32:1e4919a44196 | 101 | |
sbouber1 | 32:1e4919a44196 | 102 | float idealpptconstant = 0.007f; //7 ppt / 1000 |
sbouber1 | 32:1e4919a44196 | 103 | float x1 = volumeinbody * idealpptconstant; |
sbouber1 | 32:1e4919a44196 | 104 | float x2 = solvolume * idealpptconstant; |
sbouber1 | 32:1e4919a44196 | 105 | |
sbouber1 | 32:1e4919a44196 | 106 | x1 = x1 - gramsinbody; |
sbouber1 | 32:1e4919a44196 | 107 | x2 = 1 - x2; |
sbouber1 | 32:1e4919a44196 | 108 | |
sbouber1 | 32:1e4919a44196 | 109 | float outputml = (x1 / x2 * solvolume); |
sbouber1 | 32:1e4919a44196 | 110 | |
sbouber1 | 32:1e4919a44196 | 111 | return outputml; // amount in ml to get 7 ppt. |
sbouber1 | 35:c9261391a995 | 112 | } |
sbouber1 | 35:c9261391a995 | 113 | |
sbouber1 | 35:c9261391a995 | 114 | bool PIDController::is_heating() { |
sbouber1 | 35:c9261391a995 | 115 | return this->heating; |
sbouber1 | 35:c9261391a995 | 116 | } |
sbouber1 | 35:c9261391a995 | 117 | |
sbouber1 | 35:c9261391a995 | 118 | void PIDController::set_heating(bool enabled) { |
sbouber1 | 35:c9261391a995 | 119 | if(enabled == this->heating) return; |
sbouber1 | 35:c9261391a995 | 120 | |
sbouber1 | 35:c9261391a995 | 121 | this->heating = enabled; |
sbouber1 | 35:c9261391a995 | 122 | if(enabled) { |
sbouber1 | 35:c9261391a995 | 123 | #ifdef RUN_TESTS |
sbouber1 | 35:c9261391a995 | 124 | printf("Should set heater to 1\r\n"); |
sbouber1 | 35:c9261391a995 | 125 | #else |
sbouber1 | 35:c9261391a995 | 126 | heater = 1; |
sbouber1 | 35:c9261391a995 | 127 | #endif |
sbouber1 | 35:c9261391a995 | 128 | } else { |
sbouber1 | 35:c9261391a995 | 129 | #ifdef RUN_TESTS |
sbouber1 | 35:c9261391a995 | 130 | printf("Should set heater to 0\r\n"); |
sbouber1 | 35:c9261391a995 | 131 | #else |
sbouber1 | 35:c9261391a995 | 132 | heater = 0; |
sbouber1 | 35:c9261391a995 | 133 | #endif |
sbouber1 | 32:1e4919a44196 | 134 | } |
sbouber1 | 35:c9261391a995 | 135 | } |
sbouber1 | 32:1e4919a44196 | 136 | |
sbouber1 | 32:1e4919a44196 | 137 | float PIDController::getMlFreshWater(float ppt, float volume) { |
sbouber1 | 32:1e4919a44196 | 138 | return (volume * (ppt / 7.0)) - volume; |
sbouber1 | 32:1e4919a44196 | 139 | } |
sbouber1 | 32:1e4919a44196 | 140 |