knut kristensen / Mbed 2 deprecated prosjektoppgave_alarm

Dependencies:   mbed

Fork of prosjektoppgave_alarm by PnM_spring17

Committer:
mathimat
Date:
Wed Jan 25 11:11:50 2017 +0000
Revision:
7:1fbdaef9052a
Parent:
6:5607f07a3863
Child:
9:7fff168507fa
fisker logging;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mathimat 0:0aa2d9849f98 1 #include "mbed.h"
mathimat 0:0aa2d9849f98 2
mathimat 1:fbad5dc82abd 3 AnalogIn ain1(p17), ain2(p18), ain3(p19), ain4(p20);
mathimat 1:fbad5dc82abd 4 DigitalIn nullut(p16);
mathimat 1:fbad5dc82abd 5 BusOut ledStatus(p21,p22,p23,p24,p25,p26,p27,p28);
mathimat 1:fbad5dc82abd 6 DigitalOut ledMaster(LED1);
mathimat 6:5607f07a3863 7 Serial pc(USBTX, USBRX);
mathimat 7:1fbdaef9052a 8 LocalFileSystem local("local");
mathimat 1:fbad5dc82abd 9
mathimat 5:b4e1656b6627 10 Ticker tick;
mathimat 7:1fbdaef9052a 11 Timer systemTid;
mathimat 1:fbad5dc82abd 12
mathimat 1:fbad5dc82abd 13 int brytere[8]; //en for hver bryter, 1 = alarm
mathimat 7:1fbdaef9052a 14
mathimat 1:fbad5dc82abd 15 int status = 0; //1 = alarm
mathimat 1:fbad5dc82abd 16
mathimat 4:3dfc937f6516 17
mathimat 4:3dfc937f6516 18 //todo: få dette i en konfigurasjons tekstfil
mathimat 1:fbad5dc82abd 19 #define passord "qwerty"
mathimat 6:5607f07a3863 20 #define bryterAhigh 0.285f
mathimat 6:5607f07a3863 21 #define bryterAlow 0.200f
mathimat 6:5607f07a3863 22 #define bryterBhigh 0.200f
mathimat 6:5607f07a3863 23 #define bryterBlow 0.150f
mathimat 6:5607f07a3863 24 #define bryterABhigh 0.150f
mathimat 6:5607f07a3863 25 #define bryterABlow 0.050f
mathimat 6:5607f07a3863 26 #define bryter0 0.97f
mathimat 1:fbad5dc82abd 27
mathimat 1:fbad5dc82abd 28
mathimat 6:5607f07a3863 29 void getFromFile(){
mathimat 6:5607f07a3863 30
mathimat 6:5607f07a3863 31 }
mathimat 6:5607f07a3863 32
mathimat 7:1fbdaef9052a 33 void log(int status = 0, int tall = 0) {
mathimat 7:1fbdaef9052a 34 FILE *fp = fopen("/local/log.txt", "a");
mathimat 7:1fbdaef9052a 35 fprintf(fp, "\r\n TIME: %f |", systemTid.read());
mathimat 7:1fbdaef9052a 36 switch(status){
mathimat 7:1fbdaef9052a 37 case 1:
mathimat 7:1fbdaef9052a 38 fprintf(fp,"ALARM, bryter: %d", tall);
mathimat 7:1fbdaef9052a 39 break;
mathimat 7:1fbdaef9052a 40 case 2:
mathimat 7:1fbdaef9052a 41 fprintf(fp,"ALARM, nullstillt ved servisbryter");
mathimat 7:1fbdaef9052a 42 break;
mathimat 7:1fbdaef9052a 43 case 3:
mathimat 7:1fbdaef9052a 44 fprintf(fp,"ALARM, nullstillt ved terminal");
mathimat 7:1fbdaef9052a 45 break;
mathimat 7:1fbdaef9052a 46 default:
mathimat 7:1fbdaef9052a 47 break;
mathimat 7:1fbdaef9052a 48 }
mathimat 7:1fbdaef9052a 49 fclose(fp);
mathimat 7:1fbdaef9052a 50 }
mathimat 7:1fbdaef9052a 51
mathimat 7:1fbdaef9052a 52
mathimat 1:fbad5dc82abd 53 int alarmSjekk(AnalogIn& krets, int kretsn){
mathimat 7:1fbdaef9052a 54 //TODO: oppdater tabell med hvilken alarm som har gått
mathimat 1:fbad5dc82abd 55 /* Sjekker kretsen etter spenningsendring og returnerer hvilke bryter som er åpen
mathimat 1:fbad5dc82abd 56 -1 error
mathimat 1:fbad5dc82abd 57 0 alle lukket
mathimat 1:fbad5dc82abd 58 1 bryter A
mathimat 1:fbad5dc82abd 59 2 bryter B
mathimat 1:fbad5dc82abd 60 3 bryter AB
mathimat 6:5607f07a3863 61 ---
mathimat 1:fbad5dc82abd 62 */
mathimat 1:fbad5dc82abd 63 int bryter = -1;
mathimat 1:fbad5dc82abd 64 float volt = krets.read();
mathimat 6:5607f07a3863 65 if(volt <= bryter0){
mathimat 1:fbad5dc82abd 66 status = 1;
mathimat 6:5607f07a3863 67 if(volt < bryterAhigh){
mathimat 6:5607f07a3863 68 if(volt < bryterBhigh){
mathimat 6:5607f07a3863 69 if(volt < bryterABhigh){
mathimat 6:5607f07a3863 70 bryter = 3;
mathimat 7:1fbdaef9052a 71
mathimat 6:5607f07a3863 72 } else {
mathimat 6:5607f07a3863 73 bryter = 2;
mathimat 6:5607f07a3863 74 }
mathimat 6:5607f07a3863 75 } else {
mathimat 6:5607f07a3863 76 bryter = 1;
mathimat 6:5607f07a3863 77 }
mathimat 6:5607f07a3863 78 } else {
mathimat 6:5607f07a3863 79 bryter = -2;
mathimat 1:fbad5dc82abd 80 }
mathimat 7:1fbdaef9052a 81 log(1,bryter);
mathimat 1:fbad5dc82abd 82 } else(bryter = 0);
mathimat 1:fbad5dc82abd 83 return bryter;
mathimat 1:fbad5dc82abd 84 }
mathimat 1:fbad5dc82abd 85
mathimat 1:fbad5dc82abd 86 void sjekkAlleKretser(){
mathimat 1:fbad5dc82abd 87 //sjekker alle kretser skvensielt for aktivering
mathimat 7:1fbdaef9052a 88 int a = alarmSjekk(ain1, 0);
mathimat 7:1fbdaef9052a 89 int b = alarmSjekk(ain2, 1);
mathimat 7:1fbdaef9052a 90 int c = alarmSjekk(ain3, 2);
mathimat 7:1fbdaef9052a 91 int d = alarmSjekk(ain4, 3);
mathimat 7:1fbdaef9052a 92 pc.printf("krets 1: %d krets 2: %d krets 3: %d krets 4: %d ",a,b,c,d);
mathimat 1:fbad5dc82abd 93 }
mathimat 1:fbad5dc82abd 94
mathimat 1:fbad5dc82abd 95 void statusLED(){
mathimat 1:fbad5dc82abd 96 //oppdatere statusLED til aktiverte alarmer
mathimat 1:fbad5dc82abd 97 for(int i=0; brytere[i]!=NULL;i++){
mathimat 1:fbad5dc82abd 98 if(brytere[i] == 1){
mathimat 3:d3f95e1e06cf 99 ledStatus = ledStatus+i;
mathimat 1:fbad5dc82abd 100 }
mathimat 1:fbad5dc82abd 101 }
mathimat 1:fbad5dc82abd 102 }
mathimat 1:fbad5dc82abd 103
mathimat 3:d3f95e1e06cf 104
mathimat 3:d3f95e1e06cf 105
mathimat 1:fbad5dc82abd 106 void resetSystem(){
mathimat 1:fbad5dc82abd 107 //reseter alarmsystemet
mathimat 1:fbad5dc82abd 108 for(int i=0; brytere[i]!=NULL;i++){
mathimat 1:fbad5dc82abd 109 brytere[i] = 0;
mathimat 1:fbad5dc82abd 110 }
mathimat 1:fbad5dc82abd 111 status = 0;
mathimat 1:fbad5dc82abd 112
mathimat 1:fbad5dc82abd 113 }
mathimat 0:0aa2d9849f98 114
mathimat 2:2d97a168af1e 115
mathimat 5:b4e1656b6627 116 void chron(){
mathimat 5:b4e1656b6627 117 sjekkAlleKretser();
mathimat 5:b4e1656b6627 118 statusLED();
mathimat 5:b4e1656b6627 119 }
mathimat 5:b4e1656b6627 120
mathimat 5:b4e1656b6627 121
mathimat 0:0aa2d9849f98 122 int main() {
mathimat 7:1fbdaef9052a 123 systemTid.start();
mathimat 3:d3f95e1e06cf 124 while(1){
mathimat 7:1fbdaef9052a 125 pc.printf("\r ");
mathimat 7:1fbdaef9052a 126 sjekkAlleKretser();
mathimat 7:1fbdaef9052a 127 pc.printf(" | Alarm Status: %d\r\n",status);
mathimat 7:1fbdaef9052a 128 wait(0.5);
mathimat 3:d3f95e1e06cf 129 }
mathimat 0:0aa2d9849f98 130 }