Skip to content

Commit 799ea30

Browse files
committed
Add rtcMillis() calculation. Fix actual read rate output for >2s logging.
1 parent f5229db commit 799ea30

File tree

4 files changed

+61
-31
lines changed

4 files changed

+61
-31
lines changed

Firmware/OpenLog_Artemis/OpenLog_Artemis.ino

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
*/
1919

20-
const float FIRMWARE_VERSION = 1.1;
20+
const float FIRMWARE_VERSION = 1.2;
2121

2222
#include "settings.h"
2323

@@ -145,7 +145,7 @@ MS8607 pressureSensor_MS8607;
145145

146146
//Global variables
147147
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
148-
unsigned long measurementStartTime; //Used to calc the actual update rate.
148+
uint64_t measurementStartTime; //Used to calc the actual update rate. Max is ~80,000,000ms in a 24 hour period.
149149
unsigned long measurementCount = 0; //Used to calc the actual update rate.
150150
String outputData;
151151
String beginSensorOutput;
@@ -158,7 +158,9 @@ const uint32_t maxUsBeforeSleep = 2000000; //Number of us between readings befor
158158
const byte menuTimeout = 45; //Menus will exit/timeout after this number of seconds
159159
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
160160

161-
unsigned long startTime = 0;
161+
//unsigned long startTime = 0;
162+
163+
#define DUMP(varname) {Serial.printf("%s: %llu\n", #varname, varname);}
162164

163165
void setup() {
164166
//If 3.3V rail drops below 3V, system will power down and maintain RTC
@@ -210,16 +212,18 @@ void setup() {
210212
if (beginSensors() == true) Serial.println(beginSensorOutput); //159 - 865ms but varies based on number of devices attached
211213
else msg("No sensors detected");
212214

213-
measurementStartTime = millis();
215+
//If we are sleeping between readings then we cannot rely on millis() as it is powered down. Used RTC instead.
216+
if (settings.usBetweenReadings >= maxUsBeforeSleep)
217+
measurementStartTime = rtcMillis();
218+
else
219+
measurementStartTime = millis();
214220

215221
//Serial.printf("Setup time: %.02f ms\n", (micros() - startTime) / 1000.0);
216222

217223
//If we are immediately going to go to sleep after the first reading then
218224
//first present the user with the config menu in case they need to change something
219225
if (settings.usBetweenReadings >= maxUsBeforeSleep)
220-
{
221-
menuMain(); //Present user menu at startup to allow configuration even in LP mode
222-
}
226+
menuMain();
223227
}
224228

225229
void loop() {

Firmware/OpenLog_Artemis/Sensors.ino

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ bool beginSensors()
212212
{
213213
if (pressureSensor_MS8607.begin(qwiic) == true) //Wire port. This checks both 0x40 and 0x76 sensor addresses
214214
{
215-
if(settings.sensor_MS8607.enableHeater == true)
215+
if (settings.sensor_MS8607.enableHeater == true)
216216
pressureSensor_MS8607.enable_heater();
217217
else
218218
pressureSensor_MS8607.disable_heater();
219-
219+
220220
pressureSensor_MS8607.set_pressure_resolution(settings.sensor_MS8607.pressureResolution);
221221
pressureSensor_MS8607.set_humidity_resolution(settings.sensor_MS8607.humidityResolution);
222222

@@ -683,10 +683,22 @@ void getData()
683683

684684
if (settings.logHertz)
685685
{
686-
//Calculate the actual update rate based on the sketch start time and the
687-
//number of updates we've completed.
688-
float actualRate = measurementCount * 1000.0 / (millis() - measurementStartTime);
686+
uint64_t currentMillis;
687+
688+
//If we are sleeping between readings then we cannot rely on millis() as it is powered down
689+
//Used RTC instead
690+
if (settings.usBetweenReadings >= maxUsBeforeSleep)
691+
{
692+
currentMillis = rtcMillis();
693+
}
694+
else
695+
{
696+
//Calculate the actual update rate based on the sketch start time and the
697+
//number of updates we've completed.
698+
currentMillis = millis();
699+
}
689700

701+
float actualRate = measurementCount * 1000.0 / (currentMillis - measurementStartTime);
690702
outputData += (String)actualRate + ","; //Hz
691703
helperText += "output_Hz,";
692704
}
@@ -717,7 +729,7 @@ void determineMaxI2CSpeed()
717729
maxSpeed = 100000;
718730

719731
//If user wants to limit the I2C bus speed, do it here
720-
if(maxSpeed > settings.qwiicBusMaxSpeed)
732+
if (maxSpeed > settings.qwiicBusMaxSpeed)
721733
maxSpeed = settings.qwiicBusMaxSpeed;
722734

723735
qwiic.setClock(maxSpeed);

Firmware/OpenLog_Artemis/lowerPower.ino

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ void powerDown()
99
detachInterrupt(digitalPinToInterrupt(PIN_POWER_LOSS)); //Prevent voltage supervisor from waking us from sleep
1010

1111
//Save files before going to sleep
12-
// if (online.dataLogging == true)
13-
// {
14-
// sensorDataFile.sync();
15-
// }
16-
// if (online.serialLogging == true)
17-
// {
18-
// serialDataFile.sync();
19-
// }
20-
12+
// if (online.dataLogging == true)
13+
// {
14+
// sensorDataFile.sync();
15+
// }
16+
// if (online.serialLogging == true)
17+
// {
18+
// serialDataFile.sync();
19+
// }
20+
2121
//Serial.flush(); //Don't waste time waiting for prints to finish
2222

2323
// Wire.end(); //Power down I2C
@@ -29,7 +29,7 @@ void powerDown()
2929

3030
Serial.end(); //Power down UART
3131
Serial1.end();
32-
32+
3333
//Force the peripherals off
3434
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM0);
3535
am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_IOM1);
@@ -49,7 +49,7 @@ void powerDown()
4949
qwiicPowerOff();
5050
imuPowerOff();
5151
microSDPowerOff();
52-
52+
5353
//Power down Flash, SRAM, cache
5454
am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_CACHE); //Turn off CACHE
5555
am_hal_pwrctrl_memory_deepsleep_powerdown(AM_HAL_PWRCTRL_MEM_FLASH_512K); //Turn off everything but lower 512k
@@ -77,12 +77,12 @@ void goToSleep()
7777
if (online.dataLogging == true)
7878
{
7979
sensorDataFile.sync();
80-
sensorDataFile.close(); //No need to close files. https://forum.arduino.cc/index.php?topic=149504.msg1125098#msg1125098
80+
//sensorDataFile.close(); //No need to close files. https://forum.arduino.cc/index.php?topic=149504.msg1125098#msg1125098
8181
}
8282
if (online.serialLogging == true)
8383
{
8484
serialDataFile.sync();
85-
serialDataFile.close();
85+
//serialDataFile.close();
8686
}
8787

8888
Serial.flush(); //Finish any prints
@@ -173,8 +173,6 @@ void wakeFromSleep()
173173
am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE);
174174
am_hal_stimer_config(AM_HAL_STIMER_HFRC_3MHZ);
175175

