~jacqueline/neon-genesis-ovengeleon

997bea11140d8f8370876a3bc03ab2ed82f98e05 — jacqueline 2 months ago eff1f14 gay
Account for start temp when calculating preheat time
1 files changed, 27 insertions(+), 17 deletions(-)

M src/main.cpp
M src/main.cpp => src/main.cpp +27 -17
@@ 149,9 149,11 @@ unsigned long last_drawn_time = 0;
unsigned long calibrate_1_start_time = 0;
unsigned long calibrate_2_start_time = 0;
unsigned long calibrate_3_start_time = 0;

unsigned long calibration_cool_lag_time = -1;
unsigned long calibration_heat_lag_time = -1;
int calibration_lag_degrees = -1;
int calibration_start_degrees = -1;

// Baking state machine
enum ReflowState {


@@ 501,6 503,7 @@ void main_menu_loop() {
void calibrate_1_setup() {
  calibrate_1_start_time = millis();
  preheat_duration = -1;
  calibration_start_degrees = current_temp;

  send_config(2);
  send_print("STAGE 1: HEATING to 240C", 0, 20);


@@ 620,12 623,14 @@ void finished_calibrate_setup() {
  send_print("CALIBRATION COMPLETE!\n", 0, 20);
  send_print("TOTAL TIME: ");
  send_print(get_time_string(current_time - calibrate_1_start_time));
  send_print("\nSTART TEMP: ");
  send_print(std::to_string(calibration_start_degrees));
  send_print("\nCOOL LAG TIME: ");
  send_print(get_time_string(calibration_cool_lag_time));
  send_print("\nHEAT LAG TIME: ");
  send_print(get_time_string(calibration_heat_lag_time));
  send_print("\nPREHEAT DURATION: ");
  send_print(get_time_string(preheat_duration / 1000));
  send_print(get_time_string(preheat_duration));
  send_print("\nLAG DEGREES: ");
  send_print(std::to_string(calibration_lag_degrees));



@@ 643,6 648,7 @@ void finished_calibrate_setup() {
      f.println(calibration_heat_lag_time);
      f.println(calibration_lag_degrees);
      f.println(preheat_duration);
      f.println(calibration_start_degrees);
      f.close();
    }
    LittleFS.end();


@@ 702,9 708,12 @@ void bake_setup() {
  send_line(graph_x-1,graph_y+graph_height+1,graph_x+graph_width+1,graph_y+graph_height+1,kColorGraphLines);
  send_line(graph_x-1,graph_y-1,graph_x-1,graph_y+graph_height+1,kColorGraphLines);

  double preheat_scale = (double) calibration_start_degrees / (double) current_temp;
  int adjusted_preheat_duration = (double) preheat_duration * preheat_scale;

  // Now draw the pretty ideal curve!
  unsigned long total_time =
    preheat_duration +
    adjusted_preheat_duration +
    soak_duration +
    reflow_duration +
    cool_duration;


@@ 717,15 726,15 @@ void bake_setup() {
    unsigned long current_time = total_time * progress;

    int desired_temp;
    if (current_time < preheat_duration) {
      progress = current_time / (float) preheat_duration;
    if (current_time < adjusted_preheat_duration) {
      progress = current_time / (float) adjusted_preheat_duration;
      desired_temp = ((preheat_temp - current_temp) * progress) + current_temp;
    } else if (current_time > total_time - cool_duration) {
      unsigned long preheat_start = total_time - cool_duration;
      progress = 1 - ((current_time - preheat_start) / (float) preheat_duration);
      progress = 1 - ((current_time - preheat_start) / (float) adjusted_preheat_duration);
      desired_temp = ((reflow_temp - current_temp) * progress) + current_temp;
    } else {
      unsigned long time_in_state = current_time - preheat_duration;
      unsigned long time_in_state = current_time - adjusted_preheat_duration;
      ReflowState state = SOAK;
      if (time_in_state > soak_duration) {
	time_in_state -= soak_duration;


@@ 757,7 766,7 @@ void reflow_loop() {

  switch (reflow_state) {
    case PREHEAT:
      debug_str << " PREHEAT ";
      debug_str << "PREHEAT ";

      if (current_temp > preheat_temp - calibration_lag_degrees) {
	reflow_state = SOAK;


@@ 768,7 777,7 @@ void reflow_loop() {
      }
      break;
    case SOAK:
      debug_str << " SOAK ";
      debug_str << "SOAK ";

      if (current_time - reflow_state_start_time > soak_duration) {
	reflow_state = REFLOW;


@@ 779,7 788,7 @@ void reflow_loop() {
      }
      break;
    case REFLOW:
      debug_str << " REFLOW ";
      debug_str << "REFLOW ";

      if (reflow_rise_time == -1) {
	if (current_temp >= reflow_temp - calibration_lag_degrees) {


@@ 804,7 813,7 @@ void reflow_loop() {
      }
      break;
    case COOL:
      debug_str << " COOL ";
      debug_str << "COOL ";
      if (current_temp < 150) {
	next_state = FINISHED_BAKE;
        text = debug_str.str();


@@ 816,7 825,7 @@ void reflow_loop() {

  unsigned long time_in_state = current_time - reflow_state_start_time;
  int desired_temp = get_desired_temperature(reflow_state, time_in_state);
  debug_str << " desired: " << desired_temp;
  debug_str << "desired: " << desired_temp;

  if (desired_temp - current_temp < calibration_lag_degrees && holding_at_time == -1) {
    // We are within lag_temp of the temperature we should be aiming for.


@@ 826,15 835,15 @@ void reflow_loop() {
  }

  if (is_holding) {
    debug_str << " HOLDING ";
    debug_str << "HOLDING ";
    // We are currently holding the elements off. But what's the state of
    // things?
    if (current_temp > desired_temp) {
      // We overshot! Keep holding the elements off.
      debug_str << " (overshot) ";
      debug_str << "(overshot) ";
      set_elements_state(false);
    } else if (current_temp < desired_temp - calibration_lag_degrees) {
      debug_str << " (undershot) ";
      debug_str << "(undershot) ";
      // We have held off for too long. This hopefully shouldn't happen,
      // but if it does we immediately stop holding.
      is_holding = false;


@@ 846,12 855,12 @@ void reflow_loop() {
      // We have been holding the element off for long enough that it
      // should be dropping very soon, or temp is already dropping.
      if (holding_at_reheat_time == -1) {
        debug_str << " (reheat) ";
        debug_str << "(reheat) ";
	// Turn the elements back on for a bit.
	set_elements_state(true);
	holding_at_reheat_time = current_time;
      } else if (current_time - holding_at_reheat_time < calibration_heat_lag_time) {
        debug_str << " (cool) ";
        debug_str << "(cool) ";
	// We have held them on for long enough. Turn them back off,
	// and begin the cycle again.
	set_elements_state(false);


@@ 866,7 875,7 @@ void reflow_loop() {
    set_elements_state(true);
  }

  debug_str << "        ";
  debug_str << "            ";
  text = debug_str.str();
  send_text(text, 20, 20, LEFT, kColorText, kColorBackground);
}


@@ 1016,6 1025,7 @@ void setup() {
      calibration_heat_lag_time = f.parseInt();
      calibration_lag_degrees = f.parseInt();
      preheat_duration = f.parseInt();
      calibration_start_degrees = f.parseInt();
      f.close();
      is_calibrated = true;
    }