Pour toi Jerem

Dependencies:   mbed bloc_io mbed-rtos html EthernetInterface

main.cpp

Committer:
RomainCorradi
Date:
2019-10-28
Revision:
1:687bcd4b1c6c
Parent:
0:e30c9ba95bd4
Child:
2:172619ae2eb7

File content as of revision 1:687bcd4b1c6c:



//#include "EthernetInterface.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mbed.h"
#include "rtos.h" // need for main thread sleep
#include "html.h" // need for html patch working with web server
#include "bloc_io.h"
#define RADIUS  0.2F // wheel size
#define NBPOLES 8 // magnetic pole number
#define DELTA_T 0.1F // speed measurement counting period

DigitalOut PWM_VALID (p21);                         // valid pmw mbed pin
AnalogIn POIGNEE (p17);                            // analog input connected to mbed
Bloc_IO MyPLD(p25,p26,p5,p6,p7,p8,p9,p10,p23,p24);// instantiate object needed to communicate with PLD
Serial pc(USBTX, USBRX);                         // tx, rx
void PWM ();
Ticker AUTO;
float GazMax,GazMin;
int val,x,Overcurrent,Brake,FLTA,Direction,HALLC,HALLB,HALLA;
char cChoix=0;


float valpwm;
// Top_Hall Pin




/************ persistent file parameters section *****************/
LocalFileSystem local("local");// Create the local filesystem under the name "local"







/********************* web server section **********************************/

var_field_t tab_balise[10];  //une balise est présente dans le squelette
int giCounter=0;// acces counting


/*********************** can bus section  ************/
// determine message ID used to send Gaz ref over can bus
#define _CAN_DEBUG // used to debug can bus activity
//#define USE_CAN_REF // uncomment to receive gaz ref over can_bus
CAN can_port (p30, p29); // initialisation du Bus CAN sur les broches 30 (rd) et 29(td) for lpc1768 + mbed shield
bool bCan_Active=false;



DigitalOut led1(LED1); //initialisation des Leds présentes sur le micro-controleur Mbed*/
DigitalOut led2(LED2);
DigitalOut led3(LED3); // blink when can message is sent
DigitalOut led4(LED4); // blink when can message is received





//************ local function prototypes *******************







/**************** Read persistent data from text file located on local file system ****************/



/**************** write persitant  data to text file located on local file system ****************/






//************** calibation gaz function needed to record min_gaz and max_gaz value to persistent text file  ******************


// ************top hall counting interrupt needed for speed measurement


//********************** timer interrupt for speed measurement each 100ms  *************************






//********************* Timer Interrupt for gaz ref management each 10ms   ********************



/********* main cgi function used to patch data to the web server thread **********************************/
void CGI_Function(void) // cgi function that patch web data to empty web page
{
    char ma_chaine4[20]= {}; // needed to form html response

}


/*********************** CAN BUS SECTION  **********************/



void CAN_REC_THREAD(void const *args)
{
    int iCount,iError;

    while (bCan_Active) {
        Thread::wait(100);// wait 100ms
        // code todo

    }

}



