diff --git a/firmware/heater_control.cpp b/firmware/heater_control.cpp index 712f611..4e54aeb 100644 --- a/firmware/heater_control.cpp +++ b/firmware/heater_control.cpp @@ -32,17 +32,20 @@ static HeaterState GetNextState(HeaterState state, float sensorEsr) rampDuty = 0.5f; return HeaterState::WarmupRamp; } - else - { - // Stay in preheat - wait for time to elapse - return HeaterState::Preheat; - } + + // Stay in preheat - wait for time to elapse + break; case HeaterState::WarmupRamp: if (sensorEsr < 2000) { return HeaterState::ClosedLoop; } + + break; + case HeaterState::ClosedLoop: break; } + + return state; } static float GetDutyForState(HeaterState state, float heaterEsr) @@ -61,17 +64,18 @@ static float GetDutyForState(HeaterState state, float heaterEsr) case HeaterState::ClosedLoop: { // do something more intelligent here - float error = (300 - heaterEsr) / 100; + float error = (heaterEsr - 250) / 100; return error * 1.0f; } } } +static HeaterState state = HeaterState::Preheat; + static THD_WORKING_AREA(waHeaterThread, 256); static void HeaterThread(void*) { - HeaterState state = HeaterState::Preheat; while (true) { @@ -97,3 +101,8 @@ void StartHeaterControl() chThdCreateStatic(waHeaterThread, sizeof(waHeaterThread), NORMALPRIO + 1, HeaterThread, nullptr); } + +bool IsRunningClosedLoop() +{ + return state == HeaterState::ClosedLoop; +} diff --git a/firmware/heater_control.h b/firmware/heater_control.h index 2bfdca7..5f94282 100644 --- a/firmware/heater_control.h +++ b/firmware/heater_control.h @@ -1,3 +1,4 @@ #pragma once void StartHeaterControl(); +bool IsRunningClosedLoop(); diff --git a/firmware/pump_dac.cpp b/firmware/pump_dac.cpp index 2875a10..8a820db 100644 --- a/firmware/pump_dac.cpp +++ b/firmware/pump_dac.cpp @@ -1,5 +1,6 @@ #include "pump_dac.h" #include "pwm.h" +#include "heater_control.h" #include "wideband_config.h" @@ -19,10 +20,16 @@ void InitPumpDac() void SetPumpCurrentTarget(int32_t microampere) { + // Don't allow pump current when the sensor isn't hot + if (!IsRunningClosedLoop()) + { + microampere = 0; + } + // 47 ohm resistor // 0.147 gain // effective resistance of 317 ohms - float volts = 0.000321162f * microampere; + float volts = -0.000321162f * microampere; // offset by half vcc volts += HALF_VCC; diff --git a/firmware/sampling.cpp b/firmware/sampling.cpp index 68f92e9..91cd589 100644 --- a/firmware/sampling.cpp +++ b/firmware/sampling.cpp @@ -78,6 +78,6 @@ float GetPumpNominalCurrent() // Gain is 10x, then a 61.9 ohm resistor // Effective resistance with the gain is 619 ohms // 1000 is to convert to milliamperes - constexpr float ratio = 1000 / (PUMP_CURRENT_SENSE_GAIN * LSU_SENSE_R); + constexpr float ratio = -1000 / (PUMP_CURRENT_SENSE_GAIN * LSU_SENSE_R); return pumpCurrentSenseVoltage * ratio; }