Project aiming to make a self-controlled solar reflector

Dependencies:   Accelerometer LCD Inverter Algorithm MotorDriver Anemometer GUI ArduinoJson Misc Definitions Pushbutton WebSocketClient temp_fan

Revision:
17:238ccf7e3676
Parent:
16:326404a7a8b8
Child:
18:ee0aa6db3c8d
--- a/main.cpp	Wed Mar 17 14:21:51 2021 +0000
+++ b/main.cpp	Wed Mar 17 20:20:07 2021 +0000
@@ -18,6 +18,9 @@
 #include "Misc.h"
 #include "temp_fans.h"
 #include <string>
+#include "EthernetInterface.h"
+#include "Inverter.h"
+#include "GUI.h"
 
 #define timer_read_s(x)     chrono::duration_cast<chrono::seconds>((x).elapsed_time()).count()
 #define timer_read_ms(x)    chrono::duration_cast<chrono::milliseconds>((x).elapsed_time()).count()
@@ -28,6 +31,7 @@
 MotorDriver motor;
 LowPowerTimer t,t_mode, t_disp;
 int mode = OP_CALIBRATION;
+EthernetInterface eth;
 
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
@@ -42,7 +46,6 @@
 float ref_R1,ref_R2;
 int t_elapsed;
 int wthres = WIND_THRES_INIT;
-char buffer[16];
 
 //FLAGS
 int flag_time = 1; //Normal mode time
@@ -50,6 +53,9 @@
 int flag_disp = 1; //Anti-flickering
 int flag_bres = 0; //Flag for checking button released
 int flag_flas = 1; //Flag for flashing symbol
+bool flag_eth; //Flag for ethernet connection
+bool flag_aTrack; //Flag for active tracking (0 - offline, 1 - online)
+bool flag_powerOn;
 
 //PUSH BUTTONS
 Pushbutton bt_fn(PIN_BTFN,&mode,&flag_disp);
@@ -60,6 +66,12 @@
 string botL = "";
 lcd.LCD_display(topL, botL);
 
+
+
+eth.connect();
+GUI gui("ws://int-sol-ref.herokuapp.com/", &eth, &flag_eth);
+Inverter inverter("int-sol-ref.herokuapp.com", 80, &eth);
+    
 t.start(); //Start timer
 t_disp.start();
 wait_us(1000000);
