Skip to content

Commit

Permalink
merge devel into master -> code base "done" (#4):
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ayham291 and Jay-S-oth authored Jun 2, 2021
1 parent ab7fc1c commit 351d87b
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 24 deletions.
128 changes: 122 additions & 6 deletions app/lib/FindSun/FindSun.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include <FindSun.h>

#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()
Expand All @@ -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;
}
18 changes: 13 additions & 5 deletions app/lib/FindSun/FindSun.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
#include <Arduino.h>
#include <SunPosition.h>
#include <QMC5883LCompass.h>
#include <math.h>
#include <SunPosition.h>
#include <MoveMotors.h>
#include <EndPos.h>

#define TOLERANCE 3


#define Mode_Standby 0b00000000
Expand All @@ -23,8 +27,6 @@
#define OSR_128 0b10000000
#define OSR_64 0b11000000



class Find_Sun
{
private:
Expand All @@ -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
18 changes: 9 additions & 9 deletions app/lib/MoveMotors/MoveMotors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@ 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;
}
}
void Motor::rotate(int dir)
{
if (dir == RIGHT)
if(dir==RIGHT)
{
pinMode(this->dir_pin, OUTPUT);
digitalWrite(this->dir_pin, LOW);
this->rot = RIGHT;
this->dir = UP;
switch_pwr(ON);
}
if (dir == LEFT)
if(dir==LEFT)
{
pinMode(this->dir_pin, OUTPUT);
digitalWrite(this->dir_pin, HIGH);
Expand Down
105 changes: 101 additions & 4 deletions app/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,109 @@
#include <Arduino.h>
#include <EndPos.h>
#include <MoveMotors.h>
#include <SunPosition.h>
#include <RTClib.h>
#include <Wire.h>
#include <FindSun.h>
#include <ESP8266WiFi.h>

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:
}
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;
}
Loading

0 comments on commit 351d87b

Please sign in to comment.