This commit is contained in:
Matthew Kennedy 2020-10-31 17:01:37 -07:00
commit e16c1b626d
8 changed files with 95 additions and 13 deletions

View File

@ -128,6 +128,8 @@ CPPSRC = $(ALLCPPSRC) \
pump_dac.cpp \ pump_dac.cpp \
sampling.cpp \ sampling.cpp \
heater_control.cpp \ heater_control.cpp \
pid.cpp \
pump_control.cpp \
main.cpp main.cpp
# List ASM source files here. # List ASM source files here.

View File

@ -1,9 +1,11 @@
#include "heater_control.h" #include "heater_control.h"
#include "wideband_config.h"
#include "ch.h" #include "ch.h"
#include "hal.h" #include "hal.h"
#include "pwm.h" #include "pwm.h"
#include "sampling.h" #include "sampling.h"
#include "pid.h"
// 400khz / 1024 = 390hz PWM // 400khz / 1024 = 390hz PWM
Pwm heaterPwm(PWMD1, 0, 400'000, 1024); Pwm heaterPwm(PWMD1, 0, 400'000, 1024);
@ -48,6 +50,8 @@ static HeaterState GetNextState(HeaterState state, float sensorEsr)
return state; return state;
} }
static Pid heaterPid(0.1f, 0, HEATER_CONTROL_PERIOD);
static float GetDutyForState(HeaterState state, float heaterEsr) static float GetDutyForState(HeaterState state, float heaterEsr)
{ {
switch (state) switch (state)
@ -62,12 +66,7 @@ static float GetDutyForState(HeaterState state, float heaterEsr)
return rampDuty; return rampDuty;
case HeaterState::ClosedLoop: case HeaterState::ClosedLoop:
{ return heaterPid.GetOutput(HEATER_TARGET_ESR, heaterEsr);
// do something more intelligent here
float error = (heaterEsr - 250) / 100;
return error * 1.0f;
}
} }
} }
@ -90,7 +89,7 @@ static void HeaterThread(void*)
heaterPwm.SetDuty(duty); heaterPwm.SetDuty(duty);
// Loop at ~20hz // Loop at ~20hz
chThdSleepMilliseconds(50); chThdSleepMilliseconds(HEATER_CONTROL_PERIOD);
} }
} }

View File

@ -4,6 +4,7 @@
#include "can.h" #include "can.h"
#include "heater_control.h" #include "heater_control.h"
#include "pump_control.h"
#include "pump_dac.h" #include "pump_dac.h"
#include "sampling.h" #include "sampling.h"
@ -32,15 +33,16 @@ int main() {
halInit(); halInit();
chSysInit(); chSysInit();
// Fire up all of our threads
StartSampling(); StartSampling();
InitPumpDac(); InitPumpDac();
StartHeaterControl();
InitCan(); StartPumpControl();
uartStart(&UARTD1, &uartCfg); uartStart(&UARTD1, &uartCfg);
StartHeaterControl();
InitCan();
/*for (int i = 0; i < 500; i++) { /*for (int i = 0; i < 500; i++) {
SetPumpCurrentTarget(current); SetPumpCurrentTarget(current);

13
firmware/pid.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "pid.h"
float Pid::GetOutput(float setpoint, float observation)
{
// TODO: is this backwards?
float error = setpoint - observation;
// Integrate error
m_integrator += error * m_period;
// Multiply by gains and sum
return m_kp * error + m_ki * m_integrator;
}

22
firmware/pid.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
class Pid
{
public:
Pid(float kP, float kI, int periodMs)
: m_period(periodMs / 1000.0f)
, m_kp(kP)
, m_ki(kI)
{
}
float GetOutput(float setpoint, float observation);
private:
const float m_period;
const float m_kp;
const float m_ki;
float m_lastError;
float m_integrator;
};

36
firmware/pump_control.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "pump_control.h"
#include "wideband_config.h"
#include "heater_control.h"
#include "sampling.h"
#include "pump_dac.h"
#include "pid.h"
#include "ch.h"
static Pid pumpPid(0, 0.01f, 2);
static THD_WORKING_AREA(waPumpThread, 256);
static void PumpThread(void*)
{
while(true)
{
// Only actuate pump when running closed loop!
if (IsRunningClosedLoop())
{
float nernstVoltage = GetNernstDc();
float result = pumpPid.GetOutput(NERNST_TARGET, nernstVoltage);
// result is in mA
SetPumpCurrentTarget(result / 1000.0f);
// Run at 500hz
chThdSleepMilliseconds(2);
}
}
}
void StartPumpControl()
{
chThdCreateStatic(waPumpThread, sizeof(waPumpThread), NORMALPRIO + 4, PumpThread, nullptr);
}

3
firmware/pump_control.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void StartPumpControl();

View File

@ -24,7 +24,12 @@
#define LSU_SENSE_R (61.9f) #define LSU_SENSE_R (61.9f)
// ******************************* // *******************************
// Pump driver // Pump controller
// ******************************* // *******************************
#define NERNST_TARGET (0.45f)
// todo // *******************************
// Heater controller config
// *******************************
#define HEATER_CONTROL_PERIOD 50
#define HEATER_TARGET_ESR 300