![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
School Project
main.cpp@1:b3ae0d9f02ad, 2017-01-30 (annotated)
- Committer:
- ericbisson
- Date:
- Mon Jan 30 22:40:48 2017 +0000
- Revision:
- 1:b3ae0d9f02ad
- Parent:
- 0:c637467eeb8f
- Child:
- 2:c6465d4e82d2
pour joey
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ericbisson | 0:c637467eeb8f | 1 | #include "mbed.h" |
ericbisson | 0:c637467eeb8f | 2 | #include "rtos.h" |
ericbisson | 0:c637467eeb8f | 3 | |
ericbisson | 1:b3ae0d9f02ad | 4 | Ticker tick1; |
ericbisson | 1:b3ae0d9f02ad | 5 | Ticker tick2; |
ericbisson | 0:c637467eeb8f | 6 | Thread* t1; |
ericbisson | 0:c637467eeb8f | 7 | Thread* t2; |
ericbisson | 0:c637467eeb8f | 8 | Thread* t3; |
ericbisson | 0:c637467eeb8f | 9 | |
ericbisson | 0:c637467eeb8f | 10 | DigitalIn en_1(p15); |
ericbisson | 0:c637467eeb8f | 11 | DigitalIn en_2(p16); |
ericbisson | 0:c637467eeb8f | 12 | AnalogIn ea_1(p19); |
ericbisson | 0:c637467eeb8f | 13 | AnalogIn ea_2(p20); |
ericbisson | 0:c637467eeb8f | 14 | Serial pc(USBTX, USBRX); |
ericbisson | 1:b3ae0d9f02ad | 15 | Mutex mutex; |
ericbisson | 1:b3ae0d9f02ad | 16 | |
ericbisson | 1:b3ae0d9f02ad | 17 | struct message_t |
ericbisson | 1:b3ae0d9f02ad | 18 | { |
ericbisson | 1:b3ae0d9f02ad | 19 | time_t timestamp; |
ericbisson | 1:b3ae0d9f02ad | 20 | char pin_id; |
ericbisson | 1:b3ae0d9f02ad | 21 | unsigned short value; |
ericbisson | 1:b3ae0d9f02ad | 22 | }; |
ericbisson | 1:b3ae0d9f02ad | 23 | |
ericbisson | 1:b3ae0d9f02ad | 24 | Mail<message_t, 16> Mailbox; |
ericbisson | 1:b3ae0d9f02ad | 25 | |
ericbisson | 1:b3ae0d9f02ad | 26 | #define UN_HUITIEME_SHORT 0x1FFF |
ericbisson | 1:b3ae0d9f02ad | 27 | #define MOVING_AVG_SIZE 5 |
ericbisson | 1:b3ae0d9f02ad | 28 | struct MovingAverage_t |
ericbisson | 1:b3ae0d9f02ad | 29 | { |
ericbisson | 1:b3ae0d9f02ad | 30 | unsigned short buffer[MOVING_AVG_SIZE]; |
ericbisson | 1:b3ae0d9f02ad | 31 | char cursor; |
ericbisson | 1:b3ae0d9f02ad | 32 | bool bFilled; |
ericbisson | 1:b3ae0d9f02ad | 33 | }; |
ericbisson | 1:b3ae0d9f02ad | 34 | MovingAverage_t MovingAverageP19 = { |
ericbisson | 1:b3ae0d9f02ad | 35 | {}, |
ericbisson | 1:b3ae0d9f02ad | 36 | 0, |
ericbisson | 1:b3ae0d9f02ad | 37 | false |
ericbisson | 1:b3ae0d9f02ad | 38 | }; |
ericbisson | 1:b3ae0d9f02ad | 39 | |
ericbisson | 1:b3ae0d9f02ad | 40 | MovingAverage_t MovingAverageP20 = { |
ericbisson | 1:b3ae0d9f02ad | 41 | {}, |
ericbisson | 1:b3ae0d9f02ad | 42 | 0, |
ericbisson | 1:b3ae0d9f02ad | 43 | false |
ericbisson | 1:b3ae0d9f02ad | 44 | }; |
ericbisson | 1:b3ae0d9f02ad | 45 | |
ericbisson | 1:b3ae0d9f02ad | 46 | bool bLast_p15 = false; |
ericbisson | 1:b3ae0d9f02ad | 47 | bool bLast_p16 = false; |
ericbisson | 1:b3ae0d9f02ad | 48 | unsigned short u16Last_p19 = 0; |
ericbisson | 1:b3ae0d9f02ad | 49 | unsigned short u16Last_p20 = 0; |
ericbisson | 0:c637467eeb8f | 50 | |
ericbisson | 0:c637467eeb8f | 51 | void signal_analog() |
ericbisson | 0:c637467eeb8f | 52 | { |
ericbisson | 0:c637467eeb8f | 53 | t1->signal_set(1); |
ericbisson | 0:c637467eeb8f | 54 | } |
ericbisson | 0:c637467eeb8f | 55 | |
ericbisson | 0:c637467eeb8f | 56 | void signal_digital() |
ericbisson | 0:c637467eeb8f | 57 | { |
ericbisson | 0:c637467eeb8f | 58 | t2->signal_set(2); |
ericbisson | 0:c637467eeb8f | 59 | } |
ericbisson | 0:c637467eeb8f | 60 | |
ericbisson | 1:b3ae0d9f02ad | 61 | unsigned short moyenne_mobile(MovingAverage_t* MA, unsigned short newData) |
ericbisson | 1:b3ae0d9f02ad | 62 | { |
ericbisson | 1:b3ae0d9f02ad | 63 | int sum = 0; |
ericbisson | 1:b3ae0d9f02ad | 64 | MA->buffer[MA->cursor] = newData; |
ericbisson | 1:b3ae0d9f02ad | 65 | MA->cursor++; |
ericbisson | 1:b3ae0d9f02ad | 66 | if (MA->cursor >= MOVING_AVG_SIZE) |
ericbisson | 1:b3ae0d9f02ad | 67 | { |
ericbisson | 1:b3ae0d9f02ad | 68 | MA->cursor = 0; |
ericbisson | 1:b3ae0d9f02ad | 69 | MA->bFilled = true; |
ericbisson | 1:b3ae0d9f02ad | 70 | } |
ericbisson | 1:b3ae0d9f02ad | 71 | |
ericbisson | 1:b3ae0d9f02ad | 72 | if (MA->bFilled) |
ericbisson | 1:b3ae0d9f02ad | 73 | { |
ericbisson | 1:b3ae0d9f02ad | 74 | for (char i = 0; i < MOVING_AVG_SIZE; i++) |
ericbisson | 1:b3ae0d9f02ad | 75 | { |
ericbisson | 1:b3ae0d9f02ad | 76 | sum += MA->buffer[i]; |
ericbisson | 1:b3ae0d9f02ad | 77 | } |
ericbisson | 1:b3ae0d9f02ad | 78 | sum = sum / MOVING_AVG_SIZE; |
ericbisson | 1:b3ae0d9f02ad | 79 | } |
ericbisson | 1:b3ae0d9f02ad | 80 | else |
ericbisson | 1:b3ae0d9f02ad | 81 | { |
ericbisson | 1:b3ae0d9f02ad | 82 | for (char i = 0; i < MA->cursor; i++) |
ericbisson | 1:b3ae0d9f02ad | 83 | { |
ericbisson | 1:b3ae0d9f02ad | 84 | sum += MA->buffer[i]; |
ericbisson | 1:b3ae0d9f02ad | 85 | } |
ericbisson | 1:b3ae0d9f02ad | 86 | sum = sum / MA->cursor; |
ericbisson | 1:b3ae0d9f02ad | 87 | } |
ericbisson | 1:b3ae0d9f02ad | 88 | |
ericbisson | 1:b3ae0d9f02ad | 89 | return sum; |
ericbisson | 1:b3ae0d9f02ad | 90 | } |
ericbisson | 1:b3ae0d9f02ad | 91 | |
ericbisson | 1:b3ae0d9f02ad | 92 | inline void MailBox_put(message_t* data) |
ericbisson | 1:b3ae0d9f02ad | 93 | { |
ericbisson | 1:b3ae0d9f02ad | 94 | mutex.lock(); |
ericbisson | 1:b3ae0d9f02ad | 95 | message_t* msg = Mailbox.calloc(); |
ericbisson | 1:b3ae0d9f02ad | 96 | msg->timestamp = data->timestamp; |
ericbisson | 1:b3ae0d9f02ad | 97 | msg->pin_id = data->pin_id; |
ericbisson | 1:b3ae0d9f02ad | 98 | msg->value = data->value; |
ericbisson | 1:b3ae0d9f02ad | 99 | Mailbox.put(msg); |
ericbisson | 1:b3ae0d9f02ad | 100 | mutex.unlock(); |
ericbisson | 1:b3ae0d9f02ad | 101 | t3->signal_set(4); |
ericbisson | 1:b3ae0d9f02ad | 102 | t1->yield(); |
ericbisson | 1:b3ae0d9f02ad | 103 | t2->yield(); |
ericbisson | 1:b3ae0d9f02ad | 104 | } |
ericbisson | 1:b3ae0d9f02ad | 105 | |
ericbisson | 0:c637467eeb8f | 106 | void lecture_analog(void/* const *args*/) { |
ericbisson | 0:c637467eeb8f | 107 | while (true) { |
ericbisson | 1:b3ae0d9f02ad | 108 | // synchronisation sur la période d'échantillonnage |
ericbisson | 0:c637467eeb8f | 109 | t1->signal_wait(1); |
ericbisson | 1:b3ae0d9f02ad | 110 | |
ericbisson | 0:c637467eeb8f | 111 | // lecture de l'étampe temporelle |
ericbisson | 1:b3ae0d9f02ad | 112 | message_t msg; |
ericbisson | 1:b3ae0d9f02ad | 113 | msg.timestamp = time(NULL); |
ericbisson | 1:b3ae0d9f02ad | 114 | |
ericbisson | 0:c637467eeb8f | 115 | // lecture des échantillons analogiques |
ericbisson | 0:c637467eeb8f | 116 | // calcul de la nouvelle moyenne courante |
ericbisson | 1:b3ae0d9f02ad | 117 | unsigned short res_p19 = moyenne_mobile(&MovingAverageP19, ea_1.read_u16()); |
ericbisson | 1:b3ae0d9f02ad | 118 | unsigned short res_p20 = moyenne_mobile(&MovingAverageP20, ea_2.read_u16()); |
ericbisson | 1:b3ae0d9f02ad | 119 | |
ericbisson | 0:c637467eeb8f | 120 | // génération éventuelle d'un événement |
ericbisson | 1:b3ae0d9f02ad | 121 | if ((res_p19 + UN_HUITIEME_SHORT) < u16Last_p19 || (res_p19 - UN_HUITIEME_SHORT) > u16Last_p19 ) |
ericbisson | 1:b3ae0d9f02ad | 122 | { |
ericbisson | 1:b3ae0d9f02ad | 123 | msg.pin_id = 19; |
ericbisson | 1:b3ae0d9f02ad | 124 | msg.value = res_p19; |
ericbisson | 1:b3ae0d9f02ad | 125 | MailBox_put(&msg); |
ericbisson | 1:b3ae0d9f02ad | 126 | } |
ericbisson | 1:b3ae0d9f02ad | 127 | if ((res_p20 + UN_HUITIEME_SHORT < u16Last_p20) || (res_p20 - UN_HUITIEME_SHORT) > u16Last_p20 ) |
ericbisson | 1:b3ae0d9f02ad | 128 | { |
ericbisson | 1:b3ae0d9f02ad | 129 | msg.pin_id = 20; |
ericbisson | 1:b3ae0d9f02ad | 130 | msg.value = res_p20; |
ericbisson | 1:b3ae0d9f02ad | 131 | MailBox_put(&msg); |
ericbisson | 1:b3ae0d9f02ad | 132 | } |
ericbisson | 1:b3ae0d9f02ad | 133 | |
ericbisson | 1:b3ae0d9f02ad | 134 | u16Last_p19 = res_p19; |
ericbisson | 1:b3ae0d9f02ad | 135 | u16Last_p20 = res_p20; |
ericbisson | 0:c637467eeb8f | 136 | |
ericbisson | 0:c637467eeb8f | 137 | t1->signal_clr(1); |
ericbisson | 0:c637467eeb8f | 138 | t1->yield(); |
ericbisson | 0:c637467eeb8f | 139 | } |
ericbisson | 0:c637467eeb8f | 140 | } |
ericbisson | 0:c637467eeb8f | 141 | void lecture_num(void/* const *args*/) { |
ericbisson | 0:c637467eeb8f | 142 | while (true) { |
ericbisson | 1:b3ae0d9f02ad | 143 | // synchronisation sur la période d'échantillonnage |
ericbisson | 0:c637467eeb8f | 144 | t2->signal_wait(2); |
ericbisson | 1:b3ae0d9f02ad | 145 | |
ericbisson | 0:c637467eeb8f | 146 | // lecture de l'étampe temporelle |
ericbisson | 1:b3ae0d9f02ad | 147 | message_t msg; |
ericbisson | 1:b3ae0d9f02ad | 148 | msg.timestamp = time(NULL); |
ericbisson | 1:b3ae0d9f02ad | 149 | |
ericbisson | 0:c637467eeb8f | 150 | // lecture des échantillons numériques |
ericbisson | 1:b3ae0d9f02ad | 151 | bool bP15 = en_1.read(); |
ericbisson | 1:b3ae0d9f02ad | 152 | bool bP16 = en_2.read(); |
ericbisson | 1:b3ae0d9f02ad | 153 | |
ericbisson | 0:c637467eeb8f | 154 | // prise en charge du phénomène de rebond |
ericbisson | 1:b3ae0d9f02ad | 155 | wait_ms(50); // :') |
ericbisson | 1:b3ae0d9f02ad | 156 | |
ericbisson | 0:c637467eeb8f | 157 | // génération éventuelle d'un événement |
ericbisson | 1:b3ae0d9f02ad | 158 | if (en_1.read() == bP15 && bP15 != bLast_p15) |
ericbisson | 1:b3ae0d9f02ad | 159 | { |
ericbisson | 1:b3ae0d9f02ad | 160 | msg.pin_id = 15; |
ericbisson | 1:b3ae0d9f02ad | 161 | msg.value = bP15; |
ericbisson | 1:b3ae0d9f02ad | 162 | MailBox_put(&msg); |
ericbisson | 1:b3ae0d9f02ad | 163 | } |
ericbisson | 1:b3ae0d9f02ad | 164 | if (en_2.read() == bP16 && bP16 != bLast_p16) |
ericbisson | 1:b3ae0d9f02ad | 165 | { |
ericbisson | 1:b3ae0d9f02ad | 166 | msg.pin_id = 16; |
ericbisson | 1:b3ae0d9f02ad | 167 | msg.value = bP16; |
ericbisson | 1:b3ae0d9f02ad | 168 | MailBox_put(&msg); |
ericbisson | 1:b3ae0d9f02ad | 169 | } |
ericbisson | 1:b3ae0d9f02ad | 170 | bLast_p15 = bP15; |
ericbisson | 1:b3ae0d9f02ad | 171 | bLast_p16 = bP16; |
ericbisson | 0:c637467eeb8f | 172 | |
ericbisson | 0:c637467eeb8f | 173 | t2->signal_clr(2); |
ericbisson | 0:c637467eeb8f | 174 | t2->yield(); |
ericbisson | 0:c637467eeb8f | 175 | } |
ericbisson | 0:c637467eeb8f | 176 | } |
ericbisson | 0:c637467eeb8f | 177 | void collection(void/* const *args*/) { |
ericbisson | 1:b3ae0d9f02ad | 178 | while (true) { |
ericbisson | 0:c637467eeb8f | 179 | // attente et lecture d'un événement |
ericbisson | 1:b3ae0d9f02ad | 180 | t3->signal_wait(4); |
ericbisson | 1:b3ae0d9f02ad | 181 | mutex.lock(); |
ericbisson | 1:b3ae0d9f02ad | 182 | osEvent evt = Mailbox.get(); |
ericbisson | 1:b3ae0d9f02ad | 183 | if (evt.status == osEventMail) { |
ericbisson | 1:b3ae0d9f02ad | 184 | message_t *msg = (message_t*)evt.value.p; |
ericbisson | 1:b3ae0d9f02ad | 185 | |
ericbisson | 1:b3ae0d9f02ad | 186 | // écriture de l'événement en sortie (port série) |
ericbisson | 1:b3ae0d9f02ad | 187 | pc.printf("[%s] Evenement de la pin %d avec valeur %d\n", ctime(&(msg->timestamp)), msg->pin_id, msg->value); |
ericbisson | 1:b3ae0d9f02ad | 188 | |
ericbisson | 1:b3ae0d9f02ad | 189 | Mailbox.free(msg); |
ericbisson | 1:b3ae0d9f02ad | 190 | } |
ericbisson | 1:b3ae0d9f02ad | 191 | t3->signal_clr(4); |
ericbisson | 1:b3ae0d9f02ad | 192 | mutex.unlock(); |
ericbisson | 1:b3ae0d9f02ad | 193 | t3->yield(); |
ericbisson | 0:c637467eeb8f | 194 | } |
ericbisson | 0:c637467eeb8f | 195 | } |
ericbisson | 1:b3ae0d9f02ad | 196 | int main() { |
ericbisson | 0:c637467eeb8f | 197 | // set the time |
ericbisson | 1:b3ae0d9f02ad | 198 | set_time(1485887400); |
ericbisson | 0:c637467eeb8f | 199 | |
ericbisson | 0:c637467eeb8f | 200 | // démarrage des tâches |
ericbisson | 0:c637467eeb8f | 201 | t1 = new Thread(); |
ericbisson | 0:c637467eeb8f | 202 | t2 = new Thread(); |
ericbisson | 0:c637467eeb8f | 203 | t3 = new Thread(); |
ericbisson | 0:c637467eeb8f | 204 | |
ericbisson | 0:c637467eeb8f | 205 | t1->start(lecture_analog); |
ericbisson | 0:c637467eeb8f | 206 | t2->start(lecture_num); |
ericbisson | 0:c637467eeb8f | 207 | t3->start(collection); |
ericbisson | 0:c637467eeb8f | 208 | |
ericbisson | 1:b3ae0d9f02ad | 209 | tick1.attach(&signal_digital, 0.1); // fréquence de 100ms |
ericbisson | 1:b3ae0d9f02ad | 210 | tick2.attach(&signal_analog, 0.25); // fréquence de 250ms |
ericbisson | 0:c637467eeb8f | 211 | |
ericbisson | 0:c637467eeb8f | 212 | while(1) { |
ericbisson | 1:b3ae0d9f02ad | 213 | wait_ms(1000); // wait forever |
ericbisson | 0:c637467eeb8f | 214 | } |
ericbisson | 0:c637467eeb8f | 215 | } |