klokka 15:07

Fork of AlarmsystemDel1 by Programmeringsgruppe 1

Committer:
andreped
Date:
Tue Feb 20 14:06:31 2018 +0000
Revision:
15:272fb4e610dc
Parent:
14:8e7aaf0cf5d7
Ny rev

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andreped 0:33bf28fd1ac6 1 #include "mbed.h"
andreped 12:0193024eab13 2 #include "rtos.h"
andreped 12:0193024eab13 3 #include <RtosTimer.h>
andreped 11:dad1798ed88f 4 #include <math.h>
andreped 12:0193024eab13 5 #include "mbed_error.h"
andreped 15:272fb4e610dc 6 #include <string>
andreped 15:272fb4e610dc 7 #include <iostream>
andreped 12:0193024eab13 8
Banyrud 13:0622c228b67d 9 /////DEFINISJONER/////
andreped 6:a3b97bf7902b 10 #define Inn 4
Banyrud 13:0622c228b67d 11 Serial pc(USBTX, USBRX);
andreped 6:a3b97bf7902b 12 //#define Feil 0<1
andreped 10:f0a6d74095d4 13
Banyrud 13:0622c228b67d 14 /////INNGANGER/////
andreped 0:33bf28fd1ac6 15 DigitalIn Servicebryter(p20);
andreped 12:0193024eab13 16 AnalogIn _Sone1(p15);
andreped 12:0193024eab13 17 AnalogIn _Sone2(p16);
andreped 12:0193024eab13 18 AnalogIn _Sone3(p17);
andreped 12:0193024eab13 19 AnalogIn _Sone4(p19);
Banyrud 13:0622c228b67d 20
Banyrud 13:0622c228b67d 21 /////UTGANGER/////
Banyrud 13:0622c228b67d 22 DigitalOut Driftslys(LED1);
Banyrud 13:0622c228b67d 23 BusOut AlarmUtganger1(p21, p22, p23, p24); //1 - Dør 1 Sone 1, 2 - Dør 1 Sone 2, 4 - Dør 1 Sone 3, 8 - Dør 1 Sone 4
Banyrud 13:0622c228b67d 24 BusOut AlarmUtganger2(p25, p26, p27, p28); //1 - Dør 2 Sone 1, 2 - Dør 2 Sone 2, 4 - Dør 2 Sone 3, 8 - Dør 2 Sone 4
Banyrud 13:0622c228b67d 25 //DigitalOut myled2(LED2);
Banyrud 13:0622c228b67d 26
Banyrud 13:0622c228b67d 27 /////VARIABLER/////
andreped 12:0193024eab13 28 float Soner[Inn];
andreped 12:0193024eab13 29 int tilstand = 0;
andreped 11:dad1798ed88f 30 int TilstandSjekk(int, int);
andreped 12:0193024eab13 31 volatile bool running = true;
andreped 15:272fb4e610dc 32 volatile bool admin = false;
Banyrud 14:8e7aaf0cf5d7 33 string password = ("admin");
Banyrud 14:8e7aaf0cf5d7 34 string keyinput;
Banyrud 13:0622c228b67d 35 //volatile int Klokke = 0;
andreped 6:a3b97bf7902b 36 //nt Tid = 0;
andreped 1:6917ec4f5e6d 37
Banyrud 13:0622c228b67d 38 /////FUNKSJON DEKLARASJONER/////
Banyrud 13:0622c228b67d 39 void Alarmf();
andreped 15:272fb4e610dc 40 Thread Alarm;
andreped 15:272fb4e610dc 41 Thread Sone;
andreped 1:6917ec4f5e6d 42 int Tilstandssjekk();
andreped 15:272fb4e610dc 43 void admin_access_granted();
andreped 15:272fb4e610dc 44 void sone_monitor();
andreped 15:272fb4e610dc 45 void password_check();
andreped 15:272fb4e610dc 46 //void read_keyinput();
andreped 12:0193024eab13 47 //void Alarm();
Banyrud 13:0622c228b67d 48 //Ticker AlarmBlink; // 10Hz blinking for alarmfunksjon på lys og telling
andreped 1:6917ec4f5e6d 49
andreped 12:0193024eab13 50
Banyrud 13:0622c228b67d 51 /////MAIN/////
andreped 1:6917ec4f5e6d 52 int main()
andreped 1:6917ec4f5e6d 53 {
andreped 15:272fb4e610dc 54 while(1) {
andreped 15:272fb4e610dc 55 if(admin == false) {
andreped 15:272fb4e610dc 56 password_check();
andreped 15:272fb4e610dc 57 }
andreped 15:272fb4e610dc 58 while(admin) {
andreped 15:272fb4e610dc 59 Alarm.start(Alarmf);
andreped 15:272fb4e610dc 60 sone_monitor();
Banyrud 14:8e7aaf0cf5d7 61 read_keyinput();
andreped 15:272fb4e610dc 62 }//if-end
andreped 15:272fb4e610dc 63 }//main-end
andreped 15:272fb4e610dc 64 }
Banyrud 14:8e7aaf0cf5d7 65
Banyrud 14:8e7aaf0cf5d7 66
andreped 1:6917ec4f5e6d 67
Banyrud 13:0622c228b67d 68 /////FUNKSJONER/////
Banyrud 14:8e7aaf0cf5d7 69 void Alarmf(void)
Banyrud 14:8e7aaf0cf5d7 70 {
andreped 15:272fb4e610dc 71 while(1) {
andreped 15:272fb4e610dc 72 if(running == true) {
andreped 15:272fb4e610dc 73 Driftslys = !Driftslys;
andreped 15:272fb4e610dc 74 Thread::wait(50);
andreped 15:272fb4e610dc 75 } else {
andreped 15:272fb4e610dc 76 Driftslys = 1;
andreped 15:272fb4e610dc 77 }
andreped 15:272fb4e610dc 78 }
Banyrud 13:0622c228b67d 79 }
Banyrud 13:0622c228b67d 80
andreped 11:dad1798ed88f 81 int TilstandSjekk(int i, int tilstand)
andreped 11:dad1798ed88f 82 {
andreped 11:dad1798ed88f 83 if(tilstand == 0) {
andreped 11:dad1798ed88f 84 //pc.printf("Vi er i funksjonen normaldrift i sone: %d \n", i);
andreped 12:0193024eab13 85 //AlarmBlink.detach();
andreped 15:272fb4e610dc 86 running = false;
andreped 11:dad1798ed88f 87 double n = 2; // We want 2^i
andreped 11:dad1798ed88f 88 double Y = 1;
andreped 11:dad1798ed88f 89 for(int k=1; k<=i; k++) Y = Y*n;
andreped 12:0193024eab13 90 if ( Y == AlarmUtganger1) AlarmUtganger1 = AlarmUtganger1 - Y;
andreped 12:0193024eab13 91 if ( Y == AlarmUtganger2) AlarmUtganger2 = AlarmUtganger2 - Y;
andreped 12:0193024eab13 92 int Utganger1 = AlarmUtganger1;
andreped 12:0193024eab13 93 int Utganger2 = AlarmUtganger2;
andreped 11:dad1798ed88f 94 //pc.printf("Utgangsbuss1 har naa verdien: %d \n Utgangsbuss2 har naa verdien: %d", Utganger1, Utganger2);
andreped 11:dad1798ed88f 95 }
andreped 11:dad1798ed88f 96 if(tilstand == 1) { //Hvis dør 1 er åpen...
andreped 11:dad1798ed88f 97 //pc.printf("Vi er i funksjonen tilstandssjekk dor 1, med verdi: %d\n", i);
andreped 12:0193024eab13 98 //Alarm();
andreped 15:272fb4e610dc 99 running = true;
andreped 11:dad1798ed88f 100 double n = 2; // We want 2^i
andreped 11:dad1798ed88f 101 double Y = 1;
andreped 11:dad1798ed88f 102 for(int k=1; k<=i; k++) Y = Y*n;
andreped 11:dad1798ed88f 103 if( Y == AlarmUtganger2 ) AlarmUtganger2 = AlarmUtganger2 - Y;
andreped 11:dad1798ed88f 104 if( Y!=AlarmUtganger1) AlarmUtganger1 = Y + AlarmUtganger1; //Sjekk om denne verdien har blitt sjekket før, hvis ikke starter alarm på neste dør.
andreped 11:dad1798ed88f 105 int Utganger1 = AlarmUtganger1;
andreped 11:dad1798ed88f 106 //pc.printf("Vi har verdien paa utgangsbuss: %d\n", Utganger1);
andreped 11:dad1798ed88f 107 }
andreped 11:dad1798ed88f 108 if(tilstand == 2) { //Hvis dør 2 er åpen...
andreped 11:dad1798ed88f 109 //pc.printf("Vi er i funksjonen tilstandssjekk dor 2, med verdi: %d\n", i);
andreped 12:0193024eab13 110 //Alarm();
andreped 15:272fb4e610dc 111 running = true;
andreped 11:dad1798ed88f 112 double n = 2; // We want 2^i
andreped 11:dad1798ed88f 113 double Y = 1;
andreped 11:dad1798ed88f 114 for(int k=1; k<=i; k++) Y = Y*n;
andreped 11:dad1798ed88f 115 if( Y == AlarmUtganger1 ) AlarmUtganger1 = AlarmUtganger1 - Y;
andreped 11:dad1798ed88f 116 if( Y!=AlarmUtganger2) AlarmUtganger2 = Y + AlarmUtganger2; //Sjekk om denne verdien har blitt sjekket før, hvis ikke starter alarm på neste dør.
andreped 11:dad1798ed88f 117 int Utganger2 = AlarmUtganger2;
andreped 11:dad1798ed88f 118 //pc.printf("Vi har verdien paa utgangsbuss: %d\n", Utganger2);
andreped 11:dad1798ed88f 119 }
andreped 11:dad1798ed88f 120 if(tilstand == 3) { //Hvis begge dører er åpne
andreped 12:0193024eab13 121 //pc.printf("Vi er i funksjonen tilstandssjekk alle dorer, med verdi: %d \n", i);;
andreped 12:0193024eab13 122 // Alarm();
andreped 15:272fb4e610dc 123 running = true;
andreped 11:dad1798ed88f 124 double n = 2; // We want 2^i
andreped 11:dad1798ed88f 125 double Y = 1;
andreped 11:dad1798ed88f 126 for(int k=1; k<=i; k++) Y = Y*n;
andreped 11:dad1798ed88f 127 if( Y!=AlarmUtganger1 ) AlarmUtganger1 = Y + AlarmUtganger1;
andreped 11:dad1798ed88f 128 if( Y!=AlarmUtganger2 ) AlarmUtganger2 = Y + AlarmUtganger2;
andreped 11:dad1798ed88f 129 int Utganger2 = AlarmUtganger2;
andreped 11:dad1798ed88f 130 int Utganger1 = AlarmUtganger2;
andreped 11:dad1798ed88f 131 //pc.printf("Utgangsbuss1 har verdien: %d \n Utgangsbuss2 har verdien: %d \n", Utganger1, Utganger2);
andreped 11:dad1798ed88f 132 }
andreped 11:dad1798ed88f 133 return 0;
andreped 10:f0a6d74095d4 134 }
andreped 15:272fb4e610dc 135 void sone_monitor()
Banyrud 14:8e7aaf0cf5d7 136 {
Banyrud 14:8e7aaf0cf5d7 137 Soner[0] = _Sone1;
Banyrud 14:8e7aaf0cf5d7 138 //Soner[1] = _Sone2;
Banyrud 14:8e7aaf0cf5d7 139 //Soner[2] = _Sone3;
Banyrud 14:8e7aaf0cf5d7 140 //Soner[3] = _Sone4;
Banyrud 14:8e7aaf0cf5d7 141 for(int i=0; i < Inn; i++) {
andreped 15:272fb4e610dc 142 read_keyinput(i);
Banyrud 14:8e7aaf0cf5d7 143 //pc.printf("%f\n", Soner[i]);
Banyrud 14:8e7aaf0cf5d7 144 if((Soner[i] > 0.75 )&&( Soner[i] < 0.83)) { //Tilstand1, Dør 1 åpen
Banyrud 14:8e7aaf0cf5d7 145 //pc.printf("Vi er i Tilstand1\n");
Banyrud 14:8e7aaf0cf5d7 146 tilstand = 1;
Banyrud 14:8e7aaf0cf5d7 147 TilstandSjekk(i, tilstand);
Banyrud 14:8e7aaf0cf5d7 148 }
Banyrud 14:8e7aaf0cf5d7 149 if((Soner[i] > 0.55 )&&( Soner[i] < 0.59)) { //Normaldrift
Banyrud 14:8e7aaf0cf5d7 150 //pc.printf("Vi er i Normaldrift\n")
Banyrud 14:8e7aaf0cf5d7 151 tilstand = 0;
Banyrud 14:8e7aaf0cf5d7 152 TilstandSjekk(i, tilstand);
Banyrud 14:8e7aaf0cf5d7 153 }
Banyrud 14:8e7aaf0cf5d7 154 if((Soner[i] > 0.65 )&&( Soner[i] < 0.69)) { //Tilstand 2, Dør 2 åpen
Banyrud 14:8e7aaf0cf5d7 155 //pc.printf("Vi er i tilstand2\n");
Banyrud 14:8e7aaf0cf5d7 156 tilstand = 2;
Banyrud 14:8e7aaf0cf5d7 157 TilstandSjekk(i, tilstand);
Banyrud 14:8e7aaf0cf5d7 158 }
Banyrud 14:8e7aaf0cf5d7 159 if(Soner[i] >= 1) { //Begge dører er åpne
Banyrud 14:8e7aaf0cf5d7 160 //pc.printf("Vi er i tilstand 3, begge doorer er aapne");
Banyrud 14:8e7aaf0cf5d7 161 tilstand = 3;
Banyrud 14:8e7aaf0cf5d7 162 TilstandSjekk(i, tilstand);
Banyrud 14:8e7aaf0cf5d7 163 }
Banyrud 14:8e7aaf0cf5d7 164 //wait(1.5);
Banyrud 14:8e7aaf0cf5d7 165 }
Banyrud 14:8e7aaf0cf5d7 166 //pc.printf("%f\n", Sone1[]);
Banyrud 14:8e7aaf0cf5d7 167 //Tilstandssjekk(Sone1[]);
Banyrud 14:8e7aaf0cf5d7 168 //float volt = Sone1[];
Banyrud 14:8e7aaf0cf5d7 169 //pc.printf("%f\n", volt);
Banyrud 14:8e7aaf0cf5d7 170 //while(sysclock == 1);
Banyrud 14:8e7aaf0cf5d7 171 //while(sysclock == 0);
Banyrud 14:8e7aaf0cf5d7 172 //Tid++;
Banyrud 14:8e7aaf0cf5d7 173 }
andreped 15:272fb4e610dc 174 void admin_access_granted()
andreped 15:272fb4e610dc 175 {
andreped 15:272fb4e610dc 176 admin = true;
andreped 15:272fb4e610dc 177 pc.printf("Admin access granted\n");//info
andreped 15:272fb4e610dc 178 pc.printf("Folgende kommandoer kan brukes: \n");// liste over funksjoner tilgjenglig.
andreped 15:272fb4e610dc 179 pc.printf("Status sone og dor(sone forst, dor etter) Kommando: 1,2 \n");// funksjon 1 - request spesific status
andreped 15:272fb4e610dc 180 pc.printf("Status alle soner og dorer. Kommando: Status \n");// funksjon 1 - request status all
andreped 15:272fb4e610dc 181 pc.printf("Reset hele systemet. Kommando: 'Reset' \n");// funksjon 4 - request log all
andreped 15:272fb4e610dc 182 }
andreped 15:272fb4e610dc 183 void password_check()
Banyrud 14:8e7aaf0cf5d7 184 {
andreped 15:272fb4e610dc 185 if(pc.readable() == 1) {
andreped 15:272fb4e610dc 186 pc.printf("Write password: ");
andreped 15:272fb4e610dc 187 string b;
andreped 15:272fb4e610dc 188 char s;
andreped 15:272fb4e610dc 189 for(int i = 0; i < 5; i++) {
andreped 15:272fb4e610dc 190 s = pc.getc();
andreped 15:272fb4e610dc 191 pc.printf("%c", s);
andreped 15:272fb4e610dc 192 b += s;
andreped 15:272fb4e610dc 193 if(b == password) {
andreped 15:272fb4e610dc 194 admin_access_granted();
andreped 15:272fb4e610dc 195 }
andreped 15:272fb4e610dc 196 }
andreped 15:272fb4e610dc 197 }
Banyrud 14:8e7aaf0cf5d7 198 }
Banyrud 14:8e7aaf0cf5d7 199
andreped 15:272fb4e610dc 200 void read_keyinput(int i)
andreped 1:6917ec4f5e6d 201 {
andreped 15:272fb4e610dc 202 if(pc.readable() == 1) {
andreped 15:272fb4e610dc 203 pc.printf("Du skriver: ");
andreped 15:272fb4e610dc 204 string read;
andreped 15:272fb4e610dc 205 char c;
andreped 15:272fb4e610dc 206 for(i = 0; i < 6; i++) {
andreped 15:272fb4e610dc 207 c = pc.getc();
andreped 15:272fb4e610dc 208 pc.printf("%c", c);
andreped 15:272fb4e610dc 209 read += c;
andreped 15:272fb4e610dc 210 switch(read) {
andreped 15:272fb4e610dc 211 case "1, 1":
andreped 15:272fb4e610dc 212 request_status_sone_dor(1, 1);
andreped 15:272fb4e610dc 213 break;
andreped 15:272fb4e610dc 214 case "1, 2":
andreped 15:272fb4e610dc 215 request_status_sone_dor(1, 2);
andreped 15:272fb4e610dc 216 break;
andreped 15:272fb4e610dc 217 case "2, 1":
andreped 15:272fb4e610dc 218 request_status_sone_dor(2, 1);
andreped 15:272fb4e610dc 219 break;
andreped 15:272fb4e610dc 220 case "2, 2":
andreped 15:272fb4e610dc 221 request_status_sone_dor(2, 2);
andreped 15:272fb4e610dc 222 break;
andreped 15:272fb4e610dc 223 case "3, 1":
andreped 15:272fb4e610dc 224 request_status_sone_dor(3, 1);
andreped 15:272fb4e610dc 225 break;
andreped 15:272fb4e610dc 226 case "3, 2":
andreped 15:272fb4e610dc 227 request_status_sone_dor(3, 2);
andreped 15:272fb4e610dc 228 break;
andreped 15:272fb4e610dc 229 case "4, 1":
andreped 15:272fb4e610dc 230 request_status_sone_dor(4, 1);
andreped 15:272fb4e610dc 231 break;
andreped 15:272fb4e610dc 232 case "4, 2":
andreped 15:272fb4e610dc 233 request_status_sone_dor(4, 2);
andreped 15:272fb4e610dc 234 break;
andreped 15:272fb4e610dc 235 case "Status":
andreped 15:272fb4e610dc 236 request_status();
andreped 15:272fb4e610dc 237 break;
andreped 15:272fb4e610dc 238 case "Reset":
andreped 15:272fb4e610dc 239 request_reset();
andreped 15:272fb4e610dc 240 break;
andreped 15:272fb4e610dc 241 }
andreped 15:272fb4e610dc 242 }
andreped 15:272fb4e610dc 243 }
andreped 15:272fb4e610dc 244 }
andreped 15:272fb4e610dc 245
andreped 15:272fb4e610dc 246 void request_status_sone_dor(int sone, int dor)
andreped 15:272fb4e610dc 247 {
andreped 15:272fb4e610dc 248 double n = 2; // We want 2^i
andreped 15:272fb4e610dc 249 double Y = 1;
andreped 15:272fb4e610dc 250 for(int k=1; k<=sone; k++) Y = Y*n;
andreped 15:272fb4e610dc 251 if(dor == 1) {
andreped 15:272fb4e610dc 252 if (Alarmutganger1 == Y) pc.printf("Dor: %d i sone: %d er lukket", dor, Y);
andreped 15:272fb4e610dc 253 else pc.printf("Dor: %d i sone: %d er apnet", dor, Y);
andreped 15:272fb4e610dc 254 }
andreped 15:272fb4e610dc 255 if(dor == 2) {
andreped 15:272fb4e610dc 256 if (AlarmUtganger2 == Y) pc.printf("Dor: %d i sone: %d er lukket", dor, Y);
andreped 15:272fb4e610dc 257 else pc.printf("Dor: %d i sone: %d er apnet", dor, Y);
andreped 15:272fb4e610dc 258 }
andreped 15:272fb4e610dc 259 }
andreped 15:272fb4e610dc 260
andreped 15:272fb4e610dc 261 void request_status() {
andreped 15:272fb4e610dc 262 int dor1[Inn] = {
andreped 15:272fb4e610dc 263 dor1[Inn] = AlarmUtganger2;
andreped 15:272fb4e610dc 264
andreped 15:272fb4e610dc 265
andreped 15:272fb4e610dc 266 //BusOut AlarmUtganger1(p21, p22, p23, p24); //1 - Dør 1 Sone 1, 2 - Dør 1 Sone 2, 4 - Dør 1 Sone 3, 8 - Dør 1 Sone 4
andreped 15:272fb4e610dc 267 //BusOut AlarmUtganger2(p25, p26, p27, p28); //1 - Dør 2 Sone 1, 2 - Dør 2 Sone 2, 4 - Dør 2 Sone 3, 8 - Dør 2 Sone 4