Windshape control / Mbed OS Controle_ventilo_ethernet_v1_2_4

Fork of Controle_ventilo_ethernet_v1_2_4 by Sergio Márquez

Files at this revision

API Documentation at this revision

Comitter:
YuK41
Date:
Fri Jun 16 17:41:25 2017 +0000
Parent:
11:aa1cb8df15dc
Commit message:
Modifications by WindShape ; - Add comments in the code; - Add functionality to get rpm by counting time between rising edges; - Add function to set a different PWM to all fans for debugging purpose

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r aa1cb8df15dc -r b9ac1a23ac41 main.cpp
--- a/main.cpp	Wed Jun 14 16:16:54 2017 +0000
+++ b/main.cpp	Fri Jun 16 17:41:25 2017 +0000
@@ -6,8 +6,8 @@
 #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 char* ECHO_SERVER_ADDRESS = "192.168.1.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 = 1036;
 //const int BROADCAST_PORT = 58083;
 
 Timer t;
@@ -17,82 +17,74 @@
 DigitalOut PSU_DEAD(D8); // Kill PSU
 DigitalOut PSU_OFF(D9); // Power On PSU
 
-volatile long int count[18] = {0};
+volatile long int count = 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
+// PWM PINS
+PwmOut rearFans[9]={PB_5, PB_15, PC_8,  PB_10, PB_8, PE_10, PE_14, PE_6, PB_1};
+PwmOut frontFans[9]= {PB_3, PB_9, PC_9, PB_11, PC_6, PD_13, PE_12, PE_5, PE_9};
 
-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};
+// F00 PINS
+PinName counterFront[9]={PA_3,PC_3,PF_5,PF_7,PD_7,PE_4,PC_2,PB_2,PE_3};
 PinName counterRear[9]={PC_0,PF_3,PF_10,PF_9,PE_0,PG_0,PF_8,PD_11,PF_4};
 
+// LEDS
 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;
+// ARGUMENTS TO BE RECEIVED BY ETHERNET
+char arg1_str[2]  = {0};
+char arg2_str[10] = {0};
+char arg3_str[4]  = {0};
+
+int arg1 = 0;
+int arg3 = 0;
 int fan[10] = {0};
+int fan_nb = 0;
 float pwm_f = 0.0;
-int FR_len = 0;
-int fan_len = 0;
-int pwm_len = 0;
+
+int arg1_len = 0;
+int arg2_len = 0;
+int arg3_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[]) {
 
+    // Turn off the PSU (Standby Mode)
     PSU_OFF = true;
     
+    // Set PWM timer frequency and set PWM to 0.0%
     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
+
+    // Initialize ethernet interface and UDP socket
+    EthernetInterface eth;                          
+    UDPSocket sock;                                
     SocketAddress echo_server; 
     
+    // Connect to the ethernet port
     eth.connect();
     sock.open(&eth);
     
+    // Get IP address of the board
     const char *local_ip = eth.get_ip_address();
+    printf("\nClient IP Address is %s \n", local_ip);
+    
+    // Get the MAC address of the board
     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);
@@ -126,52 +118,54 @@
         if(ack_message == 1){
             ack = 1;
             sock.set_timeout(-1);
-            PSU_OFF = false;            // turns on the PSU if init is complet
-            }
+            //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);
+        char out_buffer[] = "";
+        printf("\n-----------------------------------\n");
         sock.sendto(echo_server, out_buffer, sizeof(out_buffer));
         
+        
         char in_buffer[256];
         int n = sock.recvfrom(&echo_server, in_buffer, sizeof(in_buffer));    
         
+        // Add \0 as end of line character to the input message
         in_buffer[n] = '\0';
         
-        printf("%s\n", in_buffer);
-        
+        // Split the input message and save value in FR_str, fan_str and pwm_str
         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);
+        // Print the order that was received
+        printf("ORDER RECEIVED : %s, %s, %s\n", arg1_str, arg2_str, arg3_str);
+
+        switch(arg1){
+            
+            // Set the PWM of a front fan in particular
+            case 0:
+                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);
+                else{
+                    for(int i = 0; i < arg2_len; i++){
+                        frontFans[fan[i] - 1].write(pwm_f);
+                    }
                 }
-            }
             break;
             
