carte esclave pompe 2020 V1
Dependencies: mbed ident_crac
main.cpp@19:952208276c63, 2020-03-05 (annotated)
- Committer:
- maximilienlv
- Date:
- Thu Mar 05 16:32:21 2020 +0000
- Revision:
- 19:952208276c63
- Parent:
- 18:972d29c668a5
- Child:
- 20:677dcf689523
carte esclave pompe 2020
Who changed what in which revision?
User | Revision | Line number | New 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 | |
marwanesaich | 0:84a8c24e29ae | 59 | switch(identifiant){ |
marwanesaich | 0:84a8c24e29ae | 60 | |
maximilienlv | 19:952208276c63 | 61 | /*case HACHEUR_GET_ATOM: |
marwanesaich | 1:8a55f1d5cb26 | 62 | BRAS = msgRxBuffer[FIFO_lecture].data[0]; |
marwanesaich | 2:4cc8fa2f64be | 63 | ventouse[BRAS]->action(1); |
marwanesaich | 0:84a8c24e29ae | 64 | break; |
kyxstark | 8:a25a9c22ba91 | 65 | |
marwanesaich | 0:84a8c24e29ae | 66 | case HACHEUR_RELEASE_ATOM: |
marwanesaich | 1:8a55f1d5cb26 | 67 | BRAS = msgRxBuffer[FIFO_lecture].data[0]; |
marwanesaich | 2:4cc8fa2f64be | 68 | ventouse[BRAS]->action(0); |
kyxstark | 8:a25a9c22ba91 | 69 | break; |
kyxstark | 8:a25a9c22ba91 | 70 | |
kyxstark | 8:a25a9c22ba91 | 71 | case HACHEUR_STATUT_VENTOUSES: |
kyxstark | 8:a25a9c22ba91 | 72 | get_etat_ventouses(); |
kyxstark | 8:a25a9c22ba91 | 73 | break; |
kyxstark | 8:a25a9c22ba91 | 74 | |
kyxstark | 8:a25a9c22ba91 | 75 | case GLOBAL_GAME_END: |
kyxstark | 8:a25a9c22ba91 | 76 | end_game = 1; |
kyxstark | 8:a25a9c22ba91 | 77 | break; |
kyxstark | 17:fcfdd7de9039 | 78 | |
kyxstark | 17:fcfdd7de9039 | 79 | case HACHEUR_ETAT_CONTACTS: |
kyxstark | 17:fcfdd7de9039 | 80 | char contacts_tmp = contacts; |
kyxstark | 17:fcfdd7de9039 | 81 | can.write(CANMessage(HACHEUR_ETAT_CONTACTS,&contacts_tmp,1)); |
kyxstark | 17:fcfdd7de9039 | 82 | break; |
kyxstark | 8:a25a9c22ba91 | 83 | |
marwanesaich | 4:82c90657da4a | 84 | case HACHEUR_GET_PRESENTOIR_AR: |
marwanesaich | 5:176e7353ba1c | 85 | ventouse[AR_GAUCHE]->action(1); |
marwanesaich | 5:176e7353ba1c | 86 | ventouse[AR_CENTRE]->action(1); |
marwanesaich | 5:176e7353ba1c | 87 | ventouse[AR_DROIT]->action(1); |
marwanesaich | 0:84a8c24e29ae | 88 | break; |
marwanesaich | 0:84a8c24e29ae | 89 | |
marwanesaich | 5:176e7353ba1c | 90 | case HACHEUR_RELEASE_AR: |
marwanesaich | 5:176e7353ba1c | 91 | ventouse[AR_GAUCHE]->action(0); |
marwanesaich | 5:176e7353ba1c | 92 | ventouse[AR_CENTRE]->action(0); |
marwanesaich | 5:176e7353ba1c | 93 | ventouse[AR_DROIT]->action(0); |
marwanesaich | 5:176e7353ba1c | 94 | break; |
marwanesaich | 5:176e7353ba1c | 95 | |
marwanesaich | 1:8a55f1d5cb26 | 96 | case HACHEUR_GET_PRESENTOIR_AV : |
marwanesaich | 5:176e7353ba1c | 97 | ventouse[AV_GAUCHE]->action(1); |
marwanesaich | 5:176e7353ba1c | 98 | ventouse[AV_CENTRE]->action(1); |
marwanesaich | 5:176e7353ba1c | 99 | ventouse[AV_DROIT]->action(1); |
marwanesaich | 0:84a8c24e29ae | 100 | break; |
marwanesaich | 0:84a8c24e29ae | 101 | |
marwanesaich | 5:176e7353ba1c | 102 | case HACHEUR_RELEASE_AV : |
marwanesaich | 5:176e7353ba1c | 103 | ventouse[AV_GAUCHE]->action(0); |
marwanesaich | 5:176e7353ba1c | 104 | ventouse[AV_CENTRE]->action(0); |
marwanesaich | 5:176e7353ba1c | 105 | ventouse[AV_DROIT]->action(0); |
maximilienlv | 19:952208276c63 | 106 | break;*/ |
marwanesaich | 5:176e7353ba1c | 107 | |
maximilienlv | 19:952208276c63 | 108 | case TEST_ELECTROV_1: |
maximilienlv | 19:952208276c63 | 109 | ventouse[AR_GAUCHE]->action(1); |
maximilienlv | 19:952208276c63 | 110 | ventouse[AR_CENTRE]->action(1); |
maximilienlv | 19:952208276c63 | 111 | ventouse[AR_DROIT]->action(1); |
kyxstark | 13:178eaf022d60 | 112 | ventouse[AV_GAUCHE]->action(1); |
kyxstark | 13:178eaf022d60 | 113 | ventouse[AV_CENTRE]->action(1); |
kyxstark | 13:178eaf022d60 | 114 | ventouse[AV_DROIT]->action(1); |
marwanesaich | 0:84a8c24e29ae | 115 | break; |
maximilienlv | 19:952208276c63 | 116 | |
maximilienlv | 19:952208276c63 | 117 | case TEST_ELECTROV_2: |
maximilienlv | 19:952208276c63 | 118 | ventouse[AR_GAUCHE]->action(0); |
maximilienlv | 19:952208276c63 | 119 | ventouse[AR_CENTRE]->action(0); |
maximilienlv | 19:952208276c63 | 120 | ventouse[AR_DROIT]->action(0); |
kyxstark | 13:178eaf022d60 | 121 | ventouse[AV_GAUCHE]->action(0); |
kyxstark | 13:178eaf022d60 | 122 | ventouse[AV_CENTRE]->action(0); |
kyxstark | 13:178eaf022d60 | 123 | ventouse[AV_DROIT]->action(0); |
marwanesaich | 0:84a8c24e29ae | 124 | break; |
marwanesaich | 0:84a8c24e29ae | 125 | } |
marwanesaich | 0:84a8c24e29ae | 126 | FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO; |
marwanesaich | 0:84a8c24e29ae | 127 | } |
marwanesaich | 0:84a8c24e29ae | 128 | } |
marwanesaich | 0:84a8c24e29ae | 129 | |
marwanesaich | 0:84a8c24e29ae | 130 | int main() { |
marwanesaich | 0:84a8c24e29ae | 131 | |
marwanesaich | 0:84a8c24e29ae | 132 | can.attach(&canRx_ISR); // création de l'interrupt attachée à la réception sur le CAN |
marwanesaich | 0:84a8c24e29ae | 133 | wait(1); |
maximilienlv | 19:952208276c63 | 134 | |
kyxstark | 10:765b4ff6d721 | 135 | pc.printf("\nPetit robot\n\n"); |
marwanesaich | 16:5e00af31ae8a | 136 | ventouse[AV_GAUCHE] = new Ventouse(PB_7, PB_6, PC_0, AV_GAUCHE); |
marwanesaich | 2:4cc8fa2f64be | 137 | ventouse[AR_CENTRE] = new Ventouse(PC_9, PA_8, PB_0, AR_CENTRE); |
marwanesaich | 2:4cc8fa2f64be | 138 | ventouse[AR_DROIT] = new Ventouse(PA_9, PA_10, PB_1, AR_DROIT); |
marwanesaich | 0:84a8c24e29ae | 139 | |
marwanesaich | 16:5e00af31ae8a | 140 | ventouse[AV_DROIT] = new Ventouse(PA_0, PA_1, PA_4, AV_DROIT); |
marwanesaich | 2:4cc8fa2f64be | 141 | ventouse[AV_CENTRE] = new Ventouse(PC_7,PC_8 , PC_5, AV_CENTRE); |
marwanesaich | 4:82c90657da4a | 142 | ventouse[AR_GAUCHE] = new Ventouse(PB_10, PB_2, PC_4, AR_GAUCHE); |
kyxstark | 8:a25a9c22ba91 | 143 | |
marwanesaich | 1:8a55f1d5cb26 | 144 | pc.printf("LAUNCHED"); |
marwanesaich | 0:84a8c24e29ae | 145 | while(1) { |
marwanesaich | 0:84a8c24e29ae | 146 | canProcessRx(); |
kyxstark | 8:a25a9c22ba91 | 147 | for(int i=0; i<NB_VENTOUSES; i++){ |
marwanesaich | 2:4cc8fa2f64be | 148 | ventouse[i]->automate(); |
kyxstark | 17:fcfdd7de9039 | 149 | } |
kyxstark | 17:fcfdd7de9039 | 150 | lecture_contacts(); |
kyxstark | 17:fcfdd7de9039 | 151 | |
kyxstark | 17:fcfdd7de9039 | 152 | |
marwanesaich | 0:84a8c24e29ae | 153 | if(end_game){ |
kyxstark | 8:a25a9c22ba91 | 154 | for(int i=0; i<NB_VENTOUSES;i++){ |
marwanesaich | 0:84a8c24e29ae | 155 | ventouse[i]->setPompe(0.0); |
kyxstark | 8:a25a9c22ba91 | 156 | ventouse[i]->setElectrovanne(0); |
marwanesaich | 0:84a8c24e29ae | 157 | } |
marwanesaich | 0:84a8c24e29ae | 158 | while(1){;} |
marwanesaich | 0:84a8c24e29ae | 159 | } |
marwanesaich | 0:84a8c24e29ae | 160 | } |
marwanesaich | 0:84a8c24e29ae | 161 | } |
marwanesaich | 0:84a8c24e29ae | 162 | |
kyxstark | 17:fcfdd7de9039 | 163 | void lecture_contacts(void) |
kyxstark | 17:fcfdd7de9039 | 164 | { |
kyxstark | 17:fcfdd7de9039 | 165 | static char prev_contacts = contacts; |
kyxstark | 17:fcfdd7de9039 | 166 | |
kyxstark | 17:fcfdd7de9039 | 167 | if(contacts != prev_contacts) |
kyxstark | 17:fcfdd7de9039 | 168 | { |
kyxstark | 17:fcfdd7de9039 | 169 | prev_contacts = contacts; |
kyxstark | 17:fcfdd7de9039 | 170 | can.write(CANMessage(HACHEUR_ETAT_CONTACTS,&prev_contacts,1)); |
kyxstark | 17:fcfdd7de9039 | 171 | } |
kyxstark | 17:fcfdd7de9039 | 172 | } |
kyxstark | 8:a25a9c22ba91 | 173 | |
marwanesaich | 0:84a8c24e29ae | 174 | void get_etat_ventouses() |
marwanesaich | 0:84a8c24e29ae | 175 | { |
marwanesaich | 0:84a8c24e29ae | 176 | CANMessage etat_ventouses = CANMessage(); |
marwanesaich | 0:84a8c24e29ae | 177 | etat_ventouses.id=HACHEUR_STATUT_VENTOUSES; |
marwanesaich | 0:84a8c24e29ae | 178 | etat_ventouses.len=2; |
marwanesaich | 0:84a8c24e29ae | 179 | etat_ventouses.format=CANStandard; |
marwanesaich | 0:84a8c24e29ae | 180 | etat_ventouses.type=CANData; |
marwanesaich | 2:4cc8fa2f64be | 181 | etat_ventouses.data[0]=0; |
marwanesaich | 2:4cc8fa2f64be | 182 | etat_ventouses.data[0]=1; |
kyxstark | 10:765b4ff6d721 | 183 | for(int i = (NB_VENTOUSES - 1); i >= 0; i--){ |
marwanesaich | 0:84a8c24e29ae | 184 | etat_ventouses.data[0] = (etat_ventouses.data[0]<<1) + ventouse[i]->getPompe(); |
marwanesaich | 0:84a8c24e29ae | 185 | etat_ventouses.data[1] = (etat_ventouses.data[1]<<1) + ventouse[i]->getPression(); |
marwanesaich | 0:84a8c24e29ae | 186 | } |
marwanesaich | 0:84a8c24e29ae | 187 | can.write(etat_ventouses); |
marwanesaich | 0:84a8c24e29ae | 188 | |
marwanesaich | 0:84a8c24e29ae | 189 | } |