1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
/******************************************************************************
Name: ARDDEV_SunTrackerSD.ino
Created: 28.07.2020 16 :00 :00
Author: jpbbricole
'******************************************************************************
*/
/* ****************************************************************************************************
AUTHOR: JAY, for the arduino forum on developpez.net
*******************************************************************************
*/
//**********************************************************
// VARIABLES GLOBALES: EN QUEL POINT DU GLOBE ETES VOUS ?
//double latitude = 48.85717; // PARIS
//double longitude = 2.34140; // PARIS
double latitude = 47.4907682; // La Chevallerais, Loire-Atlantique
double longitude = -1.6653535;
//**********************************************************
// ***********************************************************************
// IDEALEMENT CALER LA RTC SUR L'HEURE UTC ET METTRE deltaTimeZone à 0
// comme cela il n'y a pas à changer le programme aux changements d'heure
// Si votre RTC est à l'heure locale, définissez ici la différence d'heure.
int8_t deltaTimeZone = +2; // UTC+1 pendant l'heure d'hiver et UTC+2 à l'heure d'été
// ***********************************************************************
#include <RTClib.h> // https://github.com/adafruit/RTClib
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
float pot1 = A0;
float pot2 = A1;
//===================================== Parametres
//------------------------------------- Sun tracker (sunTra)
String sunTraFileName= "SunData.csv"; // Nom du fichier de donnees (Max. 8 caracteres + extension)
const float sunTraElevationMin = 0.0; // Elevation minimum
const float sunTraElevationMax = 180.0; // Elevation maximum
//------------------------------------- Moteur pas a pas (MPAP)
#define mpapRpm 180 // MPAP tours/Minute
const unsigned long mpapStepsTour = 200; // MPAP pas/tour
const boolean mpapReverse = true; // MPAP inverser le sens de rotation
const unsigned long mpapVisLongMm = 100; // MPAP longueure vis en MM
#define mpapVisPasMm100 80 // MPAP pas de vis en MM100
#define mpapEnaPinStatus LOW // Etat de la pin pour mettre le A4988 ENAble
const unsigned long mpapEnaTimout = 4000; // Temps avant mise en Disable le A4988 ENA = !mpapEnaPinStatus
unsigned long mpapEnaTimoutChrono = 0; // Pour chronometrer le timout
//------------------------------------- Demo
const unsigned long demoTempo = 1000; // Toutes les secondes
//===================================== Parametres FIN
//------------------------------------- Sun tracker (sunTra)
struct sunTrackerDef // Objet motaur pas à pas
{String date; int heure; float posElevation[24]; int elevationMin; int elevationMax; float sunTraSteps;};
sunTrackerDef sunTra;
#define sunTraLedActivityPin 3 // LED temoin d'activite
unsigned long sunTraLedActivityChrono = millis();
//------------------------------------- Stepper (mpap)
#define mpapMicroSteps 1 // MPAP 1 = pas de micro pas
#define mpapDirPin 7 // Driver A4988 DIR
#define mpapStepPin 8 // Driver A4988 STEP
#define mpapEnaPin 9 // Driver A4988 ENA
#include "A4988.h" // https://www.arduinolibraries.info/libraries/stepper-driver
A4988 mpap(mpapStepsTour, mpapDirPin, mpapStepPin);
struct mpapParamDef // Objet motaur pas à pas
{long posSteps; unsigned long pasTotal; boolean reverse;};
mpapParamDef mpapPar;
//------------------------------------- RTC
RTC_DS3231 rtc;
const char* nomDesJours[] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
void erreurFatale(const __FlashStringHelper *message)
{
Serial.print(F("ERREUR: ")); Serial.println(message);
while (true); // on meurt là
}
void afficherSurDeuxChiffres(uint16_t nombre)
{
if (nombre < 10) Serial.write('0');
Serial.print(nombre);
}
inline double RADIANS(double a)
{
return a * (M_PI / 180.0);
}
inline double DEGRES(double a)
{
return a * (180.0 / M_PI);
}
void calculAstro(double latitude, /* Latitude (positive vers le Nord) */
double longitude, /* Longitude (positive vers l'Est */
int8_t deltaTimeZone, /* Time Zone (positive vers l'Est) */
uint32_t nombreDeJours, /* nombre de jours depuis le 1er Janvier 1900 */
uint32_t nbSec /* nombre de secondes écoulées depuis minuit */)
{
double heureLocale = ((double) nbSec) / (24.0 * 3600.0); // fraction de jour
double jourJulien = 2415018.5 + (double) nombreDeJours + (double) heureLocale - ((double) deltaTimeZone) / 24.0;
double siecleJulien = (jourJulien - 2451545L) / 36525L;
double geomMeanLong = fmod(280.46646 + siecleJulien * (36000.76983 + siecleJulien * 0.0003032), 360);
double geomMeanAnom = 357.52911 + siecleJulien * (35999.05029 - 0.0001537 * siecleJulien);
double eccentEarthOrbit = 0.016708634 - siecleJulien * (0.000042037 + 0.0000001267 * siecleJulien);
double sunEquationOfCtr = sin(RADIANS(geomMeanAnom)) * (1.914602 - siecleJulien * (0.004817 + 0.000014 * siecleJulien)) + sin(RADIANS(2 * geomMeanAnom)) * (0.019993 - 0.000101 * siecleJulien) + sin(RADIANS(3 * geomMeanAnom)) * 0.000289;
double sunTrueLong = geomMeanLong + sunEquationOfCtr;
double sunAppLong = sunTrueLong - 0.00569 - 0.00478 * sin(RADIANS(125.04 - 1934.136 * siecleJulien));
double meanObliqEcliptic = 23 + (26 + ((21.448 - siecleJulien * (46.815 + siecleJulien * (0.00059 - siecleJulien * 0.001813)))) / 60) / 60;
double obliqCorrection = meanObliqEcliptic + 0.00256 * cos(RADIANS(125.04 - 1934.136 * siecleJulien));
double sunDeclin = DEGRES(asin(sin(RADIANS(obliqCorrection)) * sin(RADIANS(sunAppLong))));
double varY = tan(RADIANS(obliqCorrection / 2)) * tan(RADIANS(obliqCorrection / 2));
double equationOfTime = 4 * DEGRES(varY * sin(2 * RADIANS(geomMeanLong)) - 2 * eccentEarthOrbit * sin(RADIANS(geomMeanAnom)) + 4 * eccentEarthOrbit * varY * sin(RADIANS(geomMeanAnom)) * cos(2 * RADIANS(geomMeanLong)) - 0.5 * varY * varY * sin(4 * RADIANS(geomMeanLong)) - 1.25 * eccentEarthOrbit * eccentEarthOrbit * sin(2 * RADIANS(geomMeanAnom)));
double trueSolarTime = fmod(heureLocale * 1440 + equationOfTime + 4 * longitude - 60 * deltaTimeZone, 1440);
double hourAngle = (trueSolarTime / 4 < 0) ? (trueSolarTime / 4 + 180) : (trueSolarTime / 4 - 180);
double solarZenithAngle = DEGRES(acos(sin(RADIANS(latitude)) * sin(RADIANS(sunDeclin)) + cos(RADIANS(latitude)) * cos(RADIANS(sunDeclin)) * cos(RADIANS(hourAngle))));
double solarElevationAngle = 90 - solarZenithAngle;
double approxAtmosphericRefractionAngle ;
if (solarElevationAngle > 85) approxAtmosphericRefractionAngle = 0;
else if (solarElevationAngle > 5)
approxAtmosphericRefractionAngle = 58.1 / tan(RADIANS(solarElevationAngle)) - 0.07 / pow(tan(RADIANS(solarElevationAngle)), 3) + 0.000086 / pow(tan(RADIANS(solarElevationAngle)), 5);
else if (solarElevationAngle > -0.575) approxAtmosphericRefractionAngle = 1735 + solarElevationAngle * (-518.2 + solarElevationAngle * (103.4 + solarElevationAngle * (-12.79 + solarElevationAngle * 0.711)));
else approxAtmosphericRefractionAngle = -20.772 / tan(RADIANS(solarElevationAngle));
approxAtmosphericRefractionAngle /= 3600.0;
double finalSolarElevation = solarElevationAngle + approxAtmosphericRefractionAngle;
double finalAzimutAngle = (hourAngle > 0) ?
fmod(DEGRES(acos(((sin(RADIANS(latitude)) * cos(RADIANS(solarZenithAngle))) - sin(RADIANS(sunDeclin))) / (cos(RADIANS(latitude)) * sin(RADIANS(solarZenithAngle))))) + 180, 360) :
fmod(540 - DEGRES(acos(((sin(RADIANS(latitude)) * cos(RADIANS(solarZenithAngle))) - sin(RADIANS(sunDeclin))) / (cos(RADIANS(latitude)) * sin(RADIANS(solarZenithAngle))))), 360);
Serial.print(nbSec / 3600UL);
Serial.write(':');
afficherSurDeuxChiffres((nbSec % 3600UL) / 60);
Serial.write('\t');
Serial.print(finalAzimutAngle, 1);
Serial.print("°\t");
Serial.print(finalSolarElevation, 1);
Serial.print("°\n");
}
//------------------------------------- Demo
#define demoStrapPin 5 // Pont pour mode demo
struct demoTimeDef
{int year; int month; int day; int hour;};
demoTimeDef demoTime;
boolean demoMode = false;
unsigned long demoTempoStart = millis();
int demoHeure = 0;
void setup(){
{
pinMode(pot1, INPUT);
pinMode(pot2, INPUT);
lcd.begin(16, 2);
}
Serial.begin(115200);
delay(500);
{
uint32_t nombreDeJours; // nombre de jours depuis le 1er Janvier 1900
Serial.begin(115200);
if (! rtc.begin()) erreurFatale(F("Horloge RTC introuvable"));
// IDEALEMENT UTILISER L'HEURE UTC ET PAS L'HEURE LOCALE
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // à faire UNE FOIS si votre RTC n'est pas à jour pour la mettre à l'heure sinon commenter.
// rtc.adjust(DateTime(2020, 1, 2, 10, 30, 0)); // ou pour essayer une date précise, ici le 2 janvier à 10h30
DateTime maintenant = rtc.now(); // l'heure actuelle
DateTime premierJanvier2020(2020, 1, 1, 0, 0, 0); // point de référence 1er Janvier 2020 à 0h00 UTC
TimeSpan depuisLePremierJanvier2020 = maintenant - premierJanvier2020 ; // la différence de temps entre maintenant et le 1/1/1900
nombreDeJours = 43831UL + depuisLePremierJanvier2020.days() ; // + 43831 jours pour compter à partir de 1/1/1900
Serial.print(nomDesJours[maintenant.dayOfTheWeek()]);
Serial.write(' ');
Serial.print(maintenant.day());
Serial.write('/');
Serial.print(maintenant.month());
Serial.write('/');
Serial.print(maintenant.year());
Serial.write('\t');
Serial.print(maintenant.hour());
Serial.write(':');
afficherSurDeuxChiffres(maintenant.minute());
Serial.write(':');
afficherSurDeuxChiffres(maintenant.second());
Serial.print(F("\nJour N° ")); Serial.println(nombreDeJours);
Serial.println("\nHeure\tAzimut\tElevation");
for (uint32_t moment = 22500; moment <= 79200UL; moment += 900UL) { // de 6h15 à 22h00
calculAstro(latitude, longitude, deltaTimeZone, nombreDeJours, moment); // toutes les 15 minutes
}
}
progInitialisation();
sunTra.date = "";
}
void loop(){
{
delay(60000);
float Ax = analogRead(pot1);
float Ay = analogRead(pot2);
/*
Nous utilisons -1000 et 1000 au lieu de -1 et 1 car la fonction map() ne travail qu'avec des nombres entiers.
268 et 404 sont les valeur de tension de sortie minimale et maximale de l'accéléromètre en x. (de même pour 271 et 406 en y)
*/
float AX = map(Ax, 268, 404, -1000, 1000);
float AY = map(Ay, 271, 406, -1000, 1000);
//*180/PI pour convertir les radians en degré
float I = atan(AX/AY)*180/PI;
//Affiche la valeur de l'angle en degré
Serial.print("I: ");
Serial.println(I);
}
{
if (sunTra.date != rtcGetDate()) // Si la date est changee
sunTra.date = rtcGetDate(); // On recupere la date de la RTC auformat sunTra
// sdSunFileGetDateData(sunTra.date); // Recherche dxes donnees du jour et mise en tableau des donnees ????????????????????
// elevation (posElevation[24]) du jour sunTra.date
Serial.print(F("\n--- ")); Serial.print(sunTra.date); Serial.println(F(" ---"));
}
if (sunTra.heure != rtcGetHour()) // Si l'heure a change
{
sunTra.heure = rtcGetHour();
sunTraDayGotoPositionHeure(sunTra.heure);
}
//--------------------------------- Desactivation du MPAP quand pas utilise
if (mpapEnaTimoutChrono != 0 && (millis()- mpapEnaTimoutChrono >= mpapEnaTimout)) // Si plus d'activite du MPAP
{
digitalWrite(mpapEnaPin, !mpapEnaPinStatus);
}
//--------------------------------- LED activite
if (millis() - sunTraLedActivityChrono >= 10000) // Toutes les 10 secondes
{
digitalWrite(sunTraLedActivityPin, HIGH);
delay(10);
digitalWrite(sunTraLedActivityPin, LOW);
sunTraLedActivityChrono = millis();
}
//--------------------------------- Demo
if (demoMode)
{
if (millis() - demoTempoStart >= demoTempo) // Toutes les secondes, 1 heure
{
// demoIncrementHour(1);
demoTempoStart = millis();
}
}
}
//===================================== Initialisation
void progInitialisation()
{
//--------------------------------- MPAP
mpap.begin(mpapRpm, mpapMicroSteps);
mpapPar.pasTotal = ((mpapVisLongMm * 100) / mpapVisPasMm100) * mpapStepsTour;
mpapPar.posSteps = 0;
mpapPar.reverse = mpapReverse;
pinMode(mpapEnaPin, OUTPUT);
digitalWrite(mpapEnaPin, !mpapEnaPinStatus);
//--------------------------------- sunTra
sunTra.elevationMin = sunTraElevationMin;
sunTra.elevationMax = sunTraElevationMax;
long moveTotalMM100 = (sunTra.elevationMax * 100) - (sunTra.elevationMin * 100);
sunTra.sunTraSteps = (float)moveTotalMM100 / (float)mpapPar.pasTotal;
sunTra.heure = 0;
pinMode(sunTraLedActivityPin, OUTPUT);
digitalWrite(sunTraLedActivityPin, LOW);
}
//===================================== Sun trace
void sunTraDayGotoPositionHeure(int heurePosition)
{
Serial.print(F("Heure\t")); Serial.print(heurePosition); //+
Serial.print(F("\tElevation\t")); Serial.print(sunTra.posElevation[heurePosition]);
mpapGotoPos(sunTra.posElevation[heurePosition]);
if (demoMode && sunTra.posElevation[heurePosition] != 0.00)
{
delay(4000); // Pour le demo
}
}
//===================================== MPAP
void mpapGotoPos(float newPosition)
{
boolean moveStepsIfNegatif = false;
long sunTraSteps = (newPosition * 100.0)/sunTra.sunTraSteps;
long moveSteps = sunTraSteps - mpapPar.posSteps;
if (moveSteps < 0)
{
moveStepsIfNegatif = true;
}
Serial.print(F("\tMPAP move ")); Serial.println(moveSteps);
if (moveSteps != 0)
{
mpapMove(moveSteps);
}
mpapPar.posSteps = sunTraSteps;
}
void mpapMove(long stepsNbr)
{
if (mpapPar.reverse)
{
stepsNbr = -stepsNbr;
}
digitalWrite(mpapEnaPin, mpapEnaPinStatus);
mpap.move(stepsNbr);
mpapEnaTimoutChrono = millis(); // Redemarrer timout
}
//===================================== SD
//------------------------------------- Recherche les donnees pour une date (2020-07-11 par ex.) = "" si pas trouve
//===================================== RTC
String rtcGetDate()
{
DateTime nowDate = rtc.now();
if (!demoMode)
{
return String(nowDate.year()) + "-" + zeroPadding(nowDate.month(), 2) + "-" + zeroPadding(nowDate.day(), 2);
}
else
{
return String(demoTime.year) + "-" + zeroPadding(demoTime.month, 2) + "-" + zeroPadding(demoTime.day, 2);
}
}
int rtcGetHour()
{
if (!demoMode)
{
DateTime nowDate = rtc.now();
return nowDate.hour();
}
else
{
return demoTime.hour;
}
}
//===================================== Demo
void demoInitialisation()
{
pinMode(demoStrapPin, INPUT_PULLUP);
DateTime nowDate = rtc.now();
demoTime.year = nowDate.year();
demoTime.month = nowDate.month();
demoTime.day = nowDate.day();
if (digitalRead(demoStrapPin) == LOW)
{demoMode = true;}
else
{demoMode = false;}
}
void demoIncrementHour(int incrHour)
{
demoTime.hour += incrHour;
if (demoTime.hour > 23)
{
demoTime.hour = demoTime.hour- 24;
demoTime.day ++;
}
if (demoTime.day > 28)
{
demoTime.month ++;
demoTime.day = demoTime.day -28;
}
if (demoTime.month > 12)
{
demoTime.month = 1;
}
DateTime nowDate = rtc.now();
demoTime.year = nowDate.year();
}
//===================================== Outils
String zeroPadding(int padValue, byte padSize)
{
String retVal = "000000" + String(padValue);
retVal = retVal.substring(retVal.length()-padSize);
return retVal;
} |
Partager