School Project

Dependencies:   mbed-rtos mbed

Committer:
ericbisson
Date:
Tue Jan 31 19:05:27 2017 +0000
Revision:
3:3ecbcc05bc85
Parent:
2:c6465d4e82d2
Child:
4:87e9b434bb4d
fin app2

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