Skip to content

Commit

Permalink
Improvements to TimeLeft estimation, by using floating point. This go…
Browse files Browse the repository at this point in the history
…es with commit c5e0603
  • Loading branch information
jetty840 committed Dec 24, 2011
1 parent c5e0603 commit 9da86f0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
27 changes: 12 additions & 15 deletions firmware/src/Motherboard/SDCard.cc
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -286,19 +281,17 @@ SdErrorCode startPlayback(char* filename) {
}
capturedBytes = 0L;

countupBytes = 0L;
playedBytes = 0L;

file = 0;
if (!openFile(filename, &file) || file == 0) {
return SD_ERR_FILE_NOT_FOUND;
}
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);

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/Motherboard/SDCard.hh
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions firmware/src/Motherboard/boards/mb24/Motherboard.cc
Expand Up @@ -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);
}


Expand Down
2 changes: 1 addition & 1 deletion firmware/src/Motherboard/boards/mb24/Motherboard.hh
Expand Up @@ -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.
Expand Down
30 changes: 16 additions & 14 deletions firmware/src/shared/Menu.cc
Expand Up @@ -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";
Expand Down Expand Up @@ -606,15 +608,15 @@ 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:
lcd.setCursor(0,1);
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;
Expand All @@ -637,34 +639,34 @@ 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);
}
}
else lcd.writeFromPgmspace(time_left_calc);

//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();
}
}
break;
}

buildTimePhase ++;
if ( buildTimePhase >= 3 ) buildTimePhase = 0;
break;
Expand Down
9 changes: 5 additions & 4 deletions firmware/src/shared/Menu.hh
Expand Up @@ -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;}
Expand Down

0 comments on commit 9da86f0

Please sign in to comment.