School Project

Dependencies:   mbed-rtos mbed

Committer:
ericbisson
Date:
Tue Jan 31 19:15:03 2017 +0000
Revision:
4:87e9b434bb4d
Parent:
3:3ecbcc05bc85
Child:
5:e878d87782ce
new version avec t3

Who changed what in which revision?

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