@@ -100,8 +112,10 @@
         }
 ////////////////////////////////////////////////////////////////////////////////
         case OP_NORMAL:{
+            if (flag_eth){gui.receives(&wthres, &flag_aTrack, &flag_powerOn);}
             fan.checkTemp(&mode);if(mode != OP_NORMAL){break;}
             ane.checkWind(&mode);if(mode != OP_NORMAL){break;}
+            if (flag_eth){gui.windSpeed(ane.getWind());}
             topL = "NORMAL:IDLING";
             if(timer_read_ms(t_disp) >= LCD_RRATE){
                 botL = Misc::itos(ane.getWind(&flag_disp),3) + "kph  |  " + Misc::itos(fan.getTemp()) + "C";
@@ -127,6 +141,8 @@
                 ang_R = acc.getAngle(S_R1);
                 while(ang_R <= ref_R1 && !acc.checkAngle(ref_R1,ang_R) && mode == OP_NORMAL)
                 {
+                    if (flag_eth){gui.receives(&wthres, &flag_aTrack, &flag_powerOn);}
+                    if (flag_eth){gui.windSpeed(ane.getWind());}
                     fan.checkTemp(&mode);if(mode != OP_NORMAL){break;}
                     ane.checkWind(&mode);if(mode != OP_NORMAL){break;}
                     if(timer_read_ms(t_disp) >= LCD_RRATE){
@@ -142,6 +158,8 @@
                 }
                 while(ang_R >= ref_R1 && !acc.checkAngle(ref_R1,ang_R) && mode == OP_NORMAL)
                 {
+                    if (flag_eth){gui.receives(&wthres, &flag_aTrack, &flag_powerOn);}
+                    if (flag_eth){gui.windSpeed(ane.getWind());}
                     fan.checkTemp(&mode);if(mode != OP_NORMAL){break;}
                     ane.checkWind(&mode);if(mode != OP_NORMAL){break;}
                     if(timer_read_ms(t_disp) >= LCD_RRATE){
@@ -161,6 +179,8 @@
                 ang_R = acc.getAngle(S_R2);
                 while(ang_R <= ref_R2 && !acc.checkAngle(ref_R2,ang_R) && mode == OP_NORMAL)
                 {
+                    if (flag_eth){gui.receives(&wthres, &flag_aTrack, &flag_powerOn);}
+                    if (flag_eth){gui.windSpeed(ane.getWind());}
                     fan.checkTemp(&mode);if(mode != OP_NORMAL){break;}
                     ane.checkWind(&mode);if(mode != OP_NORMAL){break;}
                     if(timer_read_ms(t_disp) >= LCD_RRATE){
@@ -176,6 +196,8 @@
                 }
                 while(ang_R >= ref_R2 && !acc.checkAngle(ref_R2,ang_R) && mode == OP_NORMAL)
                 {
+                    if (flag_eth){gui.receives(&wthres, &flag_aTrack, &flag_powerOn);}
+                    if (flag_eth){gui.windSpeed(ane.getWind());}
                     fan.checkTemp(&mode);if(mode != OP_NORMAL){break;}
                     ane.checkWind(&mode);if(mode != OP_NORMAL){break;}
                     if(timer_read_ms(t_disp) >= LCD_RRATE){
@@ -201,6 +223,8 @@
         }
 ////////////////////////////////////////////////////////////////////////////////
         case OP_WIND:{
+            if (flag_eth){gui.receives(&wthres, &flag_aTrack, &flag_powerOn);}
+            if (flag_eth){gui.windSpeed(ane.getWind());}
             fan.checkTemp(&mode);if(mode != OP_WIND){break;}
             ane.checkWind(&mode);if(mode != OP_WIND){break;}
             if(timer_read_ms(t_disp) >= LCD_RRATE){
@@ -225,9 +249,11 @@
         }
 ////////////////////////////////////////////////////////////////////////////////
         case OP_MANUAL1:{
+            if (flag_eth){gui.windSpeed(ane.getWind());}
             fan.checkTemp(&mode);if(mode != OP_MANUAL1){break;}
             topL = "MANUAL:M1";
-            botL = Misc::itos(ane.getWind(&flag_disp),3) + "kph  |  " + Misc::itos(fan.getTemp()) + "C";
+            botL = Misc::itos(ane.getWind(&flag_disp)) + "kph*" + Misc::itos(fan.getTemp()) + "C*"
+                        + ((flag_eth)?Misc::itos(inverter.getPower()):"---") + "W";
             if(flag_disp){
                 lcd.LCD_display(topL,botL);
                 flag_disp = 0;
@@ -251,6 +277,7 @@
             
             while(bt_inc.read()) //Extend
             {
+                if (flag_eth){gui.windSpeed(ane.getWind());}
                 if(timer_read_ms(t_disp) >= LCD_RRATE){
                     topL = (flag_flas)? "MANUAL:M1   <=> " : "MANUAL:M1  <-=->";
                     flag_flas = !flag_flas;
@@ -265,6 +292,7 @@
             }
             while(bt_dec.read()) //Retract
             {
+                if (flag_eth){gui.windSpeed(ane.getWind());}
                 if(timer_read_ms(t_disp) >= LCD_RRATE){
                     topL = (flag_flas)? "MANUAL:M1  > = <" : "MANUAL:M1  ->=<-";
                     flag_flas = !flag_flas;
@@ -286,6 +314,7 @@
         }
 ////////////////////////////////////////////////////////////////////////////////
         case OP_MANUAL2:{
+            if (flag_eth){gui.windSpeed(ane.getWind());}
             fan.checkTemp(&mode);if(mode != OP_MANUAL2){break;}
             topL = "MANUAL:M2";
             botL = Misc::itos(ane.getWind(&flag_disp),3) + "kph  |  " + Misc::itos(fan.getTemp()) + "C";
@@ -312,6 +341,7 @@
             
             while(bt_inc.read()) //Extend
             {
+                if (flag_eth){gui.windSpeed(ane.getWind());}
                 if(timer_read_ms(t_disp) >= LCD_RRATE){
                     topL = (flag_flas)? "MANUAL:M2   <=> " : "MANUAL:M2  <-=->";
                     flag_flas = !flag_flas;
@@ -326,6 +356,7 @@
             }
             while(bt_dec.read()) //Retract
             {
+                if (flag_eth){gui.windSpeed(ane.getWind());}
                 if(timer_read_ms(t_disp) >= LCD_RRATE){
                     topL = (flag_flas)? "MANUAL:M2  > = <" : "MANUAL:M2  ->=<-";
                     flag_flas = !flag_flas;
@@ -347,6 +378,7 @@
         }
 ////////////////////////////////////////////////////////////////////////////////
         case OP_MANUAL_ALL:{
+            if (flag_eth){gui.windSpeed(ane.getWind());}
             fan.checkTemp(&mode);if(mode != OP_MANUAL_ALL){break;}
             topL = "MANUAL:ALL";
             botL = Misc::itos(ane.getWind(&flag_disp),3) + "kph  |  " + Misc::itos(fan.getTemp()) + "C";
@@ -373,6 +405,7 @@
             
             while(bt_inc.read()) //Extend
             {
+                if (flag_eth){gui.windSpeed(ane.getWind());}
                 if(timer_read_ms(t_disp) >= LCD_RRATE){
                     topL = (flag_flas)? "MANUAL:ALL  <=> " : "MANUAL:ALL <-=->";
                     flag_flas = !flag_flas;
@@ -387,6 +420,7 @@
             }
             while(bt_dec.read()) //Retract
             {
+                if (flag_eth){gui.windSpeed(ane.getWind());}
                 if(timer_read_ms(t_disp) >= LCD_RRATE){
                     topL = (flag_flas)? "MANUAL:ALL > = <" : "MANUAL:ALL ->=<-";
                     flag_flas = !flag_flas;
@@ -408,6 +442,7 @@
         }
 ////////////////////////////////////////////////////////////////////////////////
         case OP_WSETTING:{
+            if (flag_eth){gui.windSpeed(ane.getWind());}
             fan.checkTemp(&mode);if(mode != OP_WSETTING){break;}
             topL = "Threshold:";
             botL = Misc::itos(wthres) + " kph";
@@ -436,6 +471,7 @@
                 if(bt_inc.read() && wthres < WIND_THRES_MAX)
                 {
                     ane.setThres(++wthres);
+                    if(flag_eth){gui.survivalSpeed(wthres);}
                     botL = Misc::itos(wthres) + " kph";
                     lcd.LCD_display(topL,botL);
                     flag_idle = 0;
@@ -444,6 +480,7 @@
                 else if(bt_dec.read() && wthres > WIND_THRES_MIN)
                 {
                     ane.setThres(--wthres);
+                    if(flag_eth){gui.survivalSpeed(wthres);}
                     botL = Misc::itos(wthres) + " kph";
                     lcd.LCD_display(topL,botL);
                     flag_idle = 0;
@@ -459,6 +496,7 @@
         }
         ////////////////////////////////////////////////////////////////////////
         case OP_OVERHEAT:{
+            if (flag_eth){gui.windSpeed(ane.getWind());}
             if(timer_read_ms(t_disp) >= LCD_RRATE){
                     topL = (flag_flas)? "*!* OVERHEAT *!*": "    OVERHEAT    ";
                     botL = (flag_flas)? " KEEP DISTANCE " : ("    " + Misc::itos(fan.getTemp(),3) + "C");
@@ -475,6 +513,7 @@
         }
         ////////////////////////////////////////////////////////////////////////
         case OP_OVERHEAT_MAN:{
+            if (flag_eth){gui.windSpeed(ane.getWind());}
             if(timer_read_ms(t_disp) >= LCD_RRATE){
                     topL = (flag_flas)? "*!* OVERHEAT *!*": "    OVERHEAT    ";
                     botL = (flag_flas)? "    PRESS FN    " : ("    " + Misc::itos(fan.getTemp(),3) + "C");
@@ -487,8 +526,40 @@
             flag_disp = 1;
             break;
         }
+        ////////////////////////////////////////////////////////////////////////
+        case OP_POWER_OFF:{
+            if (flag_eth){gui.receives(&wthres, &flag_aTrack, &flag_powerOn);}
+            if(mode != OP_POWER_OFF){break;}
+            if (flag_eth){gui.windSpeed(ane.getWind());}
+            fan.checkTemp(&mode);if(mode != OP_WIND){break;}
+            if(timer_read_ms(t_disp) >= LCD_RRATE){
+                topL = "SLEEP MODE";
+                flag_flas = !flag_flas;
+                botL = Misc::itos(ane.getWind(&flag_disp),3) + "kph  |  " + Misc::itos(fan.getTemp()) + "C";
+                t_disp.reset();
+                t_disp.start();
+            }
+            if(flag_disp){
+                lcd.LCD_display(topL,botL);
+                flag_disp = 0;
+            }
+            
+            //Move all motor backward
+            motor.moveBackward(M_ALL);
+            flag_time = 1; //Set the system in motion once windspeed has subsided
+            break;
+            
+            flag_idle = 0;
+            break;
+        }
     }
+    
+    if (flag_eth){
+        gui.state(mode);
+    }
+        
     wait_us(LOOP_DELAY);
+    flag_disp = 1;
 }
 
 }