Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of prosjektoppgave_alarm by
main.cpp@10:557f325a6119, 2017-01-25 (annotated)
- Committer:
- mathimat
- Date:
- Wed Jan 25 11:46:24 2017 +0000
- Revision:
- 10:557f325a6119
- Parent:
- 9:7fff168507fa
- Child:
- 11:2af2150e495d
DOES IT WORK?
Who changed what in which revision?
| User | Revision | Line number | New 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 | 9:7fff168507fa | 13 | struct alarmKretser { //1 = aktivert |
| mathimat | 9:7fff168507fa | 14 | int A; |
| mathimat | 9:7fff168507fa | 15 | int B; |
| mathimat | 10:557f325a6119 | 16 | }; |
| mathimat | 10:557f325a6119 | 17 | struct alarmKretser brytere[4]; |
| mathimat | 7:1fbdaef9052a | 18 | |
| mathimat | 1:fbad5dc82abd | 19 | int status = 0; //1 = alarm |
| mathimat | 1:fbad5dc82abd | 20 | |
| mathimat | 4:3dfc937f6516 | 21 | |
| mathimat | 4:3dfc937f6516 | 22 | //todo: få dette i en konfigurasjons tekstfil |
| mathimat | 1:fbad5dc82abd | 23 | #define passord "qwerty" |
| mathimat | 6:5607f07a3863 | 24 | #define bryterAhigh 0.285f |
| mathimat | 6:5607f07a3863 | 25 | #define bryterAlow 0.200f |
| mathimat | 6:5607f07a3863 | 26 | #define bryterBhigh 0.200f |
| mathimat | 6:5607f07a3863 | 27 | #define bryterBlow 0.150f |
| mathimat | 6:5607f07a3863 | 28 | #define bryterABhigh 0.150f |
| mathimat | 6:5607f07a3863 | 29 | #define bryterABlow 0.050f |
| mathimat | 6:5607f07a3863 | 30 | #define bryter0 0.97f |
| mathimat | 1:fbad5dc82abd | 31 | |
| mathimat | 1:fbad5dc82abd | 32 | |
| mathimat | 6:5607f07a3863 | 33 | void getFromFile(){ |
| mathimat | 6:5607f07a3863 | 34 | |
| mathimat | 6:5607f07a3863 | 35 | } |
| mathimat | 6:5607f07a3863 | 36 | |
| mathimat | 10:557f325a6119 | 37 | void log(int status = 0, int tall = 0, char bokstav = '0') { |
| mathimat | 7:1fbdaef9052a | 38 | FILE *fp = fopen("/local/log.txt", "a"); |
| mathimat | 7:1fbdaef9052a | 39 | fprintf(fp, "\r\n TIME: %f |", systemTid.read()); |
| mathimat | 7:1fbdaef9052a | 40 | switch(status){ |
| mathimat | 7:1fbdaef9052a | 41 | case 1: |
| mathimat | 7:1fbdaef9052a | 42 | fprintf(fp,"ALARM, bryter: %d", tall); |
| mathimat | 7:1fbdaef9052a | 43 | break; |
| mathimat | 7:1fbdaef9052a | 44 | case 2: |
| mathimat | 7:1fbdaef9052a | 45 | fprintf(fp,"ALARM, nullstillt ved servisbryter"); |
| mathimat | 7:1fbdaef9052a | 46 | break; |
| mathimat | 7:1fbdaef9052a | 47 | case 3: |
| mathimat | 7:1fbdaef9052a | 48 | fprintf(fp,"ALARM, nullstillt ved terminal"); |
| mathimat | 7:1fbdaef9052a | 49 | break; |
| mathimat | 7:1fbdaef9052a | 50 | default: |
| mathimat | 7:1fbdaef9052a | 51 | break; |
| mathimat | 7:1fbdaef9052a | 52 | } |
| mathimat | 7:1fbdaef9052a | 53 | fclose(fp); |
| mathimat | 7:1fbdaef9052a | 54 | } |
| mathimat | 7:1fbdaef9052a | 55 | |
| mathimat | 7:1fbdaef9052a | 56 | |
| mathimat | 1:fbad5dc82abd | 57 | int alarmSjekk(AnalogIn& krets, int kretsn){ |
| mathimat | 7:1fbdaef9052a | 58 | //TODO: oppdater tabell med hvilken alarm som har gått |
| mathimat | 1:fbad5dc82abd | 59 | /* Sjekker kretsen etter spenningsendring og returnerer hvilke bryter som er åpen |
| mathimat | 1:fbad5dc82abd | 60 | -1 error |
| mathimat | 1:fbad5dc82abd | 61 | 0 alle lukket |
| mathimat | 1:fbad5dc82abd | 62 | 1 bryter A |
| mathimat | 1:fbad5dc82abd | 63 | 2 bryter B |
| mathimat | 1:fbad5dc82abd | 64 | 3 bryter AB |
| mathimat | 6:5607f07a3863 | 65 | --- |
| mathimat | 1:fbad5dc82abd | 66 | */ |
| mathimat | 1:fbad5dc82abd | 67 | int bryter = -1; |
| mathimat | 1:fbad5dc82abd | 68 | float volt = krets.read(); |
| mathimat | 6:5607f07a3863 | 69 | if(volt <= bryter0){ |
| mathimat | 1:fbad5dc82abd | 70 | status = 1; |
| mathimat | 6:5607f07a3863 | 71 | if(volt < bryterAhigh){ |
| mathimat | 6:5607f07a3863 | 72 | if(volt < bryterBhigh){ |
| mathimat | 6:5607f07a3863 | 73 | if(volt < bryterABhigh){ |
| mathimat | 6:5607f07a3863 | 74 | bryter = 3; |
| mathimat | 9:7fff168507fa | 75 | if (brytere[kretsn-1].A != 1 || brytere[kretsn-1].B != 1){ |
| mathimat | 10:557f325a6119 | 76 | log(1,kretsn,'A'); |
| mathimat | 10:557f325a6119 | 77 | log(1,kretsn,'B'); |
| mathimat | 9:7fff168507fa | 78 | } |
| mathimat | 9:7fff168507fa | 79 | brytere[kretsn-1].A = brytere[kretsn-1].B = 1; |
| mathimat | 7:1fbdaef9052a | 80 | |
| mathimat | 6:5607f07a3863 | 81 | } else { |
| mathimat | 6:5607f07a3863 | 82 | bryter = 2; |
| mathimat | 9:7fff168507fa | 83 | if ( brytere[kretsn-1].B != 1){ |
| mathimat | 10:557f325a6119 | 84 | log(1,kretsn,'B'); |
| mathimat | 9:7fff168507fa | 85 | } |
| mathimat | 9:7fff168507fa | 86 | brytere[kretsn-1].B = 1; |
| mathimat | 6:5607f07a3863 | 87 | } |
| mathimat | 6:5607f07a3863 | 88 | } else { |
| mathimat | 6:5607f07a3863 | 89 | bryter = 1; |
| mathimat | 9:7fff168507fa | 90 | if ( brytere[kretsn-1].B != 1){ |
| mathimat | 10:557f325a6119 | 91 | log(1,kretsn,'B'); |
| mathimat | 9:7fff168507fa | 92 | } |
| mathimat | 9:7fff168507fa | 93 | brytere[kretsn-1].A = 1; |
| mathimat | 6:5607f07a3863 | 94 | } |
| mathimat | 6:5607f07a3863 | 95 | } else { |
| mathimat | 6:5607f07a3863 | 96 | bryter = -2; |
| mathimat | 1:fbad5dc82abd | 97 | } |
| mathimat | 1:fbad5dc82abd | 98 | } else(bryter = 0); |
| mathimat | 1:fbad5dc82abd | 99 | return bryter; |
| mathimat | 1:fbad5dc82abd | 100 | } |
| mathimat | 1:fbad5dc82abd | 101 | |
| mathimat | 1:fbad5dc82abd | 102 | void sjekkAlleKretser(){ |
| mathimat | 1:fbad5dc82abd | 103 | //sjekker alle kretser skvensielt for aktivering |
| mathimat | 7:1fbdaef9052a | 104 | int a = alarmSjekk(ain1, 0); |
| mathimat | 7:1fbdaef9052a | 105 | int b = alarmSjekk(ain2, 1); |
| mathimat | 7:1fbdaef9052a | 106 | int c = alarmSjekk(ain3, 2); |
| mathimat | 7:1fbdaef9052a | 107 | int d = alarmSjekk(ain4, 3); |
| mathimat | 1:fbad5dc82abd | 108 | } |
| mathimat | 1:fbad5dc82abd | 109 | |
| mathimat | 1:fbad5dc82abd | 110 | void statusLED(){ |
| mathimat | 1:fbad5dc82abd | 111 | //oppdatere statusLED til aktiverte alarmer |
| mathimat | 1:fbad5dc82abd | 112 | } |
| mathimat | 1:fbad5dc82abd | 113 | |
| mathimat | 3:d3f95e1e06cf | 114 | |
| mathimat | 3:d3f95e1e06cf | 115 | |
| mathimat | 1:fbad5dc82abd | 116 | void resetSystem(){ |
| mathimat | 1:fbad5dc82abd | 117 | //reseter alarmsystemet |
| mathimat | 1:fbad5dc82abd | 118 | status = 0; |
| mathimat | 1:fbad5dc82abd | 119 | |
| mathimat | 1:fbad5dc82abd | 120 | } |
| mathimat | 0:0aa2d9849f98 | 121 | |
| mathimat | 2:2d97a168af1e | 122 | |
| mathimat | 5:b4e1656b6627 | 123 | void chron(){ |
| mathimat | 5:b4e1656b6627 | 124 | sjekkAlleKretser(); |
| mathimat | 5:b4e1656b6627 | 125 | statusLED(); |
| mathimat | 5:b4e1656b6627 | 126 | } |
| mathimat | 5:b4e1656b6627 | 127 | |
| mathimat | 5:b4e1656b6627 | 128 | |
| mathimat | 0:0aa2d9849f98 | 129 | int main() { |
| mathimat | 7:1fbdaef9052a | 130 | systemTid.start(); |
| mathimat | 3:d3f95e1e06cf | 131 | while(1){ |
| mathimat | 7:1fbdaef9052a | 132 | pc.printf("\r "); |
| mathimat | 7:1fbdaef9052a | 133 | sjekkAlleKretser(); |
| mathimat | 7:1fbdaef9052a | 134 | pc.printf(" | Alarm Status: %d\r\n",status); |
| mathimat | 7:1fbdaef9052a | 135 | wait(0.5); |
| mathimat | 3:d3f95e1e06cf | 136 | } |
| mathimat | 0:0aa2d9849f98 | 137 | } |
