scooter correction for mbed_os V5.2
Dependencies: bloc_io html_mbed_os
Fork of scooter_mbed by
Diff: main.cpp
- Revision:
- 0:8871b0c9af3b
- Child:
- 1:e3cb81b3cbea
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Aug 11 15:01:10 2015 +0000 @@ -0,0 +1,378 @@ + + +#include "EthernetInterface.h" +#include <stdlib.h> +#include <string.h> +#include "mbed.h" +#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 + +Ticker IT_Timer_Gaz,IT_Speed; + float fSpeed=0; +float fTabVal[10];// need to store analog value + float fGaz,fTemp,fBat,fIdc; + float fGAZ_MAX=70,fGAZ_MIN=15; + unsigned int uiCptImpuls=0; +Bloc_IO MyPLD(p25,p26,p5,p6,p7,p8,p9,p10,p23,p24);// instantiate object needed to communicate with PLD +AnalogIn Gaz(p17),Bat(p18),Temp(p19),Idc(p20);// analog input connected to mbed +DigitalOut ValidPWM (p21,0);// reset valid pmw pin +Serial pc(USBTX, USBRX); // tx, rx +InterruptIn TopHall(p22); + + + float fGaz_Brute; + +/************ persistent file parameters section *****************/ +LocalFileSystem local("local"); // Create the local filesystem under the name "local" + + + + + + + +/********************* web server section **********************************/ +char * ma_chaine3; +var_field_t tab_balise[10]; //une balise est présente dans le squelette +int giCounter=0;// acces counting + + + + +EthernetInterface eth; // ethernet layer management object +TCPSocketServer svr; // +bool serverIsListened = false;// flag to indicate server is listening + +#define PORT 80 // tcp port for socket listeningt +TCPSocketConnection client;// socket to identify client side +bool clientIsConnected = false;// flag that indicate if a web client is avalaible + +//************ prototypes ******************* +int Init_Web_Server(void); + + + + + + + //*********** +//* functions + + +int Read_File_Data(char * pcFileName) +{ +float fRayon; +int iPoles,iVit_Max; +FILE *DataFile; +DataFile= fopen(pcFileName, "rt"); +if ( DataFile == NULL) + { printf("\r\nError Open File: %s", pcFileName); //open data file from disk + return(NULL); + } + else + {fscanf (DataFile,"rayon:%f ",&fRayon);// insert ' ' after %format mode to separe numeric data + fscanf (DataFile,"poles:%d ",&iPoles); + fscanf (DataFile,"vit_max:%d ",&iVit_Max); + printf("valeurs lues: rayon:%f poles:%d vit_max:%d \n\r",fRayon,iPoles,iVit_Max); + fclose(DataFile); + return 0; + } + +} + +int Write_File_Data(char * pcFileName) +{ +float fRayon=0.45; +int iPoles=10,iVit_Max=15000; +FILE *DataFile; +DataFile= fopen(pcFileName,"wt"); + + + +if ( DataFile == NULL) + { printf("\r\nError Open File: %s", pcFileName); //open data file from disk + return(NULL); + } + else + { fprintf (DataFile, "rayon:%f poles:%d vit_max:%d ",fRayon,iPoles,iVit_Max); + + printf("valeurs ecrites: rayon:%f poles:%d vit_max:%d \n\r",fRayon,iPoles,iVit_Max); + fclose(DataFile); + return 0; + } + +} + + +int Init_Web_Server(void) + { + + //setup ethernet interface + eth.init(); //Use DHCP + eth.connect(); + pc.printf("IP Address is %s\n\r", eth.getIPAddress());// display server ip address + + // Set the callbacks for Listening + //srv.setOnEvent(&onListeningTCPSocketEvent); // create a call back fonction associated with listning socket + //setup srv tcp socket + if(svr.bind(PORT)< 0) { + printf("tcp server bind failed.\n\r"); + return -1; + } else { + printf("tcp server bind successed.\n\r"); + serverIsListened = true; + } + + if(svr.listen(1) < 0) { + printf("tcp server listen failed.\n\r"); + return -1; + } else { + printf("tcp server is listening...\n\r"); + + + } + return 0; + } + + + + + + + + + + +/***************** thread section **************/ +#include "rtos.h" + +void Web_Server_Thread(void const *args) +{ + while ( serverIsListened) { + //blocking mode(never timeout) + if(svr.accept(client)<0) { + pc.printf("failed to accept connection.\n\r"); + } else { + pc.printf("connection success!\n\rIP: %s\n\r",client.get_address()); + clientIsConnected = true; + // Setup the new socket events + // client.setOnEvent(&onConnectedTCPSocketEvent); + // We can find out from where the connection is coming by looking at the + // Host parameter of the accept() method + + while(clientIsConnected) { + char buffer[1024] = {}; + char ma_chaine4[20]={};// needed to form html response + switch(client.receive(buffer, 1023)) { + case 0: + pc.printf("recieved buffer is empty.\n\r"); + clientIsConnected = false; + break; + case -1: + pc.printf("failed to read data from client.\n\r"); + clientIsConnected = false; + break; + default: + pc.printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); + if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) { + pc.printf("GET request incomming.\n\r"); + //setup http response header & data + /************* patch all dynamic data in html response page ************/ + giCounter++; + sprintf (ma_chaine4,"%d",(int)fSpeed);// convert speed as ascii string + Html_Patch (tab_balise,0,ma_chaine4);// patch first label with dyn.string + + sprintf (ma_chaine4,"%d",(int)(fGaz_Brute*100)/255);// convert count as ascii string + Html_Patch (tab_balise,1,ma_chaine4);// patch first label with dyn.string + + sprintf (ma_chaine4,"%d",(int)fBat);// convert count as ascii string + Html_Patch (tab_balise,2,ma_chaine4);// patch first label with dyn.string + + sprintf (ma_chaine4,"%d",(int)fIdc);// convert count as ascii string + Html_Patch (tab_balise,3,ma_chaine4);// patch first label with dyn.string + + sprintf (ma_chaine4,"%d",(int)fTemp);// convert count as ascii string + Html_Patch (tab_balise,4,ma_chaine4);// patch first label with dyn.string + + char echoHeader[256] = {}; + sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r",strlen(ma_chaine3)); + client.send(echoHeader,strlen(echoHeader)); + pc.printf("%s",echoHeader);// debut http header sent + + //client.send(buffer,strlen(buffer));// realise echo request to client + + + client.send(ma_chaine3,strlen(ma_chaine3));// realise echo request to client + + pc.printf("%s",ma_chaine3); + + + clientIsConnected = false;// close connection with this client at end of http response + pc.printf("web page sent.\n\r"); + } + break; + }// end case + //Thread::wait(10);// sleep for 10 ms + }// end while Client is Connected + + pc.printf("close connection.\n\rtcp server is listening...\n\r"); + client.close(); + + + } + Thread::wait(10);// sleep for 10 ms + } // end while ServerIs listening +} + + + + + + + + + +// needed to record min_gaz and max_gaz value to persistent text file +void Calibrate_Gaz(void) +{ char cJunk; + + fGaz=Gaz.read()*100; + fTemp=Temp.read()*100; + fBat=Bat.read()*100; + fIdc=Idc.read()*100; + + pc.printf("mettre la poignee au minimum et appuyer sur une touche \n"); + pc.scanf(" %c",&cJunk); + fTabVal[0]=Gaz.read()*100;// read min gaz value) + fGAZ_MIN=fTabVal[0];// update local GAZ_MIN constante + pc.printf("mettre la poignee au maximum et appuyer sur une touche \n"); + pc.scanf(" %c",&cJunk); + fTabVal[1]=Gaz.read()*100;// read max gaz value + fGAZ_MAX=fTabVal[1];// update local GAZ_MIN constante + pc.printf("calibration terminee: gaz_min=%f gaz_max=%f \n",fTabVal[0],fTabVal[1]); + +} + +// top hall counting interre +void SpeedCount(void) +{uiCptImpuls++;// top hall encoured increment speed counter + } + + +// timer interrupt for speed measurement each 100ms +void MyITSpeed(void) +{unsigned int uiCptSpeed; + +uiCptSpeed=uiCptImpuls;// read actual count +uiCptImpuls=0;// reset counter for next counting period +fSpeed=2*(float)3.14159*RADIUS*(float)uiCptSpeed*3600/(6*NBPOLES*DELTA_T); +} + + + + + +//Timer Interrupt for gaz ref management each 10ms +void MyITGaz (void) +{unsigned char byRefPWM=0; + + fGaz=Gaz.read()*100;// read actual gaz + fBat=Bat.read()*100;// read actual Bat voltage + fIdc=Idc.read()*100;// read actual Idc current + fTemp=Temp.read()*100;// readf actual Temp + fGaz_Brute= (float)255/(fGAZ_MAX-fGAZ_MIN)* (fGaz-fGAZ_MIN); + if (fGaz_Brute>255) fGaz_Brute=255; + if (fGaz_Brute<0) fGaz_Brute=0; + byRefPWM=(unsigned char) fGaz_Brute; + MyPLD.write(byRefPWM);// update pwm ref on pld +} + + +int main() { + + char cChoix=0; +int iIndex,iVal,iRefPWM; + + +Read_File_Data("/local/persist.txt"); // read persistent data from text file + Write_File_Data("/local/persist.txt"); // write persistent data to text file +//***************************************** web section ******************************************** + + +Init_Web_Server(); // create and initialize tcp server socket +Thread WebThread(Web_Server_Thread);// create and launch web server thread +//******************************************* end web section ************************************* + + + ma_chaine3 = Gen_HtmlCode_From_File("/local/pagecgi2.htm",tab_balise,5);// read and localise ^VARDEF[X] tag + +IT_Timer_Gaz.attach_us(&MyITGaz, 10000);// install timer it each 10ms +IT_Speed.attach_us(&MyITSpeed, 100000);// install timer it each 10ms +TopHall.rise(&SpeedCount); + +pc.printf(" programme scooter mbed \n"); +MyPLD.write(0);// set initial PWM ref=0 +ValidPWM.write(1);// enable pwm generator + +while(cChoix!='q' and cChoix!='Q') +{pc.printf(" veuillez saisir un choix parmi la liste proposee: \n"); + pc.printf(" a:saisie consigne pwm \n"); +pc.printf(" b:lecture registre PLD \n"); +pc.printf(" c:lecture ADC 4 voies \n"); +pc.printf(" d:calibration poignee Gaz \n"); +pc.printf(" e:affichage vitesse \n"); + pc.printf(" q:quitter \n"); + + while (pc.readable()==0)// determine if char available) + {Thread::wait(10);} // wait 10 until char available on serial input + pc.scanf(" %c",&cChoix); + switch (cChoix){ + case 'a': pc.printf(" veuillez saisir une consigne pwm entre 0 et 255 \n"); + pc.scanf("%d",&iRefPWM); + if (iRefPWM>255) iRefPWM=255; + if (iRefPWM<0) iRefPWM=0; + MyPLD.write((unsigned char)iRefPWM);// update pwm ref on pld + break; + case 'b': iVal=MyPLD.read();// read PLd internal register + pc.printf("valeur lue dans le registre PLD: %02x \n",iVal); + pc.printf("secteur hall: %01d \n",iVal&0x07); + pc.printf("direction:%01d \n",(iVal&0x08)>>3); + pc.printf("flta:%01d \n",(iVal&0x10)>>4); + pc.printf("brake:%01d \n",(iVal&0x20)>>5); + pc.printf("overCurrent:%01d \n",(iVal&0x40)>>6); + break; + + case 'c': // lecture ADC 4 voies + fGaz=Gaz.read()*100; + fTemp=Temp.read()*100; + fBat=Bat.read()*100; + fIdc=Idc.read()*100; + pc.printf("valeur gaz:%3f Temperature:%3f Vbat:%3f Idc:%3f \n",fGaz,fTemp,fBat,fIdc); + break; + + case 'd': // calibration poignée gaz + Calibrate_Gaz(); + break; + case 'e': // display actual speed m/h + pc.printf("vitesse=%f m/h \n",fSpeed); + break; + + case 'q': break; + } + + /*for (iIndex=0;iIndex<=255;iIndex++) + {MyPLD.write((unsigned char) iIndex); + // iVal=MyPLD.read(); + + wait_ms(0.02); + }*/ + } + IT_Timer_Gaz.detach();// uninstall timer interrupt + IT_Speed.detach();// uninstall timer interrupt + MyPLD.write(0);// stop motor before exiting + ValidPWM.write(0);// disable pwm generator + pc.printf(" fin programme scooter mbed \n"); +}