//*************************** main function *****************************************
int main()
{
    MyPLD.write(0);
    char carac;
    FILE *fp = fopen("/local/gr1.txt", "r");
    if(fp != NULL) {
        fscanf(fp,"valeur max : %g, valeur min: %g",&GazMin,&GazMax);
        printf("Valeur min:%g , Valeur max:%g\n\r",GazMin,GazMax);
    } else
        printf("ERREUR FICHIER NON VALIDE");
    fclose(fp);

    // int write,read;
    // f_poignee = fopen("nom-de-fichier","r");
    // read=fgetc(f_poignee);
    //  fclose(f_poignee);
    //  printf("&d",read);




//***************************************** web section ********************************************/
//Init_Web_Server(&CGI_Function); // create and initialize tcp server socket and pass function pointer to local CGI function
//Thread WebThread(Web_Server_Thread);// create and launch web server thread
    /********* main cgi function used to patch data to the web server thread **********************************/

//******************************************* end web section  ************************************* /


//********************* can bus section initialisation *******************************************
//bCan_Active=true;// needed to lauchn CAN thread
//Thread CanThread(CAN_REC_THREAD);// create and launch can receiver  thread
//********************* end can bus section *****************************************************
    AUTO.attach(&PWM,0.1);
    pc.printf("programme scooter mbed \n\r");
    while(cChoix!='q' and cChoix!='Q') {
        pc.printf("Veuillez saisir un choix parmi la liste proposee:\n\r");
        pc.printf("a:saisie consigne pwm\n\r");
        pc.printf("b:Calibration poignee\n\r");
        pc.printf("d:Mode automatique\n\r");
        pc.printf("e:Lecture du registre interne\n\r");
        pc.printf("q:quitter\n\r");

        //************* multithreading : main thread need to sleep in order to allow web response */
        while (pc.readable()==0) { // determine if char availabler
            Thread::wait(10);   // wait 10 until char available on serial input
        }
        /************* end of main thread sleep  ****************/
        pc.scanf("%c",&cChoix);
        switch (cChoix) {
            case 'a':
                printf("veuillez entrer une valeur de PWM entre 0 et 255\n\r");
                scanf("%d",&val);
                /*  if (val < 0 && val > 255) {
                      PWM_VALID.write(0);
                      MyPLD.write(0);
                      printf("Erreur\n\r ");
                      printf("veuillez entrer une valeur de PWM entre 0 et 255 \n\r");
                      scanf("%d",&val);
                      }*/

                /*   PWM_VALID.write(1);
                   MyPLD.write(val);*/
                break;
            case 'q':
                AUTO.detach();
                PWM_VALID.write(0);
                if (PWM_VALID.read()==0) MyPLD.write(0);
                printf("BYE");
                break;
            case 'b':
                AUTO.detach();
                PWM_VALID.write(0);
                if (PWM_VALID.read()==0) MyPLD.write(0);
                printf("Veuillez entrer n'importe quel caractere si vous etes OK pour la valeur min\n\r");
                scanf("%c ",&carac);
                GazMin=POIGNEE.read();
                printf("\n\r");
                printf("Veuillez entrer n'importe quel caractere si vous etes OK pour la valeur max\n\r");
                scanf("%c ",&carac);
                GazMax=POIGNEE.read();
                printf("\n\r");
                printf("Valeur de la poignee min:%g\n\r",GazMin);
                printf("Valeur de la poignee max:%g\n\r",GazMax);
                FILE *fp = fopen("/local/gr1.txt", "w");
                if(fp != NULL) fprintf(fp,"valeur max : %f, valeur min: %f",GazMin,GazMax);
                else printf("ERREUR FICHIER NON VALIDE");
                fclose(fp);
                break;
            case 'd':
                PWM_VALID.write(1);
                AUTO.attach(&PWM,0.1);
                printf("Veuillez entrer n'importe quel caractere si vous voulez sortir du mode automatique\n\r");
                scanf("%c ",&carac);
                AUTO.detach();
                MyPLD.write(0);
                break;

            case 'e':
                x=MyPLD.read();
                Overcurrent= (x & 64)/64 ; //Technique pour avoir la valeur en binaire
                Brake= (x & 32)/32;
                FLTA=(x & 16)/16;
                Direction=(x & 8)/8;
                HALLC=(x & 4)/4;
                HALLB=(x & 2)/2;
                HALLA= x & 1;
                int hall=0;
                if (HALLA == 1){
                    hall++;      
                }
                if (HALLB == 1){
                    hall+=2;    
                }
                if (HALLC == 1){
                    hall+=4;
                }
                pc.printf("hall=%d\r\n", hall);
                if (Overcurrent == 1) {
                    printf("Surintensité ");
                } else {
                    printf( " desactive");
                }
                
                if (Brake == 1) {
                    printf(" desactive");
                } else {
                    printf( " active");
                }
                
                if (FLTA == 1) {
                    printf("Overcurrent active");
                } else {
                    printf( " desactive");
                }
                
                if (Direction == 1) {
                    printf("marche avant active");
                } else {
                    printf( "mzrche avant desactive");
                }
               // printf("%d\n\r",x);
                //printf("Overcurrent:%d, Brake:%d,FLTA:%d,Direction:%d,HALLC:%d,HALLB:%d,HALLA:%d\n\r",Overcurrent,Brake,FLTA,Direction,HALLC,HALLB,HALLA);
               // break;



                //f_poignee = fopen("nom-de-fichier","w");
                // while(read=fgetc(f_poignee)!= E0F) {
                //  write= GazMin;
                //  fputc('Valeur min:',f_poignee);
                // fputc(write,f_poignee);
                //write= GazMax;
                // fputc('Valeur max':,f_poignee);
                //fputc(write,f_poignee);
                // fclose(f_poignee);
        }
    }
    pc.printf("fin programme scooter mbed\n\r");
} // end while

//************** thread deinit *********************
//DeInit_Web_Server();
//bCan_Active=false;
//CanThread=false;// close can received thread

// end main
void PWM()
{
    x=MyPLD.read();
    Overcurrent= (x & 64)/64;
    Brake= (x & 32)/32;
    FLTA=(x & 16)/16;
    Direction=(x & 8)/8;
    HALLC=(x & 4)/4;
    HALLB=(x & 2)/2;
    HALLA= x & 1;
    if (cChoix =='a') {
        MyPLD.write(0);
        if (val > 0 && val <=255) {

            PWM_VALID.write(1);
            MyPLD.write(val);
        }
    } else {
        if ( Brake == 0 ) {
            PWM_VALID.write(0);
            MyPLD.write(0);
        } else if ( Brake == 1 ) {
            PWM_VALID.write(1);
            valpwm =(int)(255/(GazMax-GazMin))*POIGNEE.read()-(255/(GazMax-GazMin))*GazMin;
            MyPLD.write(valpwm);
        }
    }

}