carte esclave pompe 2020 V1

Dependencies:   mbed ident_crac

Committer:
marwanesaich
Date:
Thu May 23 06:50:27 2019 +0000
Revision:
11:98dfe1a61df5
Parent:
10:765b4ff6d721
Child:
13:178eaf022d60
ajout endgame courroies

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marwanesaich 0:84a8c24e29ae 1 #include "mbed.h"
marwanesaich 0:84a8c24e29ae 2 #include "class_ventouse.h"
kyxstark 8:a25a9c22ba91 3 #include "ident_crac.h"
marwanesaich 4:82c90657da4a 4 #include "extern.h"
kyxstark 8:a25a9c22ba91 5 #define SIZE_FIFO 50
marwanesaich 1:8a55f1d5cb26 6
kyxstark 8:a25a9c22ba91 7
marwanesaich 0:84a8c24e29ae 8
kyxstark 8:a25a9c22ba91 9
marwanesaich 0:84a8c24e29ae 10
marwanesaich 0:84a8c24e29ae 11 CANMessage msgRxBuffer[SIZE_FIFO];
marwanesaich 0:84a8c24e29ae 12 unsigned char FIFO_ecriture=0; //Position du fifo pour la reception CAN
marwanesaich 0:84a8c24e29ae 13 signed char FIFO_lecture=0;//Position du fifo de lecture des messages CAN
kyxstark 8:a25a9c22ba91 14 char end_game = 0;
marwanesaich 0:84a8c24e29ae 15
kyxstark 8:a25a9c22ba91 16 Ventouse* ventouse[NB_VENTOUSES];
kyxstark 8:a25a9c22ba91 17
kyxstark 8:a25a9c22ba91 18 #ifdef ROBOT_SMALL
kyxstark 8:a25a9c22ba91 19
kyxstark 8:a25a9c22ba91 20 #endif
kyxstark 8:a25a9c22ba91 21
kyxstark 8:a25a9c22ba91 22 #ifdef ROBOT_BIG
kyxstark 8:a25a9c22ba91 23 PwmOut ascenseur[8] = {PC_9, PA_8, PA_9, PA_10, PC_7, PC_8, PB_10 ,PB_2};
kyxstark 8:a25a9c22ba91 24 PwmOut* ascenseur_D;
kyxstark 8:a25a9c22ba91 25 PwmOut* ascenseur_G;
kyxstark 8:a25a9c22ba91 26
kyxstark 8:a25a9c22ba91 27 #define VIT_G 0.7
kyxstark 8:a25a9c22ba91 28 #define VIT_D 0.65
kyxstark 8:a25a9c22ba91 29
kyxstark 8:a25a9c22ba91 30 void set_ascensseur(int allume);
kyxstark 8:a25a9c22ba91 31 #endif
kyxstark 8:a25a9c22ba91 32
marwanesaich 0:84a8c24e29ae 33
marwanesaich 0:84a8c24e29ae 34
marwanesaich 0:84a8c24e29ae 35 void get_etat_ventouses();
marwanesaich 0:84a8c24e29ae 36
kyxstark 8:a25a9c22ba91 37
marwanesaich 2:4cc8fa2f64be 38 CAN can(PB_8,PB_9,1000000);
marwanesaich 2:4cc8fa2f64be 39 Serial pc(USBTX,USBRX);
marwanesaich 0:84a8c24e29ae 40
marwanesaich 0:84a8c24e29ae 41 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 42 /* FUNCTION NAME: canRx_ISR */
marwanesaich 0:84a8c24e29ae 43 /* DESCRIPTION : lit les messages sur le can et les stocke dans la FIFO */
marwanesaich 0:84a8c24e29ae 44 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 45 void canRx_ISR (void)
marwanesaich 0:84a8c24e29ae 46 {
marwanesaich 0:84a8c24e29ae 47 if (can.read(msgRxBuffer[FIFO_ecriture]))
marwanesaich 0:84a8c24e29ae 48 FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO;
marwanesaich 0:84a8c24e29ae 49 }
marwanesaich 0:84a8c24e29ae 50
marwanesaich 0:84a8c24e29ae 51
marwanesaich 0:84a8c24e29ae 52 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 53 /* FUNCTION NAME: canProcessRx */
marwanesaich 0:84a8c24e29ae 54 /* DESCRIPTION : Fonction de traitement des messages CAN */
marwanesaich 0:84a8c24e29ae 55 /*********************************************************************************************************/
marwanesaich 0:84a8c24e29ae 56 void canProcessRx(void){
marwanesaich 0:84a8c24e29ae 57 static signed char FIFO_occupation=0,FIFO_max_occupation=0;
marwanesaich 0:84a8c24e29ae 58 CANMessage msgTx=CANMessage();
marwanesaich 0:84a8c24e29ae 59 FIFO_occupation=FIFO_ecriture-FIFO_lecture;
marwanesaich 0:84a8c24e29ae 60 if(FIFO_occupation<0)
marwanesaich 0:84a8c24e29ae 61 FIFO_occupation=FIFO_occupation+SIZE_FIFO;
marwanesaich 0:84a8c24e29ae 62 if(FIFO_max_occupation<FIFO_occupation)
marwanesaich 0:84a8c24e29ae 63 FIFO_max_occupation=FIFO_occupation;
marwanesaich 0:84a8c24e29ae 64 if(FIFO_occupation!=0) {
marwanesaich 0:84a8c24e29ae 65 int identifiant=msgRxBuffer[FIFO_lecture].id;
marwanesaich 1:8a55f1d5cb26 66 char BRAS = 0;
marwanesaich 1:8a55f1d5cb26 67
marwanesaich 0:84a8c24e29ae 68 switch(identifiant){
marwanesaich 0:84a8c24e29ae 69
marwanesaich 0:84a8c24e29ae 70 case HACHEUR_GET_ATOM:
marwanesaich 1:8a55f1d5cb26 71 BRAS = msgRxBuffer[FIFO_lecture].data[0];
marwanesaich 2:4cc8fa2f64be 72 ventouse[BRAS]->action(1);
marwanesaich 0:84a8c24e29ae 73 break;
kyxstark 8:a25a9c22ba91 74
marwanesaich 0:84a8c24e29ae 75 case HACHEUR_RELEASE_ATOM:
marwanesaich 1:8a55f1d5cb26 76 BRAS = msgRxBuffer[FIFO_lecture].data[0];
marwanesaich 2:4cc8fa2f64be 77 ventouse[BRAS]->action(0);
kyxstark 8:a25a9c22ba91 78 break;
kyxstark 8:a25a9c22ba91 79
kyxstark 8:a25a9c22ba91 80 case HACHEUR_STATUT_VENTOUSES:
kyxstark 8:a25a9c22ba91 81 get_etat_ventouses();
kyxstark 8:a25a9c22ba91 82 break;
kyxstark 8:a25a9c22ba91 83
kyxstark 8:a25a9c22ba91 84 case GLOBAL_GAME_END:
kyxstark 8:a25a9c22ba91 85 end_game = 1;
kyxstark 8:a25a9c22ba91 86 break;
kyxstark 8:a25a9c22ba91 87
kyxstark 8:a25a9c22ba91 88 #ifdef ROBOT_SMALL
kyxstark 8:a25a9c22ba91 89
marwanesaich 0:84a8c24e29ae 90
marwanesaich 4:82c90657da4a 91 case HACHEUR_GET_PRESENTOIR_AR:
marwanesaich 5:176e7353ba1c 92 ventouse[AR_GAUCHE]->action(1);
marwanesaich 5:176e7353ba1c 93 ventouse[AR_CENTRE]->action(1);
marwanesaich 5:176e7353ba1c 94 ventouse[AR_DROIT]->action(1);
marwanesaich 0:84a8c24e29ae 95 break;
marwanesaich 0:84a8c24e29ae 96
marwanesaich 5:176e7353ba1c 97 case HACHEUR_RELEASE_AR:
marwanesaich 5:176e7353ba1c 98 ventouse[AR_GAUCHE]->action(0);
marwanesaich 5:176e7353ba1c 99 ventouse[AR_CENTRE]->action(0);
marwanesaich 5:176e7353ba1c 100 ventouse[AR_DROIT]->action(0);
marwanesaich 5:176e7353ba1c 101 break;
marwanesaich 5:176e7353ba1c 102
marwanesaich 1:8a55f1d5cb26 103 case HACHEUR_GET_PRESENTOIR_AV :
marwanesaich 5:176e7353ba1c 104 ventouse[AV_GAUCHE]->action(1);
marwanesaich 5:176e7353ba1c 105 ventouse[AV_CENTRE]->action(1);
marwanesaich 5:176e7353ba1c 106 ventouse[AV_DROIT]->action(1);
marwanesaich 0:84a8c24e29ae 107 break;
marwanesaich 0:84a8c24e29ae 108
marwanesaich 5:176e7353ba1c 109 case HACHEUR_RELEASE_AV :
marwanesaich 5:176e7353ba1c 110 ventouse[AV_GAUCHE]->action(0);
marwanesaich 5:176e7353ba1c 111 ventouse[AV_CENTRE]->action(0);
marwanesaich 5:176e7353ba1c 112 ventouse[AV_DROIT]->action(0);
marwanesaich 5:176e7353ba1c 113 break;
marwanesaich 5:176e7353ba1c 114
kyxstark 8:a25a9c22ba91 115
kyxstark 8:a25a9c22ba91 116
kyxstark 8:a25a9c22ba91 117
kyxstark 8:a25a9c22ba91 118 #endif
kyxstark 8:a25a9c22ba91 119
kyxstark 8:a25a9c22ba91 120 #ifdef ROBOT_BIG
kyxstark 8:a25a9c22ba91 121
kyxstark 8:a25a9c22ba91 122 case HACHEUR_GET_PRESENTOIR_AV :
kyxstark 8:a25a9c22ba91 123 ventouse[PGR_GAUCHE]->action(1);
kyxstark 8:a25a9c22ba91 124 ventouse[PGR_CENTRE]->action(1);
kyxstark 8:a25a9c22ba91 125 ventouse[PGR_DROIT]->action(1);
marwanesaich 0:84a8c24e29ae 126 break;
marwanesaich 0:84a8c24e29ae 127
kyxstark 8:a25a9c22ba91 128 case HACHEUR_RELEASE_AV :
kyxstark 8:a25a9c22ba91 129 ventouse[PGR_GAUCHE]->action(0);
kyxstark 8:a25a9c22ba91 130 ventouse[PGR_CENTRE]->action(0);
kyxstark 8:a25a9c22ba91 131 ventouse[PGR_DROIT]->action(0);
marwanesaich 0:84a8c24e29ae 132 break;
kyxstark 8:a25a9c22ba91 133
kyxstark 9:8193c05bd0a0 134 case HACHEUR_ID_COUROIES :
kyxstark 8:a25a9c22ba91 135 set_ascensseur(msgRxBuffer[FIFO_lecture].data[0]);
kyxstark 8:a25a9c22ba91 136 break;
marwanesaich 0:84a8c24e29ae 137
marwanesaich 0:84a8c24e29ae 138 #endif
marwanesaich 0:84a8c24e29ae 139 }
marwanesaich 0:84a8c24e29ae 140 FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
marwanesaich 0:84a8c24e29ae 141 }
marwanesaich 0:84a8c24e29ae 142 }
marwanesaich 0:84a8c24e29ae 143
marwanesaich 0:84a8c24e29ae 144 int main() {
marwanesaich 0:84a8c24e29ae 145
marwanesaich 0:84a8c24e29ae 146 can.attach(&canRx_ISR); // création de l'interrupt attachée à la réception sur le CAN
marwanesaich 0:84a8c24e29ae 147 wait(1);
marwanesaich 1:8a55f1d5cb26 148 #ifdef ROBOT_SMALL
kyxstark 10:765b4ff6d721 149 pc.printf("\nPetit robot\n\n");
marwanesaich 2:4cc8fa2f64be 150 ventouse[AR_CENTRE] = new Ventouse(PC_9, PA_8, PB_0, AR_CENTRE);
marwanesaich 2:4cc8fa2f64be 151 ventouse[AR_DROIT] = new Ventouse(PA_9, PA_10, PB_1, AR_DROIT);
marwanesaich 5:176e7353ba1c 152 //ventouse[AR_BAS] = new Ventouse(PA_11, PA_15, PC_1, AR_BAS);
marwanesaich 5:176e7353ba1c 153 ventouse[AR_BAS] = new Ventouse(PA_11, PC_1, AR_BAS);
marwanesaich 2:4cc8fa2f64be 154 ventouse[AV_GAUCHE] = new Ventouse(PB_7, PB_6, PC_0, AV_GAUCHE);
marwanesaich 0:84a8c24e29ae 155
marwanesaich 2:4cc8fa2f64be 156 ventouse[AV_CENTRE] = new Ventouse(PC_7,PC_8 , PC_5, AV_CENTRE);
marwanesaich 4:82c90657da4a 157 ventouse[AR_GAUCHE] = new Ventouse(PB_10, PB_2, PC_4, AR_GAUCHE);
marwanesaich 5:176e7353ba1c 158 //ventouse[AV_BAS] = new Ventouse(PA_6, PA_5, PA_7, AV_BAS);
marwanesaich 5:176e7353ba1c 159 ventouse[AV_BAS] = new Ventouse(PA_6, PA_7, AV_BAS);
marwanesaich 2:4cc8fa2f64be 160 ventouse[AV_DROIT] = new Ventouse(PA_0, PA_1, PA_4, AV_DROIT);
kyxstark 8:a25a9c22ba91 161
marwanesaich 4:82c90657da4a 162
kyxstark 8:a25a9c22ba91 163 #endif
kyxstark 8:a25a9c22ba91 164
kyxstark 8:a25a9c22ba91 165 #ifdef ROBOT_BIG
kyxstark 10:765b4ff6d721 166 pc.printf("\nGros robot\n\n");
kyxstark 8:a25a9c22ba91 167 ventouse[PGR_GAUCHE] = new Ventouse(PA_6, PA_5, PA_7, PGR_GAUCHE);
kyxstark 8:a25a9c22ba91 168 ventouse[PGR_CENTRE] = new Ventouse(PA_0, PA_1, PA_4, PGR_CENTRE);
kyxstark 8:a25a9c22ba91 169 ventouse[PGR_DROIT] = new Ventouse(PA_11, PA_15, PC_1, PGR_DROIT);
kyxstark 8:a25a9c22ba91 170
kyxstark 8:a25a9c22ba91 171 for (int i=0; i<8; i++){
kyxstark 8:a25a9c22ba91 172 ascenseur[i].period_us(50);
kyxstark 8:a25a9c22ba91 173 }
kyxstark 8:a25a9c22ba91 174
kyxstark 8:a25a9c22ba91 175 ascenseur_D = ascenseur;
kyxstark 8:a25a9c22ba91 176 ascenseur_G = ascenseur + 4;
kyxstark 8:a25a9c22ba91 177
kyxstark 8:a25a9c22ba91 178
kyxstark 8:a25a9c22ba91 179 set_ascensseur(0);
kyxstark 8:a25a9c22ba91 180 wait(1);
marwanesaich 1:8a55f1d5cb26 181 #endif
kyxstark 8:a25a9c22ba91 182
marwanesaich 1:8a55f1d5cb26 183 pc.printf("LAUNCHED");
marwanesaich 0:84a8c24e29ae 184 while(1) {
marwanesaich 0:84a8c24e29ae 185 canProcessRx();
kyxstark 8:a25a9c22ba91 186 for(int i=0; i<NB_VENTOUSES; i++){
marwanesaich 2:4cc8fa2f64be 187 ventouse[i]->automate();
marwanesaich 2:4cc8fa2f64be 188 }
marwanesaich 0:84a8c24e29ae 189 if(end_game){
kyxstark 8:a25a9c22ba91 190 for(int i=0; i<NB_VENTOUSES;i++){
marwanesaich 0:84a8c24e29ae 191 ventouse[i]->setPompe(0.0);
kyxstark 8:a25a9c22ba91 192 ventouse[i]->setElectrovanne(0);
marwanesaich 0:84a8c24e29ae 193 }
marwanesaich 11:98dfe1a61df5 194 #ifdef ROBOT_BIG
marwanesaich 11:98dfe1a61df5 195 set_ascensseur(0);
marwanesaich 11:98dfe1a61df5 196 #endif
marwanesaich 0:84a8c24e29ae 197 while(1){;}
marwanesaich 0:84a8c24e29ae 198 }
marwanesaich 0:84a8c24e29ae 199 }
marwanesaich 0:84a8c24e29ae 200 }
marwanesaich 0:84a8c24e29ae 201
marwanesaich 0:84a8c24e29ae 202
kyxstark 8:a25a9c22ba91 203
kyxstark 8:a25a9c22ba91 204 #ifdef ROBOT_BIG
kyxstark 8:a25a9c22ba91 205 void set_ascensseur(int allume){
kyxstark 8:a25a9c22ba91 206 float vit = VIT_D*allume;
kyxstark 8:a25a9c22ba91 207 for (int i=0; i<4; i++){
kyxstark 8:a25a9c22ba91 208 ascenseur_D[i] = vit;
kyxstark 8:a25a9c22ba91 209 }
kyxstark 8:a25a9c22ba91 210 vit = VIT_G*allume;
kyxstark 8:a25a9c22ba91 211 for (int i=0; i<4; i++){
kyxstark 8:a25a9c22ba91 212 ascenseur_G[i] = vit;
kyxstark 8:a25a9c22ba91 213 }
kyxstark 8:a25a9c22ba91 214 }
kyxstark 8:a25a9c22ba91 215 #endif
kyxstark 8:a25a9c22ba91 216
marwanesaich 0:84a8c24e29ae 217 void get_etat_ventouses()
marwanesaich 0:84a8c24e29ae 218 {
marwanesaich 0:84a8c24e29ae 219 CANMessage etat_ventouses = CANMessage();
marwanesaich 0:84a8c24e29ae 220 etat_ventouses.id=HACHEUR_STATUT_VENTOUSES;
marwanesaich 0:84a8c24e29ae 221 etat_ventouses.len=2;
marwanesaich 0:84a8c24e29ae 222 etat_ventouses.format=CANStandard;
marwanesaich 0:84a8c24e29ae 223 etat_ventouses.type=CANData;
marwanesaich 2:4cc8fa2f64be 224 etat_ventouses.data[0]=0;
marwanesaich 2:4cc8fa2f64be 225 etat_ventouses.data[0]=1;
kyxstark 10:765b4ff6d721 226 for(int i = (NB_VENTOUSES - 1); i >= 0; i--){
marwanesaich 0:84a8c24e29ae 227 etat_ventouses.data[0] = (etat_ventouses.data[0]<<1) + ventouse[i]->getPompe();
marwanesaich 0:84a8c24e29ae 228 etat_ventouses.data[1] = (etat_ventouses.data[1]<<1) + ventouse[i]->getPression();
marwanesaich 0:84a8c24e29ae 229 }
marwanesaich 0:84a8c24e29ae 230 can.write(etat_ventouses);
marwanesaich 0:84a8c24e29ae 231
marwanesaich 0:84a8c24e29ae 232 }