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