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