![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
test
main.cpp@1:a7bdc6b05f96, 2019-01-24 (annotated)
- Committer:
- hodgins
- Date:
- Thu Jan 24 06:52:36 2019 +0000
- Revision:
- 1:a7bdc6b05f96
- Parent:
- 0:e541909ea6b0
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hodgins | 0:e541909ea6b0 | 1 | #include "Communication.h" |
hodgins | 0:e541909ea6b0 | 2 | #include "ElevatorThreads.h" |
hodgins | 0:e541909ea6b0 | 3 | #include "ElevatorMessenge.h" |
hodgins | 0:e541909ea6b0 | 4 | #include "FreeFall.h" |
hodgins | 0:e541909ea6b0 | 5 | #include "WatchDog.h" |
hodgins | 0:e541909ea6b0 | 6 | |
hodgins | 0:e541909ea6b0 | 7 | Serial serial(USBTX, USBRX, BAUDRATE); |
hodgins | 0:e541909ea6b0 | 8 | Ticker elevatorOpen; //Časovač, ktorý sa spustí pri orvorení dverý, aby sa hneď nezavreli |
hodgins | 0:e541909ea6b0 | 9 | |
hodgins | 0:e541909ea6b0 | 10 | uint32_t tick = 0; // Globalna premenna - pocet tikov Tikera od jeho spustenia |
hodgins | 0:e541909ea6b0 | 11 | struct paket buffer[MESS_BUFFER]; //Zásobník priajtých správ |
hodgins | 0:e541909ea6b0 | 12 | int indexBuffer = 0; //Index, ktorý ukazuje ktorá správa bola pridaná naposledy |
hodgins | 0:e541909ea6b0 | 13 | |
hodgins | 0:e541909ea6b0 | 14 | int elevatorButton[] = {0, 0, 0, 0, 0}; //Pole pre tlačidlá výťahu, index reprezentuje dané poschodie |
hodgins | 0:e541909ea6b0 | 15 | int elevatorSensor[] = {0, 0, 0, 0, 0}; //Pole pre senzory výťahu, index reprezentuje dané poschodie |
hodgins | 0:e541909ea6b0 | 16 | |
hodgins | 0:e541909ea6b0 | 17 | int actualMess = 0; //Index správy ktorá bola spracovaná ako posledná |
hodgins | 0:e541909ea6b0 | 18 | int endFloor; //Poschodie kam sa výťah musí po stlačení tlačidla dosať |
hodgins | 0:e541909ea6b0 | 19 | int actualFloor = 5; //Poschodie, kde sa výŤah aktuálne nachádza |
hodgins | 0:e541909ea6b0 | 20 | int moveDirection = 0; //Smer pohybu výťahu |
hodgins | 0:e541909ea6b0 | 21 | |
hodgins | 0:e541909ea6b0 | 22 | //Funkcia, ktorá počíta tiky - vyvolávaná prerušením |
hodgins | 0:e541909ea6b0 | 23 | void timeTick(){ tick++; } |
hodgins | 0:e541909ea6b0 | 24 | |
hodgins | 0:e541909ea6b0 | 25 | //Funkcia, ktora vracia cas v ms od zaciatku behu programu |
hodgins | 0:e541909ea6b0 | 26 | int getTimer(){ return (tick * msPerTick); } |
hodgins | 0:e541909ea6b0 | 27 | |
hodgins | 0:e541909ea6b0 | 28 | //Funkcia zisťuje, či sa má výťah kam pohnúť alebo má ostať stáť |
hodgins | 0:e541909ea6b0 | 29 | bool moveOn(int actPos) |
hodgins | 0:e541909ea6b0 | 30 | { |
hodgins | 0:e541909ea6b0 | 31 | for (int i = 0; i < 5; i++) //Cyklus prechádza všetky poschodia |
hodgins | 0:e541909ea6b0 | 32 | { |
hodgins | 0:e541909ea6b0 | 33 | if (elevatorButton[i] > 0) // Ak bolo stlačené tlačidlo |
hodgins | 0:e541909ea6b0 | 34 | { |
hodgins | 0:e541909ea6b0 | 35 | if (i == actPos) //AK bolo stlačené tlačidlo na poschodí, kde stojí výťah |
hodgins | 0:e541909ea6b0 | 36 | { |
hodgins | 0:e541909ea6b0 | 37 | elevatorButton[i] = 0; //Nulovanie tohot tlačidla |
hodgins | 0:e541909ea6b0 | 38 | elevatorLedOFF(i); //Vypnutie LED na tomto poschodí |
hodgins | 0:e541909ea6b0 | 39 | elevatorCabineLock(false); // Otvorenie dveri |
hodgins | 0:e541909ea6b0 | 40 | } |
hodgins | 0:e541909ea6b0 | 41 | else |
hodgins | 0:e541909ea6b0 | 42 | { |
hodgins | 0:e541909ea6b0 | 43 | elevatorCabineLock(true); // Zatvorenie dveri |
hodgins | 0:e541909ea6b0 | 44 | return true; //Výťah sa má kam pohnúť |
hodgins | 0:e541909ea6b0 | 45 | } |
hodgins | 0:e541909ea6b0 | 46 | } |
hodgins | 0:e541909ea6b0 | 47 | } |
hodgins | 0:e541909ea6b0 | 48 | return false; //Výťah má stáť na danom poschodí |
hodgins | 0:e541909ea6b0 | 49 | } |
hodgins | 0:e541909ea6b0 | 50 | |
hodgins | 0:e541909ea6b0 | 51 | //Funkcia vracia číslo poschodia, kam sa má výťah pohnúť |
hodgins | 0:e541909ea6b0 | 52 | int nextFloor() |
hodgins | 0:e541909ea6b0 | 53 | { |
hodgins | 0:e541909ea6b0 | 54 | for(int i=actualFloor; i<5; i++) |
hodgins | 0:e541909ea6b0 | 55 | { |
hodgins | 0:e541909ea6b0 | 56 | if( elevatorButton[i] > 0) |
hodgins | 0:e541909ea6b0 | 57 | { |
hodgins | 0:e541909ea6b0 | 58 | return i; |
hodgins | 0:e541909ea6b0 | 59 | } |
hodgins | 0:e541909ea6b0 | 60 | } |
hodgins | 0:e541909ea6b0 | 61 | |
hodgins | 0:e541909ea6b0 | 62 | for(int i=actualFloor - 1; i>=0; i--) |
hodgins | 0:e541909ea6b0 | 63 | { |
hodgins | 0:e541909ea6b0 | 64 | if( elevatorButton[i] > 0) |
hodgins | 0:e541909ea6b0 | 65 | { |
hodgins | 0:e541909ea6b0 | 66 | return i; |
hodgins | 0:e541909ea6b0 | 67 | } |
hodgins | 0:e541909ea6b0 | 68 | } |
hodgins | 0:e541909ea6b0 | 69 | return -1; |
hodgins | 0:e541909ea6b0 | 70 | } |
hodgins | 0:e541909ea6b0 | 71 | |
hodgins | 0:e541909ea6b0 | 72 | // Proto-vlakno na spracovanie prijatých správ zo zásobníka |
hodgins | 0:e541909ea6b0 | 73 | bool MessExecute::Run() |
hodgins | 0:e541909ea6b0 | 74 | { |
hodgins | 0:e541909ea6b0 | 75 | PT_BEGIN(); // inicializácia vlákna |
hodgins | 0:e541909ea6b0 | 76 | while(1) { // nekonečná slučka |
hodgins | 0:e541909ea6b0 | 77 | PT_WAIT_UNTIL(actualMess != indexBuffer); //Čakanie kým nepribudne správa, ktorá nebola spracovaná |
hodgins | 0:e541909ea6b0 | 78 | switch (buffer[actualMess].adresa) //Vykonanie správy, podľa toho komu bola správa odoslaná |
hodgins | 0:e541909ea6b0 | 79 | { |
hodgins | 0:e541909ea6b0 | 80 | //Správa indikovala, že bolo stlačené tlačidlo na privolanie výťahu |
hodgins | 0:e541909ea6b0 | 81 | case 0xc0: elevatorLedON(01);elevatorButton[0] = 1; break; //Tlačidlo na poschodí, prízemie |
hodgins | 0:e541909ea6b0 | 82 | case 0xb0: elevatorLedON(02);elevatorButton[0] = 2; break; //Tlačidlo vo výťahu, prízemie |
hodgins | 0:e541909ea6b0 | 83 | case 0xc1: elevatorLedON(11);elevatorButton[1] = 1; break; |
hodgins | 0:e541909ea6b0 | 84 | case 0xb1: elevatorLedON(12);elevatorButton[1] = 2; break; |
hodgins | 0:e541909ea6b0 | 85 | case 0xc2: elevatorLedON(21);elevatorButton[2] = 1; break; |
hodgins | 0:e541909ea6b0 | 86 | case 0xb2: elevatorLedON(22);elevatorButton[2] = 2; break; |
hodgins | 0:e541909ea6b0 | 87 | case 0xc3: elevatorLedON(31);elevatorButton[3] = 1; break; |
hodgins | 0:e541909ea6b0 | 88 | case 0xb3: elevatorLedON(32);elevatorButton[3] = 2; break; |
hodgins | 0:e541909ea6b0 | 89 | case 0xc4: elevatorLedON(41);elevatorButton[4] = 1; break; |
hodgins | 0:e541909ea6b0 | 90 | case 0xb4: elevatorLedON(42);elevatorButton[4] = 2; break; //Tlačidlo vo výťahu, 4.poschodie |
hodgins | 0:e541909ea6b0 | 91 | |
hodgins | 0:e541909ea6b0 | 92 | //Správa indikovala, že bol zopnutý/uvolnený niektorý zo snímačov |
hodgins | 0:e541909ea6b0 | 93 | case 0xe0: if(buffer[actualMess].data[0] == 0x00){ elevatorSensor[0] = 0;break;} //Uvolenie spínačov, prízemie |
hodgins | 0:e541909ea6b0 | 94 | if(buffer[actualMess].data[0] == 0x01){ elevatorSensor[0] = 1;break;} //Žltý spínač, prízemie |
hodgins | 0:e541909ea6b0 | 95 | if(buffer[actualMess].data[0] == 0x02){ elevatorSensor[0] = 2;}break; //Červený snímač, prízemie |
hodgins | 0:e541909ea6b0 | 96 | case 0xe1: if(buffer[actualMess].data[0] == 0x00){ elevatorSensor[1] = 0;break;} |
hodgins | 0:e541909ea6b0 | 97 | if(buffer[actualMess].data[0] == 0x01){ elevatorSensor[1] = 1;break;} |
hodgins | 0:e541909ea6b0 | 98 | if(buffer[actualMess].data[0] == 0x02){ elevatorSensor[1] = 2;}break; |
hodgins | 0:e541909ea6b0 | 99 | case 0xe2: if(buffer[actualMess].data[0] == 0x00){ elevatorSensor[2] = 0;break;} |
hodgins | 0:e541909ea6b0 | 100 | if(buffer[actualMess].data[0] == 0x01){ elevatorSensor[2] = 1;break;} |
hodgins | 0:e541909ea6b0 | 101 | if(buffer[actualMess].data[0] == 0x02){ elevatorSensor[2] = 2;}break; |
hodgins | 0:e541909ea6b0 | 102 | case 0xe3: if(buffer[actualMess].data[0] == 0x00){ elevatorSensor[3] = 0;break;} |
hodgins | 0:e541909ea6b0 | 103 | if(buffer[actualMess].data[0] == 0x01){ elevatorSensor[3] = 1;break;} |
hodgins | 0:e541909ea6b0 | 104 | if(buffer[actualMess].data[0] == 0x02){ elevatorSensor[3] = 2;}break; |
hodgins | 0:e541909ea6b0 | 105 | case 0xe4: if(buffer[actualMess].data[0] == 0x00){ elevatorSensor[4] = 0;break;} |
hodgins | 0:e541909ea6b0 | 106 | if(buffer[actualMess].data[0] == 0x01){ elevatorSensor[4] = 1;break;} |
hodgins | 0:e541909ea6b0 | 107 | if(buffer[actualMess].data[0] == 0x02){ elevatorSensor[4] = 2;}break; //Červený snímač, 4.poschodie |
hodgins | 0:e541909ea6b0 | 108 | |
hodgins | 0:e541909ea6b0 | 109 | //Správa pre konzolu alebo pre iné periférie, odosielanie na konzolu výťahu |
hodgins | 0:e541909ea6b0 | 110 | default: |
hodgins | 0:e541909ea6b0 | 111 | char sendBackMess[buffer[actualMess].dlzkaDat + 1]; //Vysklanie paketu pre odoslanie správy |
hodgins | 0:e541909ea6b0 | 112 | sendBackMess[0] = buffer[actualMess].adresa; |
hodgins | 0:e541909ea6b0 | 113 | for (int i=0; i < buffer[actualMess].dlzkaDat; i++) |
hodgins | 0:e541909ea6b0 | 114 | { |
hodgins | 0:e541909ea6b0 | 115 | sendBackMess[i+1] = buffer[actualMess].data[i]; |
hodgins | 0:e541909ea6b0 | 116 | } |
hodgins | 0:e541909ea6b0 | 117 | paket messege = {.adresa = 0xd0, .dlzkaDat = buffer[actualMess].dlzkaDat + 1, .data = sendBackMess}; |
hodgins | 0:e541909ea6b0 | 118 | sendData(messege); //Odoslanie správy |
hodgins | 0:e541909ea6b0 | 119 | } |
hodgins | 0:e541909ea6b0 | 120 | |
hodgins | 0:e541909ea6b0 | 121 | free(buffer[actualMess].data); // Uvolenie pamäte, kde boli uložené dáta |
hodgins | 0:e541909ea6b0 | 122 | if((actualMess++) > MESS_BUFFER) // Cyklický Buffer |
hodgins | 0:e541909ea6b0 | 123 | { |
hodgins | 0:e541909ea6b0 | 124 | actualMess = 0; |
hodgins | 0:e541909ea6b0 | 125 | } |
hodgins | 0:e541909ea6b0 | 126 | } |
hodgins | 0:e541909ea6b0 | 127 | PT_END(); |
hodgins | 0:e541909ea6b0 | 128 | } |
hodgins | 0:e541909ea6b0 | 129 | |
hodgins | 0:e541909ea6b0 | 130 | //Vlakon na pohyb vytahu |
hodgins | 0:e541909ea6b0 | 131 | bool Elevator::Run() |
hodgins | 0:e541909ea6b0 | 132 | { |
hodgins | 0:e541909ea6b0 | 133 | PT_BEGIN(); // inicializácia vlákna |
hodgins | 0:e541909ea6b0 | 134 | while(1) { // nekonečná slučka |
hodgins | 0:e541909ea6b0 | 135 | |
hodgins | 0:e541909ea6b0 | 136 | PT_WAIT_UNTIL(moveOn(actualFloor)); //Vlákno čaká, kým sa výťah nemá kam pohnúť |
hodgins | 0:e541909ea6b0 | 137 | endFloor = nextFloor(); //kam idem - poschodie |
hodgins | 0:e541909ea6b0 | 138 | |
hodgins | 0:e541909ea6b0 | 139 | if ((endFloor - actualFloor) > 0) //Zisťovanie smeru pohybu |
hodgins | 0:e541909ea6b0 | 140 | { |
hodgins | 0:e541909ea6b0 | 141 | moveDirection = 1; |
hodgins | 0:e541909ea6b0 | 142 | } |
hodgins | 0:e541909ea6b0 | 143 | else |
hodgins | 0:e541909ea6b0 | 144 | { |
hodgins | 0:e541909ea6b0 | 145 | moveDirection = -1; |
hodgins | 0:e541909ea6b0 | 146 | } |
hodgins | 0:e541909ea6b0 | 147 | moveElevator(moveDirection); //pohnem sa smerom, ktorý som si vyššie zistil |
hodgins | 0:e541909ea6b0 | 148 | elevatorDisplay(moveDirection, actualFloor); //Zobrazenie pohybu na dispeji |
hodgins | 0:e541909ea6b0 | 149 | elevatorCabineLock(true); //Zamknutie kabíny |
hodgins | 0:e541909ea6b0 | 150 | |
hodgins | 0:e541909ea6b0 | 151 | while (((endFloor - actualFloor) * moveDirection) > 1) // Ošetrenie medzizatávok |
hodgins | 0:e541909ea6b0 | 152 | { |
hodgins | 0:e541909ea6b0 | 153 | elevatorLedOFF(actualFloor); |
hodgins | 0:e541909ea6b0 | 154 | |
hodgins | 0:e541909ea6b0 | 155 | PT_WAIT_UNTIL(elevatorSensor[actualFloor + moveDirection] == 1); //Čakám na žltý senzor |
hodgins | 0:e541909ea6b0 | 156 | if (elevatorButton[actualFloor + moveDirection] > 0) //Zisťujem, či na danom poschodí mám zastaviť |
hodgins | 0:e541909ea6b0 | 157 | { |
hodgins | 0:e541909ea6b0 | 158 | moveElevator(0); |
hodgins | 0:e541909ea6b0 | 159 | } |
hodgins | 0:e541909ea6b0 | 160 | PT_WAIT_UNTIL(elevatorSensor[actualFloor + moveDirection] == 2); //čakám na červený senzor |
hodgins | 0:e541909ea6b0 | 161 | if (elevatorButton[actualFloor + moveDirection] > 0) //Zisťujem či mám zastaviť |
hodgins | 0:e541909ea6b0 | 162 | { |
hodgins | 0:e541909ea6b0 | 163 | elevatorDisplay(0, actualFloor + moveDirection); //Rutina pri zastavení výťahu |
hodgins | 0:e541909ea6b0 | 164 | elevatorButton[actualFloor + moveDirection] = 0; |
hodgins | 0:e541909ea6b0 | 165 | elevatorLedOFF(actualFloor + moveDirection); |
hodgins | 0:e541909ea6b0 | 166 | elevatorCabineLock(false); |
hodgins | 0:e541909ea6b0 | 167 | |
hodgins | 0:e541909ea6b0 | 168 | swTimer1.startTimer(_timer); //Doba na nástup pasažierov |
hodgins | 0:e541909ea6b0 | 169 | while (!swTimer1.isExpired()) |
hodgins | 0:e541909ea6b0 | 170 | { |
hodgins | 0:e541909ea6b0 | 171 | PT_YIELD(); //Uspanie vlákna |
hodgins | 0:e541909ea6b0 | 172 | } |
hodgins | 0:e541909ea6b0 | 173 | |
hodgins | 0:e541909ea6b0 | 174 | elevatorDisplay(moveDirection, actualFloor); |
hodgins | 0:e541909ea6b0 | 175 | elevatorCabineLock(true); |
hodgins | 0:e541909ea6b0 | 176 | moveElevator(moveDirection); //Pohnutie sa výŤahu na ďalšie poschodie |
hodgins | 0:e541909ea6b0 | 177 | } |
hodgins | 0:e541909ea6b0 | 178 | |
hodgins | 0:e541909ea6b0 | 179 | actualFloor += moveDirection; //Zápis, o aktuálnom poschodí |
hodgins | 0:e541909ea6b0 | 180 | elevatorDisplay(moveDirection, actualFloor); |
hodgins | 0:e541909ea6b0 | 181 | } |
hodgins | 0:e541909ea6b0 | 182 | |
hodgins | 0:e541909ea6b0 | 183 | PT_WAIT_UNTIL(elevatorSensor[endFloor] == 1); // Čakanie na žltý snezor na koncovom poschodí |
hodgins | 0:e541909ea6b0 | 184 | moveElevator(0); // Zastavenie |
hodgins | 0:e541909ea6b0 | 185 | |
hodgins | 0:e541909ea6b0 | 186 | PT_WAIT_UNTIL(elevatorSensor[endFloor] == 2); // Čakanie na Červený snezor na koncovom poschodí |
hodgins | 0:e541909ea6b0 | 187 | elevatorButton[endFloor] = 0; // Runita pri zastavení |
hodgins | 0:e541909ea6b0 | 188 | actualFloor = endFloor; |
hodgins | 0:e541909ea6b0 | 189 | |
hodgins | 0:e541909ea6b0 | 190 | elevatorDisplay(0,actualFloor); |
hodgins | 0:e541909ea6b0 | 191 | elevatorLedOFF(actualFloor); |
hodgins | 0:e541909ea6b0 | 192 | elevatorCabineLock(false); |
hodgins | 0:e541909ea6b0 | 193 | |
hodgins | 0:e541909ea6b0 | 194 | swTimer1.startTimer(_timer); //Doba na nástup pasažierov |
hodgins | 0:e541909ea6b0 | 195 | while(!swTimer1.isExpired()) { |
hodgins | 0:e541909ea6b0 | 196 | PT_YIELD(); |
hodgins | 0:e541909ea6b0 | 197 | } |
hodgins | 0:e541909ea6b0 | 198 | } |
hodgins | 0:e541909ea6b0 | 199 | PT_END(); |
hodgins | 0:e541909ea6b0 | 200 | } |
hodgins | 0:e541909ea6b0 | 201 | |
hodgins | 0:e541909ea6b0 | 202 | //Funkcia na odosielanie správ |
hodgins | 0:e541909ea6b0 | 203 | void sendData(paket sprava) { |
hodgins | 0:e541909ea6b0 | 204 | unsigned char crc = 0; |
hodgins | 0:e541909ea6b0 | 205 | |
hodgins | 0:e541909ea6b0 | 206 | serial.putc(START_BYTE); //Štartovaí znak |
hodgins | 0:e541909ea6b0 | 207 | |
hodgins | 0:e541909ea6b0 | 208 | crc = CRC8_TAB[crc ^ sprava.adresa]; //Výpočet CRC |
hodgins | 0:e541909ea6b0 | 209 | serial.putc(sprava.adresa); //Odoslanie adresi príjemcu |
hodgins | 0:e541909ea6b0 | 210 | |
hodgins | 0:e541909ea6b0 | 211 | crc = CRC8_TAB[crc ^ MY_ADDR]; //Výpočet CRC |
hodgins | 0:e541909ea6b0 | 212 | serial.putc(MY_ADDR); //Odoslanie adresi odosielateľa |
hodgins | 0:e541909ea6b0 | 213 | |
hodgins | 0:e541909ea6b0 | 214 | serial.putc(sprava.dlzkaDat); //Odoslanie informácií o veľkosti dát |
hodgins | 0:e541909ea6b0 | 215 | |
hodgins | 0:e541909ea6b0 | 216 | for (int i = 0; i < sprava.dlzkaDat ; i++) |
hodgins | 0:e541909ea6b0 | 217 | { |
hodgins | 0:e541909ea6b0 | 218 | crc = CRC8_TAB[crc ^ sprava.data[i]]; //Výpočet CRC |
hodgins | 0:e541909ea6b0 | 219 | serial.putc(sprava.data[i]); //Odosielanie dát |
hodgins | 0:e541909ea6b0 | 220 | } |
hodgins | 0:e541909ea6b0 | 221 | |
hodgins | 0:e541909ea6b0 | 222 | serial.putc(crc); //Odoslanie CRC kontolného súčtu |
hodgins | 0:e541909ea6b0 | 223 | } |
hodgins | 0:e541909ea6b0 | 224 | |
hodgins | 0:e541909ea6b0 | 225 | //Funkcia na príjem správ |
hodgins | 0:e541909ea6b0 | 226 | void recieveData() |
hodgins | 0:e541909ea6b0 | 227 | { |
hodgins | 0:e541909ea6b0 | 228 | unsigned char adresa_O; |
hodgins | 0:e541909ea6b0 | 229 | unsigned char adresa_P; |
hodgins | 0:e541909ea6b0 | 230 | unsigned char dlzkaDat; |
hodgins | 0:e541909ea6b0 | 231 | unsigned char crc = 0; |
hodgins | 0:e541909ea6b0 | 232 | unsigned char recievedCRC; |
hodgins | 0:e541909ea6b0 | 233 | |
hodgins | 0:e541909ea6b0 | 234 | if(serial.getc() == START_BYTE) //Čakaniena start byte a jeho kontrola |
hodgins | 0:e541909ea6b0 | 235 | { |
hodgins | 0:e541909ea6b0 | 236 | adresa_O = serial.getc(); //Adresa odosielateľa |
hodgins | 0:e541909ea6b0 | 237 | crc = CRC8_TAB[crc ^ adresa_O]; |
hodgins | 0:e541909ea6b0 | 238 | |
hodgins | 0:e541909ea6b0 | 239 | adresa_P = serial.getc(); //Adresa príjmateľa |
hodgins | 0:e541909ea6b0 | 240 | crc = CRC8_TAB[crc ^ adresa_P]; |
hodgins | 0:e541909ea6b0 | 241 | |
hodgins | 0:e541909ea6b0 | 242 | dlzkaDat = serial.getc(); //Dĺžka dátovej časti |
hodgins | 0:e541909ea6b0 | 243 | char *data=(char*)malloc(sizeof(char)*dlzkaDat); //Alokácia miesta pre dáta |
hodgins | 0:e541909ea6b0 | 244 | |
hodgins | 0:e541909ea6b0 | 245 | for(int i=0; i<dlzkaDat; i++) //Príjem dát |
hodgins | 0:e541909ea6b0 | 246 | { |
hodgins | 0:e541909ea6b0 | 247 | data[i] = serial.getc(); |
hodgins | 0:e541909ea6b0 | 248 | crc = CRC8_TAB[crc ^ data[i]]; |
hodgins | 0:e541909ea6b0 | 249 | |
hodgins | 0:e541909ea6b0 | 250 | } |
hodgins | 0:e541909ea6b0 | 251 | recievedCRC = serial.getc(); // Príjem CRC |
hodgins | 0:e541909ea6b0 | 252 | |
hodgins | 0:e541909ea6b0 | 253 | if(recievedCRC == crc && dlzkaDat != 0) // Kontola CRC a potvrdzovacích správ |
hodgins | 0:e541909ea6b0 | 254 | { |
hodgins | 0:e541909ea6b0 | 255 | paket sprava = { .adresa = adresa_P, .dlzkaDat = dlzkaDat, .data = data}; |
hodgins | 0:e541909ea6b0 | 256 | buffer [indexBuffer] = sprava; //pridanie správy do buffra |
hodgins | 0:e541909ea6b0 | 257 | if((indexBuffer++) > MESS_BUFFER){ //Kruhový buffer |
hodgins | 0:e541909ea6b0 | 258 | indexBuffer = 0; |
hodgins | 0:e541909ea6b0 | 259 | } |
hodgins | 0:e541909ea6b0 | 260 | } |
hodgins | 0:e541909ea6b0 | 261 | } |
hodgins | 0:e541909ea6b0 | 262 | //serial.attach(&recieveData); //Povolenie prerušenia |
hodgins | 0:e541909ea6b0 | 263 | } |
hodgins | 0:e541909ea6b0 | 264 | |
hodgins | 0:e541909ea6b0 | 265 | void initData() |
hodgins | 0:e541909ea6b0 | 266 | { |
hodgins | 0:e541909ea6b0 | 267 | elevatorButton[0] = 0; |
hodgins | 0:e541909ea6b0 | 268 | elevatorButton[1] = 0; |
hodgins | 0:e541909ea6b0 | 269 | elevatorButton[2] = 0; |
hodgins | 0:e541909ea6b0 | 270 | elevatorButton[3] = 0; |
hodgins | 0:e541909ea6b0 | 271 | elevatorButton[4] = 0; |
hodgins | 0:e541909ea6b0 | 272 | |
hodgins | 0:e541909ea6b0 | 273 | elevatorLedOFF(0); |
hodgins | 0:e541909ea6b0 | 274 | elevatorLedOFF(1); |
hodgins | 0:e541909ea6b0 | 275 | elevatorLedOFF(2); |
hodgins | 0:e541909ea6b0 | 276 | elevatorLedOFF(3); |
hodgins | 0:e541909ea6b0 | 277 | elevatorLedOFF(4); |
hodgins | 0:e541909ea6b0 | 278 | |
hodgins | 0:e541909ea6b0 | 279 | elevatorButton[0] = 1; //Inicializácia simulátora - výťah sa pohne na prízemie |
hodgins | 0:e541909ea6b0 | 280 | } |
hodgins | 0:e541909ea6b0 | 281 | |
hodgins | 0:e541909ea6b0 | 282 | //Začiatok aplikácie |
hodgins | 0:e541909ea6b0 | 283 | int main() |
hodgins | 0:e541909ea6b0 | 284 | { |
hodgins | 0:e541909ea6b0 | 285 | init_freeFall(); //Inicializácie kontroly na voľný pád |
hodgins | 0:e541909ea6b0 | 286 | init_watchDog(0.25f); //Inicializácie pre WatchDog |
hodgins | 0:e541909ea6b0 | 287 | |
hodgins | 0:e541909ea6b0 | 288 | serial.attach(&recieveData); //Nastavenie prerušenia na sériový port |
hodgins | 0:e541909ea6b0 | 289 | elevatorOpen.attach(&timeTick, (float)msPerTick / 1000 ); //Nastavnie prerušenia na počítadlo |
hodgins | 0:e541909ea6b0 | 290 | |
hodgins | 0:e541909ea6b0 | 291 | initData(); |
hodgins | 0:e541909ea6b0 | 292 | |
hodgins | 0:e541909ea6b0 | 293 | MessExecute messExecute(1); //Inicializácia 1. vlákno na spracovanie prijatých správ |
hodgins | 0:e541909ea6b0 | 294 | Elevator elevator(2, &getTimer, 3000); //Inicializácia vlákna pre ovládanie výťahu |
hodgins | 0:e541909ea6b0 | 295 | |
hodgins | 0:e541909ea6b0 | 296 | wait(1); |
hodgins | 0:e541909ea6b0 | 297 | |
hodgins | 0:e541909ea6b0 | 298 | // Pokial nedostanem spravu o tom ze vytah pada volnym padom spustma vlakna |
hodgins | 0:e541909ea6b0 | 299 | while (!freeFall_status()) { // Round-robin planovac |
hodgins | 0:e541909ea6b0 | 300 | messExecute.Run(); // Spustenie vlakna 1 |
hodgins | 0:e541909ea6b0 | 301 | elevator.Run(); // Spustenie vlakna 2 |
hodgins | 0:e541909ea6b0 | 302 | } |
hodgins | 0:e541909ea6b0 | 303 | |
hodgins | 0:e541909ea6b0 | 304 | elevatorCabineLock(false); // Po volnom pade je nutne zavolat mechnika, vytah sa neda viac pouzivat |
hodgins | 0:e541909ea6b0 | 305 | } |