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

Committer:
joran
Date:
Tue Jun 14 09:23:29 2016 +0000
Revision:
27:4f73f754fdc9
Parent:
20:521f795ea9d7
Child:
38:930469a33001
Alarm for overflow, with message and error=true raising

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sbouber1 11:1a0a8fd74bc0 1 #include "AlarmController.h"
joran 27:4f73f754fdc9 2 //timers
joran 20:521f795ea9d7 3 Timer temptimer;
joran 20:521f795ea9d7 4 Timer salttimer;
joran 27:4f73f754fdc9 5 Timer overflowtimer;
joran 20:521f795ea9d7 6
joran 27:4f73f754fdc9 7 //outputpins for alarm
joran 20:521f795ea9d7 8 DigitalOut buzzer(p17);
joran 20:521f795ea9d7 9 DigitalOut led1(LED1);
joran 20:521f795ea9d7 10 DigitalOut led2(LED2);
joran 20:521f795ea9d7 11 DigitalOut led3(LED3);
joran 20:521f795ea9d7 12 DigitalOut led4(LED4);
joran 20:521f795ea9d7 13
joran 27:4f73f754fdc9 14 //tresholds for temperature (in celcius)
joran 20:521f795ea9d7 15 float temp_value_min_crit = 30.0f;
joran 20:521f795ea9d7 16 float temp_value_min_undesired = 32.0f;
joran 20:521f795ea9d7 17 float temp_value_max_crit = 40.0f;
joran 20:521f795ea9d7 18 float temp_value_max_undesired = 38.0f;
joran 20:521f795ea9d7 19
joran 27:4f73f754fdc9 20 //tresholds for salinity (in ppt)
joran 20:521f795ea9d7 21 float salt_value_min_crit = 3.0f;
joran 20:521f795ea9d7 22 float salt_value_min_undesired = 6.0f;
joran 20:521f795ea9d7 23 float salt_value_max_crit = 12.0f;
joran 20:521f795ea9d7 24 float salt_value_max_undesired = 9.0f;
joran 20:521f795ea9d7 25
joran 27:4f73f754fdc9 26 //tresholds for overflow (in ml)
joran 27:4f73f754fdc9 27 float overflow_value_min_crit = 0.0f;
joran 27:4f73f754fdc9 28 float overflow_value_min_undesired = 100.0f;
joran 27:4f73f754fdc9 29 float overflow_value_max_crit = 1000.0f;
joran 27:4f73f754fdc9 30 float overflow_value_max_undesired = 900.0f;
joran 27:4f73f754fdc9 31
joran 20:521f795ea9d7 32 float readtemp;
joran 20:521f795ea9d7 33 float readsalt;
joran 27:4f73f754fdc9 34 float readoverflow;
joran 20:521f795ea9d7 35
sbouber1 11:1a0a8fd74bc0 36 void AlarmController::update() {
joran 20:521f795ea9d7 37 readtemp = this->temp->getValue();
joran 20:521f795ea9d7 38
joran 20:521f795ea9d7 39 if ((readtemp < temp_value_min_undesired) || (readtemp > temp_value_max_undesired))
joran 20:521f795ea9d7 40 {
joran 20:521f795ea9d7 41 if ((readtemp < temp_value_min_crit) || (readtemp > temp_value_max_crit))
joran 20:521f795ea9d7 42 {
joran 20:521f795ea9d7 43 this->raiseAlarmTemp(true);
joran 20:521f795ea9d7 44 } else {
joran 20:521f795ea9d7 45 this->raiseAlarmTemp(false);
joran 20:521f795ea9d7 46 }
joran 20:521f795ea9d7 47 } else {
joran 20:521f795ea9d7 48 //clear alarm
joran 20:521f795ea9d7 49 temptimer.stop();
joran 20:521f795ea9d7 50 this->tempiscrit = false;
joran 20:521f795ea9d7 51 }
joran 20:521f795ea9d7 52
sbouber1 11:1a0a8fd74bc0 53
sbouber1 11:1a0a8fd74bc0 54
joran 20:521f795ea9d7 55 readsalt = this->salt->getValue();
joran 20:521f795ea9d7 56
joran 20:521f795ea9d7 57 if ((readsalt < salt_value_min_undesired) || (readsalt > salt_value_max_undesired))
joran 20:521f795ea9d7 58 {
joran 20:521f795ea9d7 59 if ((readsalt < salt_value_min_crit) || (readsalt > salt_value_max_crit))
joran 20:521f795ea9d7 60 {
joran 20:521f795ea9d7 61 this->raiseAlarmSalt(true);
joran 20:521f795ea9d7 62 } else {
joran 20:521f795ea9d7 63 this->raiseAlarmSalt(false);
joran 20:521f795ea9d7 64 }
joran 20:521f795ea9d7 65 } else {
joran 20:521f795ea9d7 66 //clear alarm
joran 20:521f795ea9d7 67 salttimer.stop();
joran 20:521f795ea9d7 68 this->saltiscrit = false;
joran 20:521f795ea9d7 69 }
joran 20:521f795ea9d7 70
joran 27:4f73f754fdc9 71 readoverflow = this->proximity->getValue();
joran 27:4f73f754fdc9 72 if ((readoverflow < overflow_value_min_undesired) || (readoverflow > overflow_value_max_undesired))
joran 27:4f73f754fdc9 73 {
joran 27:4f73f754fdc9 74 if ((readoverflow < overflow_value_min_crit) || (readoverflow > overflow_value_max_crit))
joran 27:4f73f754fdc9 75 {
joran 27:4f73f754fdc9 76 this->raiseAlarmOverFlow(true);
joran 27:4f73f754fdc9 77 } else {
joran 27:4f73f754fdc9 78 this->raiseAlarmOverFlow(false);
joran 27:4f73f754fdc9 79 }
joran 27:4f73f754fdc9 80 } else {
joran 27:4f73f754fdc9 81 //clear alarm
joran 27:4f73f754fdc9 82 overflowtimer.stop();
joran 27:4f73f754fdc9 83 this->overflowiscrit = false;
joran 27:4f73f754fdc9 84 }
joran 27:4f73f754fdc9 85
joran 20:521f795ea9d7 86 }
joran 20:521f795ea9d7 87
joran 20:521f795ea9d7 88 void AlarmController::raiseAlarmTemp(bool isCrit)
joran 20:521f795ea9d7 89 {
joran 20:521f795ea9d7 90 this->tempiscrit = isCrit;
joran 20:521f795ea9d7 91 int readtimer = temptimer.read();
joran 20:521f795ea9d7 92 if (isCrit) printf("Received a critical temperature alarm (%.1f), timer is at %d\r\n",readtemp,readtimer);
joran 20:521f795ea9d7 93 if (!isCrit) printf("Received a non-critical temperature alarm (%.1f), timer is at %d\r\n",readtemp,readtimer);
joran 20:521f795ea9d7 94 if (readtimer > 0 ) { //already running
joran 20:521f795ea9d7 95 if ((readtimer >= 30 && tempiscrit) || (readtimer >= 300 && !tempiscrit))
joran 20:521f795ea9d7 96 {
joran 20:521f795ea9d7 97 //DO STUFF!!
joran 27:4f73f754fdc9 98
joran 27:4f73f754fdc9 99 this->error = true;
joran 27:4f73f754fdc9 100 if (this->tempiscrit) {this->error_msg = "Crit temp!"; } else { this->error_msg = "Undes temp!"; }
joran 20:521f795ea9d7 101 printf("### Temperature alarm has been triggered after %d ###\r\n",readtimer);
joran 20:521f795ea9d7 102 buzzOnce();
joran 20:521f795ea9d7 103 temptimer.stop();
joran 20:521f795ea9d7 104 temptimer.reset();
joran 20:521f795ea9d7 105 this->tempiscrit = false;
joran 20:521f795ea9d7 106 }
joran 20:521f795ea9d7 107 } else {
joran 20:521f795ea9d7 108 temptimer.start();
joran 20:521f795ea9d7 109 }
joran 20:521f795ea9d7 110 }
joran 20:521f795ea9d7 111
joran 20:521f795ea9d7 112 void AlarmController::raiseAlarmSalt(bool isCrit)
joran 20:521f795ea9d7 113 {
joran 20:521f795ea9d7 114 this->saltiscrit = isCrit;
joran 20:521f795ea9d7 115 int readtimer = salttimer.read();
joran 20:521f795ea9d7 116 if (isCrit) printf("Received a critical salt alarm (%.3f), timer is at %d\r\n",readsalt,readtimer);
joran 20:521f795ea9d7 117 if (!isCrit) printf("Received a non-critical salt alarm (%.3f), timer is at %d\r\n",readsalt,readtimer);
joran 20:521f795ea9d7 118 if (readtimer > 0 ) { //already running
joran 20:521f795ea9d7 119 if ((readtimer >= 30 && saltiscrit) || (readtimer >= 300 && !saltiscrit))
joran 20:521f795ea9d7 120 {
joran 20:521f795ea9d7 121 //DO STUFF!!
joran 27:4f73f754fdc9 122
joran 27:4f73f754fdc9 123 this->error = true;
joran 27:4f73f754fdc9 124 if (this->saltiscrit) {this->error_msg = "Crit salt!"; } else { this->error_msg = "Undes salt!"; }
joran 20:521f795ea9d7 125 printf("### Salt alarm has been triggered after %d seconds ###\r\n",readtimer);
joran 20:521f795ea9d7 126 buzzOnce();
joran 20:521f795ea9d7 127 salttimer.stop();
joran 20:521f795ea9d7 128 salttimer.reset();
joran 20:521f795ea9d7 129 this->saltiscrit = false;
joran 20:521f795ea9d7 130 }
joran 20:521f795ea9d7 131 } else {
joran 20:521f795ea9d7 132 salttimer.start();
joran 20:521f795ea9d7 133 }
sbouber1 11:1a0a8fd74bc0 134 }
sbouber1 11:1a0a8fd74bc0 135
joran 27:4f73f754fdc9 136 void AlarmController::raiseAlarmOverFlow(bool isCrit)
joran 27:4f73f754fdc9 137 {
joran 27:4f73f754fdc9 138 this->overflowiscrit = isCrit;
joran 27:4f73f754fdc9 139 int readtimer = overflowtimer.read();
joran 27:4f73f754fdc9 140 if (isCrit) printf("Received a critical overflow alarm (%.3f), timer is at %d\r\n",readoverflow,readtimer);
joran 27:4f73f754fdc9 141 if (!isCrit) printf("Received a non-critical overflow alarm (%.3f), timer is at %d\r\n",readoverflow,readtimer);
joran 27:4f73f754fdc9 142 if (readtimer > 0 ) { //already running
joran 27:4f73f754fdc9 143 if ((readtimer >= 30 && overflowiscrit) || (readtimer >= 300 && !overflowiscrit))
joran 27:4f73f754fdc9 144 {
joran 27:4f73f754fdc9 145 //DO STUFF!!
joran 27:4f73f754fdc9 146 this->error = true;
joran 27:4f73f754fdc9 147 if (this->overflowiscrit) {this->error_msg = "Crit volume!"; } else { this->error_msg = "Undes volume!"; }
joran 27:4f73f754fdc9 148
joran 27:4f73f754fdc9 149 printf("### Overflow alarm has been triggered after %d seconds ###\r\n",readtimer);
joran 27:4f73f754fdc9 150 buzzOnce();
joran 27:4f73f754fdc9 151 overflowtimer.stop();
joran 27:4f73f754fdc9 152 overflowtimer.reset();
joran 27:4f73f754fdc9 153 this->overflowiscrit = false;
joran 27:4f73f754fdc9 154 }
joran 27:4f73f754fdc9 155 } else {
joran 27:4f73f754fdc9 156 overflowtimer.start();
joran 27:4f73f754fdc9 157 }
joran 27:4f73f754fdc9 158 }
joran 27:4f73f754fdc9 159
sbouber1 11:1a0a8fd74bc0 160 std::string AlarmController::get_name() {
sbouber1 11:1a0a8fd74bc0 161 return "AlarmController";
sbouber1 11:1a0a8fd74bc0 162 }
sbouber1 11:1a0a8fd74bc0 163
sbouber1 11:1a0a8fd74bc0 164 bool AlarmController::is_error() {
sbouber1 11:1a0a8fd74bc0 165 return this->error;
sbouber1 11:1a0a8fd74bc0 166 }
sbouber1 11:1a0a8fd74bc0 167
sbouber1 11:1a0a8fd74bc0 168 char *AlarmController::get_error_message() {
sbouber1 11:1a0a8fd74bc0 169 return this->error_msg;
sbouber1 11:1a0a8fd74bc0 170 }
sbouber1 11:1a0a8fd74bc0 171
sbouber1 11:1a0a8fd74bc0 172 void AlarmController::activate_buzzer() {
sbouber1 11:1a0a8fd74bc0 173 printf("Should activate buzzer now\r\n");
joran 20:521f795ea9d7 174 }
joran 20:521f795ea9d7 175
joran 20:521f795ea9d7 176 void AlarmController::buzzOnce() {
joran 20:521f795ea9d7 177
joran 20:521f795ea9d7 178 buzzer = 1;
joran 20:521f795ea9d7 179 led1=1;
joran 20:521f795ea9d7 180 led2=1;
joran 20:521f795ea9d7 181 led3=1;
joran 20:521f795ea9d7 182 led4=1;
joran 20:521f795ea9d7 183 wait(0.1);
joran 20:521f795ea9d7 184 buzzer = 0;
joran 20:521f795ea9d7 185 led1=0;
joran 20:521f795ea9d7 186 led2=0;
joran 20:521f795ea9d7 187 led3=0;
joran 20:521f795ea9d7 188 led4=0;
joran 20:521f795ea9d7 189
sbouber1 11:1a0a8fd74bc0 190 }