carte esclave pompe 2020 V1

Dependencies:   mbed ident_crac

Committer:
kyxstark
Date:
Wed May 22 09:52:47 2019 +0000
Revision:
8:a25a9c22ba91
Parent:
7:43d9c70b6689
Child:
9:8193c05bd0a0
merge PR GR

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 8:a25a9c22ba91 134 case ETAT_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
marwanesaich 2:4cc8fa2f64be 149 ventouse[AR_CENTRE] = new Ventouse(PC_9, PA_8, PB_0, AR_CENTRE);
marwanesaich 2:4cc8fa2f64be 150 ventouse[AR_DROIT] = new Ventouse(PA_9, PA_10, PB_1, AR_DROIT);
marwanesaich 5:176e7353ba1c 151 //ventouse[AR_BAS] = new Ventouse(PA_11, PA_15, PC_1, AR_BAS);
marwanesaich 5:176e7353ba1c 152 ventouse[AR_BAS] = new Ventouse(PA_11, PC_1, AR_BAS);
marwanesaich 2:4cc8fa2f64be 153 ventouse[AV_GAUCHE] = new Ventouse(PB_7, PB_6, PC_0, AV_GAUCHE);
marwanesaich 0:84a8c24e29ae 154
marwanesaich 2:4cc8fa2f64be 155 ventouse[AV_CENTRE] = new Ventouse(PC_7,PC_8 , PC_5, AV_CENTRE);
marwanesaich 4:82c90657da4a 156 ventouse[AR_GAUCHE] = new Ventouse(PB_10, PB_2, PC_4, AR_GAUCHE);
marwanesaich 5:176e7353ba1c 157 //ventouse[AV_BAS] = new Ventouse(PA_6, PA_5, PA_7, AV_BAS);
marwanesaich 5:176e7353ba1c 158 ventouse[AV_BAS] = new Ventouse(PA_6, PA_7, AV_BAS);
marwanesaich 2:4cc8fa2f64be 159 ventouse[AV_DROIT] = new Ventouse(PA_0, PA_1, PA_4, AV_DROIT);
kyxstark 8:a25a9c22ba91 160
marwanesaich 4:82c90657da4a 161
kyxstark 8:a25a9c22ba91 162 #endif
kyxstark 8:a25a9c22ba91 163
kyxstark 8:a25a9c22ba91 164 #ifdef ROBOT_BIG
kyxstark 8:a25a9c22ba91 165 ventouse[PGR_GAUCHE] = new Ventouse(PA_6, PA_5, PA_7, PGR_GAUCHE);
kyxstark 8:a25a9c22ba91 166 ventouse[PGR_CENTRE] = new Ventouse(PA_0, PA_1, PA_4, PGR_CENTRE);
kyxstark 8:a25a9c22ba91 167 ventouse[PGR_DROIT] = new Ventouse(PA_11, PA_15, PC_1, PGR_DROIT);
kyxstark 8:a25a9c22ba91 168
kyxstark 8:a25a9c22ba91 169 for (int i=0; i<8; i++){
kyxstark 8:a25a9c22ba91 170 ascenseur[i].period_us(50);
kyxstark 8:a25a9c22ba91 171 }
kyxstark 8:a25a9c22ba91 172
kyxstark 8:a25a9c22ba91 173 ascenseur_D = ascenseur;
kyxstark 8:a25a9c22ba91 174 ascenseur_G = ascenseur + 4;
kyxstark 8:a25a9c22ba91 175
kyxstark 8:a25a9c22ba91 176
kyxstark 8:a25a9c22ba91 177 set_ascensseur(0);
kyxstark 8:a25a9c22ba91 178 wait(1);
kyxstark 8:a25a9c22ba91 179 set_ascensseur(1);
kyxstark 8:a25a9c22ba91 180 wait(2);
kyxstark 8:a25a9c22ba91 181 set_ascensseur(0);
marwanesaich 1:8a55f1d5cb26 182 #endif
kyxstark 8:a25a9c22ba91 183
marwanesaich 1:8a55f1d5cb26 184 pc.printf("LAUNCHED");
marwanesaich 0:84a8c24e29ae 185 while(1) {
marwanesaich 0:84a8c24e29ae 186 canProcessRx();
kyxstark 8:a25a9c22ba91 187 for(int i=0; i<NB_VENTOUSES; i++){
marwanesaich 2:4cc8fa2f64be 188 ventouse[i]->automate();
marwanesaich 2:4cc8fa2f64be 189 }
marwanesaich 0:84a8c24e29ae 190 if(end_game){
kyxstark 8:a25a9c22ba91 191 for(int i=0; i<NB_VENTOUSES;i++){
marwanesaich 0:84a8c24e29ae 192 ventouse[i]->setPompe(0.0);
kyxstark 8:a25a9c22ba91 193 ventouse[i]->setElectrovanne(0);
marwanesaich 0:84a8c24e29ae 194 }
marwanesaich 0:84a8c24e29ae 195 while(1){;}
marwanesaich 0:84a8c24e29ae 196 }
marwanesaich 0:84a8c24e29ae 197 }
marwanesaich 0:84a8c24e29ae 198 }
marwanesaich 0:84a8c24e29ae 199
marwanesaich 0:84a8c24e29ae 200
kyxstark 8:a25a9c22ba91 201
kyxstark 8:a25a9c22ba91 202 #ifdef ROBOT_BIG
kyxstark 8:a25a9c22ba91 203 void set_ascensseur(int allume){
kyxstark 8:a25a9c22ba91 204 float vit = VIT_D*allume;
kyxstark 8:a25a9c22ba91 205 for (int i=0; i<4; i++){
kyxstark 8:a25a9c22ba91 206 ascenseur_D[i] = vit;
kyxstark 8:a25a9c22ba91 207 }
kyxstark 8:a25a9c22ba91 208 vit = VIT_G*allume;
kyxstark 8:a25a9c22ba91 209 for (int i=0; i<4; i++){
kyxstark 8:a25a9c22ba91 210 ascenseur_G[i] = vit;
kyxstark 8:a25a9c22ba91 211 }
kyxstark 8:a25a9c22ba91 212 }
kyxstark 8:a25a9c22ba91 213 #endif
kyxstark 8:a25a9c22ba91 214
marwanesaich 0:84a8c24e29ae 215 void get_etat_ventouses()
marwanesaich 0:84a8c24e29ae 216 {
marwanesaich 0:84a8c24e29ae 217 CANMessage etat_ventouses = CANMessage();
marwanesaich 0:84a8c24e29ae 218 etat_ventouses.id=HACHEUR_STATUT_VENTOUSES;
marwanesaich 0:84a8c24e29ae 219 etat_ventouses.len=2;
marwanesaich 0:84a8c24e29ae 220 etat_ventouses.format=CANStandard;
marwanesaich 0:84a8c24e29ae 221 etat_ventouses.type=CANData;
marwanesaich 2:4cc8fa2f64be 222 etat_ventouses.data[0]=0;
marwanesaich 2:4cc8fa2f64be 223 etat_ventouses.data[0]=1;
kyxstark 8:a25a9c22ba91 224 for(int i=NB_VENTOUSES; i>=0; i--){
marwanesaich 0:84a8c24e29ae 225 etat_ventouses.data[0] = (etat_ventouses.data[0]<<1) + ventouse[i]->getPompe();
marwanesaich 0:84a8c24e29ae 226 etat_ventouses.data[1] = (etat_ventouses.data[1]<<1) + ventouse[i]->getPression();
marwanesaich 0:84a8c24e29ae 227 }
marwanesaich 0:84a8c24e29ae 228 can.write(etat_ventouses);
marwanesaich 0:84a8c24e29ae 229
marwanesaich 0:84a8c24e29ae 230 }