Skip to content

Commit

Permalink
Refactor GPS code to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
amandel committed Jan 8, 2021
1 parent 553e6c8 commit ed7c9d5
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 193 deletions.
91 changes: 22 additions & 69 deletions src/OpenBikeSensorFirmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Config config;
SSD1306DisplayDevice* displayTest;
HCSR04SensorManager* sensorManager;
BluetoothManager* bluetoothManager;

Gps gps;

const long BLUETOOTH_INTERVAL_MILLIS = 200;
long lastBluetoothInterval = 0;

Expand Down Expand Up @@ -184,11 +187,7 @@ void setup() {
config.sensorOffsets[RIGHT_SENSOR_ID]);
displayTest->showTextOnGrid(2, 1, buffer,DEFAULT_FONT);


//##############################################################
// GPS
//##############################################################
SerialGPS.begin(9600, SERIAL_8N1, 16, 17);
gps.begin();

//##############################################################
// Handle SD
Expand Down Expand Up @@ -282,11 +281,9 @@ void setup() {
//##############################################################

displayTest->showTextOnGrid(2, 4, "Wait for GPS",DEFAULT_FONT);
Serial.println("Waiting for GPS fix...");
bool validGPSData = false;
readGPSData();
gps.handle();
voltageMeter = new VoltageMeter; // takes a moment, so do it here
readGPSData();
gps.handle();

//##############################################################
// Temperatur Sensor BMP280
Expand All @@ -313,83 +310,37 @@ void setup() {
ESP_ERROR_CHECK_WITHOUT_ABORT(
esp_bt_mem_release(ESP_BT_MODE_BTDM)); // no bluetooth at all here.
}
readGPSData();
int gpsWaitFor = cfg.getProperty<int>(ObsConfig::PROPERTY_GPS_FIX);
while (!validGPSData) {
readGPSData();

switch (gpsWaitFor) {
case GPS::FIX_POS:
validGPSData = gps.sentencesWithFix() > 0;
if (validGPSData) {
Serial.println("Got location...");
displayTest->showTextOnGrid(2, 4, "Got location",DEFAULT_FONT);
}
break;
case GPS::FIX_TIME:
validGPSData = gps.time.isValid()
&& !(gps.time.second() == 00 && gps.time.minute() == 00 && gps.time.hour() == 00);
if (validGPSData) {
Serial.println("Got time...");
displayTest->showTextOnGrid(2, 4, "Got time",DEFAULT_FONT);
}
break;
case GPS::FIX_NO_WAIT:
validGPSData = true;
if (validGPSData) {
Serial.println("GPS, no wait");
displayTest->showTextOnGrid(2, 4, "GPS, no wait",DEFAULT_FONT);
}
break;
default:
validGPSData = gps.satellites.value() >= gpsWaitFor;
if (validGPSData) {
Serial.println("Got required number of satellites...");
}
break;
}

Serial.println("Waiting for GPS fix...");
gps.handle();
int gpsWaitFor = cfg.getProperty<int>(ObsConfig::PROPERTY_GPS_FIX);
while (!gps.hasState(gpsWaitFor, displayTest)) {
currentTimeMillis = millis();
if (bluetoothManager
&& lastBluetoothInterval != (currentTimeMillis / BLUETOOTH_INTERVAL_MILLIS)) {
lastBluetoothInterval = currentTimeMillis / BLUETOOTH_INTERVAL_MILLIS;
bluetoothManager->newSensorValues(currentTimeMillis, MAX_SENSOR_VALUE, MAX_SENSOR_VALUE);
}

delay(50);

String satellitesString;
if (gps.passedChecksum() == 0) { // could not get any valid char from GPS module
satellitesString = "OFF?";
} else if (!gps.time.isValid()
|| (gps.time.second() == 00 && gps.time.minute() == 00 && gps.time.hour() == 00)) {
satellitesString = "no time";
} else {
char timeStr[32];
snprintf(timeStr, sizeof(timeStr), "%02d:%02d:%02d %dsa",
gps.time.hour(), gps.time.minute(), gps.time.second(), gps.satellites.value());
satellitesString = String(timeStr);
}
displayTest->showTextOnGrid(2, 5, satellitesString,DEFAULT_FONT);
gps.showWaitStatus(displayTest);

buttonState = digitalRead(PushButton_PIN);
if (buttonState == HIGH
|| (config.simRaMode && gps.passedChecksum() == 0) // no module && simRaMode
|| (config.simRaMode && !gps.moduleIsAlive()) // no module && simRaMode
) {
Serial.println("Skipped get GPS...");
log_d("Skipped get GPS...");
displayTest->showTextOnGrid(2, 5, "...skipped",DEFAULT_FONT);
break;
}
}

delay(1000); // Added for user experience

// Clear the display once!
displayTest->clear();
}

void serverLoop() {
readGPSData();
gps.handle();
server.handleClient();
ArduinoOTA.handle();
sensorManager->getDistancesNoWait();
Expand Down Expand Up @@ -428,22 +379,22 @@ void loop() {
auto* currentSet = new DataSet;
//specify which sensors value can be confirmed by pressing the button, should be configurable
const uint8_t confirmationSensorID = LEFT_SENSOR_ID;
readGPSData(); // needs <=1ms
gps.handle(); // needs <=1ms

currentTimeMillis = millis();
if (startTimeMillis == 0) {
startTimeMillis = (currentTimeMillis / measureInterval) * measureInterval;
}
currentSet->time = currentTime();
currentSet->time = gps.currentTime();
currentSet->millis = currentTimeMillis;
currentSet->location = gps.location;
currentSet->altitude = gps.altitude;
currentSet->course = gps.course;
currentSet->speed = gps.speed;
currentSet->hdop = gps.hdop;
currentSet->validSatellites = gps.satellites.isValid() ? (uint8_t) gps.satellites.value() : 0;
currentSet->validSatellites = gps.getValidSatellites();
currentSet->batteryLevel = voltageMeter->read();
currentSet->isInsidePrivacyArea = isInsidePrivacyArea(currentSet->location);
currentSet->isInsidePrivacyArea = gps.isInsidePrivacyArea();

sensorManager->reset();

Expand All @@ -462,7 +413,7 @@ void loop() {

currentTimeMillis = millis();
sensorManager->getDistances();
readGPSData();
gps.handle();

displayTest->showValues(
sensorManager->m_sensors[LEFT_SENSOR_ID],
Expand All @@ -471,7 +422,9 @@ void loop() {
BatteryValue,
(int16_t) TemperatureValue,
lastMeasurements,
currentSet->isInsidePrivacyArea
currentSet->isInsidePrivacyArea,
gps.getSpeed(),
gps.getValidSatellites()
);


Expand Down
31 changes: 12 additions & 19 deletions src/configServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <uploader.h>
#include "SPIFFS.h"

extern std::vector<String> gpsMessages;

static const char *const HTML_ENTITY_FAILED_CROSS = "&#x274C;";
static const char *const HTML_ENTITY_OK_MARK = "&#x2705;";

Expand Down Expand Up @@ -564,13 +562,7 @@ void aboutPage() {
page += keyValue("GPS time", gps.time.value()); // fill all digits?
page += keyValue("GPS date", gps.date.value()); // fill all digits?
page += keyValue("GPS hdop", gps.hdop.value()); // fill all digits?

String theGpsMessage = "";
for (String msg : gpsMessages) {
theGpsMessage += "<br/>";
theGpsMessage += msg;
}
page += keyValue("GPS messages", theGpsMessage);
page += keyValue("GPS messages", gps.getMessages());

page += "<h3>Display / Button</h3>";
page += keyValue("Button State", digitalRead(PushButton_PIN));
Expand All @@ -594,7 +586,7 @@ void privacyAction() {
if ( (latitude != "") && (longitude != "") && (radius != "") ) {
Serial.println(F("Valid privacyArea!"));
theObsConfig->addPrivacyArea(0,
newPrivacyArea(atof(latitude.c_str()), atof(longitude.c_str()), atoi(radius.c_str())));
Gps::newPrivacyArea(atof(latitude.c_str()), atof(longitude.c_str()), atoi(radius.c_str())));
}

String s = "<meta http-equiv='refresh' content='0; url=/settings/privacy'><a href='/settings/privacy'>Go Back</a>";
Expand Down Expand Up @@ -642,7 +634,7 @@ bool CreateWifiSoftAP(String chipID) {
void startServer(ObsConfig *obsConfig) {
theObsConfig = obsConfig;

readGPSData();
gps.handle();
uint64_t chipid_num;
chipid_num = ESP.getEfuseMac();
esp_chipid = String((uint16_t)(chipid_num >> 32), HEX);
Expand Down Expand Up @@ -681,13 +673,13 @@ void startServer(ObsConfig *obsConfig) {
}
/*return index page which is stored in serverIndex */

readGPSData();
gps.handle();
ObsUtils::setClockByNtp(WiFi.gatewayIP().toString().c_str());
readGPSData();
gps.handle();
if (!voltageMeter) {
voltageMeter = new VoltageMeter();
}
readGPSData();
gps.handle();

// #############################################
// Handle web pages
Expand Down Expand Up @@ -946,13 +938,14 @@ void startServer(ObsConfig *obsConfig) {
bool validGPSData = false;
buttonState = digitalRead(PushButton_PIN);
while (!validGPSData && (buttonState == LOW)) {
Serial.println("GPSData not valid");
log_d("GPSData not valid");
buttonState = digitalRead(PushButton_PIN);
readGPSData();
gps.handle();
validGPSData = gps.location.isValid();
if (validGPSData) {
Serial.println("GPSData valid");
newPrivacyArea(gps.location.lat(), gps.location.lng(), 500);
log_d("GPSData valid");
// FIXME: Not used?
Gps::newPrivacyArea(gps.location.lat(), gps.location.lng(), 500);
}
delay(300);
}
Expand All @@ -978,7 +971,7 @@ void startServer(ObsConfig *obsConfig) {
}

privacyPage += "<h3>New Privacy Area <a href='javascript:window.location.reload()'>&#8635;</a></h3>";
readGPSData();
gps.handle();
bool validGPSData = gps.location.isValid();
if (validGPSData) {
privacyPage += "Latitude<input name='newlatitude' value='" + String(gps.location.lat(), 7) + "' />";
Expand Down
16 changes: 6 additions & 10 deletions src/displays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ void SSD1306DisplayDevice::showNumButtonPressed() {

void SSD1306DisplayDevice::showValues(
HCSR04SensorInfo sensor1, HCSR04SensorInfo sensor2, uint16_t minDistanceToConfirm, int16_t BatterieVolt,
int16_t TemperaturValue, int lastMeasurements, boolean insidePrivacyArea) {
int16_t TemperaturValue, int lastMeasurements, boolean insidePrivacyArea,
double speed, uint8_t satellites) {
// Show sensor1, when DisplaySimple or DisplayLeft is configured
if (config.displayConfig & DisplaySimple || config.displayConfig & DisplayLeft) {
uint16_t value1 = sensor1.minDistance;
Expand Down Expand Up @@ -93,16 +94,12 @@ void SSD1306DisplayDevice::showValues(
} else {
// Show GPS info, when DisplaySatellites is configured
if (config.displayConfig & DisplaySatellites) {
showGPS();
showGPS(satellites);
}

// Show velocity, when DisplayVelocity is configured
if (config.displayConfig & DisplayVelocity) {
if (gps.speed.age() < 2000) {
showVelocity(gps.speed.kmph());
} else {
showVelocity(-1);
}
showSpeed(speed);
}


Expand All @@ -123,8 +120,7 @@ void SSD1306DisplayDevice::showValues(

}

void SSD1306DisplayDevice::showGPS() {
int sats = gps.satellites.value();
void SSD1306DisplayDevice::showGPS(uint8_t sats) {
String val = String(sats);
if (sats <= 9) {
val = "0" + val;
Expand Down Expand Up @@ -204,7 +200,7 @@ void SSD1306DisplayDevice::showTemperatureValue(int16_t input_val){
this->showTextOnGrid(1, 0, " " + val + "°C", Dialog_plain_8,-3,0);
}

void SSD1306DisplayDevice::showVelocity(double velocity) {
void SSD1306DisplayDevice::showSpeed(double velocity) {
const int bufSize = 4;
char buffer[bufSize];
if (velocity >= 0) {
Expand Down
7 changes: 4 additions & 3 deletions src/displays.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ class SSD1306DisplayDevice : public DisplayDevice {

// TODO: Move to the logic, since this is only the basic "display" class

void showGPS();
void showGPS(uint8_t sats);

void showVelocity(double velocity);
void showSpeed(double velocity);

void showBatterieValue(int16_t input_val);

Expand All @@ -270,7 +270,8 @@ class SSD1306DisplayDevice : public DisplayDevice {

void showValues(
HCSR04SensorInfo sensor1, HCSR04SensorInfo sensor2,
uint16_t minDistanceToConfirm,int16_t BatterieVolt, int16_t TemperaturValue, int lastMeasurements, boolean insidePrivacyArea);
uint16_t minDistanceToConfirm,int16_t BatterieVolt, int16_t TemperaturValue,
int lastMeasurements, boolean insidePrivacyArea, double speed, uint8_t satellites);

};

Expand Down
3 changes: 3 additions & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ extern HCSR04SensorManager* sensorManager;

extern VoltageMeter* voltageMeter;

class Gps;
extern Gps gps;

extern String esp_chipid;

extern const uint32_t MAX_DURATION_MICRO_SEC;
Expand Down
Loading

0 comments on commit ed7c9d5

Please sign in to comment.