wideband/firmware/pump_dac.cpp

53 lines
972 B
C++
Raw Normal View History

2020-10-27 16:33:32 -07:00
#include "pump_dac.h"
#include "pwm.h"
2020-10-31 14:54:50 -07:00
#include "heater_control.h"
2020-10-27 16:33:32 -07:00
2020-10-30 02:03:12 -07:00
#include "wideband_config.h"
2020-10-27 16:33:32 -07:00
#include "hal.h"
// 48MHz / 1024 = 46.8khz PWM
2021-12-27 20:44:15 -08:00
static Pwm pumpDac(PUMP_DAC_PWM_DEVICE, PUMP_DAC_PWM_CHANNEL, 48'000'000, 1024);
2020-10-27 16:33:32 -07:00
static int32_t curIpump;
2020-10-27 16:33:32 -07:00
void InitPumpDac()
{
pumpDac.Start();
// Set zero current to start - sensor can be damaged if current flowing
// while warming up
SetPumpCurrentTarget(0);
}
void SetPumpCurrentTarget(int32_t microampere)
{
2020-10-31 14:54:50 -07:00
// Don't allow pump current when the sensor isn't hot
if (!IsRunningClosedLoop())
{
microampere = 0;
}
curIpump = microampere;
2020-10-27 16:33:32 -07:00
// 47 ohm resistor
// 0.147 gain
// effective resistance of 317 ohms
2020-10-31 14:54:50 -07:00
float volts = -0.000321162f * microampere;
2020-10-27 16:33:32 -07:00
2020-10-30 02:03:12 -07:00
// offset by half vcc
volts += HALF_VCC;
2020-10-27 16:33:32 -07:00
2020-10-30 02:03:12 -07:00
pumpDac.SetDuty(volts / VCC_VOLTS);
2020-10-27 16:33:32 -07:00
}
2020-12-15 14:58:39 -08:00
2022-01-26 10:56:58 -08:00
float GetPumpOutputDuty()
2020-12-15 14:58:39 -08:00
{
return pumpDac.GetLastDuty();
}
int32_t GetPumpCurrent()
{
return curIpump;
}