Browse Source

reorganized code a little and added servo code

master
Luis Rodil-Fernandez 2 years ago
parent
commit
16f25acc94
  1. 162
      src/main.cpp

162
src/main.cpp

@ -11,7 +11,6 @@
#include <SoftwareSerial.h>
#include <wmm.h>
// #include <SolarPosition.h>
#include <sunpos.h>
#define SCREEN_WIDTH 128
@ -24,7 +23,7 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Servo servo;
int servoPin = 13;
SoftwareSerial altserial(5, 4); // rx, tx
SoftwareSerial altserial(14, 12); // rx, tx
Adafruit_GPS GPS(&altserial);
uint32_t tstamp = millis();
@ -57,23 +56,23 @@ void init_gps() {
altserial.begin(9600);
delay(2000);
// Serial.println("Software Serial GPS Test Echo Test");
//Serial.println("Software Serial GPS Test Echo Test");
// you can send various commands to get it started
//mySerial.println(PMTK_SET_NMEA_OUTPUT_RMCGGA);
altserial.println(PMTK_SET_NMEA_OUTPUT_ALLDATA);
altserial.println(PMTK_SET_NMEA_UPDATE_1HZ);
// Serial.println("Get version!");
//Serial.println("Get version!");
altserial.println(PMTK_Q_RELEASE);
}
// void init_servo() {
// ESP32PWM::allocateTimer(0);
// ESP32PWM::allocateTimer(1);
// ESP32PWM::allocateTimer(2);
// ESP32PWM::allocateTimer(3);
// servo.setPeriodHertz(50);// Standard 50hz servo
// servo.attach(servoPin, 500, 2400);
// }
void init_servo() {
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
servo.setPeriodHertz(50);// Standard 50hz servo
servo.attach(servoPin, 500, 2400);
}
// ///////////////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////////////////
@ -86,6 +85,16 @@ void setup() {
wmm_init();
}
void ui_nogpsfix() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
//display.print(F("lat ------> "));
display.print(F("NO GPS FIX"));
display.display();
}
void ui_sunpos() {
display.clearDisplay();
@ -156,83 +165,86 @@ void debug_solar() {
// Serial.print("Antenna status: "); Serial.println((int)GPS.antenna);
}
void servo_sweep() {
int pos;
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void loop() {
//char c = GPS.read();
// debug print
//if (c) Serial.write(c);
// void servo_sweep() {
// int pos;
// for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// // in steps of 1 degree
// servo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
// }
// for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
// servo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
// }
// }
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA()))
return;
}
float compute_magnetic_declination() {
float wmmtime = wmm_get_date(GPS.year, GPS.month, GPS.day);
E0000(GPS.latitudeDegrees, GPS.longitudeDegrees, wmmtime, &sun.declination);
}
// approximately every 2 seconds or so, print out the current stats
if (millis() - tstamp > 2000) {
tstamp = millis(); // reset the timer
float wmmtime = wmm_get_date(GPS.year, GPS.month, GPS.day);
float compute_sun_position() {
// prepare location and time data to call the sun position API
cLocation loc;
loc.dLatitude = GPS.latitudeDegrees;
loc.dLongitude = GPS.longitudeDegrees;
sun.latitude = GPS.latitudeDegrees;
sun.longitude = GPS.longitudeDegrees;
cTime t;
t.dHours = GPS.hour;
t.dMinutes = GPS.minute;
t.dSeconds = GPS.seconds;
t.iDay = GPS.day;
t.iMonth = GPS.month;
t.iYear = GPS.year;
// method 2 - using SolarPosition library
// create a fixed UNIX time to test fixed time method
// tmElements_t someTime = {GPS.seconds, GPS.minute, GPS.hour, 0, GPS.day, GPS.month, CalendarYrToTm(GPS.year) };
// sun.epoch = makeTime(someTime);
cSunCoordinates sunloc;
if (GPS.fix) {
// SolarPosition location(GPS.latitudeDegrees, GPS.longitudeDegrees);
sunpos(t, loc, &sunloc);
// calculate magnetic declination
E0000(GPS.latitudeDegrees, GPS.longitudeDegrees, wmmtime, &sun.declination);
sun.azimuth = sunloc.dAzimuth;
sun.zenith = sunloc.dZenithAngle;
sun.elevation = 90 - sun.zenith;
}
// prepare location and time data to call the sun position API
cLocation loc;
loc.dLatitude = GPS.latitudeDegrees;
loc.dLongitude = GPS.longitudeDegrees;
void gps_pump() {
char c = GPS.read();
cTime t;
t.dHours = GPS.hour;
t.dMinutes = GPS.minute;
t.dSeconds = GPS.seconds;
t.iDay = GPS.day;
t.iMonth = GPS.month;
t.iYear = GPS.year;
#ifdef DEBUG_GPS
debug print
if (c) {
Serial.write(c);
}
#endif
cSunCoordinates sunloc;
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA())) {
return;
}
}
}
sunpos(t, loc, &sunloc);
void loop() {
gps_pump();
// // approximately every 2 seconds or so, print out the current stats
if (millis() - tstamp > 2000) {
tstamp = millis(); // reset the timer
sun.azimuth = sunloc.dAzimuth;
sun.zenith = sunloc.dZenithAngle;
sun.elevation = 90 - sun.zenith;
if (GPS.fix) {
sun.latitude = GPS.latitudeDegrees;
sun.longitude = GPS.longitudeDegrees;
// method 2 - using SolarPosition library
// sun.elevation = location.getSolarPosition(sun.epoch).elevation;
// sun.azimuth = location.getSolarPosition(sun.epoch).azimuth;
compute_magnetic_declination();
compute_sun_position();
// servo.write( ceil(sun.elevation) );
// delay(20);
// // servo.write( ceil(sun.elevation) );
// // delay(20);
debug_solar();
//ui_sunpos();
// debug_solar();
ui_sunpos();
} else {
ui_nogpsfix();
}
}
ui_sunpos();
} // loop()
Loading…
Cancel
Save