From 351d87b1b111da3a54f9e44ac4b99e0093eacd4f Mon Sep 17 00:00:00 2001 From: Ayham <69654717+ayham291@users.noreply.github.com> Date: Wed, 2 Jun 2021 11:27:51 +0200 Subject: [PATCH] merge devel into master -> code base "done" (#4): * add EndPos library: - to detect if the solar panel is at its "endpostions" * add EndPostion to dependencies in platform.ini and rename endpos files * fix includes in QMC5883L lib * add Wire lib locally * add Wire and change SonnenStand * add library.properties to all libs for better autogen configs * add FindSun lib * add MoveMotors lib *NOT TESTED* * fix EndPos - pinMode to INPUT - read pins in own function keeps being called in loop * fix FindSun - add missing time init - split azimith reading to keep being called in loop * fix MoveMotors - swith LOW and HIGH for powering Motors - make bunch of defines for the code to make sense * fix SunPosition - change floats to ints - init rtc and time once in own function * add example code for RTC * add main.cpp with test code - [x] turn table to find sun - [x] get azimuth and time * add check tilt to FindSun * add final main.cpp ->: - check suns position with kompass and time - check suns angle and adjust according to light intensity - post the data to a django server * comment wifi/webserver settings out for testting * add check rotation * adjust logic -> first rotation then tilt * add comments * add comments and fix typo (#2) explanatory comments to check_tilt * add start pos * uncomment wifi settings, remove test code and add postData - every fifth tick send postData - change server and server port - add rtc_temp_sensor * remove test code/output * add features to main plus fixes Co-authored-by: Johannes Schiessel --- app/lib/FindSun/FindSun.cpp | 128 +++++++++++++++++++++++++++-- app/lib/FindSun/FindSun.h | 18 ++-- app/lib/MoveMotors/MoveMotors.cpp | 18 ++-- app/src/main.cpp | 105 ++++++++++++++++++++++- resources/examples/example_RTC.cpp | 47 +++++++++++ 5 files changed, 292 insertions(+), 24 deletions(-) create mode 100644 resources/examples/example_RTC.cpp diff --git a/app/lib/FindSun/FindSun.cpp b/app/lib/FindSun/FindSun.cpp index 0827d98..c5ff8fc 100644 --- a/app/lib/FindSun/FindSun.cpp +++ b/app/lib/FindSun/FindSun.cpp @@ -1,12 +1,18 @@ #include +#define PEAKVALUE 350 + // init the sensor void Find_Sun::init_compass() { compass.setReset(); compass.init(); + endPos.init_sensors(D6, D5); compass.setMode(Mode_Continuous, ODR_10Hz, RNG_8G, OSR_512); SunPos.init_time(); + pinMode(A0, INPUT); + tiltpanel.init_motor(D7, D4, 2); + turnTable.init_motor(D0, D3, 1); } // gets the azimuth value of the sensor int Find_Sun::get_current_azimuth() @@ -15,18 +21,128 @@ int Find_Sun::get_current_azimuth() this->current_azimuth = compass.getAzimuth(); return current_azimuth; } - -// TO BE TESTED WITH NEGATIVE OFFSETS!!! -int Find_Sun::offest_to_sun() +void Find_Sun::start_pos() +{ + endPos.read_pins(); + tiltpanel.switch_pwr(OFF); + turnTable.switch_pwr(OFF); + while (endPos.getPosPhiDown() == 1) + { + endPos.read_pins(); + tiltpanel.rotate(DOWN); + // Serial.println("A"); + delay(10); + } + tiltpanel.switch_pwr(OFF); + while (get_current_azimuth() != 0) + { + turnTable.rotate(LEFT); + get_current_azimuth(); + // Serial.println("X"); + delay(10); + } + turnTable.switch_pwr(OFF); + delay(500); +} +// calc the offset to sun then sent to the motors +int Find_Sun::offset_to_Sun() { Sun_at_azimuth = SunPos.get_azimuth(SunPos.get_arr_pos()); offset = current_azimuth - Sun_at_azimuth; return offset; } // -int Find_Sun::if_offset() +void Find_Sun::check_tilt() { - if (offset > 2 || 2 > offset) + endPos.read_pins(); + int light_intens; + light_intens = analogRead(A0); + int time_var = SunPos.get_arr_pos(); + if (time_var >= 19 && time_var <= 41) // time betwee 9:30 and 15:00 -> Adustable time + { + if (light_intens > PEAKVALUE) // light inensity not enough + { + if (endPos.getPosPhiUp() == 1 && endPos.getPosPhiDown() == 1) //Pos=Tilt Max + { + tiltpanel.switch_pwr(OFF); + tiltpanel.rotate(DOWN); + + while (endPos.getPosPhiDown() == 1 && light_intens > PEAKVALUE) // If light intensity is to low and upper end is reched -> search down + { + endPos.read_pins(); + light_intens = analogRead(A0); + turnTable.switch_pwr(OFF); + // Serial.println("DOWN"); + delay(10); + } + } + else if (endPos.getPosPhiUp() == 0 && endPos.getPosPhiDown() == 0) // Pos=Tilt Min + { + tiltpanel.switch_pwr(OFF); + tiltpanel.rotate(UP); + while (endPos.getPosPhiUp() == 0 && light_intens > PEAKVALUE) // If light intensity is to low and down end is reched -> search up + { + endPos.read_pins(); + light_intens = analogRead(A0); + turnTable.switch_pwr(OFF); + // Serial.println("UP"); + delay(10); + } + } + else if (endPos.getPosPhiUp() == 0 && endPos.getPosPhiDown() == 1) + { + if (time_var >= 34) // if light intensity is lost and its before true noon -> search up + { + tiltpanel.switch_pwr(OFF); + tiltpanel.rotate(UP); + } + else + { + tiltpanel.switch_pwr(OFF); // if light intensity is lost and its after true noon -> search down + tiltpanel.rotate(DOWN); + } + } + } + else + { + delay(1000); // delay stop driving after light intensity reached to go into better tolerance + tiltpanel.switch_pwr(OFF); + } + } + else // Time before and after adustable time -> Tilt goto Max + { + if (endPos.getPosPhiUp() == 1) + { + tiltpanel.switch_pwr(OFF); + } + else + { + tiltpanel.switch_pwr(OFF); + tiltpanel.rotate(UP); + } + } +} + +int Find_Sun::check_rotation() +{ + SunPos.get_azimuth(SunPos.get_arr_pos()); //update target angle + get_current_azimuth(); //update current angle + + if (offset_to_Sun() < -TOLERANCE) //Angle is not ok + { + turnTable.rotate(RIGHT); return 1; - return 0; + } + else if (offset_to_Sun() > TOLERANCE) //Angle is not ok + { + turnTable.rotate(LEFT); + return 1; + } + + else if (offset_to_Sun() >= TOLERANCE || offset_to_Sun() >= -TOLERANCE) //Angle is ok + { + turnTable.switch_pwr(OFF); + return 0; + } + return 1; } \ No newline at end of file diff --git a/app/lib/FindSun/FindSun.h b/app/lib/FindSun/FindSun.h index f1ca916..4c884c1 100644 --- a/app/lib/FindSun/FindSun.h +++ b/app/lib/FindSun/FindSun.h @@ -4,7 +4,11 @@ #include #include #include -#include +#include +#include +#include + +#define TOLERANCE 3 #define Mode_Standby 0b00000000 @@ -23,8 +27,6 @@ #define OSR_128 0b10000000 #define OSR_64 0b11000000 - - class Find_Sun { private: @@ -33,11 +35,17 @@ class Find_Sun int Sun_at_azimuth; int current_azimuth; int offset; + Motor tiltpanel; + Motor turnTable; + EndPos endPos; + public: void init_compass(); int get_current_azimuth(); - int offest_to_sun(); - int if_offset(); + int offset_to_Sun(); + void check_tilt(); + int check_rotation(); + void start_pos(); }; #endif \ No newline at end of file diff --git a/app/lib/MoveMotors/MoveMotors.cpp b/app/lib/MoveMotors/MoveMotors.cpp index 2e8d69c..a90f64e 100644 --- a/app/lib/MoveMotors/MoveMotors.cpp +++ b/app/lib/MoveMotors/MoveMotors.cpp @@ -4,21 +4,21 @@ void Motor::init_motor(uint8_t pwr_pin, uint8_t dir_pin, int pos) { pinMode(pwr_pin, OUTPUT); digitalWrite(pwr_pin, HIGH); // initial state as off - this->dir_pin = dir_pin; - this->pwr_pin = pwr_pin; - this->pos = pos; - this->pwr = 0; - this->rot = NXN; // means error ie no rotation + this->dir_pin=dir_pin; + this->pwr_pin=pwr_pin; + this->pos=pos; + this->pwr=0; + this->rot=NXN; // means error ie no rotation } // switch motor power on and off void Motor::switch_pwr(int pwr) { - if (pwr == ON) + if(pwr==ON) { digitalWrite(pwr_pin, LOW); this->pwr = 1; } - if (pwr == OFF) + if(pwr==OFF) { digitalWrite(pwr_pin, HIGH); this->pwr = 0; @@ -26,7 +26,7 @@ void Motor::switch_pwr(int pwr) } void Motor::rotate(int dir) { - if (dir == RIGHT) + if(dir==RIGHT) { pinMode(this->dir_pin, OUTPUT); digitalWrite(this->dir_pin, LOW); @@ -34,7 +34,7 @@ void Motor::rotate(int dir) this->dir = UP; switch_pwr(ON); } - if (dir == LEFT) + if(dir==LEFT) { pinMode(this->dir_pin, OUTPUT); digitalWrite(this->dir_pin, HIGH); diff --git a/app/src/main.cpp b/app/src/main.cpp index 6dd524e..9c46d35 100644 --- a/app/src/main.cpp +++ b/app/src/main.cpp @@ -1,12 +1,109 @@ #include +#include +#include +#include +#include +#include +#include +#include + +char ssid[] = "ADD_SSID"; +char pass[] = "ADD_PASSWORD"; +int status = WL_IDLE_STATUS; +int check_rotation=1; +char server[] = "arduino.ayhamcloud.de"; +String postData_k; +String postData_d; +String postData; + +WiFiClient client; +int ticks = 0; +int gen_status = 1; +String endpos_val; +int az; +float diode; +int offset; +float temperature; + +RTC_DS3231 rtc_temp_only; +EndPos endPos; +Motor turnTable; +Motor tiltPanel; +SonnenStand sunpos; +Find_Sun FindSun; + void setup() { - - // put your setup code here, to run once: + Serial.begin(9600); + rtc_temp_only.begin(); + turnTable.init_motor(D0, D3, 1); + tiltPanel.init_motor(D7, D4, 2); + FindSun.init_compass(); + WiFi.begin(ssid, pass); + while (WiFi.status() != WL_CONNECTED) + { + Serial.println("Attempting to connect to Network named: "); + Serial.println(ssid); + delay(3000); + } + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + pinMode(A0, OUTPUT); + FindSun.start_pos(); } void loop() { - // put your main code here, to run repeatedly: -} \ No newline at end of file + ticks++; + sunpos.get_azimuth(sunpos.get_arr_pos()); + az = FindSun.get_current_azimuth(); + diode = (analogRead(A0) * 5) / 1024.0; + offset = FindSun.offset_to_Sun(); + temperature = rtc_temp_only.getTemperature(); + + + check_rotation=FindSun.check_rotation(); + + if(check_rotation==0) + FindSun.check_tilt(); + + if(ticks==5) + { + if(endPos.getPosPhiDown()==0) + endpos_val = "Down"; + else if(endPos.getPosPhiUp()==1) + endpos_val = "Up"; + else + endpos_val = "Tilted"; + + postData = "kompass=" + (String)az; + postData += "&diode=" + (String)diode; + postData += "&offset=" + (String)offset; + postData += "&temperature" + (String)temperature; + postData += "&endpost=" + (String)endpos_val; + postData += "&status=" + (String)gen_status; + postData += "&tiltPanel=" + (String)tiltPanel.get_rot(); + postData += "&turnTable=" + (String)turnTable.get_rot(); + + if (client.connect(server, 80)) + { + client.println("POST / HTTP/1.1"); + client.println("Content-Type: application/x-www-form-urlencoded"); + client.print("Content-Length: "); + client.println(postData.length()); + client.println(); + client.print(postData); + } + if (client.connected()) + { + client.stop(); + } + ticks = 0; + delay(100); + } + gen_status = 0; +} diff --git a/resources/examples/example_RTC.cpp b/resources/examples/example_RTC.cpp new file mode 100644 index 0000000..2e1dd46 --- /dev/null +++ b/resources/examples/example_RTC.cpp @@ -0,0 +1,47 @@ +#include +#include +#include "RTClib.h" +RTC_DS3231 rtc; +char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; +void setup() +{ + Serial.begin(9600); + // delay(2000); + rtc.begin(); + // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); /* To manualy set date and time, remove the coment // signs and enter new values in the followingline in this sequence: year, day, month, hour, minute and second.*/ + //rtc.adjust(DateTime(2020, 2, 24, 10, 00, 0)); +} +void loop() +{ + DateTime now = rtc.now(); //Day of the weekSerial.print("Day of the week: "); + if (now.hour() < 10) + { + Serial.print("0"); + Serial.print(now.hour()); + } + else + { + Serial.print(now.hour(), DEC); + } + Serial.print(':'); + if (now.minute() < 10) + { + Serial.print("0"); + Serial.print(now.minute()); + } + else + { + Serial.print(now.minute(), DEC); + } + Serial.print(':'); + if (now.second() < 10) + { + Serial.print("0"); + Serial.print(now.second()); + } + else + { + Serial.print(now.second(), DEC); + } + Serial.println(""); +} \ No newline at end of file