Browse Source

reorganized code a little and added servo code

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

126
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();
@ -66,14 +65,14 @@ void init_gps() {
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,50 +165,26 @@ 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;
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA()))
return;
}
// 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
// }
// }
// approximately every 2 seconds or so, print out the current stats
if (millis() - tstamp > 2000) {
tstamp = millis(); // reset the timer
float compute_magnetic_declination() {
float wmmtime = wmm_get_date(GPS.year, GPS.month, GPS.day);
sun.latitude = GPS.latitudeDegrees;
sun.longitude = GPS.longitudeDegrees;
// 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);
if (GPS.fix) {
// SolarPosition location(GPS.latitudeDegrees, GPS.longitudeDegrees);
// calculate magnetic declination
E0000(GPS.latitudeDegrees, GPS.longitudeDegrees, wmmtime, &sun.declination);
}
float compute_sun_position() {
// prepare location and time data to call the sun position API
cLocation loc;
loc.dLatitude = GPS.latitudeDegrees;
@ -220,19 +205,46 @@ void loop() {
sun.azimuth = sunloc.dAzimuth;
sun.zenith = sunloc.dZenithAngle;
sun.elevation = 90 - sun.zenith;
}
// method 2 - using SolarPosition library
// sun.elevation = location.getSolarPosition(sun.epoch).elevation;
// sun.azimuth = location.getSolarPosition(sun.epoch).azimuth;
void gps_pump() {
char c = GPS.read();
// servo.write( ceil(sun.elevation) );
// delay(20);
#ifdef DEBUG_GPS
debug print
if (c) {
Serial.write(c);
}
#endif
debug_solar();
//ui_sunpos();
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA())) {
return;
}
}
}
ui_sunpos();
void loop() {
gps_pump();
// // approximately every 2 seconds or so, print out the current stats
if (millis() - tstamp > 2000) {
tstamp = millis(); // reset the timer
if (GPS.fix) {
sun.latitude = GPS.latitudeDegrees;
sun.longitude = GPS.longitudeDegrees;
compute_magnetic_declination();
compute_sun_position();
// // servo.write( ceil(sun.elevation) );
// // delay(20);
// debug_solar();
ui_sunpos();
} else {
ui_nogpsfix();
}
}
} // loop()
Loading…
Cancel
Save