carte esclave pompe 2020 V1

Dependencies:   mbed ident_crac

Committer:
maximilienlv
Date:
Sat Mar 07 17:40:59 2020 +0000
Revision:
21:42d6cead9cc2
Parent:
20:677dcf689523
Child:
22:f104afbecb98
pompe utilisant la pwm( fonctionnment des 6 pompes en demo)

Who changed what in which revision?

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