From 9da86f0125028321c68cc6e23f94dfe58c4a49a1 Mon Sep 17 00:00:00 2001 From: Jetty 840 Date: Fri, 23 Dec 2011 18:25:05 -0700 Subject: [PATCH] Improvements to TimeLeft estimation, by using floating point. This goes with commit c5e0603 --- firmware/src/Motherboard/SDCard.cc | 27 ++++++++--------- firmware/src/Motherboard/SDCard.hh | 2 +- .../Motherboard/boards/mb24/Motherboard.cc | 8 +++-- .../Motherboard/boards/mb24/Motherboard.hh | 2 +- firmware/src/shared/Menu.cc | 30 ++++++++++--------- firmware/src/shared/Menu.hh | 9 +++--- 6 files changed, 40 insertions(+), 38 deletions(-) diff --git a/firmware/src/Motherboard/SDCard.cc b/firmware/src/Motherboard/SDCard.cc index a9f9aab..b12b8c4 100644 --- a/firmware/src/Motherboard/SDCard.cc +++ b/firmware/src/Motherboard/SDCard.cc @@ -190,10 +190,9 @@ bool createFile(char *name) bool capturing = false; bool playing = false; +int32_t fileSizeBytes = 0L; +int32_t playedBytes = 0L; uint32_t capturedBytes = 0L; -uint32_t countupBytes = 0L; -uint32_t percentBytes = 0L; -uint8_t percentPlayed = 0L; bool isPlaying() { return playing; @@ -211,7 +210,7 @@ SdErrorCode startCapture(char* filename) return result; } capturedBytes = 0L; - countupBytes = 0L; + playedBytes = 0L; file = 0; // Always operate in truncation mode. deleteFile(filename); @@ -260,11 +259,7 @@ bool has_more; void fetchNextByte() { int16_t read = fat_read_file(file, &next_byte, 1); has_more = read > 0; - countupBytes++; - if (countupBytes >= percentBytes) { - countupBytes -= percentBytes; - percentPlayed++; - } + playedBytes++; } bool playbackHasNext() { @@ -286,7 +281,7 @@ SdErrorCode startPlayback(char* filename) { } capturedBytes = 0L; - countupBytes = 0L; + playedBytes = 0L; file = 0; if (!openFile(filename, &file) || file == 0) { @@ -294,11 +289,9 @@ SdErrorCode startPlayback(char* filename) { } playing = true; - percentPlayed = 0; - percentBytes = 0L; int32_t off = 0L; fat_seek_file(file, &off, FAT_SEEK_END); - percentBytes = off / 100L; + fileSizeBytes = off; off = 0L; fat_seek_file(file, &off, FAT_SEEK_SET); @@ -308,8 +301,12 @@ SdErrorCode startPlayback(char* filename) { return SD_SUCCESS; } -uint8_t getPercentPlayed() { - return percentPlayed; +float getPercentPlayed() { + float percentPlayed = (float)(playedBytes * 100) / (float)fileSizeBytes; + + if ( percentPlayed > 100.0 ) return 100.0; + else if ( percentPlayed < 0.0 ) return 0.0; + else return percentPlayed; } void playbackRewind(uint8_t bytes) { diff --git a/firmware/src/Motherboard/SDCard.hh b/firmware/src/Motherboard/SDCard.hh index f3c4975..fd84e6b 100644 --- a/firmware/src/Motherboard/SDCard.hh +++ b/firmware/src/Motherboard/SDCard.hh @@ -88,7 +88,7 @@ namespace sdcard { /// Return the percentage of the file printed. - uint8_t getPercentPlayed(); + float getPercentPlayed(); /// See if there is more data available in the playback file. /// \return True if there is more data in the file diff --git a/firmware/src/Motherboard/boards/mb24/Motherboard.cc b/firmware/src/Motherboard/boards/mb24/Motherboard.cc index fdf0a5e..fbcc1af 100644 --- a/firmware/src/Motherboard/boards/mb24/Motherboard.cc +++ b/firmware/src/Motherboard/boards/mb24/Motherboard.cc @@ -165,12 +165,14 @@ micros_t Motherboard::getCurrentMicros() { /// Get the number of seconds that have passed since /// the board was booted or the timer reset. -micros_t Motherboard::getCurrentSeconds() { +float Motherboard::getCurrentSeconds() { micros_t seconds_snapshot; + micros_t countupMicros_snapshot; ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - seconds_snapshot = seconds; + countupMicros_snapshot = countupMicros; + seconds_snapshot = seconds; } - return seconds_snapshot; + return (float)seconds_snapshot + ((float)countupMicros_snapshot / (float)1000000); } diff --git a/firmware/src/Motherboard/boards/mb24/Motherboard.hh b/firmware/src/Motherboard/boards/mb24/Motherboard.hh index dcac51c..6254b8f 100644 --- a/firmware/src/Motherboard/boards/mb24/Motherboard.hh +++ b/firmware/src/Motherboard/boards/mb24/Motherboard.hh @@ -89,7 +89,7 @@ public: /// the board was initialized. This value will wrap after /// 2**32 microseconds (ca. 70 minutes); callers should compensate for this. micros_t getCurrentMicros(); - micros_t getCurrentSeconds(); + float getCurrentSeconds(); void resetCurrentSeconds(); /// Write an error code to the debug pin. diff --git a/firmware/src/shared/Menu.cc b/firmware/src/shared/Menu.cc index 571aea6..0d8374f 100644 --- a/firmware/src/shared/Menu.cc +++ b/firmware/src/shared/Menu.cc @@ -518,14 +518,16 @@ void SnakeMode::notifyButtonPressed(ButtonArray::ButtonName button) { void MonitorMode::reset() { updatePhase = 0; buildTimePhase = 0; - extruderStartSeconds = 0; + buildComplete = false; + extruderStartSeconds = 0.0; + lastElapsedSeconds = 0.0; } void MonitorMode::update(LiquidCrystal& lcd, bool forceRedraw) { static PROGMEM prog_uchar extruder_temp[] = "Tool: ---/---C"; static PROGMEM prog_uchar platform_temp[] = "Bed: ---/---C"; static PROGMEM prog_uchar elapsed_time[] = "Elapsed: 0h00m"; - static PROGMEM prog_uchar completed_percent[] = "Completed: 0%"; + static PROGMEM prog_uchar completed_percent[] = "Completed: 0% "; static PROGMEM prog_uchar time_left[] = "TimeLeft: 0h00m"; static PROGMEM prog_uchar time_left_calc[] = " calc.."; static PROGMEM prog_uchar time_left_1min[] = " <1min"; @@ -606,7 +608,7 @@ void MonitorMode::update(LiquidCrystal& lcd, bool forceRedraw) { if ( (hostState != host::HOST_STATE_BUILDING ) && ( hostState != host::HOST_STATE_BUILDING_FROM_SD )) break; - seconds_t secs; + float secs; switch (buildTimePhase) { case 0: @@ -614,7 +616,7 @@ void MonitorMode::update(LiquidCrystal& lcd, bool forceRedraw) { lcd.writeFromPgmspace(completed_percent); lcd.setCursor(11,1); buf[0] = '\0'; - appendUint8(buf, sizeof(buf), sdcard::getPercentPlayed()); + appendUint8(buf, sizeof(buf), (uint8_t)sdcard::getPercentPlayed()); strcat(buf, "% "); lcd.writeString(buf); break; @@ -637,18 +639,18 @@ void MonitorMode::update(LiquidCrystal& lcd, bool forceRedraw) { lcd.writeFromPgmspace(time_left); lcd.setCursor(9,1); - if (( sdcard::getPercentPlayed() >= 1 ) && ( extruderStartSeconds )) { + if (( sdcard::getPercentPlayed() >= 1.0 ) && ( extruderStartSeconds > 0.0)) { buf[0] = '\0'; - seconds_t currentSeconds = Motherboard::getBoard().getCurrentSeconds() - extruderStartSeconds; - float secsf = (((float)currentSeconds / (float)sdcard::getPercentPlayed()) * 100.0 ) - - (float)currentSeconds; + float currentSeconds = Motherboard::getBoard().getCurrentSeconds() - extruderStartSeconds; + secs = ((currentSeconds / sdcard::getPercentPlayed()) * 100.0 ) - currentSeconds; - if ((secsf > 0.0 ) && (secsf < 60.0)) + if ((secs > 0.0 ) && (secs < 60.0) && ( ! buildComplete ) ) lcd.writeFromPgmspace(time_left_1min); - else if (( secsf <= 0.0) || ( host::isBuildComplete() )) + else if (( secs <= 0.0) || ( host::isBuildComplete() ) || ( buildComplete ) ) { + buildComplete = true; lcd.writeFromPgmspace(time_left_none); - else { - appendTime(buf, sizeof(buf), (uint32_t)secsf); + } else { + appendTime(buf, sizeof(buf), (uint32_t)secs); lcd.writeString(buf); } } @@ -656,7 +658,7 @@ void MonitorMode::update(LiquidCrystal& lcd, bool forceRedraw) { //Set extruderStartSeconds to when the extruder starts extruding, so we can //get an accurate TimeLeft: - if ( ! extruderStartSeconds ) { + if ( extruderStartSeconds == 0.0 ) { if (queryExtruderParameter(SLAVE_CMD_GET_MOTOR_1_PWM, responsePacket)) { uint8_t pwm = responsePacket.read8(1); if ( pwm ) extruderStartSeconds = Motherboard::getBoard().getCurrentSeconds(); @@ -664,7 +666,7 @@ void MonitorMode::update(LiquidCrystal& lcd, bool forceRedraw) { } break; } - + buildTimePhase ++; if ( buildTimePhase >= 3 ) buildTimePhase = 0; break; diff --git a/firmware/src/shared/Menu.hh b/firmware/src/shared/Menu.hh index e43d484..c75db4b 100644 --- a/firmware/src/shared/Menu.hh +++ b/firmware/src/shared/Menu.hh @@ -193,10 +193,11 @@ class MonitorMode: public Screen { private: CancelBuildMenu cancelBuildMenu; - uint8_t updatePhase; - uint8_t buildTimePhase; - seconds_t lastElapsedSeconds; - seconds_t extruderStartSeconds; + uint8_t updatePhase; + uint8_t buildTimePhase; + float lastElapsedSeconds; + float extruderStartSeconds; + bool buildComplete; //For solving floating point rounding issues public: micros_t getUpdateRate() {return 500L * 1000L;}