-            case 1:         // rear fans
+            // Set the PWM of a rear fan in particular
+            case 1:        
             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++){
+                for(int i = 0; i < arg2_len; i++){
                     rearFans[fan[i] - 1].write(pwm_f);
                 }
             }
@@ -185,7 +179,7 @@
                 }
             }
             else{
-                for(int i = 0; i < fan_len; i++){
+                for(int i = 0; i < arg2_len; i++){
                     frontFans[fan[i] - 1].write(pwm_f);
                     rearFans[fan[i] - 1].write(pwm_f);
                 }
@@ -216,68 +210,56 @@
             //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]);
-                    
+            // Display F00 for front fans
+            case 20:{
+                for (int i =0;i<9;i++){
+                    InterruptIn rpmIn1(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;
-                        }
+                    count = 0;
+                    rpmIn1.rise(&countFunction);
+                    int c = 0;
+                    while(count<=4 && c<1000){
+                        c++;
                     }
-                    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;
-                        }
+                    double rpm1 = 60*1000.0/(t1.read_ms()/2.0);
+                    if (i%3==0)
+                        printf("\n");
+                    printf(" %lf ", rpm1);
+                }
+                printf("\n");
+            }
+            break;
+            
+            // Display F00 for rear fans
+            case 21:{
+                for (int i =0;i<9;i++){
+                    InterruptIn rpmIn1(counterRear[i]);
+                    t1.reset();
+                    count = 0;
+                    rpmIn1.rise(&countFunction);
+                    int c = 0;
+                    while(count<=4 && c<1000){
+                        c++;
                     }
-                    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);
+                    rpmIn1.rise(NULL);
+                    double rpm1 = 60*1000.0/(t1.read_ms()/2.0);
+                    if (i%3==0)
+                        printf("\n");
+                    printf(" %lf ", rpm1);
+                }
+                printf("\n");
+                
+            }
+            break;
+            
+            // Set a diffenrent PWM value to all fans from 8% to 32% for test purpose
+            case 30:{
+                int pwm_17 = 8;
+                for(int i = 0; i < 9; i++){
+                    float p=(pwm_17+2*i)/100.0;
+                    frontFans[i].write(p);
+                    rearFans[i].write(p);
                 }
             }
             break;
@@ -294,10 +276,10 @@
                 }
         }
         for(int i = 0; i <= 2; i++){
-            FR_str[i] = '\0';       // "resets" the FR_str
+            arg1_str[i] = '\0';       // "resets" the arg1_str
         }      
         for(int i = 0; i <= 4; i++){
-            pwm_str[i] = '\0';      // "resets" the pwm_str
+            arg3_str[i] = '\0';      // "resets" the arg3_str
         }
     }
 }
@@ -307,21 +289,29 @@
     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);
+    arg1_len = comma_1 - stringToSplit - 1;
+    arg2_len = comma_2 - comma_1 - 1;
+    arg3_len = comma_3 - comma_2;
     
-    strncpy(fan_str, comma_1, fan_len);
-    for(int i = 0; i < fan_len; i++){
-        fan[i] = fan_str[i] - '0';
+    // Argument 1
+    strncpy(arg1_str, stringToSplit, arg1_len);
+    arg1 = strtol(arg1_str, NULL, 10);
+    
+    // Argument 2
+    strncpy(arg2_str, comma_1, arg2_len);
+    for(int i = 0; i < arg2_len; i++){
+        fan[i] = arg2_str[i] - '0';
     }
     
-    strncpy(pwm_str, comma_2, pwm_len);
-    double pwm = strtod(pwm_str, NULL);
-    pwm_f = pwm/100.0;
+    // Save the last number of arg2 into fan_nb as int
+    for(int i = 0; i<arg2_len; i++){
+        fan_nb = arg2_str[i] - '0';
+    } 
+
+    // Argument 3
+    strncpy(arg3_str, comma_2, arg3_len);
+    double arg3 = strtod(arg3_str, NULL);
+    pwm_f = arg3/100.0;
 }
 
 int transString(char stringToTrans[]){
@@ -331,73 +321,14 @@
 }
 
 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]++;
+    // Start timer when the first rising edge is reached
+    if (count == 0)
+        t1.start(); 
+    
+    // Stop timer when the fourth rising edge is reached (2 rotation)
+    if (count == 4)
+        t1.stop(); 
+        
+    count++;
 }
 
-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]++;
-}*/
\ No newline at end of file