carte esclave pompe 2020 V1

Dependencies:   mbed ident_crac

main.cpp

Committer:
kyxstark
Date:
2019-05-22
Revision:
10:765b4ff6d721
Parent:
9:8193c05bd0a0
Child:
11:98dfe1a61df5
Child:
12:2491a5b0f90b

File content as of revision 10:765b4ff6d721:

#include "mbed.h"
#include "class_ventouse.h"
#include "ident_crac.h"
#include "extern.h"
#define SIZE_FIFO 50





CANMessage msgRxBuffer[SIZE_FIFO];
unsigned char FIFO_ecriture=0; //Position du fifo pour la reception CAN
signed char FIFO_lecture=0;//Position du fifo de lecture des messages CAN
char end_game = 0;

Ventouse* ventouse[NB_VENTOUSES];

#ifdef ROBOT_SMALL
         
#endif

#ifdef ROBOT_BIG
PwmOut ascenseur[8] = {PC_9, PA_8, PA_9, PA_10, PC_7, PC_8, PB_10 ,PB_2};
PwmOut* ascenseur_D;
PwmOut* ascenseur_G;

#define VIT_G 0.7
#define VIT_D 0.65

void set_ascensseur(int allume);
#endif



void get_etat_ventouses();


CAN can(PB_8,PB_9,1000000);
Serial pc(USBTX,USBRX);

/*********************************************************************************************************/
/* FUNCTION NAME: canRx_ISR                                                                              */
/* DESCRIPTION  : lit les messages sur le can et les stocke dans la FIFO                                 */
/*********************************************************************************************************/
void canRx_ISR (void)
{
    if (can.read(msgRxBuffer[FIFO_ecriture]))
        FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO;
}


/*********************************************************************************************************/
/* FUNCTION NAME: canProcessRx                                                                           */
/* DESCRIPTION  : Fonction de traitement des messages CAN                                                */
/*********************************************************************************************************/
void canProcessRx(void){
    static signed char FIFO_occupation=0,FIFO_max_occupation=0;
    CANMessage msgTx=CANMessage();
    FIFO_occupation=FIFO_ecriture-FIFO_lecture;
    if(FIFO_occupation<0)
        FIFO_occupation=FIFO_occupation+SIZE_FIFO;
    if(FIFO_max_occupation<FIFO_occupation)
        FIFO_max_occupation=FIFO_occupation;
    if(FIFO_occupation!=0) {
    int identifiant=msgRxBuffer[FIFO_lecture].id;
    char BRAS = 0;
    
        switch(identifiant){
            
            case HACHEUR_GET_ATOM:
                BRAS = msgRxBuffer[FIFO_lecture].data[0];
                ventouse[BRAS]->action(1);
                break;
                
            case HACHEUR_RELEASE_ATOM:
                BRAS = msgRxBuffer[FIFO_lecture].data[0];
                ventouse[BRAS]->action(0);
                break;   
            
            case HACHEUR_STATUT_VENTOUSES:
                get_etat_ventouses();
                break;
            
            case GLOBAL_GAME_END:
                end_game = 1;
                break;
            
            #ifdef ROBOT_SMALL
                       
            
            case HACHEUR_GET_PRESENTOIR_AR:           
                ventouse[AR_GAUCHE]->action(1);
                ventouse[AR_CENTRE]->action(1);
                ventouse[AR_DROIT]->action(1);
                break;
            
            case HACHEUR_RELEASE_AR:           
                ventouse[AR_GAUCHE]->action(0);
                ventouse[AR_CENTRE]->action(0);
                ventouse[AR_DROIT]->action(0);
                break;
                
            case HACHEUR_GET_PRESENTOIR_AV :
                ventouse[AV_GAUCHE]->action(1);
                ventouse[AV_CENTRE]->action(1);
                ventouse[AV_DROIT]->action(1);
                break;
            
            case HACHEUR_RELEASE_AV :
                ventouse[AV_GAUCHE]->action(0);
                ventouse[AV_CENTRE]->action(0);
                ventouse[AV_DROIT]->action(0);
                break;
                

            

            #endif

            #ifdef ROBOT_BIG            
            
            case HACHEUR_GET_PRESENTOIR_AV :
                ventouse[PGR_GAUCHE]->action(1);
                ventouse[PGR_CENTRE]->action(1);
                ventouse[PGR_DROIT]->action(1);
                break;
            
            case HACHEUR_RELEASE_AV :
                ventouse[PGR_GAUCHE]->action(0);
                ventouse[PGR_CENTRE]->action(0);
                ventouse[PGR_DROIT]->action(0);
                break;
            
            case HACHEUR_ID_COUROIES :
                set_ascensseur(msgRxBuffer[FIFO_lecture].data[0]);
                break;
            
            #endif
        }
        FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
    }
}

