
S05APP2: Noyau temps réel
main.cpp@18:01c6a7ee0914, 2017-01-30 (annotated)
- Committer:
- benjaminroy
- Date:
- Mon Jan 30 15:34:59 2017 +0000
- Revision:
- 18:01c6a7ee0914
- Parent:
- 14:3b72daf433fb
- Child:
- 19:abddc058f6d1
Essayer de r?duire ? 32 ko...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benjaminroy | 18:01c6a7ee0914 | 1 | #include "AnalogIn.h" |
benjaminroy | 18:01c6a7ee0914 | 2 | #include "DigitalIn.h" |
benjaminroy | 18:01c6a7ee0914 | 3 | #include "Ticker.h" |
benjaminroy | 18:01c6a7ee0914 | 4 | #include "Serial.h" |
benjaminroy | 1:d37b1a61bd0b | 5 | #include "rtos.h" |
benjaminroy | 13:29991a8767fe | 6 | |
benjaminroy | 13:29991a8767fe | 7 | Serial pc(USBTX, USBRX); // tx, rx |
benjaminroy | 3:fa3112b2216c | 8 | Ticker ticker; |
benjaminroy | 1:d37b1a61bd0b | 9 | DigitalIn en_1(p15); |
benjaminroy | 1:d37b1a61bd0b | 10 | DigitalIn en_2(p16); |
benjaminroy | 1:d37b1a61bd0b | 11 | AnalogIn ea_1(p19); |
benjaminroy | 1:d37b1a61bd0b | 12 | AnalogIn ea_2(p20); |
marc1119 | 2:94e2eebf1524 | 13 | Thread *t1; |
marc1119 | 2:94e2eebf1524 | 14 | Thread *t2; |
marc1119 | 2:94e2eebf1524 | 15 | Thread *t3; |
benjaminroy | 18:01c6a7ee0914 | 16 | Mutex mutex; |
marc1119 | 2:94e2eebf1524 | 17 | |
benjaminroy | 3:fa3112b2216c | 18 | uint8_t compteur = 0; |
benjaminroy | 5:79b5773e6764 | 19 | bool verifierSiStable = false; |
benjaminroy | 6:6da143aff561 | 20 | |
benjaminroy | 9:8549816b1cac | 21 | /* Mail */ |
marc1119 | 7:e9f5280d47b3 | 22 | typedef struct { |
benjaminroy | 9:8549816b1cac | 23 | char* temps; // Étampe indiquant l’heure et la date de l'occurrence |
benjaminroy | 9:8549816b1cac | 24 | uint8_t broche; // Le numéro de la broche |
marc1119 | 7:e9f5280d47b3 | 25 | } mail_t; |
marc1119 | 7:e9f5280d47b3 | 26 | |
benjaminroy | 14:3b72daf433fb | 27 | Mail<mail_t, 16> mail_box; |
benjaminroy | 9:8549816b1cac | 28 | |
benjaminroy | 3:fa3112b2216c | 29 | // ------------------------------------------------------- |
benjaminroy | 3:fa3112b2216c | 30 | // ------------------------------------------------------- |
benjaminroy | 3:fa3112b2216c | 31 | |
benjaminroy | 18:01c6a7ee0914 | 32 | void envoyer_mail(uint8_t broche) { |
benjaminroy | 18:01c6a7ee0914 | 33 | mutex.lock(); |
benjaminroy | 9:8549816b1cac | 34 | time_t secondes = time(NULL); |
benjaminroy | 18:01c6a7ee0914 | 35 | mutex.unlock(); |
benjaminroy | 18:01c6a7ee0914 | 36 | |
benjaminroy | 9:8549816b1cac | 37 | mail_t *mail = mail_box.alloc(); |
benjaminroy | 9:8549816b1cac | 38 | mail->temps = ctime(&secondes); |
benjaminroy | 9:8549816b1cac | 39 | mail->broche = broche; |
marc1119 | 7:e9f5280d47b3 | 40 | mail_box.put(mail); |
marc1119 | 7:e9f5280d47b3 | 41 | } |
marc1119 | 7:e9f5280d47b3 | 42 | |
benjaminroy | 1:d37b1a61bd0b | 43 | void lecture_analog(void const *args) { |
benjaminroy | 9:8549816b1cac | 44 | uint8_t i = 0; |
benjaminroy | 6:6da143aff561 | 45 | float moyenne_passee1 = -1; |
benjaminroy | 6:6da143aff561 | 46 | float moyenne_passee2 = -1; |
benjaminroy | 14:3b72daf433fb | 47 | float echantillons1[5]; |
benjaminroy | 18:01c6a7ee0914 | 48 | float echantillons2[5]; |
benjaminroy | 18:01c6a7ee0914 | 49 | float seuil = 0.125; |
benjaminroy | 14:3b72daf433fb | 50 | |
benjaminroy | 6:6da143aff561 | 51 | while (true) { |
benjaminroy | 9:8549816b1cac | 52 | /*** Synchronisation sur la période d'échantillonnage ***/ |
benjaminroy | 6:6da143aff561 | 53 | Thread::signal_wait(0x1); |
benjaminroy | 6:6da143aff561 | 54 | |
benjaminroy | 9:8549816b1cac | 55 | /*** Lecture des échantillons analogiques ***/ |
benjaminroy | 6:6da143aff561 | 56 | float entreeanal_1 = ea_1.read(); |
benjaminroy | 6:6da143aff561 | 57 | float entreeanal_2 = ea_2.read(); |
benjaminroy | 6:6da143aff561 | 58 | |
benjaminroy | 6:6da143aff561 | 59 | echantillons1[i] = entreeanal_1; |
benjaminroy | 6:6da143aff561 | 60 | echantillons2[i] = entreeanal_2; |
benjaminroy | 6:6da143aff561 | 61 | |
benjaminroy | 6:6da143aff561 | 62 | i++; |
benjaminroy | 6:6da143aff561 | 63 | if (i == 5) { |
benjaminroy | 9:8549816b1cac | 64 | /*** Calcul de la moyenne courante du signal (calculée sur 5 échantillons successifs) ***/ |
benjaminroy | 6:6da143aff561 | 65 | float moyenne_courante1 = (echantillons1[0] + echantillons1[1] + echantillons1[2] + echantillons1[3] + echantillons1[4]) / 5; |
benjaminroy | 6:6da143aff561 | 66 | float moyenne_courante2 = (echantillons2[0] + echantillons2[1] + echantillons2[2] + echantillons2[3] + echantillons2[4]) / 5; |
benjaminroy | 6:6da143aff561 | 67 | |
benjaminroy | 6:6da143aff561 | 68 | if (moyenne_passee1 != -1) { |
benjaminroy | 6:6da143aff561 | 69 | if ((std::abs(1 - (moyenne_courante1 / moyenne_passee1))) > seuil) { |
benjaminroy | 18:01c6a7ee0914 | 70 | envoyer_mail(19); /*** Génération éventuelle d'un événement ***/ |
benjaminroy | 6:6da143aff561 | 71 | } |
benjaminroy | 6:6da143aff561 | 72 | } |
benjaminroy | 6:6da143aff561 | 73 | if (moyenne_passee2 != -1) { |
benjaminroy | 6:6da143aff561 | 74 | if ((std::abs(1 - (moyenne_courante2 / moyenne_passee2))) > seuil) { |
benjaminroy | 18:01c6a7ee0914 | 75 | envoyer_mail(20); /*** Génération éventuelle d'un événement ***/ |
benjaminroy | 6:6da143aff561 | 76 | } |
benjaminroy | 6:6da143aff561 | 77 | } |
benjaminroy | 6:6da143aff561 | 78 | moyenne_passee1 = moyenne_courante1; |
benjaminroy | 6:6da143aff561 | 79 | moyenne_passee2 = moyenne_courante2; |
benjaminroy | 6:6da143aff561 | 80 | i = 0; |
benjaminroy | 6:6da143aff561 | 81 | } |
benjaminroy | 0:4f447b02a32f | 82 | } |
benjaminroy | 0:4f447b02a32f | 83 | } |
marc1119 | 2:94e2eebf1524 | 84 | |
benjaminroy | 14:3b72daf433fb | 85 | void lecture_num(void const *args) { |
benjaminroy | 18:01c6a7ee0914 | 86 | bool valeurs[2] = {0}; |
benjaminroy | 14:3b72daf433fb | 87 | |
benjaminroy | 1:d37b1a61bd0b | 88 | while (true) { |
benjaminroy | 9:8549816b1cac | 89 | /*** Synchronisation sur la période d'échantillonnage ***/ |
marc1119 | 2:94e2eebf1524 | 90 | Thread::signal_wait(0x1); |
benjaminroy | 6:6da143aff561 | 91 | |
benjaminroy | 9:8549816b1cac | 92 | /*** Lecture des échantillons numériques ***/ |
benjaminroy | 3:fa3112b2216c | 93 | uint8_t entreenum_1 = en_1.read(); |
benjaminroy | 3:fa3112b2216c | 94 | uint8_t entreenum_2 = en_2.read(); |
benjaminroy | 3:fa3112b2216c | 95 | |
benjaminroy | 9:8549816b1cac | 96 | /*** Prise en charge du phénomène de rebond ***/ |
benjaminroy | 18:01c6a7ee0914 | 97 | if (entreenum_1 != valeurs[0]) { |
benjaminroy | 5:79b5773e6764 | 98 | verifierSiStable = true; |
benjaminroy | 5:79b5773e6764 | 99 | Thread::signal_wait(0x2); |
benjaminroy | 18:01c6a7ee0914 | 100 | valeurs[0] = entreenum_1; |
benjaminroy | 5:79b5773e6764 | 101 | |
benjaminroy | 13:29991a8767fe | 102 | if (entreenum_1 == en_1.read()) { |
benjaminroy | 18:01c6a7ee0914 | 103 | envoyer_mail(15); /*** Génération d'un évènement... ***/ |
benjaminroy | 10:200ebb491b60 | 104 | } |
benjaminroy | 13:29991a8767fe | 105 | } |
benjaminroy | 18:01c6a7ee0914 | 106 | if (entreenum_2 != valeurs[1]) { |
benjaminroy | 13:29991a8767fe | 107 | verifierSiStable = true; |
benjaminroy | 13:29991a8767fe | 108 | Thread::signal_wait(0x2); |
benjaminroy | 18:01c6a7ee0914 | 109 | valeurs[1] = entreenum_2; |
benjaminroy | 13:29991a8767fe | 110 | |
benjaminroy | 13:29991a8767fe | 111 | if (entreenum_2 == en_2.read()) { |
benjaminroy | 18:01c6a7ee0914 | 112 | envoyer_mail(16); /*** Génération d'un évènement... ***/ |
benjaminroy | 5:79b5773e6764 | 113 | } |
benjaminroy | 13:29991a8767fe | 114 | } |
benjaminroy | 13:29991a8767fe | 115 | Thread::yield(); |
benjaminroy | 1:d37b1a61bd0b | 116 | } |
benjaminroy | 1:d37b1a61bd0b | 117 | } |
marc1119 | 2:94e2eebf1524 | 118 | |
benjaminroy | 1:d37b1a61bd0b | 119 | void collection(void const *args) { |
benjaminroy | 1:d37b1a61bd0b | 120 | while (true) { |
benjaminroy | 9:8549816b1cac | 121 | /*** Attente et lecture d'un événement ***/ |
marc1119 | 7:e9f5280d47b3 | 122 | osEvent evt = mail_box.get(); |
marc1119 | 7:e9f5280d47b3 | 123 | if (evt.status == osEventMail) { |
marc1119 | 7:e9f5280d47b3 | 124 | mail_t *mail = (mail_t*)evt.value.p; |
benjaminroy | 9:8549816b1cac | 125 | |
benjaminroy | 9:8549816b1cac | 126 | /*** Écriture de l'événement en sortie (port série) ***/ |
benjaminroy | 18:01c6a7ee0914 | 127 | pc.printf("Entree %d\nDate: %s\n", mail->broche, mail->temps); |
marc1119 | 7:e9f5280d47b3 | 128 | mail_box.free(mail); |
benjaminroy | 18:01c6a7ee0914 | 129 | } |
benjaminroy | 1:d37b1a61bd0b | 130 | } |
benjaminroy | 1:d37b1a61bd0b | 131 | } |
marc1119 | 2:94e2eebf1524 | 132 | |
benjaminroy | 3:fa3112b2216c | 133 | void alarm() { |
benjaminroy | 3:fa3112b2216c | 134 | compteur++; |
benjaminroy | 5:79b5773e6764 | 135 | |
benjaminroy | 5:79b5773e6764 | 136 | if (verifierSiStable) { |
benjaminroy | 5:79b5773e6764 | 137 | t1->signal_set(0x2); |
benjaminroy | 5:79b5773e6764 | 138 | verifierSiStable = false; |
benjaminroy | 5:79b5773e6764 | 139 | } |
benjaminroy | 3:fa3112b2216c | 140 | |
benjaminroy | 9:8549816b1cac | 141 | if (compteur % 2 == 0) { // Entrées numériques échantillionnées à tous les 100 ms |
benjaminroy | 3:fa3112b2216c | 142 | t1->signal_set(0x1); |
benjaminroy | 9:8549816b1cac | 143 | } else if (compteur % 5 == 0) { // Entrées analogiques échantillonnées à tous les 250 ms |
benjaminroy | 6:6da143aff561 | 144 | t2->signal_set(0x1); |
marc1119 | 2:94e2eebf1524 | 145 | } |
benjaminroy | 3:fa3112b2216c | 146 | |
benjaminroy | 3:fa3112b2216c | 147 | if (compteur % 10 == 0) { |
benjaminroy | 3:fa3112b2216c | 148 | compteur = 0; |
benjaminroy | 3:fa3112b2216c | 149 | } |
marc1119 | 2:94e2eebf1524 | 150 | } |
marc1119 | 2:94e2eebf1524 | 151 | |
benjaminroy | 1:d37b1a61bd0b | 152 | int main() { |
benjaminroy | 9:8549816b1cac | 153 | /*** Démarrage des tâches ***/ |
benjaminroy | 14:3b72daf433fb | 154 | |
marc1119 | 2:94e2eebf1524 | 155 | Thread t_lectureNum(lecture_num); |
benjaminroy | 6:6da143aff561 | 156 | Thread t_lectureAnalog(lecture_analog); |
marc1119 | 2:94e2eebf1524 | 157 | Thread t_collection(collection); |
marc1119 | 2:94e2eebf1524 | 158 | |
marc1119 | 2:94e2eebf1524 | 159 | t1 = &t_lectureNum; |
benjaminroy | 6:6da143aff561 | 160 | t2 = &t_lectureAnalog; |
marc1119 | 2:94e2eebf1524 | 161 | t3 = &t_collection; |
benjaminroy | 3:fa3112b2216c | 162 | |
benjaminroy | 9:8549816b1cac | 163 | /*** Initialisation du RTC ***/ |
benjaminroy | 9:8549816b1cac | 164 | set_time(1485514920); // Set RTC time to... |
benjaminroy | 6:6da143aff561 | 165 | ticker.attach(&alarm, 0.05); |
benjaminroy | 5:79b5773e6764 | 166 | |
benjaminroy | 8:56f2b8db5361 | 167 | while(1); |
benjaminroy | 1:d37b1a61bd0b | 168 | } |