scooter correction for mbed_os V5.2
Dependencies: bloc_io html_mbed_os
Fork of scooter_mbed by
main.cpp@0:8871b0c9af3b, 2015-08-11 (annotated)
- Committer:
- superphil06
- Date:
- Tue Aug 11 15:01:10 2015 +0000
- Revision:
- 0:8871b0c9af3b
- Child:
- 1:e3cb81b3cbea
version beta1, web, store persistent data to text file
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
superphil06 | 0:8871b0c9af3b | 1 | |
superphil06 | 0:8871b0c9af3b | 2 | |
superphil06 | 0:8871b0c9af3b | 3 | #include "EthernetInterface.h" |
superphil06 | 0:8871b0c9af3b | 4 | #include <stdlib.h> |
superphil06 | 0:8871b0c9af3b | 5 | #include <string.h> |
superphil06 | 0:8871b0c9af3b | 6 | #include "mbed.h" |
superphil06 | 0:8871b0c9af3b | 7 | #include "html.h" // need for html patch working with web server |
superphil06 | 0:8871b0c9af3b | 8 | #include "bloc_io.h" |
superphil06 | 0:8871b0c9af3b | 9 | #define RADIUS 0.2F // wheel size |
superphil06 | 0:8871b0c9af3b | 10 | #define NBPOLES 8 // magnetic pole number |
superphil06 | 0:8871b0c9af3b | 11 | #define DELTA_T 0.1F // speed measurement counting period |
superphil06 | 0:8871b0c9af3b | 12 | |
superphil06 | 0:8871b0c9af3b | 13 | Ticker IT_Timer_Gaz,IT_Speed; |
superphil06 | 0:8871b0c9af3b | 14 | float fSpeed=0; |
superphil06 | 0:8871b0c9af3b | 15 | float fTabVal[10];// need to store analog value |
superphil06 | 0:8871b0c9af3b | 16 | float fGaz,fTemp,fBat,fIdc; |
superphil06 | 0:8871b0c9af3b | 17 | float fGAZ_MAX=70,fGAZ_MIN=15; |
superphil06 | 0:8871b0c9af3b | 18 | unsigned int uiCptImpuls=0; |
superphil06 | 0:8871b0c9af3b | 19 | Bloc_IO MyPLD(p25,p26,p5,p6,p7,p8,p9,p10,p23,p24);// instantiate object needed to communicate with PLD |
superphil06 | 0:8871b0c9af3b | 20 | AnalogIn Gaz(p17),Bat(p18),Temp(p19),Idc(p20);// analog input connected to mbed |
superphil06 | 0:8871b0c9af3b | 21 | DigitalOut ValidPWM (p21,0);// reset valid pmw pin |
superphil06 | 0:8871b0c9af3b | 22 | Serial pc(USBTX, USBRX); // tx, rx |
superphil06 | 0:8871b0c9af3b | 23 | InterruptIn TopHall(p22); |
superphil06 | 0:8871b0c9af3b | 24 | |
superphil06 | 0:8871b0c9af3b | 25 | |
superphil06 | 0:8871b0c9af3b | 26 | float fGaz_Brute; |
superphil06 | 0:8871b0c9af3b | 27 | |
superphil06 | 0:8871b0c9af3b | 28 | /************ persistent file parameters section *****************/ |
superphil06 | 0:8871b0c9af3b | 29 | LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
superphil06 | 0:8871b0c9af3b | 30 | |
superphil06 | 0:8871b0c9af3b | 31 | |
superphil06 | 0:8871b0c9af3b | 32 | |
superphil06 | 0:8871b0c9af3b | 33 | |
superphil06 | 0:8871b0c9af3b | 34 | |
superphil06 | 0:8871b0c9af3b | 35 | |
superphil06 | 0:8871b0c9af3b | 36 | |
superphil06 | 0:8871b0c9af3b | 37 | /********************* web server section **********************************/ |
superphil06 | 0:8871b0c9af3b | 38 | char * ma_chaine3; |
superphil06 | 0:8871b0c9af3b | 39 | var_field_t tab_balise[10]; //une balise est présente dans le squelette |
superphil06 | 0:8871b0c9af3b | 40 | int giCounter=0;// acces counting |
superphil06 | 0:8871b0c9af3b | 41 | |
superphil06 | 0:8871b0c9af3b | 42 | |
superphil06 | 0:8871b0c9af3b | 43 | |
superphil06 | 0:8871b0c9af3b | 44 | |
superphil06 | 0:8871b0c9af3b | 45 | EthernetInterface eth; // ethernet layer management object |
superphil06 | 0:8871b0c9af3b | 46 | TCPSocketServer svr; // |
superphil06 | 0:8871b0c9af3b | 47 | bool serverIsListened = false;// flag to indicate server is listening |
superphil06 | 0:8871b0c9af3b | 48 | |
superphil06 | 0:8871b0c9af3b | 49 | #define PORT 80 // tcp port for socket listeningt |
superphil06 | 0:8871b0c9af3b | 50 | TCPSocketConnection client;// socket to identify client side |
superphil06 | 0:8871b0c9af3b | 51 | bool clientIsConnected = false;// flag that indicate if a web client is avalaible |
superphil06 | 0:8871b0c9af3b | 52 | |
superphil06 | 0:8871b0c9af3b | 53 | //************ prototypes ******************* |
superphil06 | 0:8871b0c9af3b | 54 | int Init_Web_Server(void); |
superphil06 | 0:8871b0c9af3b | 55 | |
superphil06 | 0:8871b0c9af3b | 56 | |
superphil06 | 0:8871b0c9af3b | 57 | |
superphil06 | 0:8871b0c9af3b | 58 | |
superphil06 | 0:8871b0c9af3b | 59 | |
superphil06 | 0:8871b0c9af3b | 60 | |
superphil06 | 0:8871b0c9af3b | 61 | //*********** |
superphil06 | 0:8871b0c9af3b | 62 | //* functions |
superphil06 | 0:8871b0c9af3b | 63 | |
superphil06 | 0:8871b0c9af3b | 64 | |
superphil06 | 0:8871b0c9af3b | 65 | int Read_File_Data(char * pcFileName) |
superphil06 | 0:8871b0c9af3b | 66 | { |
superphil06 | 0:8871b0c9af3b | 67 | float fRayon; |
superphil06 | 0:8871b0c9af3b | 68 | int iPoles,iVit_Max; |
superphil06 | 0:8871b0c9af3b | 69 | FILE *DataFile; |
superphil06 | 0:8871b0c9af3b | 70 | DataFile= fopen(pcFileName, "rt"); |
superphil06 | 0:8871b0c9af3b | 71 | if ( DataFile == NULL) |
superphil06 | 0:8871b0c9af3b | 72 | { printf("\r\nError Open File: %s", pcFileName); //open data file from disk |
superphil06 | 0:8871b0c9af3b | 73 | return(NULL); |
superphil06 | 0:8871b0c9af3b | 74 | } |
superphil06 | 0:8871b0c9af3b | 75 | else |
superphil06 | 0:8871b0c9af3b | 76 | {fscanf (DataFile,"rayon:%f ",&fRayon);// insert ' ' after %format mode to separe numeric data |
superphil06 | 0:8871b0c9af3b | 77 | fscanf (DataFile,"poles:%d ",&iPoles); |
superphil06 | 0:8871b0c9af3b | 78 | fscanf (DataFile,"vit_max:%d ",&iVit_Max); |
superphil06 | 0:8871b0c9af3b | 79 | printf("valeurs lues: rayon:%f poles:%d vit_max:%d \n\r",fRayon,iPoles,iVit_Max); |
superphil06 | 0:8871b0c9af3b | 80 | fclose(DataFile); |
superphil06 | 0:8871b0c9af3b | 81 | return 0; |
superphil06 | 0:8871b0c9af3b | 82 | } |
superphil06 | 0:8871b0c9af3b | 83 | |
superphil06 | 0:8871b0c9af3b | 84 | } |
superphil06 | 0:8871b0c9af3b | 85 | |
superphil06 | 0:8871b0c9af3b | 86 | int Write_File_Data(char * pcFileName) |
superphil06 | 0:8871b0c9af3b | 87 | { |
superphil06 | 0:8871b0c9af3b | 88 | float fRayon=0.45; |
superphil06 | 0:8871b0c9af3b | 89 | int iPoles=10,iVit_Max=15000; |
superphil06 | 0:8871b0c9af3b | 90 | FILE *DataFile; |
superphil06 | 0:8871b0c9af3b | 91 | DataFile= fopen(pcFileName,"wt"); |
superphil06 | 0:8871b0c9af3b | 92 | |
superphil06 | 0:8871b0c9af3b | 93 | |
superphil06 | 0:8871b0c9af3b | 94 | |
superphil06 | 0:8871b0c9af3b | 95 | if ( DataFile == NULL) |
superphil06 | 0:8871b0c9af3b | 96 | { printf("\r\nError Open File: %s", pcFileName); //open data file from disk |
superphil06 | 0:8871b0c9af3b | 97 | return(NULL); |
superphil06 | 0:8871b0c9af3b | 98 | } |
superphil06 | 0:8871b0c9af3b | 99 | else |
superphil06 | 0:8871b0c9af3b | 100 | { fprintf (DataFile, "rayon:%f poles:%d vit_max:%d ",fRayon,iPoles,iVit_Max); |
superphil06 | 0:8871b0c9af3b | 101 | |
superphil06 | 0:8871b0c9af3b | 102 | printf("valeurs ecrites: rayon:%f poles:%d vit_max:%d \n\r",fRayon,iPoles,iVit_Max); |
superphil06 | 0:8871b0c9af3b | 103 | fclose(DataFile); |
superphil06 | 0:8871b0c9af3b | 104 | return 0; |
superphil06 | 0:8871b0c9af3b | 105 | } |
superphil06 | 0:8871b0c9af3b | 106 | |
superphil06 | 0:8871b0c9af3b | 107 | } |
superphil06 | 0:8871b0c9af3b | 108 | |
superphil06 | 0:8871b0c9af3b | 109 | |
superphil06 | 0:8871b0c9af3b | 110 | int Init_Web_Server(void) |
superphil06 | 0:8871b0c9af3b | 111 | { |
superphil06 | 0:8871b0c9af3b | 112 | |
superphil06 | 0:8871b0c9af3b | 113 | //setup ethernet interface |
superphil06 | 0:8871b0c9af3b | 114 | eth.init(); //Use DHCP |
superphil06 | 0:8871b0c9af3b | 115 | eth.connect(); |
superphil06 | 0:8871b0c9af3b | 116 | pc.printf("IP Address is %s\n\r", eth.getIPAddress());// display server ip address |
superphil06 | 0:8871b0c9af3b | 117 | |
superphil06 | 0:8871b0c9af3b | 118 | // Set the callbacks for Listening |
superphil06 | 0:8871b0c9af3b | 119 | //srv.setOnEvent(&onListeningTCPSocketEvent); // create a call back fonction associated with listning socket |
superphil06 | 0:8871b0c9af3b | 120 | //setup srv tcp socket |
superphil06 | 0:8871b0c9af3b | 121 | if(svr.bind(PORT)< 0) { |
superphil06 | 0:8871b0c9af3b | 122 | printf("tcp server bind failed.\n\r"); |
superphil06 | 0:8871b0c9af3b | 123 | return -1; |
superphil06 | 0:8871b0c9af3b | 124 | } else { |
superphil06 | 0:8871b0c9af3b | 125 | printf("tcp server bind successed.\n\r"); |
superphil06 | 0:8871b0c9af3b | 126 | serverIsListened = true; |
superphil06 | 0:8871b0c9af3b | 127 | } |
superphil06 | 0:8871b0c9af3b | 128 | |
superphil06 | 0:8871b0c9af3b | 129 | if(svr.listen(1) < 0) { |
superphil06 | 0:8871b0c9af3b | 130 | printf("tcp server listen failed.\n\r"); |
superphil06 | 0:8871b0c9af3b | 131 | return -1; |
superphil06 | 0:8871b0c9af3b | 132 | } else { |
superphil06 | 0:8871b0c9af3b | 133 | printf("tcp server is listening...\n\r"); |
superphil06 | 0:8871b0c9af3b | 134 | |
superphil06 | 0:8871b0c9af3b | 135 | |
superphil06 | 0:8871b0c9af3b | 136 | } |
superphil06 | 0:8871b0c9af3b | 137 | return 0; |
superphil06 | 0:8871b0c9af3b | 138 | } |
superphil06 | 0:8871b0c9af3b | 139 | |
superphil06 | 0:8871b0c9af3b | 140 | |
superphil06 | 0:8871b0c9af3b | 141 | |
superphil06 | 0:8871b0c9af3b | 142 | |
superphil06 | 0:8871b0c9af3b | 143 | |
superphil06 | 0:8871b0c9af3b | 144 | |
superphil06 | 0:8871b0c9af3b | 145 | |
superphil06 | 0:8871b0c9af3b | 146 | |
superphil06 | 0:8871b0c9af3b | 147 | |
superphil06 | 0:8871b0c9af3b | 148 | |
superphil06 | 0:8871b0c9af3b | 149 | /***************** thread section **************/ |
superphil06 | 0:8871b0c9af3b | 150 | #include "rtos.h" |
superphil06 | 0:8871b0c9af3b | 151 | |
superphil06 | 0:8871b0c9af3b | 152 | void Web_Server_Thread(void const *args) |
superphil06 | 0:8871b0c9af3b | 153 | { |
superphil06 | 0:8871b0c9af3b | 154 | while ( serverIsListened) { |
superphil06 | 0:8871b0c9af3b | 155 | //blocking mode(never timeout) |
superphil06 | 0:8871b0c9af3b | 156 | if(svr.accept(client)<0) { |
superphil06 | 0:8871b0c9af3b | 157 | pc.printf("failed to accept connection.\n\r"); |
superphil06 | 0:8871b0c9af3b | 158 | } else { |
superphil06 | 0:8871b0c9af3b | 159 | pc.printf("connection success!\n\rIP: %s\n\r",client.get_address()); |
superphil06 | 0:8871b0c9af3b | 160 | clientIsConnected = true; |
superphil06 | 0:8871b0c9af3b | 161 | // Setup the new socket events |
superphil06 | 0:8871b0c9af3b | 162 | // client.setOnEvent(&onConnectedTCPSocketEvent); |
superphil06 | 0:8871b0c9af3b | 163 | // We can find out from where the connection is coming by looking at the |
superphil06 | 0:8871b0c9af3b | 164 | // Host parameter of the accept() method |
superphil06 | 0:8871b0c9af3b | 165 | |
superphil06 | 0:8871b0c9af3b | 166 | while(clientIsConnected) { |
superphil06 | 0:8871b0c9af3b | 167 | char buffer[1024] = {}; |
superphil06 | 0:8871b0c9af3b | 168 | char ma_chaine4[20]={};// needed to form html response |
superphil06 | 0:8871b0c9af3b | 169 | switch(client.receive(buffer, 1023)) { |
superphil06 | 0:8871b0c9af3b | 170 | case 0: |
superphil06 | 0:8871b0c9af3b | 171 | pc.printf("recieved buffer is empty.\n\r"); |
superphil06 | 0:8871b0c9af3b | 172 | clientIsConnected = false; |
superphil06 | 0:8871b0c9af3b | 173 | break; |
superphil06 | 0:8871b0c9af3b | 174 | case -1: |
superphil06 | 0:8871b0c9af3b | 175 | pc.printf("failed to read data from client.\n\r"); |
superphil06 | 0:8871b0c9af3b | 176 | clientIsConnected = false; |
superphil06 | 0:8871b0c9af3b | 177 | break; |
superphil06 | 0:8871b0c9af3b | 178 | default: |
superphil06 | 0:8871b0c9af3b | 179 | pc.printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); |
superphil06 | 0:8871b0c9af3b | 180 | if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) { |
superphil06 | 0:8871b0c9af3b | 181 | pc.printf("GET request incomming.\n\r"); |
superphil06 | 0:8871b0c9af3b | 182 | //setup http response header & data |
superphil06 | 0:8871b0c9af3b | 183 | /************* patch all dynamic data in html response page ************/ |
superphil06 | 0:8871b0c9af3b | 184 | giCounter++; |
superphil06 | 0:8871b0c9af3b | 185 | sprintf (ma_chaine4,"%d",(int)fSpeed);// convert speed as ascii string |
superphil06 | 0:8871b0c9af3b | 186 | Html_Patch (tab_balise,0,ma_chaine4);// patch first label with dyn.string |
superphil06 | 0:8871b0c9af3b | 187 | |
superphil06 | 0:8871b0c9af3b | 188 | sprintf (ma_chaine4,"%d",(int)(fGaz_Brute*100)/255);// convert count as ascii string |
superphil06 | 0:8871b0c9af3b | 189 | Html_Patch (tab_balise,1,ma_chaine4);// patch first label with dyn.string |
superphil06 | 0:8871b0c9af3b | 190 | |
superphil06 | 0:8871b0c9af3b | 191 | sprintf (ma_chaine4,"%d",(int)fBat);// convert count as ascii string |
superphil06 | 0:8871b0c9af3b | 192 | Html_Patch (tab_balise,2,ma_chaine4);// patch first label with dyn.string |
superphil06 | 0:8871b0c9af3b | 193 | |
superphil06 | 0:8871b0c9af3b | 194 | sprintf (ma_chaine4,"%d",(int)fIdc);// convert count as ascii string |
superphil06 | 0:8871b0c9af3b | 195 | Html_Patch (tab_balise,3,ma_chaine4);// patch first label with dyn.string |
superphil06 | 0:8871b0c9af3b | 196 | |
superphil06 | 0:8871b0c9af3b | 197 | sprintf (ma_chaine4,"%d",(int)fTemp);// convert count as ascii string |
superphil06 | 0:8871b0c9af3b | 198 | Html_Patch (tab_balise,4,ma_chaine4);// patch first label with dyn.string |
superphil06 | 0:8871b0c9af3b | 199 | |
superphil06 | 0:8871b0c9af3b | 200 | char echoHeader[256] = {}; |
superphil06 | 0:8871b0c9af3b | 201 | sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r",strlen(ma_chaine3)); |
superphil06 | 0:8871b0c9af3b | 202 | client.send(echoHeader,strlen(echoHeader)); |
superphil06 | 0:8871b0c9af3b | 203 | pc.printf("%s",echoHeader);// debut http header sent |
superphil06 | 0:8871b0c9af3b | 204 | |
superphil06 | 0:8871b0c9af3b | 205 | //client.send(buffer,strlen(buffer));// realise echo request to client |
superphil06 | 0:8871b0c9af3b | 206 | |
superphil06 | 0:8871b0c9af3b | 207 | |
superphil06 | 0:8871b0c9af3b | 208 | client.send(ma_chaine3,strlen(ma_chaine3));// realise echo request to client |
superphil06 | 0:8871b0c9af3b | 209 | |
superphil06 | 0:8871b0c9af3b | 210 | pc.printf("%s",ma_chaine3); |
superphil06 | 0:8871b0c9af3b | 211 | |
superphil06 | 0:8871b0c9af3b | 212 | |
superphil06 | 0:8871b0c9af3b | 213 | clientIsConnected = false;// close connection with this client at end of http response |
superphil06 | 0:8871b0c9af3b | 214 | pc.printf("web page sent.\n\r"); |
superphil06 | 0:8871b0c9af3b | 215 | } |
superphil06 | 0:8871b0c9af3b | 216 | break; |
superphil06 | 0:8871b0c9af3b | 217 | }// end case |
superphil06 | 0:8871b0c9af3b | 218 | //Thread::wait(10);// sleep for 10 ms |
superphil06 | 0:8871b0c9af3b | 219 | }// end while Client is Connected |
superphil06 | 0:8871b0c9af3b | 220 | |
superphil06 | 0:8871b0c9af3b | 221 | pc.printf("close connection.\n\rtcp server is listening...\n\r"); |
superphil06 | 0:8871b0c9af3b | 222 | client.close(); |
superphil06 | 0:8871b0c9af3b | 223 | |
superphil06 | 0:8871b0c9af3b | 224 | |
superphil06 | 0:8871b0c9af3b | 225 | } |
superphil06 | 0:8871b0c9af3b | 226 | Thread::wait(10);// sleep for 10 ms |
superphil06 | 0:8871b0c9af3b | 227 | } // end while ServerIs listening |
superphil06 | 0:8871b0c9af3b | 228 | } |
superphil06 | 0:8871b0c9af3b | 229 | |
superphil06 | 0:8871b0c9af3b | 230 | |
superphil06 | 0:8871b0c9af3b | 231 | |
superphil06 | 0:8871b0c9af3b | 232 | |
superphil06 | 0:8871b0c9af3b | 233 | |
superphil06 | 0:8871b0c9af3b | 234 | |
superphil06 | 0:8871b0c9af3b | 235 | |
superphil06 | 0:8871b0c9af3b | 236 | |
superphil06 | 0:8871b0c9af3b | 237 | |
superphil06 | 0:8871b0c9af3b | 238 | // needed to record min_gaz and max_gaz value to persistent text file |
superphil06 | 0:8871b0c9af3b | 239 | void Calibrate_Gaz(void) |
superphil06 | 0:8871b0c9af3b | 240 | { char cJunk; |
superphil06 | 0:8871b0c9af3b | 241 | |
superphil06 | 0:8871b0c9af3b | 242 | fGaz=Gaz.read()*100; |
superphil06 | 0:8871b0c9af3b | 243 | fTemp=Temp.read()*100; |
superphil06 | 0:8871b0c9af3b | 244 | fBat=Bat.read()*100; |
superphil06 | 0:8871b0c9af3b | 245 | fIdc=Idc.read()*100; |
superphil06 | 0:8871b0c9af3b | 246 | |
superphil06 | 0:8871b0c9af3b | 247 | pc.printf("mettre la poignee au minimum et appuyer sur une touche \n"); |
superphil06 | 0:8871b0c9af3b | 248 | pc.scanf(" %c",&cJunk); |
superphil06 | 0:8871b0c9af3b | 249 | fTabVal[0]=Gaz.read()*100;// read min gaz value) |
superphil06 | 0:8871b0c9af3b | 250 | fGAZ_MIN=fTabVal[0];// update local GAZ_MIN constante |
superphil06 | 0:8871b0c9af3b | 251 | pc.printf("mettre la poignee au maximum et appuyer sur une touche \n"); |
superphil06 | 0:8871b0c9af3b | 252 | pc.scanf(" %c",&cJunk); |
superphil06 | 0:8871b0c9af3b | 253 | fTabVal[1]=Gaz.read()*100;// read max gaz value |
superphil06 | 0:8871b0c9af3b | 254 | fGAZ_MAX=fTabVal[1];// update local GAZ_MIN constante |
superphil06 | 0:8871b0c9af3b | 255 | pc.printf("calibration terminee: gaz_min=%f gaz_max=%f \n",fTabVal[0],fTabVal[1]); |
superphil06 | 0:8871b0c9af3b | 256 | |
superphil06 | 0:8871b0c9af3b | 257 | } |
superphil06 | 0:8871b0c9af3b | 258 | |
superphil06 | 0:8871b0c9af3b | 259 | // top hall counting interre |
superphil06 | 0:8871b0c9af3b | 260 | void SpeedCount(void) |
superphil06 | 0:8871b0c9af3b | 261 | {uiCptImpuls++;// top hall encoured increment speed counter |
superphil06 | 0:8871b0c9af3b | 262 | } |
superphil06 | 0:8871b0c9af3b | 263 | |
superphil06 | 0:8871b0c9af3b | 264 | |
superphil06 | 0:8871b0c9af3b | 265 | // timer interrupt for speed measurement each 100ms |
superphil06 | 0:8871b0c9af3b | 266 | void MyITSpeed(void) |
superphil06 | 0:8871b0c9af3b | 267 | {unsigned int uiCptSpeed; |
superphil06 | 0:8871b0c9af3b | 268 | |
superphil06 | 0:8871b0c9af3b | 269 | uiCptSpeed=uiCptImpuls;// read actual count |
superphil06 | 0:8871b0c9af3b | 270 | uiCptImpuls=0;// reset counter for next counting period |
superphil06 | 0:8871b0c9af3b | 271 | fSpeed=2*(float)3.14159*RADIUS*(float)uiCptSpeed*3600/(6*NBPOLES*DELTA_T); |
superphil06 | 0:8871b0c9af3b | 272 | } |
superphil06 | 0:8871b0c9af3b | 273 | |
superphil06 | 0:8871b0c9af3b | 274 | |
superphil06 | 0:8871b0c9af3b | 275 | |
superphil06 | 0:8871b0c9af3b | 276 | |
superphil06 | 0:8871b0c9af3b | 277 | |
superphil06 | 0:8871b0c9af3b | 278 | //Timer Interrupt for gaz ref management each 10ms |
superphil06 | 0:8871b0c9af3b | 279 | void MyITGaz (void) |
superphil06 | 0:8871b0c9af3b | 280 | {unsigned char byRefPWM=0; |
superphil06 | 0:8871b0c9af3b | 281 | |
superphil06 | 0:8871b0c9af3b | 282 | fGaz=Gaz.read()*100;// read actual gaz |
superphil06 | 0:8871b0c9af3b | 283 | fBat=Bat.read()*100;// read actual Bat voltage |
superphil06 | 0:8871b0c9af3b | 284 | fIdc=Idc.read()*100;// read actual Idc current |
superphil06 | 0:8871b0c9af3b | 285 | fTemp=Temp.read()*100;// readf actual Temp |
superphil06 | 0:8871b0c9af3b | 286 | fGaz_Brute= (float)255/(fGAZ_MAX-fGAZ_MIN)* (fGaz-fGAZ_MIN); |
superphil06 | 0:8871b0c9af3b | 287 | if (fGaz_Brute>255) fGaz_Brute=255; |
superphil06 | 0:8871b0c9af3b | 288 | if (fGaz_Brute<0) fGaz_Brute=0; |
superphil06 | 0:8871b0c9af3b | 289 | byRefPWM=(unsigned char) fGaz_Brute; |
superphil06 | 0:8871b0c9af3b | 290 | MyPLD.write(byRefPWM);// update pwm ref on pld |
superphil06 | 0:8871b0c9af3b | 291 | } |
superphil06 | 0:8871b0c9af3b | 292 | |
superphil06 | 0:8871b0c9af3b | 293 | |
superphil06 | 0:8871b0c9af3b | 294 | int main() { |
superphil06 | 0:8871b0c9af3b | 295 | |
superphil06 | 0:8871b0c9af3b | 296 | char cChoix=0; |
superphil06 | 0:8871b0c9af3b | 297 | int iIndex,iVal,iRefPWM; |
superphil06 | 0:8871b0c9af3b | 298 | |
superphil06 | 0:8871b0c9af3b | 299 | |
superphil06 | 0:8871b0c9af3b | 300 | Read_File_Data("/local/persist.txt"); // read persistent data from text file |
superphil06 | 0:8871b0c9af3b | 301 | Write_File_Data("/local/persist.txt"); // write persistent data to text file |
superphil06 | 0:8871b0c9af3b | 302 | //***************************************** web section ******************************************** |
superphil06 | 0:8871b0c9af3b | 303 | |
superphil06 | 0:8871b0c9af3b | 304 | |
superphil06 | 0:8871b0c9af3b | 305 | Init_Web_Server(); // create and initialize tcp server socket |
superphil06 | 0:8871b0c9af3b | 306 | Thread WebThread(Web_Server_Thread);// create and launch web server thread |
superphil06 | 0:8871b0c9af3b | 307 | //******************************************* end web section ************************************* |
superphil06 | 0:8871b0c9af3b | 308 | |
superphil06 | 0:8871b0c9af3b | 309 | |
superphil06 | 0:8871b0c9af3b | 310 | ma_chaine3 = Gen_HtmlCode_From_File("/local/pagecgi2.htm",tab_balise,5);// read and localise ^VARDEF[X] tag |
superphil06 | 0:8871b0c9af3b | 311 | |
superphil06 | 0:8871b0c9af3b | 312 | IT_Timer_Gaz.attach_us(&MyITGaz, 10000);// install timer it each 10ms |
superphil06 | 0:8871b0c9af3b | 313 | IT_Speed.attach_us(&MyITSpeed, 100000);// install timer it each 10ms |
superphil06 | 0:8871b0c9af3b | 314 | TopHall.rise(&SpeedCount); |
superphil06 | 0:8871b0c9af3b | 315 | |
superphil06 | 0:8871b0c9af3b | 316 | pc.printf(" programme scooter mbed \n"); |
superphil06 | 0:8871b0c9af3b | 317 | MyPLD.write(0);// set initial PWM ref=0 |
superphil06 | 0:8871b0c9af3b | 318 | ValidPWM.write(1);// enable pwm generator |
superphil06 | 0:8871b0c9af3b | 319 | |
superphil06 | 0:8871b0c9af3b | 320 | while(cChoix!='q' and cChoix!='Q') |
superphil06 | 0:8871b0c9af3b | 321 | {pc.printf(" veuillez saisir un choix parmi la liste proposee: \n"); |
superphil06 | 0:8871b0c9af3b | 322 | pc.printf(" a:saisie consigne pwm \n"); |
superphil06 | 0:8871b0c9af3b | 323 | pc.printf(" b:lecture registre PLD \n"); |
superphil06 | 0:8871b0c9af3b | 324 | pc.printf(" c:lecture ADC 4 voies \n"); |
superphil06 | 0:8871b0c9af3b | 325 | pc.printf(" d:calibration poignee Gaz \n"); |
superphil06 | 0:8871b0c9af3b | 326 | pc.printf(" e:affichage vitesse \n"); |
superphil06 | 0:8871b0c9af3b | 327 | pc.printf(" q:quitter \n"); |
superphil06 | 0:8871b0c9af3b | 328 | |
superphil06 | 0:8871b0c9af3b | 329 | while (pc.readable()==0)// determine if char available) |
superphil06 | 0:8871b0c9af3b | 330 | {Thread::wait(10);} // wait 10 until char available on serial input |
superphil06 | 0:8871b0c9af3b | 331 | pc.scanf(" %c",&cChoix); |
superphil06 | 0:8871b0c9af3b | 332 | switch (cChoix){ |
superphil06 | 0:8871b0c9af3b | 333 | case 'a': pc.printf(" veuillez saisir une consigne pwm entre 0 et 255 \n"); |
superphil06 | 0:8871b0c9af3b | 334 | pc.scanf("%d",&iRefPWM); |
superphil06 | 0:8871b0c9af3b | 335 | if (iRefPWM>255) iRefPWM=255; |
superphil06 | 0:8871b0c9af3b | 336 | if (iRefPWM<0) iRefPWM=0; |
superphil06 | 0:8871b0c9af3b | 337 | MyPLD.write((unsigned char)iRefPWM);// update pwm ref on pld |
superphil06 | 0:8871b0c9af3b | 338 | break; |
superphil06 | 0:8871b0c9af3b | 339 | case 'b': iVal=MyPLD.read();// read PLd internal register |
superphil06 | 0:8871b0c9af3b | 340 | pc.printf("valeur lue dans le registre PLD: %02x \n",iVal); |
superphil06 | 0:8871b0c9af3b | 341 | pc.printf("secteur hall: %01d \n",iVal&0x07); |
superphil06 | 0:8871b0c9af3b | 342 | pc.printf("direction:%01d \n",(iVal&0x08)>>3); |
superphil06 | 0:8871b0c9af3b | 343 | pc.printf("flta:%01d \n",(iVal&0x10)>>4); |
superphil06 | 0:8871b0c9af3b | 344 | pc.printf("brake:%01d \n",(iVal&0x20)>>5); |
superphil06 | 0:8871b0c9af3b | 345 | pc.printf("overCurrent:%01d \n",(iVal&0x40)>>6); |
superphil06 | 0:8871b0c9af3b | 346 | break; |
superphil06 | 0:8871b0c9af3b | 347 | |
superphil06 | 0:8871b0c9af3b | 348 | case 'c': // lecture ADC 4 voies |
superphil06 | 0:8871b0c9af3b | 349 | fGaz=Gaz.read()*100; |
superphil06 | 0:8871b0c9af3b | 350 | fTemp=Temp.read()*100; |
superphil06 | 0:8871b0c9af3b | 351 | fBat=Bat.read()*100; |
superphil06 | 0:8871b0c9af3b | 352 | fIdc=Idc.read()*100; |
superphil06 | 0:8871b0c9af3b | 353 | pc.printf("valeur gaz:%3f Temperature:%3f Vbat:%3f Idc:%3f \n",fGaz,fTemp,fBat,fIdc); |
superphil06 | 0:8871b0c9af3b | 354 | break; |
superphil06 | 0:8871b0c9af3b | 355 | |
superphil06 | 0:8871b0c9af3b | 356 | case 'd': // calibration poignée gaz |
superphil06 | 0:8871b0c9af3b | 357 | Calibrate_Gaz(); |
superphil06 | 0:8871b0c9af3b | 358 | break; |
superphil06 | 0:8871b0c9af3b | 359 | case 'e': // display actual speed m/h |
superphil06 | 0:8871b0c9af3b | 360 | pc.printf("vitesse=%f m/h \n",fSpeed); |
superphil06 | 0:8871b0c9af3b | 361 | break; |
superphil06 | 0:8871b0c9af3b | 362 | |
superphil06 | 0:8871b0c9af3b | 363 | case 'q': break; |
superphil06 | 0:8871b0c9af3b | 364 | } |
superphil06 | 0:8871b0c9af3b | 365 | |
superphil06 | 0:8871b0c9af3b | 366 | /*for (iIndex=0;iIndex<=255;iIndex++) |
superphil06 | 0:8871b0c9af3b | 367 | {MyPLD.write((unsigned char) iIndex); |
superphil06 | 0:8871b0c9af3b | 368 | // iVal=MyPLD.read(); |
superphil06 | 0:8871b0c9af3b | 369 | |
superphil06 | 0:8871b0c9af3b | 370 | wait_ms(0.02); |
superphil06 | 0:8871b0c9af3b | 371 | }*/ |
superphil06 | 0:8871b0c9af3b | 372 | } |
superphil06 | 0:8871b0c9af3b | 373 | IT_Timer_Gaz.detach();// uninstall timer interrupt |
superphil06 | 0:8871b0c9af3b | 374 | IT_Speed.detach();// uninstall timer interrupt |
superphil06 | 0:8871b0c9af3b | 375 | MyPLD.write(0);// stop motor before exiting |
superphil06 | 0:8871b0c9af3b | 376 | ValidPWM.write(0);// disable pwm generator |
superphil06 | 0:8871b0c9af3b | 377 | pc.printf(" fin programme scooter mbed \n"); |
superphil06 | 0:8871b0c9af3b | 378 | } |