Active solar tracker for CEAMS sampler.

Dependencies:   ADS1115 BME280 LSM9DS1 CAM_M8 DRV8834 SunPosition mbed

Files at this revision

API Documentation at this revision

Comitter:
eawendtjr
Date:
Wed Sep 12 19:09:12 2018 +0000
Parent:
10:6baaca879789
Commit message:
Back to blocking model

Changed in this revision

CEAMS_Sun_Tracker.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/CEAMS_Sun_Tracker.lib	Wed Sep 12 19:05:17 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/teams/Vockens-Group-Sensors/code/CEAMS_Sun_Tracker/#e730f501c954
--- a/main.cpp	Wed Sep 12 19:05:17 2018 +0000
+++ b/main.cpp	Wed Sep 12 19:09:12 2018 +0000
@@ -23,30 +23,90 @@
 // Object
 LSM9DS1             lol(PB_9, PB_8, 0xD6, 0x3C);
 
+// Variables
+float roll;
+float pitch;
+float pitch360;
+float heading;
+
 /////////////////////////////////////////////
 //Temperature and Pressure
 /////////////////////////////////////////////
 // Object
 BME280              bme(PB_9, PB_8, 0xEE);
 
+//Variables
+float bme_press;
+float bme_temp;
+float bme_rh;
+
 /////////////////////////////////////////////
 //Quad Sensor
 /////////////////////////////////////////////
 // Object
 ADS1115    ads_quad(PB_9, PB_8, ADS1115_ADDRESS_GND);
 
+// Variables
+int16_t diff13;
+int16_t diff13_offset = 9600;
+float diff13_volt;
+int16_t diff24;
+int16_t diff24_offset = 11000;
+float diff24_volt;
+//const float quad_c = 0.125; // For 5 degree FOV
+float vph1;
+int16_t vph1_raw;
+float vph2;
+int16_t vph2_raw;
+float vph3;
+int16_t vph3_raw;
+float vph4;
+int16_t vph4_raw;
+float x1, x2, y1, y2;
+float fy, fx;
+float quad_x, quad_y;
+const int quad_thresh = 20000; // Threshold value for sun detection
+
 /////////////////////////////////////////////
 //GPS
 /////////////////////////////////////////////
 // Object
 CAM_M8              gps(PB_9, PB_8,(0x84));   
 
+// Variables
+bool    gpsReady = 0;
+uint8_t gpsquality = 0;
+uint8_t gpssatellites = 0;
+double  gpsspeed = 0.0;
+//double  gpscourse = 0.0;
+double  gpslatitude = 0.0;
+double  gpslongitude = 0.0;
+float   gpsaltitude = 0.0;
+long    gpsTime;
+long    gpsDate;
+int     gps_year;
+int     gps_month;
+int     gps_day;
+int     gps_hour;
+int     gps_minute;
+double  gps_second;
+
 /////////////////////////////////////////////
 //Sun Position
 /////////////////////////////////////////////
 // Object
 SunPosition         sun;
 
+//Variables
+double zenith;
+double azimuth;
+double radius;
+double r; 
+const double time_zone = 0; //GPS gets Greenwich time
+const double delta_t = 68;  //This parameter will be roughly constant this year
+const double slope = 30;
+const double azm_rotation = 10;
+
 /////////////////////////////////////////////
 //Stepper Motor Driver
 /////////////////////////////////////////////
@@ -62,11 +122,6 @@
 Timer t;
 
 /////////////////////////////////////////////
-//Finite state machinge
-/////////////////////////////////////////////
-enum {IDLE, SEARCHING, TRACKING, MEASURING, RETURNING};
-
-/////////////////////////////////////////////
 //Function prototypes
 /////////////////////////////////////////////
 bool check_thresh();
@@ -92,32 +147,76 @@
     az_motor.setMicrostepping(32);
     zen_motor.setMicrostepping(32);
     
-    int state = IDLE;
+    // Get initial time and position
+    gps_and_sun();
+    
     while(1)
     {   
-        // FSM for tracker control
-        switch(state) {
-            case(IDLE):
-                
-                break;
-            case(SEARCHING):
+        // Check sun once per loop
+        gps_and_sun();
         
-                break;
-            case(TRACKING):
+        // If sensor out of field of view, enter initial honing algorithm
+        // Zenith honing
+        //pc.printf("err = %f \r\n", (360 - pitch360 - zenith));
+        if(!check_thresh() && (abs(360 - pitch360 - zenith) >= 3.0))
+        {
+            if(((pitch360 - zenith)) <= 180)
+            {
+                zen_motor.stepAngle(-(360 - zenith - pitch360), 5); // Move CCW
+            }
+            else
+            {
+                zen_motor.stepAngle((360 - zenith - pitch360), 5); // Move CW  
+            } 
+        }
+        
+        // Azimuth CCW search
+        while(!check_thresh() && (steps_az <= 1600)) // Scan 90 degrees
+        {
+            // Rotate the azimuth stepper 1 step CCW
+            az_step(0, 100);
+            //pc.printf("%d \r\n", steps_az);
+        }
         
-                break;
-            case(MEASURING):
-        
-                break;
-            case(RETURNING):
+        // Azimuth CW search
+        while(!check_thresh() && (steps_az >= -1600))
+        {
+            // Rotate the azimuth stepper 1 step CW
+            az_step(1, 100);
+            //pc.printf("%d \r\n", steps_az);
+        }
         
-                break;
-            default:
-            
-                break;    
-        } // switch(state)
-    } // while(1)
-} // main
+        // If sensor in field of view, enter active tracking algorithm
+        while(check_thresh())
+        {
+            diff13 = ads_quad.readADC(ADS1115_DIFF_A0A1_GAIN_ONE); //Differential mode
+            diff24 = ads_quad.readADC(ADS1115_DIFF_A2A3_GAIN_ONE); //Differential mode
+
+            if(abs(diff24 + diff24_offset) > (500))
+            {
+                if(diff24 + diff24_offset < 0) // Pointed right of target
+                {
+                    az_step(0, 100); // Turn CCW
+                }
+                else if(diff24 + diff24_offset > 0) // Pointed left of target
+                {
+                    az_step(1, 100); // Turn CW
+                }
+            }
+            if(abs(diff13 + diff13_offset) > (500))
+            {
+                if(diff13 + diff13_offset < 0) // Pointed above target
+                {
+                    zen_step(0, 100); // Turn CCW
+                }
+                else if(diff13 + diff13_offset > 0) // Pointed below target
+                {
+                    zen_step(1, 100); // Turn CW
+                }
+            } 
+        } 
+    } 
+} 
 
 bool check_thresh()
 {