Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Controle_ventilo_ethernet_v1_2_4 by
main.cpp
- Committer:
- Brutus
- Date:
- 2017-06-09
- Revision:
- 10:617194438c6b
- Parent:
- 9:499df2f06bda
- Child:
- 11:aa1cb8df15dc
File content as of revision 10:617194438c6b:
#include "mbed.h" #include "EthernetInterface.h" #include "SocketAddress.h" #include "UDPSocket.h" #include <string.h> const char* ECHO_SERVER_ADDRESS = "131.215.118.148"; // addresse du PC home "192.168.1.2" sergio "169.254.61.104" or "129.194.185.33" or "192.168.137.1" liotard "10.136.134.73" const int ECHO_SERVER_PORT = 1039; Timer t; DigitalOut PSU_DEAD(D8); // Kill PSU DigitalOut PSU_OFF(D9); // Power On PSU volatile long int count[18] = {0}; int tours = 10; double rpm[18] = {0.0}; bool readRpm = 0; bool readAllRpm = 0; // Front fans //PwmOut frontFans[9]={PB_11,PE_14,PE_9,PB_3,PB_15,PB_6,PB_13,PE_5,PC_8}; // PE_11 also possible for front fan n°5 // Rear fans //PwmOut rearFans[9]={PB_10,PE_12,PE_10,PE_11,PC_6,PD_13,PC_7,PE_6,PC_9}; // PE_10 also possible for rear fans n°5 PwmOut frontFans[9]={PC_6,PB_5,PB_8,PC_8,PE_5,PB_1,PE_9,PB_10,PE_12}; PwmOut rearFans[9]={PB_15,PB_3,PB_9,PC_9,PE_6,PD_13,PB_11,PE_10,PE_14}; PinName counterFront[9]={PC_10,PC_12,PD_7,PD_0,PE_3,PF_0,PE_4,PD_6,PB_2}; PinName counterRear[9]={PG_2,PG_3,PF_10,PF_9,PF_1,PG_0,PD_2,PD_11,PF_4}; DigitalOut greenLed(LED1); DigitalOut bleuLed(LED2); DigitalOut redLed(LED3); // Receive string char FR_str[2] = {0}; char fan_str[10] = {0}; char pwm_str[4] = {0}; int FR = 0; int fan[10] = {0}; float pwm_f = 0.0; int FR_len = 0; int fan_len = 0; int pwm_len = 0; void splitString(char stringToSplit[]); int transString(char stringToTrans[]); void countFunction(); void countFunction2(); void countFunction3(); void countFunction4(); void countFunction5(); void countFunction6(); void countFunction7(); void countFunction8(); void countFunction9(); void countFunction10(); void countFunction11(); void countFunction12(); void countFunction13(); void countFunction14(); void countFunction15(); void countFunction16(); void countFunction17(); void countFunction18(); int main(int argc, char *argv[]) { PSU_OFF = true; //InterruptIn teesst[2](PD_13, PD_15); for(int i = 0; i < 9; i++){ frontFans[i].period_ms(10); rearFans[i].period_ms(10); frontFans[i].write(0.0); rearFans[i].write(0.0); } EthernetInterface eth; // init interface ethernet de la carte UDPSocket sock; // init le socket en UDP de la carte SocketAddress echo_server; eth.connect(); sock.open(ð); const char *local_ip = eth.get_ip_address(); const char *local_mac = eth.get_mac_address(); printf("\nClient IP Address is %s \n", local_ip); printf("Port num is %d \n", ECHO_SERVER_PORT); echo_server.set_ip_address(ECHO_SERVER_ADDRESS); echo_server.set_port(ECHO_SERVER_PORT); greenLed = 1; wait(1); greenLed = 0; wait(1); bool ack = 0; while(ack == 0){ char out_buffer[18]; snprintf(out_buffer, sizeof(out_buffer), "%s", eth.get_mac_address()); //sprintf(out_buffer, "%s", eth.get_mac_address()); sock.sendto(echo_server, out_buffer, sizeof(out_buffer)); printf("%s\n", out_buffer); sock.set_timeout(1000); char in_buffer[256]; int n = sock.recvfrom(&echo_server, in_buffer, sizeof(in_buffer)); in_buffer[n] = '\0'; printf("%s\n", in_buffer); int ack_message = transString(in_buffer); if(ack_message == 1){ ack = 1; sock.set_timeout(-1); PSU_OFF = false; // turns on the PSU if init is complet } } //printf(out_buffer); while(1){ char out_buffer[] = "Envoyez moi une vitesse plz !"; printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS); sock.sendto(echo_server, out_buffer, sizeof(out_buffer)); char in_buffer[256]; int n = sock.recvfrom(&echo_server, in_buffer, sizeof(in_buffer)); in_buffer[n] = '\0'; printf("%s\n", in_buffer); splitString(in_buffer); printf("FR_len : %d fan_len : %d pwm_len : %d\n", FR_len, fan_len, pwm_len); printf("FR_str : %s fan_str : %s pwm_str : %s\n", FR_str, fan_str, pwm_str); printf("FR : %d fan[0] : %d pwm : %lf\n", FR, fan[0], pwm_f); switch(FR){ case 0: // front fans if(fan[0] == 0){ for(int i = 0; i < 9; i++){ frontFans[i].write(pwm_f); } } else{ for(int i = 0; i < fan_len; i++){ frontFans[fan[i] - 1].write(pwm_f); } } break; case 1: // rear fans if(fan[0] == 0){ for(int i = 0; i < 9; i++){ rearFans[i].write(pwm_f); } } else{ for(int i = 0; i < fan_len; i++){ rearFans[fan[i] - 1].write(pwm_f); } } break; case 2: // both fans if(fan[0] == 0){ for(int i = 0; i < 9; i++){ frontFans[i].write(pwm_f); rearFans[i].write(pwm_f); } } else{ for(int i = 0; i < fan_len; i++){ frontFans[fan[i] - 1].write(pwm_f); rearFans[fan[i] - 1].write(pwm_f); } } break; case 3: // turns off PSU PSU_OFF = true; //char out_bufferF[] = "PSU OFF"; //sock.sendto(echo_server, out_bufferF, sizeof(out_bufferF)); break; case 4: // turns on PSU PSU_OFF = false; //char out_bufferN[] = "PSU ON"; //sock.sendto(echo_server, out_bufferN, sizeof(out_bufferN)); break; case 5: // kills the PSU PSU_DEAD = true; //char out_bufferK[] = "PSU KILLED"; //sock.sendto(echo_server, out_bufferK, sizeof(out_bufferK)); break; case 6: // revives the PSU PSU_DEAD = false; //char out_bufferR[] = "PSU REVIVED"; //sock.sendto(echo_server, out_bufferR, sizeof(out_bufferR)); break; case 10:{ InterruptIn rpmIn(counterRear[0]); count[0] = 0; rpmIn.rise(&countFunction); t.reset(); t.start(); while(t.read_ms() < 1001); t.stop(); rpmIn.rise(NULL); double rev = (double)count[0]; double rpm1 = rev*30; printf("%lf\n", rpm1); } break; case 11:{ for(int i = 0; i < fan_len; i++){ InterruptIn rpmIn(counterRear[fan[i] - 1]); t.reset(); count[0] = 0; rpmIn.rise(&countFunction); while(count[0] == 0); t.start(); while(count[0] <= (2*tours)+1); // changer ligne 15 pour augmenter ou diminuer le nombre d'interrupt (tour) t.stop(); // mettre count[0] <= 2 pour temps entre 2 pulses rpmIn.rise(NULL); double rpm1 = (60000*tours)/t.read_ms(); // mettre 30000/t.read_ms() pour convertion entre 2 pulses printf("rpm : %lf\n", rpm1); } } break; case 12:{ while(readAllRpm == 0) { if(readRpm == 0) { InterruptIn RI1(counterFront[0]); InterruptIn RI2(counterFront[1]); InterruptIn RI3(counterFront[2]); InterruptIn RI4(counterFront[3]); InterruptIn RI5(counterFront[4]); InterruptIn RI6(counterFront[5]); InterruptIn RI7(counterFront[6]); InterruptIn RI8(counterFront[7]); InterruptIn RI9(counterFront[8]); for(int i = 0; i < 9; i++) { count[i] = 0; } RI1.rise(&countFunction); RI2.rise(&countFunction2); RI3.rise(&countFunction3); RI4.rise(&countFunction4); RI5.rise(&countFunction5); RI6.rise(&countFunction6); RI7.rise(&countFunction7); RI8.rise(&countFunction8); RI9.rise(&countFunction9); t.reset(); t.start(); while(t.read_ms() < 501); t.stop(); RI1.rise(NULL); RI2.rise(NULL); RI3.rise(NULL); RI4.rise(NULL); RI5.rise(NULL); RI6.rise(NULL); RI7.rise(NULL); RI8.rise(NULL); RI9.rise(NULL); for(int i = 0; i < 9; i++) { double rev1[9] = {0.0}; rev1[i] = (double)count[i]; rpm[i] = rev1[i]*60; printf("rpm%d : %lf\n", i, rpm[i]); } readRpm = 1; } else { InterruptIn RI1(counterRear[0]); InterruptIn RI2(counterRear[1]); InterruptIn RI3(counterRear[2]); InterruptIn RI4(counterRear[3]); InterruptIn RI5(counterRear[4]); InterruptIn RI6(counterRear[5]); InterruptIn RI7(counterRear[6]); InterruptIn RI8(counterRear[7]); InterruptIn RI9(counterRear[8]); for(int i = 0; i < 9; i++) { count[9+i] = 0; } RI1.rise(&countFunction10); RI2.rise(&countFunction11); RI3.rise(&countFunction12); RI4.rise(&countFunction13); RI5.rise(&countFunction14); RI6.rise(&countFunction15); RI7.rise(&countFunction16); RI8.rise(&countFunction17); RI9.rise(&countFunction18); t.reset(); t.start(); while(t.read_ms() < 501); t.stop(); RI1.rise(NULL); RI2.rise(NULL); RI3.rise(NULL); RI4.rise(NULL); RI5.rise(NULL); RI6.rise(NULL); RI7.rise(NULL); RI8.rise(NULL); RI9.rise(NULL); for(int i = 0; i < 9; i++) { double rev1[9] = {0.0}; rev1[i] = (double)count[9+i]; rpm[9+i] = rev1[i]*60; printf("rpm%d : %lf\n", 9+i, rpm[9+i]); } readAllRpm = 1; readRpm = 0; } } readAllRpm = 0; } break; case 99:{ } break; default: for(int i = 0; i < 9; i++){ frontFans[i].write(0.0); rearFans[i].write(0.0); } } for(int i = 0; i <= 2; i++){ FR_str[i] = '\0'; // "resets" the FR_str } for(int i = 0; i <= 4; i++){ pwm_str[i] = '\0'; // "resets" the pwm_str } } } void splitString(char stringToSplit[]){ char *comma_1 = strchr(stringToSplit, ',') + 1; char *comma_2 = strchr(comma_1, ',') + 1; char *comma_3 = strchr(comma_3, '\0'); FR_len = comma_1 - stringToSplit - 1; fan_len = comma_2 - comma_1 - 1; pwm_len = comma_3 - comma_2; printf("comma_1 : %c comma_2 : %s comma_3 : %s\n", *comma_1, comma_2, comma_3); strncpy(FR_str, stringToSplit, FR_len); FR = strtol(FR_str, NULL, 10); strncpy(fan_str, comma_1, fan_len); for(int i = 0; i < fan_len; i++){ fan[i] = fan_str[i] - '0'; } strncpy(pwm_str, comma_2, pwm_len); double pwm = strtod(pwm_str, NULL); pwm_f = pwm/100.0; } int transString(char stringToTrans[]){ char in_str[2] = {0}; strncpy(in_str, stringToTrans,1); return atoi(in_str); } void countFunction(){ count[0]++; } void countFunction2(){ count[1]++; } void countFunction3(){ count[2]++; } void countFunction4(){ count[3]++; } void countFunction5(){ count[4]++; } void countFunction6(){ count[5]++; } void countFunction7(){ count[6]++; } void countFunction8(){ count[7]++; } void countFunction9(){ count[8]++; } void countFunction10(){ count[9]++; } void countFunction11(){ count[10]++; } void countFunction12(){ count[11]++; } void countFunction13(){ count[12]++; } void countFunction14(){ count[13]++; } void countFunction15(){ count[14]++; } void countFunction16(){ count[15]++; } void countFunction17(){ count[16]++; } void countFunction18(){ count[17]++; }