Skip to content

Commit

Permalink
using wait time on Temp sensor to gain more Accelerator samples
Browse files Browse the repository at this point in the history
  • Loading branch information
universam1 committed Aug 23, 2018
1 parent 92a9f77 commit 58cb816
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
24 changes: 14 additions & 10 deletions pio/lib/Globals/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ extern Ticker flasher;
#define CONSOLELN CONSOLE
#define CONSOLEF CONSOLE
#else
#define CONSOLE(...) \
do \
{ \
#define CONSOLE(...) \
do \
{ \
Serial.print(__VA_ARGS__); \
} while (0)
#define CONSOLELN(...) \
do \
{ \
#define CONSOLELN(...) \
do \
{ \
Serial.println(__VA_ARGS__); \
} while (0)
#endif
Expand All @@ -58,13 +58,17 @@ extern Ticker flasher;

#define ADCDIVISOR 191.8
#define ONE_WIRE_BUS D6 // DS18B20 on ESP pin12
#define OW_PINS (const uint8_t[]){D1, D6}
#define RESOLUTION 12 // 12bit resolution == 750ms update rate
#define OW_PINS \
(const uint8_t[]) { D1, D6 }
#define RESOLUTION 12 // 12bit resolution == 750ms update rate
#define OWinterval (760 / (1 << (12 - RESOLUTION)))
#define CFGFILE "/config.json"
#define TKIDSIZE 40
#define MEDIANROUNDS 39
#define MEDIANAVRG 29

#define MEDIANROUNDSMAX 49
#define MEDIANROUNDSMIN 29
#define MEDIANAVRG MEDIANROUNDSMIN
#define MEDIAN_MAX_SIZE MEDIANROUNDSMAX

#define CBP_ENDPOINT "/api/hydrometer/v1/data"

Expand Down
37 changes: 24 additions & 13 deletions pio/src/iSpindel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ OneWire *oneWire;
DallasTemperature DS18B20;
DeviceAddress tempDeviceAddress;
Ticker flasher;
RunningMedian samples = RunningMedian(MEDIANROUNDS);
RunningMedian samples = RunningMedian(MEDIANROUNDSMAX);
DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS);

#define TEMP_CELSIUS 0
Expand Down Expand Up @@ -725,6 +725,11 @@ void initDS18B20()
requestTemp();
}

bool isDS18B20ready()
{
return millis() - DSreqTime > OWinterval;
}

void initAccel()
{
// join I2C bus (I2Cdev library doesn't do this automatically)
Expand Down Expand Up @@ -780,22 +785,28 @@ void getAccSample()

float getTilt()
{
uint32_t start;
uint32_t start = millis();
uint8_t i = 0;

for (uint8_t i = 0; i < MEDIANROUNDS; i++)
for (; i < MEDIANROUNDSMAX; i++)
{
start = millis();
while (!accelgyro.getIntDataReadyStatus())
yield();
CONSOLE(String("IRQ: ") + (millis() - start));
delay(2);
getAccSample();
float _tilt = calculateTilt();
CONSOLE(F("ms - Spl "));
CONSOLE(i);
CONSOLE(": ");
CONSOLELN(_tilt);
samples.add(_tilt);

if (i >= MEDIANROUNDSMIN && isDS18B20ready())
break;
}
CONSOLE("Samples:");
CONSOLE(++i);
CONSOLE(" min:");
CONSOLE(samples.getLowest());
CONSOLE(" max:");
CONSOLE(samples.getHighest());
CONSOLE(" time:");
CONSOLELN(millis() - start);
return samples.getAverage(MEDIANAVRG);
}

Expand All @@ -805,12 +816,12 @@ float getTemperature(bool block = false)

// we need to wait for DS18b20 to finish conversion
if (!DSreqTime ||
(!block && (millis() - DSreqTime < OWinterval)))
(!block && !isDS18B20ready()))
return t;

// if we need the result we have to block
while (millis() - DSreqTime < OWinterval)
yield();
while (!isDS18B20ready())
delay(10);
DSreqTime = 0;

t = DS18B20.getTempCByIndex(0);
Expand Down

0 comments on commit 58cb816

Please sign in to comment.