carte esclave pompe 2020 V1

Dependencies:   mbed ident_crac

Committer:
maximilienlv
Date:
Fri Mar 06 08:17:17 2020 +0000
Revision:
20:677dcf689523
Parent:
19:952208276c63
Child:
21:42d6cead9cc2
nouveau programme JPO

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);
kyxstark 17:fcfdd7de9039 23
marwanesaich 0:84a8c24e29ae 24
marwanesaich 0:84a8c24e29ae 25
marwanesaich 0:84a8c24e29ae 26 void get_etat_ventouses();
marwanesaich 0:84a8c24e29ae 27
kyxstark 8:a25a9c22ba91 28
marwanesaich 2:4cc8fa2f64be 29 CAN can(PB_8,PB_9,1000000);
marwanesaich 2:4cc8fa2f64be 30 Serial pc(USBTX,USBRX);
marwanesaich 0:84a8c24e29ae 31
marwanesaich 0:84a8c24e29ae 32 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 33 /* FUNCTION NAME: canRx_ISR */
marwanesaich 0:84a8c24e29ae 34 /* DESCRIPTION : lit les messages sur le can et les stocke dans la FIFO */
marwanesaich 0:84a8c24e29ae 35 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 36 void canRx_ISR (void)
marwanesaich 0:84a8c24e29ae 37 {
marwanesaich 0:84a8c24e29ae 38 if (can.read(msgRxBuffer[FIFO_ecriture]))
marwanesaich 0:84a8c24e29ae 39 FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO;
marwanesaich 0:84a8c24e29ae 40 }
marwanesaich 0:84a8c24e29ae 41
marwanesaich 0:84a8c24e29ae 42
marwanesaich 0:84a8c24e29ae 43 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 44 /* FUNCTION NAME: canProcessRx */
marwanesaich 0:84a8c24e29ae 45 /* DESCRIPTION : Fonction de traitement des messages CAN */
marwanesaich 0:84a8c24e29ae 46 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 47 void canProcessRx(void){
marwanesaich 0:84a8c24e29ae 48 static signed char FIFO_occupation=0,FIFO_max_occupation=0;
marwanesaich 0:84a8c24e29ae 49 CANMessage msgTx=CANMessage();
marwanesaich 0:84a8c24e29ae 50 FIFO_occupation=FIFO_ecriture-FIFO_lecture;
marwanesaich 0:84a8c24e29ae 51 if(FIFO_occupation<0)
marwanesaich 0:84a8c24e29ae 52 FIFO_occupation=FIFO_occupation+SIZE_FIFO;
marwanesaich 0:84a8c24e29ae 53 if(FIFO_max_occupation<FIFO_occupation)
marwanesaich 0:84a8c24e29ae 54 FIFO_max_occupation=FIFO_occupation;
marwanesaich 0:84a8c24e29ae 55 if(FIFO_occupation!=0) {
marwanesaich 0:84a8c24e29ae 56 int identifiant=msgRxBuffer[FIFO_lecture].id;
marwanesaich 1:8a55f1d5cb26 57 char BRAS = 0;
marwanesaich 1:8a55f1d5cb26 58
maximilienlv 20:677dcf689523 59 switch(identifiant)
maximilienlv 20:677dcf689523 60 {
marwanesaich 0:84a8c24e29ae 61
maximilienlv 19:952208276c63 62 /*case HACHEUR_GET_ATOM:
marwanesaich 1:8a55f1d5cb26 63 BRAS = msgRxBuffer[FIFO_lecture].data[0];
marwanesaich 2:4cc8fa2f64be 64 ventouse[BRAS]->action(1);
marwanesaich 0:84a8c24e29ae 65 break;
kyxstark 8:a25a9c22ba91 66
marwanesaich 0:84a8c24e29ae 67 case HACHEUR_RELEASE_ATOM:
marwanesaich 1:8a55f1d5cb26 68 BRAS = msgRxBuffer[FIFO_lecture].data[0];
marwanesaich 2:4cc8fa2f64be 69 ventouse[BRAS]->action(0);
kyxstark 8:a25a9c22ba91 70 break;
kyxstark 8:a25a9c22ba91 71
kyxstark 8:a25a9c22ba91 72 case HACHEUR_STATUT_VENTOUSES:
kyxstark 8:a25a9c22ba91 73 get_etat_ventouses();
kyxstark 8:a25a9c22ba91 74 break;
kyxstark 8:a25a9c22ba91 75
kyxstark 8:a25a9c22ba91 76 case GLOBAL_GAME_END:
kyxstark 8:a25a9c22ba91 77 end_game = 1;
kyxstark 8:a25a9c22ba91 78 break;
kyxstark 17:fcfdd7de9039 79
kyxstark 17:fcfdd7de9039 80 case HACHEUR_ETAT_CONTACTS:
kyxstark 17:fcfdd7de9039 81 char contacts_tmp = contacts;
kyxstark 17:fcfdd7de9039 82 can.write(CANMessage(HACHEUR_ETAT_CONTACTS,&contacts_tmp,1));
maximilienlv 19:952208276c63 83 break;*/
marwanesaich 5:176e7353ba1c 84
maximilienlv 19:952208276c63 85 case TEST_ELECTROV_1:
maximilienlv 19:952208276c63 86 ventouse[AR_GAUCHE]->action(1);
maximilienlv 19:952208276c63 87 ventouse[AR_CENTRE]->action(1);
maximilienlv 19:952208276c63 88 ventouse[AR_DROIT]->action(1);
maximilienlv 20:677dcf689523 89 break;
maximilienlv 20:677dcf689523 90
maximilienlv 20:677dcf689523 91 case TEST_ELECTROV_2:
kyxstark 13:178eaf022d60 92 ventouse[AV_GAUCHE]->action(1);
kyxstark 13:178eaf022d60 93 ventouse[AV_CENTRE]->action(1);
kyxstark 13:178eaf022d60 94 ventouse[AV_DROIT]->action(1);
marwanesaich 0:84a8c24e29ae 95 break;
maximilienlv 19:952208276c63 96
maximilienlv 20:677dcf689523 97 case TEST_ELECTROV_3:
maximilienlv 19:952208276c63 98 ventouse[AR_GAUCHE]->action(0);
maximilienlv 19:952208276c63 99 ventouse[AR_CENTRE]->action(0);
maximilienlv 19:952208276c63 100 ventouse[AR_DROIT]->action(0);
kyxstark 13:178eaf022d60 101 ventouse[AV_GAUCHE]->action(0);
kyxstark 13:178eaf022d60 102 ventouse[AV_CENTRE]->action(0);
kyxstark 13:178eaf022d60 103 ventouse[AV_DROIT]->action(0);
maximilienlv 20:677dcf689523 104 break;
maximilienlv 20:677dcf689523 105 }
marwanesaich 0:84a8c24e29ae 106 FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
marwanesaich 0:84a8c24e29ae 107 }
marwanesaich 0:84a8c24e29ae 108 }
marwanesaich 0:84a8c24e29ae 109
marwanesaich 0:84a8c24e29ae 110 int main() {
marwanesaich 0:84a8c24e29ae 111
marwanesaich 0:84a8c24e29ae 112 can.attach(&canRx_ISR); // création de l'interrupt attachée à la réception sur le CAN
marwanesaich 0:84a8c24e29ae 113 wait(1);
maximilienlv 19:952208276c63 114
kyxstark 10:765b4ff6d721 115 pc.printf("\nPetit robot\n\n");
marwanesaich 16:5e00af31ae8a 116 ventouse[AV_GAUCHE] = new Ventouse(PB_7, PB_6, PC_0, AV_GAUCHE);
marwanesaich 2:4cc8fa2f64be 117 ventouse[AR_CENTRE] = new Ventouse(PC_9, PA_8, PB_0, AR_CENTRE);
marwanesaich 2:4cc8fa2f64be 118 ventouse[AR_DROIT] = new Ventouse(PA_9, PA_10, PB_1, AR_DROIT);
marwanesaich 0:84a8c24e29ae 119
marwanesaich 16:5e00af31ae8a 120 ventouse[AV_DROIT] = new Ventouse(PA_0, PA_1, PA_4, AV_DROIT);
marwanesaich 2:4cc8fa2f64be 121 ventouse[AV_CENTRE] = new Ventouse(PC_7,PC_8 , PC_5, AV_CENTRE);
marwanesaich 4:82c90657da4a 122 ventouse[AR_GAUCHE] = new Ventouse(PB_10, PB_2, PC_4, AR_GAUCHE);
kyxstark 8:a25a9c22ba91 123
marwanesaich 1:8a55f1d5cb26 124 pc.printf("LAUNCHED");
marwanesaich 0:84a8c24e29ae 125 while(1) {
marwanesaich 0:84a8c24e29ae 126 canProcessRx();
kyxstark 8:a25a9c22ba91 127 for(int i=0; i<NB_VENTOUSES; i++){
marwanesaich 2:4cc8fa2f64be 128 ventouse[i]->automate();
kyxstark 17:fcfdd7de9039 129 }
kyxstark 17:fcfdd7de9039 130 lecture_contacts();
kyxstark 17:fcfdd7de9039 131
kyxstark 17:fcfdd7de9039 132
marwanesaich 0:84a8c24e29ae 133 if(end_game){
kyxstark 8:a25a9c22ba91 134 for(int i=0; i<NB_VENTOUSES;i++){
marwanesaich 0:84a8c24e29ae 135 ventouse[i]->setPompe(0.0);
kyxstark 8:a25a9c22ba91 136 ventouse[i]->setElectrovanne(0);
marwanesaich 0:84a8c24e29ae 137 }
marwanesaich 0:84a8c24e29ae 138 while(1){;}
marwanesaich 0:84a8c24e29ae 139 }
marwanesaich 0:84a8c24e29ae 140 }
marwanesaich 0:84a8c24e29ae 141 }
marwanesaich 0:84a8c24e29ae 142
kyxstark 17:fcfdd7de9039 143 void lecture_contacts(void)
kyxstark 17:fcfdd7de9039 144 {
kyxstark 17:fcfdd7de9039 145 static char prev_contacts = contacts;
kyxstark 17:fcfdd7de9039 146
kyxstark 17:fcfdd7de9039 147 if(contacts != prev_contacts)
kyxstark 17:fcfdd7de9039 148 {
kyxstark 17:fcfdd7de9039 149 prev_contacts = contacts;
kyxstark 17:fcfdd7de9039 150 can.write(CANMessage(HACHEUR_ETAT_CONTACTS,&prev_contacts,1));
kyxstark 17:fcfdd7de9039 151 }
kyxstark 17:fcfdd7de9039 152 }
kyxstark 8:a25a9c22ba91 153
marwanesaich 0:84a8c24e29ae 154 void get_etat_ventouses()
marwanesaich 0:84a8c24e29ae 155 {
marwanesaich 0:84a8c24e29ae 156 CANMessage etat_ventouses = CANMessage();
marwanesaich 0:84a8c24e29ae 157 etat_ventouses.id=HACHEUR_STATUT_VENTOUSES;
marwanesaich 0:84a8c24e29ae 158 etat_ventouses.len=2;
marwanesaich 0:84a8c24e29ae 159 etat_ventouses.format=CANStandard;
marwanesaich 0:84a8c24e29ae 160 etat_ventouses.type=CANData;
marwanesaich 2:4cc8fa2f64be 161 etat_ventouses.data[0]=0;
marwanesaich 2:4cc8fa2f64be 162 etat_ventouses.data[0]=1;
kyxstark 10:765b4ff6d721 163 for(int i = (NB_VENTOUSES - 1); i >= 0; i--){
marwanesaich 0:84a8c24e29ae 164 etat_ventouses.data[0] = (etat_ventouses.data[0]<<1) + ventouse[i]->getPompe();
marwanesaich 0:84a8c24e29ae 165 etat_ventouses.data[1] = (etat_ventouses.data[1]<<1) + ventouse[i]->getPression();
marwanesaich 0:84a8c24e29ae 166 }
marwanesaich 0:84a8c24e29ae 167 can.write(etat_ventouses);
marwanesaich 0:84a8c24e29ae 168
marwanesaich 0:84a8c24e29ae 169 }