|
|
@ -1,5 +1,4 @@ |
|
|
|
#include <Arduino.h> |
|
|
|
#include <Arduino.h> |
|
|
|
#include "IDECompat.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <WiFi.h> |
|
|
|
#include <WiFi.h> |
|
|
|
#include <WiFiMulti.h> |
|
|
|
#include <WiFiMulti.h> |
|
|
@ -21,7 +20,7 @@ AsyncWebServer server(80); |
|
|
|
ADC currentSensor(36); |
|
|
|
ADC currentSensor(36); |
|
|
|
ADC batterySensor(39); |
|
|
|
ADC batterySensor(39); |
|
|
|
const int8_t speedSensorPin = 13; |
|
|
|
const int8_t speedSensorPin = 13; |
|
|
|
const int8_t debugLedPin = 12; |
|
|
|
const int8_t debugLedPin = 2; |
|
|
|
|
|
|
|
|
|
|
|
const float wheelDiameterInches = 20; |
|
|
|
const float wheelDiameterInches = 20; |
|
|
|
const int numImpulsesPerTurn = 2; |
|
|
|
const int numImpulsesPerTurn = 2; |
|
|
@ -58,19 +57,22 @@ void IRAM_ATTR onSpeedSensorChange(bool newState) |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned long impulseDuration = utils::elapsed(speedSensorRiseTime, now); |
|
|
|
unsigned long impulseDuration = utils::elapsed(speedSensorRiseTime, now); |
|
|
|
if(impulseDuration > 1000) return; // impulse was too long, ignore it (maybe magnet stopped near the sensor)
|
|
|
|
if(impulseDuration > 500) return; // impulse was too long, ignore it (maybe magnet stopped near the sensor)
|
|
|
|
|
|
|
|
|
|
|
|
debugLedState = !debugLedState; |
|
|
|
|
|
|
|
digitalWrite(debugLedPin, debugLedState ? HIGH : LOW); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long timeSinceLastImpulse = utils::elapsed(speedSensorLastImpulseTime, now); |
|
|
|
unsigned long timeSinceLastImpulse = utils::elapsed(speedSensorLastImpulseTime, now); |
|
|
|
speedSensorLastImpulseTime = now; |
|
|
|
if(timeSinceLastImpulse < 30) |
|
|
|
if(timeSinceLastImpulse > 30 && timeSinceLastImpulse < 4000) |
|
|
|
{ |
|
|
|
|
|
|
|
// too little time between impulses, probably some bouncing, ignore it
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if(timeSinceLastImpulse < 4000) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
speedSensorLastImpulseTime = now; |
|
|
|
speedSensorLastImpulseInterval = timeSinceLastImpulse; |
|
|
|
speedSensorLastImpulseInterval = timeSinceLastImpulse; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
// too much time between impulses, can't compute speed from that
|
|
|
|
|
|
|
|
speedSensorLastImpulseTime = now; |
|
|
|
speedSensorLastImpulseInterval = (unsigned long)-1; |
|
|
|
speedSensorLastImpulseInterval = (unsigned long)-1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -100,7 +102,10 @@ float getSpeed() |
|
|
|
|
|
|
|
|
|
|
|
float speed = wheelCircumferenceMeters / (float)interval * 1000.0f; // in meters per second
|
|
|
|
float speed = wheelCircumferenceMeters / (float)interval * 1000.0f; // in meters per second
|
|
|
|
|
|
|
|
|
|
|
|
if(speed < 0.25f) return 0.0f; // if speed is very low (less than 1km/h) it probably means we've stopped
|
|
|
|
if(speed < 0.25f) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return 0.0f; // if speed is very low (less than 1km/h) it probably means we've stopped
|
|
|
|
|
|
|
|
} |
|
|
|
return speed; |
|
|
|
return speed; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -125,12 +130,12 @@ void connectWifi() |
|
|
|
|
|
|
|
|
|
|
|
void setup() |
|
|
|
void setup() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
pinMode(debugLedPin, OUTPUT); |
|
|
|
|
|
|
|
digitalWrite(debugLedPin, HIGH); |
|
|
|
|
|
|
|
|
|
|
|
pinMode(speedSensorPin, INPUT_PULLUP); |
|
|
|
pinMode(speedSensorPin, INPUT_PULLUP); |
|
|
|
attachInterrupt(speedSensorPin, &onSpeedSensorChange, CHANGE); |
|
|
|
attachInterrupt(speedSensorPin, &onSpeedSensorChange, CHANGE); |
|
|
|
|
|
|
|
|
|
|
|
pinMode(debugLedPin, OUTPUT); |
|
|
|
|
|
|
|
digitalWrite(debugLedPin, debugLedState ? HIGH : LOW); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Serial.begin(115200); |
|
|
|
Serial.begin(115200); |
|
|
|
|
|
|
|
|
|
|
|
if(!SPIFFS.begin(false)){ |
|
|
|
if(!SPIFFS.begin(false)){ |
|
|
@ -211,6 +216,8 @@ void setup() |
|
|
|
|
|
|
|
|
|
|
|
server.begin(); |
|
|
|
server.begin(); |
|
|
|
Serial.println("HTTP server started"); |
|
|
|
Serial.println("HTTP server started"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
digitalWrite(debugLedPin, LOW); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void handle_wifi_connection() |
|
|
|
void handle_wifi_connection() |
|
|
@ -277,7 +284,7 @@ void handle_ADC_measures() |
|
|
|
if(averageV < 0.2f) averageV = 0.0f; |
|
|
|
if(averageV < 0.2f) averageV = 0.0f; |
|
|
|
|
|
|
|
|
|
|
|
averageV *= 27.000f; // account for voltage divider to retrieve the input voltage
|
|
|
|
averageV *= 27.000f; // account for voltage divider to retrieve the input voltage
|
|
|
|
averageC = max(0.0f, averageC - 2.5f) / 0.0238f; // convert voltage to current, according to the sensor linear relation
|
|
|
|
averageC = std::max(0.0f, averageC - 2.5f) / 0.0238f; // convert voltage to current, according to the sensor linear relation
|
|
|
|
|
|
|
|
|
|
|
|
batteryVoltage = (uint16_t)(averageV * 1000.0f + 0.5f); |
|
|
|
batteryVoltage = (uint16_t)(averageV * 1000.0f + 0.5f); |
|
|
|
batteryOutputCurrent = (uint16_t)(averageC * 1000.0f + 0.5f); |
|
|
|
batteryOutputCurrent = (uint16_t)(averageC * 1000.0f + 0.5f); |
|
|
@ -317,8 +324,11 @@ void loop() |
|
|
|
} |
|
|
|
} |
|
|
|
else if(utils::elapsed(stoppedSince, now) > 5 * 60 * 1000) |
|
|
|
else if(utils::elapsed(stoppedSince, now) > 5 * 60 * 1000) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Serial.println("Stopping DataLogger"); |
|
|
|
if(DataLogger::get().isOpen()) |
|
|
|
DataLogger::get().close(); |
|
|
|
{ |
|
|
|
|
|
|
|
Serial.println("Stopping DataLogger"); |
|
|
|
|
|
|
|
DataLogger::get().close(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
DataLogger::get().log(now, entry); |
|
|
|
DataLogger::get().log(now, entry); |
|
|
|