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-14
- Revision:
- 11:aa1cb8df15dc
- Parent:
- 10:617194438c6b
- Child:
- 12:b9ac1a23ac41
File content as of revision 11:aa1cb8df15dc:
#include "mbed.h" #include "EthernetInterface.h" #include "SocketAddress.h" #include "UDPSocket.h" #include "lwip/ip.h" #include "lwip/api.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; //const int BROADCAST_PORT = 58083; Timer t; Timer t1; Timer t2; Timer t3; DigitalOut PSU_DEAD(D8); // Kill PSU DigitalOut PSU_OFF(D9); // Power On PSU volatile long int count[18] = {0}; int tours = 5; double rpm[18] = {0.0}; bool readRpm = 0; bool readAllRpm = 0; bool skipRead = 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]={PA_3,PC_3,PF_5,PF_7,PE_3,PG_1,PE_4,PC_2,PB_2}; PinName counterRear[9]={PC_0,PF_3,PF_10,PF_9,PE_0,PG_0,PF_8,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; 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 14:{ for(int i = 0; i < 9; i++){ InterruptIn rpmIn1(counterRear[i]); InterruptIn rpmIn2(counterFront[i]); t1.reset(); t2.reset(); count[1] = 0; count[2] = 0; rpmIn1.rise(&countFunction2); printf("Hello\n"); while(count[1] == 0){ t3.reset(); t3.start(); if((t3.read_ms() >= 200) && (count[1] == 0)){ skipRead = 1; } //printf("Hello1\n"); if(skipRead == 1){ printf("Hello2\n"); t3.stop(); break; } } t1.start(); printf("Hello3\n"); while(count[1] <= (2*tours)+1){ if(skipRead == 1){ printf("Hello4\n"); break; } } skipRead = 0; t1.stop(); rpmIn1.rise(NULL); rpmIn2.rise(&countFunction3); while(count[2] == 0){ t3.reset(); t3.start(); if(t3.read_ms() >= 200){ skipRead = 1; } if(skipRead == 1){ t3.stop(); break; } } t2.start(); while(count[2] <= (2*tours)+1){ if(skipRead == 1){ break; } } skipRead = 0; t2.stop(); rpmIn2.rise(NULL); double rpm1 = (60000*tours)/t1.read_ms(); double rpm2 = (60000*tours)/t2.read_ms(); printf("rpm1 : %lf\nrpm2 : %lf\n", rpm1, rpm2); } } 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]++; }*/