int main() {
    
    can.attach(&canRx_ISR); // création de l'interrupt attachée à la réception sur le CAN
    wait(1);
    #ifdef ROBOT_SMALL
    pc.printf("\nPetit robot\n\n");
    ventouse[AR_CENTRE] = new Ventouse(PC_9, PA_8, PB_0, AR_CENTRE);
    ventouse[AR_DROIT] = new Ventouse(PA_9, PA_10,  PB_1, AR_DROIT);
    //ventouse[AR_BAS] = new Ventouse(PA_11, PA_15, PC_1, AR_BAS);
    ventouse[AR_BAS] = new Ventouse(PA_11, PC_1, AR_BAS);
    ventouse[AV_GAUCHE] = new Ventouse(PB_7, PB_6, PC_0, AV_GAUCHE);
    
    ventouse[AV_CENTRE] = new Ventouse(PC_7,PC_8 , PC_5, AV_CENTRE);
    ventouse[AR_GAUCHE] = new Ventouse(PB_10, PB_2, PC_4, AR_GAUCHE);
    //ventouse[AV_BAS] = new Ventouse(PA_6, PA_5, PA_7, AV_BAS); 
    ventouse[AV_BAS] = new Ventouse(PA_6, PA_7, AV_BAS); 
    ventouse[AV_DROIT] = new Ventouse(PA_0, PA_1, PA_4, AV_DROIT);
    

    #endif    
    
    #ifdef ROBOT_BIG
    pc.printf("\nGros robot\n\n");
    ventouse[PGR_GAUCHE] = new Ventouse(PA_6, PA_5, PA_7, PGR_GAUCHE);
    ventouse[PGR_CENTRE] = new Ventouse(PA_0, PA_1, PA_4, PGR_CENTRE);
    ventouse[PGR_DROIT] = new Ventouse(PA_11, PA_15, PC_1, PGR_DROIT);
    
    for (int i=0; i<8; i++){
        ascenseur[i].period_us(50);
    }
        
    ascenseur_D = ascenseur;
    ascenseur_G = ascenseur + 4;
    
    
    set_ascensseur(0);
    wait(1);
    #endif
    
    pc.printf("LAUNCHED");
    while(1) {
        canProcessRx();
        for(int i=0; i<NB_VENTOUSES; i++){
            ventouse[i]->automate();
        }
        if(end_game){
            for(int i=0; i<NB_VENTOUSES;i++){
                ventouse[i]->setPompe(0.0);
                ventouse[i]->setElectrovanne(0);
            }
            while(1){;}
        }
    }
}



#ifdef ROBOT_BIG
void set_ascensseur(int allume){
    float vit = VIT_D*allume;
    for (int i=0; i<4; i++){
        ascenseur_D[i] = vit;
    }
    vit = VIT_G*allume;
    for (int i=0; i<4; i++){
        ascenseur_G[i] = vit;
    }
}
#endif

void get_etat_ventouses()
{   
    CANMessage etat_ventouses = CANMessage();
    etat_ventouses.id=HACHEUR_STATUT_VENTOUSES;      
    etat_ventouses.len=2;
    etat_ventouses.format=CANStandard;
    etat_ventouses.type=CANData;
    etat_ventouses.data[0]=0;
    etat_ventouses.data[0]=1;
    for(int i = (NB_VENTOUSES - 1); i >= 0; i--){
        etat_ventouses.data[0] = (etat_ventouses.data[0]<<1) + ventouse[i]->getPompe();
        etat_ventouses.data[1] = (etat_ventouses.data[1]<<1) + ventouse[i]->getPression();       
    }
    can.write(etat_ventouses);

}