|
|
|
@ -33,12 +33,20 @@ const int8_t I2C_SCL = 4;
|
|
|
|
|
const float wheelDiameterInches = 20; |
|
|
|
|
const int numImpulsesPerTurn = 2; |
|
|
|
|
const float wheelCircumferenceMeters = wheelDiameterInches * 0.0254f * 3.1415f / (float)numImpulsesPerTurn; |
|
|
|
|
const uint32_t wheelCircumferenceMillimeters = (uint32_t)(wheelCircumferenceMeters * 1000.0f + 0.5f); |
|
|
|
|
|
|
|
|
|
uint16_t batteryVoltage = 0; // in mV
|
|
|
|
|
uint16_t batteryOutputCurrent = 0; // in mV
|
|
|
|
|
int16_t temperature = 0; // in tenth of °C
|
|
|
|
|
int32_t altitude = 0; // in mm above sea level (can be negative if below sea level, or depending on atmospheric conditions)
|
|
|
|
|
|
|
|
|
|
// current trip
|
|
|
|
|
uint32_t tripDistance = 0; // in meters
|
|
|
|
|
uint16_t tripMovingTime = 0; // cumulated seconds, only when moving at non-zero speed
|
|
|
|
|
uint16_t tripTotalTime = 0; // total trip time in seconds
|
|
|
|
|
uint32_t tripAscendingElevation = 0; // cumulated ascending elevation, in millimeters
|
|
|
|
|
uint32_t tripMotorEnergy = 0; // in Joules
|
|
|
|
|
|
|
|
|
|
WiFiMulti wifiMulti; |
|
|
|
|
wl_status_t wifi_STA_status = WL_NO_SHIELD; |
|
|
|
|
unsigned long wifiConnexionBegin = 0; |
|
|
|
@ -52,6 +60,7 @@ volatile bool speedSensorState = false;
|
|
|
|
|
volatile unsigned long speedSensorRiseTime = 0; |
|
|
|
|
volatile unsigned long speedSensorLastImpulseTime = 0; |
|
|
|
|
volatile unsigned long speedSensorLastImpulseInterval = (unsigned long)-1; // in milliseconds
|
|
|
|
|
volatile uint32_t speedSensorDistance = 0; // Cumulated measured distance, in millimeters. This value will overflow after about 4000km.
|
|
|
|
|
void IRAM_ATTR onSpeedSensorChange(bool newState) |
|
|
|
|
{ |
|
|
|
|
if(speedSensorState == newState) return; |
|
|
|
@ -78,7 +87,11 @@ void IRAM_ATTR onSpeedSensorChange(bool newState)
|
|
|
|
|
{ |
|
|
|
|
// too little time between impulses, probably some bouncing, ignore it
|
|
|
|
|
} |
|
|
|
|
else if(timeSinceLastImpulse < 4000) |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
speedSensorDistance += wheelCircumferenceMillimeters; |
|
|
|
|
|
|
|
|
|
if(timeSinceLastImpulse < 4000) |
|
|
|
|
{ |
|
|
|
|
speedSensorLastImpulseTime = now; |
|
|
|
|
speedSensorLastImpulseInterval = timeSinceLastImpulse; |
|
|
|
@ -91,6 +104,7 @@ void IRAM_ATTR onSpeedSensorChange(bool newState)
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void IRAM_ATTR onSpeedSensorChange() { onSpeedSensorChange(digitalRead(speedSensorPin) == HIGH); } |
|
|
|
|
|
|
|
|
@ -200,17 +214,23 @@ void setup()
|
|
|
|
|
int v = batteryVoltage; |
|
|
|
|
int c = batteryOutputCurrent; |
|
|
|
|
int s = (int)(getSpeed() * 1000.0f + 0.5f); |
|
|
|
|
int t = temperature; |
|
|
|
|
int temp = temperature; |
|
|
|
|
int alt = altitude; |
|
|
|
|
|
|
|
|
|
int td = tripDistance; |
|
|
|
|
int ttt = tripTotalTime; |
|
|
|
|
int tmt = tripMovingTime; |
|
|
|
|
int tae = tripAscendingElevation / 100; // convert mm to dm
|
|
|
|
|
int tme = tripMotorEnergy / 360; // convert Joules to dWh (tenth of Wh)
|
|
|
|
|
|
|
|
|
|
const char* logFileName = DataLogger::get().currentLogFileName(); |
|
|
|
|
if(String(logFileName).startsWith("/log/")) logFileName += 5; |
|
|
|
|
|
|
|
|
|
int totalSize = (int)(SPIFFS.totalBytes() / 1000); |
|
|
|
|
int usedSize = (int)(SPIFFS.usedBytes() / 1000); |
|
|
|
|
|
|
|
|
|
char json[128]; |
|
|
|
|
sprintf(json, "{\"v\":%d,\"c\":%d,\"s\":%d,\"t\":%d,\"alt\":%d,\"log\":\"%s\",\"tot\":%d,\"used\":%d}", v, c, s, t, alt, logFileName, totalSize, usedSize); |
|
|
|
|
char json[256]; |
|
|
|
|
sprintf(json, "{\"v\":%d,\"c\":%d,\"s\":%d,\"td\":%d,\"ttt\":%d,\"tmt\":%d,\"tae\":%d,\"tme\":%d,\"temp\":%d,\"alt\":%d,\"log\":\"%s\",\"tot\":%d,\"used\":%d}", v, c, s, td, ttt, tmt, tae, tme, temp, alt, logFileName, totalSize, usedSize); |
|
|
|
|
request->send(200, "text/json", json); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
@ -399,6 +419,10 @@ void loop()
|
|
|
|
|
handle_pressure_measure(); // also measures temperature
|
|
|
|
|
|
|
|
|
|
unsigned long now = millis(); |
|
|
|
|
static unsigned long lastLoopMillis = now; |
|
|
|
|
unsigned long dt = utils::elapsed(lastLoopMillis, now); |
|
|
|
|
lastLoopMillis = now; |
|
|
|
|
|
|
|
|
|
static DataLogger::Entry entry; |
|
|
|
|
entry.batteryVoltage = (float)batteryVoltage / 1000.0f; |
|
|
|
|
entry.batteryOutputCurrent = (float)batteryOutputCurrent / 1000.0f; |
|
|
|
@ -413,6 +437,11 @@ void loop()
|
|
|
|
|
{ |
|
|
|
|
DebugLog.println("Starting DataLogger"); |
|
|
|
|
DataLogger::get().open(); |
|
|
|
|
tripDistance = 0; |
|
|
|
|
tripMovingTime = 0; |
|
|
|
|
tripTotalTime = 0; |
|
|
|
|
tripAscendingElevation = 0; |
|
|
|
|
tripMotorEnergy = 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
@ -432,5 +461,41 @@ void loop()
|
|
|
|
|
} |
|
|
|
|
DataLogger::get().log(now, entry); |
|
|
|
|
|
|
|
|
|
delay(DataLogger::get().isOpen() ? 10 : 1000); |
|
|
|
|
bool isOnTrip = DataLogger::get().isOpen(); |
|
|
|
|
bool isMoving = entry.speed > 0.0f; |
|
|
|
|
|
|
|
|
|
static unsigned long cumulatedMillis = 0; |
|
|
|
|
cumulatedMillis += dt; |
|
|
|
|
unsigned long newSeconds = cumulatedMillis / 1000; |
|
|
|
|
cumulatedMillis -= newSeconds * 1000; |
|
|
|
|
|
|
|
|
|
uint32_t currentMillimeters = speedSensorDistance; |
|
|
|
|
static uint32_t lastLoopMillimeters = currentMillimeters; |
|
|
|
|
uint32_t newMillimeters = utils::elapsed(lastLoopMillimeters, currentMillimeters); |
|
|
|
|
lastLoopMillimeters = currentMillimeters; |
|
|
|
|
|
|
|
|
|
static uint32_t cumulatedMillimeters = 0; |
|
|
|
|
cumulatedMillimeters += newMillimeters; |
|
|
|
|
uint32_t newMeters = cumulatedMillimeters / 1000; |
|
|
|
|
cumulatedMillimeters -= newMeters * 1000; |
|
|
|
|
|
|
|
|
|
uint32_t altitudeMillimeters = (uint32_t)(entry.altitude * 1000.0f + 0.5f); |
|
|
|
|
static uint32_t lastLoopAltitude = altitudeMillimeters; |
|
|
|
|
uint32_t altitudeChange = altitudeMillimeters - lastLoopAltitude; |
|
|
|
|
lastLoopAltitude = altitudeMillimeters; |
|
|
|
|
|
|
|
|
|
if(isOnTrip) |
|
|
|
|
{ |
|
|
|
|
tripTotalTime += newSeconds; |
|
|
|
|
if(isMoving) tripMovingTime += newSeconds; |
|
|
|
|
tripDistance += newMeters; |
|
|
|
|
|
|
|
|
|
if(altitudeChange > 0) |
|
|
|
|
tripAscendingElevation += altitudeChange; |
|
|
|
|
|
|
|
|
|
uint32_t newEnergy = entry.batteryVoltage * entry.batteryOutputCurrent * ((float)dt / 1000.0f); |
|
|
|
|
tripMotorEnergy += newEnergy; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
delay(isOnTrip ? 10 : 1000); |
|
|
|
|
} |
|
|
|
|