carte esclave pompe 2020 V1

Dependencies:   mbed ident_crac

Committer:
maximilienlv
Date:
Sun Mar 08 23:05:13 2020 +0000
Revision:
22:f104afbecb98
Parent:
21:42d6cead9cc2
Child:
23:7f62f27c028f
optimisation du code des pompes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marwanesaich 4:82c90657da4a 1 #include "extern.h"
kyxstark 8:a25a9c22ba91 2 #define SIZE_FIFO 50
marwanesaich 1:8a55f1d5cb26 3
kyxstark 8:a25a9c22ba91 4
marwanesaich 0:84a8c24e29ae 5 CANMessage msgRxBuffer[SIZE_FIFO];
marwanesaich 0:84a8c24e29ae 6 unsigned char FIFO_ecriture=0; //Position du fifo pour la reception CAN
marwanesaich 0:84a8c24e29ae 7 signed char FIFO_lecture=0;//Position du fifo de lecture des messages CAN
kyxstark 8:a25a9c22ba91 8 char end_game = 0;
marwanesaich 0:84a8c24e29ae 9
kyxstark 8:a25a9c22ba91 10 Ventouse* ventouse[NB_VENTOUSES];
kyxstark 8:a25a9c22ba91 11
kyxstark 17:fcfdd7de9039 12 BusIn contacts(PC_11, PC_12, PB_4, PB_5, PC_13, PC_14, PC_2, PC_3);
kyxstark 17:fcfdd7de9039 13 DigitalIn Contact1(PC_11, PullNone);
kyxstark 17:fcfdd7de9039 14 DigitalIn Contact2(PC_12, PullNone);
kyxstark 17:fcfdd7de9039 15 DigitalIn Contact3(PB_4, PullNone);
kyxstark 17:fcfdd7de9039 16 DigitalIn Contact4(PB_5, PullNone);
kyxstark 17:fcfdd7de9039 17 DigitalIn Contact5(PC_13, PullNone);
kyxstark 17:fcfdd7de9039 18 DigitalIn Contact6(PC_14, PullNone);
kyxstark 17:fcfdd7de9039 19 DigitalIn Contact7(PC_2, PullNone);
kyxstark 17:fcfdd7de9039 20 DigitalIn Contact8(PC_3, PullNone);
kyxstark 17:fcfdd7de9039 21
kyxstark 17:fcfdd7de9039 22 void lecture_contacts(void);
marwanesaich 0:84a8c24e29ae 23 void get_etat_ventouses();
marwanesaich 0:84a8c24e29ae 24
kyxstark 8:a25a9c22ba91 25
marwanesaich 2:4cc8fa2f64be 26 CAN can(PB_8,PB_9,1000000);
marwanesaich 2:4cc8fa2f64be 27 Serial pc(USBTX,USBRX);
marwanesaich 0:84a8c24e29ae 28
marwanesaich 0:84a8c24e29ae 29 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 30 /* FUNCTION NAME: canRx_ISR */
marwanesaich 0:84a8c24e29ae 31 /* DESCRIPTION : lit les messages sur le can et les stocke dans la FIFO */
marwanesaich 0:84a8c24e29ae 32 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 33 void canRx_ISR (void)
marwanesaich 0:84a8c24e29ae 34 {
marwanesaich 0:84a8c24e29ae 35 if (can.read(msgRxBuffer[FIFO_ecriture]))
marwanesaich 0:84a8c24e29ae 36 FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO;
marwanesaich 0:84a8c24e29ae 37 }
marwanesaich 0:84a8c24e29ae 38
marwanesaich 0:84a8c24e29ae 39
marwanesaich 0:84a8c24e29ae 40 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 41 /* FUNCTION NAME: canProcessRx */
marwanesaich 0:84a8c24e29ae 42 /* DESCRIPTION : Fonction de traitement des messages CAN */
marwanesaich 0:84a8c24e29ae 43 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 44 void canProcessRx(void){
marwanesaich 0:84a8c24e29ae 45 static signed char FIFO_occupation=0,FIFO_max_occupation=0;
marwanesaich 0:84a8c24e29ae 46 CANMessage msgTx=CANMessage();
marwanesaich 0:84a8c24e29ae 47 FIFO_occupation=FIFO_ecriture-FIFO_lecture;
marwanesaich 0:84a8c24e29ae 48 if(FIFO_occupation<0)
marwanesaich 0:84a8c24e29ae 49 FIFO_occupation=FIFO_occupation+SIZE_FIFO;
marwanesaich 0:84a8c24e29ae 50 if(FIFO_max_occupation<FIFO_occupation)
marwanesaich 0:84a8c24e29ae 51 FIFO_max_occupation=FIFO_occupation;
marwanesaich 0:84a8c24e29ae 52 if(FIFO_occupation!=0) {
marwanesaich 0:84a8c24e29ae 53 int identifiant=msgRxBuffer[FIFO_lecture].id;
marwanesaich 1:8a55f1d5cb26 54 char BRAS = 0;
marwanesaich 1:8a55f1d5cb26 55
maximilienlv 20:677dcf689523 56 switch(identifiant)
maximilienlv 22:f104afbecb98 57 {
maximilienlv 22:f104afbecb98 58 //BRAS=data: donc savoir si data peut être envoyé
maximilienlv 22:f104afbecb98 59 case VENTOUSE_ATTRAPER:
marwanesaich 1:8a55f1d5cb26 60 BRAS = msgRxBuffer[FIFO_lecture].data[0];
marwanesaich 2:4cc8fa2f64be 61 ventouse[BRAS]->action(1);
marwanesaich 0:84a8c24e29ae 62 break;
kyxstark 8:a25a9c22ba91 63
maximilienlv 22:f104afbecb98 64 case VENTOUSE_RELACHER:
marwanesaich 1:8a55f1d5cb26 65 BRAS = msgRxBuffer[FIFO_lecture].data[0];
marwanesaich 2:4cc8fa2f64be 66 ventouse[BRAS]->action(0);
kyxstark 8:a25a9c22ba91 67 break;
kyxstark 8:a25a9c22ba91 68
maximilienlv 22:f104afbecb98 69 /*case HACHEUR_STATUT_VENTOUSES:
kyxstark 8:a25a9c22ba91 70 get_etat_ventouses();
maximilienlv 22:f104afbecb98 71 break;*/
kyxstark 8:a25a9c22ba91 72
kyxstark 8:a25a9c22ba91 73 case GLOBAL_GAME_END:
kyxstark 8:a25a9c22ba91 74 end_game = 1;
kyxstark 8:a25a9c22ba91 75 break;
kyxstark 17:fcfdd7de9039 76
maximilienlv 22:f104afbecb98 77 /*case HACHEUR_ETAT_CONTACTS:
kyxstark 17:fcfdd7de9039 78 char contacts_tmp = contacts;
kyxstark 17:fcfdd7de9039 79 can.write(CANMessage(HACHEUR_ETAT_CONTACTS,&contacts_tmp,1));
maximilienlv 22:f104afbecb98 80 break;
marwanesaich 5:176e7353ba1c 81
maximilienlv 21:42d6cead9cc2 82 case TEST_VENT_1_ON:
maximilienlv 21:42d6cead9cc2 83 ventouse[AV_DROIT]->action(1);
maximilienlv 21:42d6cead9cc2 84 break;
maximilienlv 21:42d6cead9cc2 85
maximilienlv 21:42d6cead9cc2 86 case TEST_VENT_1_OFF:
maximilienlv 21:42d6cead9cc2 87 ventouse[AV_GAUCHE]->action(0);
maximilienlv 22:f104afbecb98 88 break;*/
maximilienlv 22:f104afbecb98 89 default:
maximilienlv 21:42d6cead9cc2 90 break;
maximilienlv 21:42d6cead9cc2 91
maximilienlv 20:677dcf689523 92 }
marwanesaich 0:84a8c24e29ae 93 FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
marwanesaich 0:84a8c24e29ae 94 }
marwanesaich 0:84a8c24e29ae 95 }
marwanesaich 0:84a8c24e29ae 96
marwanesaich 0:84a8c24e29ae 97 int main() {
marwanesaich 0:84a8c24e29ae 98
marwanesaich 0:84a8c24e29ae 99 can.attach(&canRx_ISR); // création de l'interrupt attachée à la réception sur le CAN
maximilienlv 22:f104afbecb98 100 wait_ms(100);
maximilienlv 19:952208276c63 101
maximilienlv 21:42d6cead9cc2 102 ventouse[AR_DROIT] = new Ventouse(PB_7, PB_6, PC_0, AV_DROIT);
marwanesaich 2:4cc8fa2f64be 103 ventouse[AR_CENTRE] = new Ventouse(PC_9, PA_8, PB_0, AR_CENTRE);
maximilienlv 21:42d6cead9cc2 104 ventouse[AR_GAUCHE] = new Ventouse(PA_9, PA_10, PB_1, AR_GAUCHE);
marwanesaich 0:84a8c24e29ae 105
marwanesaich 16:5e00af31ae8a 106 ventouse[AV_DROIT] = new Ventouse(PA_0, PA_1, PA_4, AV_DROIT);
maximilienlv 21:42d6cead9cc2 107 ventouse[AV_GAUCHE] = new Ventouse(PC_7,PC_8 , PC_5, AV_GAUCHE);
maximilienlv 21:42d6cead9cc2 108 ventouse[AV_CENTRE] = new Ventouse(PB_10, PB_2, PC_4, AR_CENTRE);
kyxstark 8:a25a9c22ba91 109
maximilienlv 22:f104afbecb98 110 pc.printf("Launched carte ventouse");
maximilienlv 21:42d6cead9cc2 111 ventouse[AV_DROIT]->action(0);
maximilienlv 21:42d6cead9cc2 112 ventouse[AV_CENTRE]->action(0);
maximilienlv 21:42d6cead9cc2 113 ventouse[AV_GAUCHE]->action(0);
maximilienlv 21:42d6cead9cc2 114 ventouse[AR_DROIT]->action(0);
maximilienlv 21:42d6cead9cc2 115 ventouse[AR_CENTRE]->action(0);
maximilienlv 21:42d6cead9cc2 116 ventouse[AR_GAUCHE]->action(0);
maximilienlv 21:42d6cead9cc2 117 while(1)
maximilienlv 21:42d6cead9cc2 118 {
marwanesaich 0:84a8c24e29ae 119 canProcessRx();
maximilienlv 22:f104afbecb98 120 for(int i=0; i<NB_VENTOUSES; i++)
maximilienlv 22:f104afbecb98 121 {
marwanesaich 2:4cc8fa2f64be 122 ventouse[i]->automate();
kyxstark 17:fcfdd7de9039 123 }
kyxstark 17:fcfdd7de9039 124 lecture_contacts();
kyxstark 17:fcfdd7de9039 125
maximilienlv 22:f104afbecb98 126 if(end_game)
maximilienlv 22:f104afbecb98 127 {
maximilienlv 22:f104afbecb98 128 for(int i=0; i<NB_VENTOUSES;i++)
maximilienlv 22:f104afbecb98 129 {
marwanesaich 0:84a8c24e29ae 130 ventouse[i]->setPompe(0.0);
kyxstark 8:a25a9c22ba91 131 ventouse[i]->setElectrovanne(0);
marwanesaich 0:84a8c24e29ae 132 }
maximilienlv 22:f104afbecb98 133 while(1);
marwanesaich 0:84a8c24e29ae 134 }
marwanesaich 0:84a8c24e29ae 135 }
marwanesaich 0:84a8c24e29ae 136 }
marwanesaich 0:84a8c24e29ae 137
kyxstark 17:fcfdd7de9039 138 void lecture_contacts(void)
kyxstark 17:fcfdd7de9039 139 {
maximilienlv 22:f104afbecb98 140 static char prev_contacts = contacts;
kyxstark 17:fcfdd7de9039 141 if(contacts != prev_contacts)
kyxstark 17:fcfdd7de9039 142 {
kyxstark 17:fcfdd7de9039 143 prev_contacts = contacts;
maximilienlv 21:42d6cead9cc2 144 //can.write(CANMessage(HACHEUR_ETAT_CONTACTS,&prev_contacts,1));
kyxstark 17:fcfdd7de9039 145 }
kyxstark 17:fcfdd7de9039 146 }
kyxstark 8:a25a9c22ba91 147
marwanesaich 0:84a8c24e29ae 148 void get_etat_ventouses()
marwanesaich 0:84a8c24e29ae 149 {
maximilienlv 22:f104afbecb98 150 CANMessage etat_ventouses = CANMessage();
marwanesaich 0:84a8c24e29ae 151 etat_ventouses.len=2;
marwanesaich 0:84a8c24e29ae 152 etat_ventouses.format=CANStandard;
marwanesaich 0:84a8c24e29ae 153 etat_ventouses.type=CANData;
marwanesaich 2:4cc8fa2f64be 154 etat_ventouses.data[0]=0;
marwanesaich 2:4cc8fa2f64be 155 etat_ventouses.data[0]=1;
kyxstark 10:765b4ff6d721 156 for(int i = (NB_VENTOUSES - 1); i >= 0; i--){
marwanesaich 0:84a8c24e29ae 157 etat_ventouses.data[0] = (etat_ventouses.data[0]<<1) + ventouse[i]->getPompe();
marwanesaich 0:84a8c24e29ae 158 etat_ventouses.data[1] = (etat_ventouses.data[1]<<1) + ventouse[i]->getPression();
marwanesaich 0:84a8c24e29ae 159 }
marwanesaich 0:84a8c24e29ae 160 can.write(etat_ventouses);
maximilienlv 22:f104afbecb98 161 }