176-
startTime = micros();
177-
178176
//Turn on ADC
179177
ap3_adc_setup();
180178

@@ -207,8 +205,6 @@ void wakeFromSleep()
207205

208206
beginSensors(); //159 - 865ms but varies based on number of devices attached
209207

210-
measurementStartTime = millis();
211-
212208
//Serial.printf("Wake up time: %.02f ms\n", (micros() - startTime) / 1000.0);
213209

214210
//When we wake up micros has been reset to zero so we need to let the main loop know to take a reading
@@ -247,3 +243,17 @@ void imuPowerOff()
247243
pinMode(PIN_IMU_POWER, OUTPUT);
248244
digitalWrite(PIN_IMU_POWER, LOW);
249245
}
246+
247+
//Returns the number of milliseconds according to the RTC
248+
//Watch out for 24 hour roll over at 86,400,000ms
249+
uint32_t rtcMillis()
250+
{
251+
myRTC.getTime();
252+
uint32_t millisToday = 0;
253+
millisToday += (myRTC.hour * 3600000UL);
254+
millisToday += (myRTC.minute * 60000UL);
255+
millisToday += (myRTC.seconds * 1000UL);
256+
millisToday += (myRTC.hundredths * 10UL);
257+
258+
return(millisToday);
259+
}

Firmware/OpenLog_Artemis/menuMain.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ void menuMain()
7676
while (Serial.available()) Serial.read(); //Empty buffer of any newline chars
7777

7878
//Reset measurements
79-
measurementStartTime = millis(); //Update time
8079
measurementCount = 0;
8180
totalCharactersPrinted = 0;
81+
//If we are sleeping between readings then we cannot rely on millis() as it is powered down. Used RTC instead.
82+
if (settings.usBetweenReadings >= maxUsBeforeSleep)
83+
measurementStartTime = rtcMillis();
84+
else
85+
measurementStartTime = millis();
8286

8387
//Edge case: after 10Hz reading, user sets the log rate above 2s mark. We never go to sleep because
8488
//takeReading is not true. And since we don't wake up, takeReading never gets set to true.

0 commit comments

Comments
 (0)