added PID to primary pump, working on secondary system

This commit is contained in:
Jay Robson 2024-02-06 17:57:59 +11:00
parent ac5fc2258f
commit 83737fae66
34 changed files with 413 additions and 142 deletions

BIN
assets/model/pump_switch_1.glb (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/model/pump_switch_2.glb (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/model/pump_switch_3.glb (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
assets/scene-baked.glb (Stored with Git LFS)

Binary file not shown.

BIN
assets/unbaked/scene.blend (Stored with Git LFS)

Binary file not shown.

BIN
assets/unbaked/scene/labels.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/unbaked/scene/labels.xcf (Stored with Git LFS)

Binary file not shown.

View File

@ -0,0 +1,17 @@
#include "condenser_secondary.hpp"
using namespace sim::coolant;
condenser_secondary::condenser_secondary(condenser* primary, double volume, double level) :
primary(primary), fluid_holder(primary->fluid, volume, 0)
{
this->level = level;
}
void condenser_secondary::update(double dt)
{
((fluid_holder*)this)->update(dt);
heat = primary->add_heat(get_thermal_mass(), heat);
}

View File

@ -0,0 +1,22 @@
#pragma once
#include "fluid_holder.hpp"
#include "condenser.hpp"
namespace sim::coolant
{
class condenser_secondary : public fluid_holder
{
condenser* const primary;
public:
condenser_secondary(condenser* primary, double volume, double level);
void update(double dt);
};
};

View File

@ -0,0 +1,20 @@
#include "evaporator.hpp"
#include <cmath>
using namespace sim::coolant;
constexpr static double calc_cylinder(double h, double d)
{
double r = d / 2;
return M_PI * r * r * h;
}
evaporator::evaporator(fluid_t type, double height, double diameter, double mass, double level) :
height(height), diameter(diameter), fluid_holder(type, calc_cylinder(height, diameter), mass)
{
this->level = level;
}

View File

@ -0,0 +1,20 @@
#pragma once
#include "fluid_holder.hpp"
namespace sim::coolant
{
class evaporator : public fluid_holder
{
const double height;
const double diameter;
public:
evaporator(fluid_t type, double height, double diameter, double mass, double level);
};
};

View File

@ -7,19 +7,30 @@
using namespace sim::coolant;
pump::pump(fluid_holder* src, fluid_holder* dst, double mass, double radius, double power, double l_per_rev, double friction) :
src(src), dst(dst), mass(mass), radius(radius), l_per_rev(l_per_rev), friction(friction)
src(src),
dst(dst),
mass(mass),
radius(radius),
l_per_rev(l_per_rev),
friction(friction),
max_power(power)
{
this->power = power;
}
double pump::get_flow() const
double pump::get_flow_target() const
{
return l_per_rev * get_rpm() * 60;
}
double pump::get_flow() const
{
return flow;
}
double pump::get_flow_mass() const
{
return src->fluid.l_to_g(get_flow());
return src->fluid.l_to_g(flow);
}
double pump::get_rpm() const
@ -27,24 +38,16 @@ double pump::get_rpm() const
return velocity / (M_PI * mass * 0.001 * radius * radius);
}
double pump::get_power() const
{
return power;
}
const char* pump::get_state_string()
{
if(!powered)
{
return "Off";
}
if(idling && std::abs(get_flow()) < 1e-3)
{
return "Idle";
}
if(idling)
{
return "Coasting";
}
return "Revving";
if(!powered) return "Off";
if(power == 0) return "Idle";
return "On";
}
static double calc_work(double j, double mass)
@ -61,11 +64,10 @@ static double calc_work(double j, double mass)
void pump::update(double dt)
{
idling = false;
if(powered && !idling)
if(powered)
{
velocity += calc_work(dt * power, mass);
power = pid.calculate(dt, src->get_volume() / 2, src->get_steam_volume());
velocity += calc_work(dt * power * max_power, mass);
}
fluid_holder fh_src(*src);
@ -73,7 +75,7 @@ void pump::update(double dt)
double src_heat = src->get_heat();
double p_diff_1 = dst->get_pressure() - src->get_pressure();
double src_volume = fh_src.extract_fluid(get_flow() * dt);
double src_volume = fh_src.extract_fluid(get_flow_target() * dt);
double dst_volume = fh_dst.add_fluid(src_volume, src_heat);
src->extract_fluid(dst_volume);
@ -84,15 +86,6 @@ void pump::update(double dt)
double work = p_diff * dst_volume * 0.001 + get_rpm() * 60 * dt * friction;
velocity -= calc_work(work, mass);
if(dst->get_level() > 400 || src->get_level() < 10)
{
// idling = true;
}
else
{
// idling = false;
}
flow = dst_volume / dt;
}

View File

@ -2,6 +2,7 @@
#pragma once
#include "fluid_holder.hpp"
#include "../util/pid.hpp"
namespace sim::coolant
{
@ -11,24 +12,29 @@ class pump
fluid_holder* const src;
fluid_holder* const dst;
util::PID pid {1, 0, 100, 0, 0};
double flow = 0; // L/s
double velocity = 0; // m/s
double power = 0;
public:
const double mass; // grams
const double radius; // meters
const double l_per_rev; // litres
const double friction; // J/rev
double velocity = 0; // m/s
double power = 0; // W
public:
const double max_power; // W
bool powered = false;
bool idling = false;
pump(fluid_holder* src, fluid_holder* dst, double mass, double radius, double power, double l_per_rev, double friction);
double get_flow() const; // L/s
double get_flow_target() const; // L/s
double get_flow_mass() const; // g/s
double get_rpm() const; // rev/min
double get_power() const; // W
const char* get_state_string();
void update(double dt);

14
src/coolant/sink.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "sink.hpp"
using namespace sim::coolant;
sink::sink(fluid_t type, double heat, double pressure, double steam_density) :
heat(heat),
pressure(pressure),
steam_density(steam_density),
fluid_holder(type, 1, 1)
{
}

34
src/coolant/sink.hpp Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include "fluid_holder.hpp"
// the name isn't really important here, it's just to show that anything attached to this
// can kinda just do anything. for example, get rid of steam to the "outside",
// or pump in new coolant.
namespace sim::coolant
{
class sink : public fluid_holder
{
public:
double heat;
double pressure;
double steam_density;
sink(fluid_t type, double heat, double pressure, double steam_density);
virtual double add_heat(double m, double t) { return t; }
virtual double extract_fluid(double amount) { return amount; }
virtual double add_fluid(double amount, double heat) { return amount; }
virtual void add_steam(double amount, double t) { }
virtual void update(double dt) { }
virtual double get_pressure() const { return pressure; } // pascals
virtual double get_steam_density() const { return steam_density; } // g/L
};
};

View File

@ -18,7 +18,7 @@ turbine::turbine(coolant::fluid_t type, coolant::condenser* condenser, double le
length(length), diameter(diameter), condenser(condenser),
sim::coolant::fluid_holder(type, calc_cylinder(length, diameter), mass)
{
this->level = level;
}
void turbine::update(double secs)

View File

@ -29,19 +29,22 @@ static void cb_keypress(GLFWwindow* win, int key, int sc, int action, int mods)
switch(key)
{
case GLFW_KEY_1:
sim::system::active.speed = 1;
sim::system::active.speed = 1; // 1 s/s
break;
case GLFW_KEY_2:
sim::system::active.speed = 10;
sim::system::active.speed = 10; // 10 s/s
break;
case GLFW_KEY_3:
sim::system::active.speed = 60;
sim::system::active.speed = 60; // 1 min/s
break;
case GLFW_KEY_4:
sim::system::active.speed = 600;
sim::system::active.speed = 600; // 10 min/s
break;
case GLFW_KEY_5:
sim::system::active.speed = 3600;
sim::system::active.speed = 3600; // 1 h/s
break;
case GLFW_KEY_6:
sim::system::active.speed = 43200; // 12 h/s
break;
}
}

View File

@ -4,7 +4,7 @@
using namespace sim::graphics;
const glm::mat4 locations::monitors[4] = {
const glm::mat4 locations::monitors[7] = {
(
glm::translate(glm::mat4(1), glm::vec3(-2.949, -1.7778 + 0.05, 3 - 0.05)) *
glm::rotate(glm::mat4(1), glm::radians<float>(-90), glm::vec3(1, 0, 0)) *
@ -25,6 +25,23 @@ const glm::mat4 locations::monitors[4] = {
glm::translate(glm::mat4(1), glm::vec3(3.5 + 0.05, 3.949, 3 - 0.05)) *
glm::rotate(glm::mat4(1), glm::radians<float>(-90), glm::vec3(1, 0, 0)) *
glm::scale(glm::mat4(1), glm::vec3(1.9, 1.9, 1.9))
),
(
glm::translate(glm::mat4(1), glm::vec3(6 + 0.05, 3.949, 3 - 0.05)) *
glm::rotate(glm::mat4(1), glm::radians<float>(-90), glm::vec3(1, 0, 0)) *
glm::scale(glm::mat4(1), glm::vec3(1.9, 1.9, 1.9))
),
(
glm::translate(glm::mat4(1), glm::vec3(8.949, 7.0/3.0 - 0.05, 3 - 0.05)) *
glm::rotate(glm::mat4(1), glm::radians<float>(-90), glm::vec3(1, 0, 0)) *
glm::rotate(glm::mat4(1), glm::radians<float>(90), glm::vec3(0, 1, 0)) *
glm::scale(glm::mat4(1), glm::vec3(1.9, 1.9, 1.9))
),
(
glm::translate(glm::mat4(1), glm::vec3(8.949, -1.0/3.0 - 0.05, 3 - 0.05)) *
glm::rotate(glm::mat4(1), glm::radians<float>(-90), glm::vec3(1, 0, 0)) *
glm::rotate(glm::mat4(1), glm::radians<float>(90), glm::vec3(0, 1, 0)) *
glm::scale(glm::mat4(1), glm::vec3(1.9, 1.9, 1.9))
)
};

View File

@ -6,7 +6,7 @@
namespace sim::graphics::locations
{
extern const glm::mat4 monitors[4];
extern const glm::mat4 monitors[7];
};

View File

@ -132,7 +132,7 @@ void core::init()
m_scram.load_model("../assets/model/", "reactor_core_scram.stl");
}
void core::update()
void core::update(double dt)
{
sim::system& sys = sim::system::active;

View File

@ -19,7 +19,7 @@ public:
core();
void init();
void update();
void update(double dt);
void render();
};

View File

@ -60,7 +60,7 @@ void primary_loop::toggle_primary_pump()
bool state;
sys.primary_pump->powered = state = !sys.primary_pump->powered;
gm_switch_primary.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, state ? 0.07 : 0, 0));
gm_switch_1.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, state ? 0.07 : 0, 0));
}
void primary_loop::init()
@ -83,7 +83,7 @@ void primary_loop::init()
ss << "Turbine Inlet Valve\n\n";
ss << "Opened\nFlow\n\n";
ss << "Primary Pump\n\n";
ss << "Power\nSpeed\nFlow\n\n";
ss << "State\nPower\nSpeed\nFlow\n\n";
ss << "Condenser\n\n";
ss << "Heat\n";
ss << "Steam\n";
@ -94,21 +94,16 @@ void primary_loop::init()
mesh1.bind();
mesh1.set(rmesh, GL_STATIC_DRAW);
rmesh.load_model("../assets/model", "primary_coolant_pump_switch.glb");
gm_switch_primary.bind();
gm_switch_primary.set(rmesh, GL_STATIC_DRAW);
rmesh.load_model("../assets/model", "secondary_coolant_pump_switch.glb");
gm_switch_secondary.bind();
gm_switch_secondary.set(rmesh, GL_STATIC_DRAW);
rmesh.load_model("../assets/model", "pump_switch_1.glb");
gm_switch_1.bind();
gm_switch_1.set(rmesh, GL_STATIC_DRAW);
m_joystick_turbine_bypass.load_model("../assets/model", "turbine_valve_bypass_joystick.stl");
m_joystick_turbine_inlet.load_model("../assets/model", "turbine_valve_inlet_joystick.stl");
m_switch_primary.load_model("../assets/model", "primary_coolant_pump_switch.stl");
m_switch_secondary.load_model("../assets/model", "secondary_coolant_pump_switch.stl");
m_switch_1.load_model("../assets/model", "pump_switch_click_1.stl");
}
void primary_loop::update()
void primary_loop::update(double dt)
{
std::stringstream ss;
sim::graphics::mesh rmesh;
@ -122,6 +117,7 @@ void primary_loop::update()
ss << show( sys.turbine_inlet_valve->get_flow() / 1000 ) << " kg/s\n";
ss << "\n\n\n";
ss << sys.primary_pump->get_state_string() << "\n";
ss << show( sys.primary_pump->get_power() / 1000 ) << " kW\n";
ss << show( sys.primary_pump->get_rpm() ) << " r/min\n";
ss << show( sys.primary_pump->get_flow_mass() / 1000 ) << " kg/s\n";
ss << "\n\n\n";
@ -138,7 +134,7 @@ void primary_loop::update()
focus::set(std::make_unique<valve_joystick>(sys.turbine_bypass_valve.get()));
if(m_joystick_turbine_inlet.check_focus())
focus::set(std::make_unique<valve_joystick>(sys.turbine_inlet_valve.get()));
if(m_switch_primary.check_focus())
if(m_switch_1.check_focus())
toggle_primary_pump();
}
@ -152,12 +148,8 @@ void primary_loop::render()
mesh2.uniform();
mesh2.render();
gm_switch_primary.bind();
gm_switch_primary.uniform();
gm_switch_primary.render();
gm_switch_secondary.bind();
gm_switch_secondary.uniform();
gm_switch_secondary.render();
gm_switch_1.bind();
gm_switch_1.uniform();
gm_switch_1.render();
}

View File

@ -10,13 +10,11 @@ class primary_loop
{
sim::graphics::glmesh mesh1, mesh2;
sim::graphics::glmesh gm_switch_primary;
sim::graphics::glmesh gm_switch_secondary;
sim::graphics::glmesh gm_switch_1;
sim::graphics::mesh m_joystick_turbine_bypass;
sim::graphics::mesh m_joystick_turbine_inlet;
sim::graphics::mesh m_switch_primary;
sim::graphics::mesh m_switch_secondary;
sim::graphics::mesh m_switch_1;
void toggle_primary_pump();
@ -24,7 +22,7 @@ public:
primary_loop();
void init();
void update();
void update(double dt);
void render();
};

View File

@ -0,0 +1,114 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "helpers.hpp"
#include "secondary_loop.hpp"
#include "../locations.hpp"
#include "../../system.hpp"
#include "../../coolant/valve.hpp"
#include "../input/focus.hpp"
#include <glm/ext/matrix_transform.hpp>
#include <iostream>
using namespace sim::graphics;
using namespace sim::graphics::monitor;
secondary_loop::secondary_loop()
{
}
void secondary_loop::toggle_secondary_pump()
{
system& sys = sim::system::active;
static bool state = false;
state = !state;
// sys.secondary_pump->powered = state = !sys.secondary_pump->powered;
gm_switch_2.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, state ? 0.07 : 0, 0));
}
void secondary_loop::toggle_freight_pump()
{
system& sys = sim::system::active;
static bool state = false;
state = !state;
// sys.freight_pump->powered = state = !sys.freight_pump->powered;
gm_switch_3.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, state ? 0.07 : 0, 0));
}
void secondary_loop::init()
{
mesh1.model_matrix = locations::monitors[5];
mesh2.model_matrix = glm::translate(mesh1.model_matrix, glm::vec3(0.5, 0, 0));
mesh1.colour_matrix = mesh2.colour_matrix = {
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
std::stringstream ss;
sim::graphics::mesh rmesh;
ss << "Secondary Loop\n";
rmesh.load_text(ss.str().c_str(), 0.04);
mesh1.bind();
mesh1.set(rmesh, GL_STATIC_DRAW);
rmesh.load_model("../assets/model", "pump_switch_2.glb");
gm_switch_2.bind();
gm_switch_2.set(rmesh, GL_STATIC_DRAW);
rmesh.load_model("../assets/model", "pump_switch_3.glb");
gm_switch_3.bind();
gm_switch_3.set(rmesh, GL_STATIC_DRAW);
m_joystick_turbine_bypass.load_model("../assets/model", "turbine_valve_bypass_joystick.stl");
m_joystick_turbine_inlet.load_model("../assets/model", "turbine_valve_inlet_joystick.stl");
m_switch_2.load_model("../assets/model", "pump_switch_click_2.stl");
m_switch_3.load_model("../assets/model", "pump_switch_click_3.stl");
}
void secondary_loop::update(double dt)
{
std::stringstream ss;
sim::graphics::mesh rmesh;
system& sys = sim::system::active;
//TODO
ss << "TODO\n";
rmesh.load_text(ss.str().c_str(), 0.04);
mesh2.bind();
mesh2.set(rmesh, GL_DYNAMIC_DRAW);
if(m_switch_2.check_focus())
toggle_secondary_pump();
if(m_switch_3.check_focus())
toggle_freight_pump();
}
void secondary_loop::render()
{
mesh1.bind();
mesh1.uniform();
mesh1.render();
mesh2.bind();
mesh2.uniform();
mesh2.render();
gm_switch_2.bind();
gm_switch_2.uniform();
gm_switch_2.render();
gm_switch_3.bind();
gm_switch_3.uniform();
gm_switch_3.render();
}

View File

@ -0,0 +1,33 @@
#pragma once
#include "../mesh/glmesh.hpp"
namespace sim::graphics::monitor
{
class secondary_loop
{
sim::graphics::glmesh mesh1, mesh2;
sim::graphics::glmesh gm_switch_2;
sim::graphics::glmesh gm_switch_3;
sim::graphics::mesh m_joystick_turbine_bypass;
sim::graphics::mesh m_joystick_turbine_inlet;
sim::graphics::mesh m_switch_2;
sim::graphics::mesh m_switch_3;
void toggle_secondary_pump();
void toggle_freight_pump();
public:
secondary_loop();
void init();
void update(double dt);
void render();
};
};

View File

@ -51,12 +51,11 @@ void vessel::init()
mesh1.set(rmesh, GL_STATIC_DRAW);
}
void vessel::update()
void vessel::update(double dt)
{
sim::system& sys = sim::system::active;
std::stringstream ss;
sim::graphics::mesh rmesh;
sim::system& sys = sim::system::active;
double temp_min, temp_max;
double crod_min = INFINITY, crod_max = -INFINITY;

View File

@ -14,7 +14,7 @@ public:
vessel();
void init();
void update();
void update(double dt);
void render();
};

View File

@ -21,6 +21,7 @@
#include "monitor/vessel.hpp"
#include "monitor/core.hpp"
#include "monitor/primary_loop.hpp"
#include "monitor/secondary_loop.hpp"
#include "mesh/texture.hpp"
#include "ui.hpp"
@ -33,6 +34,7 @@ static glmesh MeshScene;
static monitor::vessel MonitorVessel;
static monitor::core MonitorCore;
static monitor::primary_loop MonitorPrimaryLoop;
static monitor::secondary_loop MonitorSecondaryLoop;
glm::mat4 window::projection_matrix;
@ -116,6 +118,7 @@ void window::create()
MonitorCore.init();
MonitorVessel.init();
MonitorPrimaryLoop.init();
MonitorSecondaryLoop.init();
glfwShowWindow(win);
glViewport(0, 0, 800, 600);
@ -125,9 +128,10 @@ void window::update(double dt)
{
glfwPollEvents();
MonitorCore.update();
MonitorVessel.update();
MonitorPrimaryLoop.update();
MonitorCore.update(dt);
MonitorVessel.update(dt);
MonitorPrimaryLoop.update(dt);
MonitorSecondaryLoop.update(dt);
ui::update(dt);
}
@ -150,6 +154,7 @@ void window::render()
MonitorCore.render();
MonitorVessel.render();
MonitorPrimaryLoop.render();
MonitorSecondaryLoop.render();
ui::render();

View File

@ -35,14 +35,14 @@ system::system()
" C C C C "
};
vessel = std::make_unique<reactor::coolant::vessel>(sim::coolant::WATER, 8, 10, 6e6, 300);
vessel = std::make_unique<reactor::coolant::vessel>(sim::coolant::WATER, 8, 10, 6e6, 500);
reactor = std::make_unique<reactor::reactor>(sim::reactor::builder(19, 19, 1.0 / 4.0, 4, reactor::fuel::fuel_rod(0.5), vessel.get(), layout));
condenser = std::make_unique<coolant::condenser>(sim::coolant::WATER, 8, 6, 3e6, 200);
condenser = std::make_unique<coolant::condenser>(sim::coolant::WATER, 8, 6, 3e6, 0);
turbine = std::make_unique<electric::turbine>(sim::coolant::WATER, condenser.get(), 6, 3, 2e6);
turbine_inlet_valve = std::make_unique<coolant::valve>(vessel.get(), turbine.get(), 0, 1e-2);
turbine_bypass_valve = std::make_unique<coolant::valve>(vessel.get(), condenser.get(), 0, 1e-2);
primary_pump = std::make_unique<coolant::pump>(condenser.get(), vessel.get(), 1e6, 1, 1e6, 1, 10);
primary_pump = std::make_unique<coolant::pump>(condenser.get(), vessel.get(), 1e5, 1, 1e4, 0.1, 10);
}
system::system(system&& o)

View File

@ -25,45 +25,13 @@
#include "pid.hpp"
using namespace std;
class PIDImpl
{
public:
PIDImpl( double dt, double max, double min, double Kp, double Kd, double Ki );
~PIDImpl();
double calculate( double setpoint, double pv );
private:
double _dt;
double _max;
double _min;
double _Kp;
double _Kd;
double _Ki;
double _pre_error;
double _integral;
};
PID::PID( double dt, double max, double min, double Kp, double Kd, double Ki )
{
pimpl = new PIDImpl(dt,max,min,Kp,Kd,Ki);
}
double PID::calculate( double setpoint, double pv )
{
return pimpl->calculate(setpoint,pv);
}
PID::~PID()
{
delete pimpl;
}
using namespace sim::util;
/**
* Implementation
*/
PIDImpl::PIDImpl( double dt, double max, double min, double Kp, double Kd, double Ki ) :
_dt(dt),
PID::PID( double max, double min, double Kp, double Ki, double Kd ) :
_max(max),
_min(min),
_Kp(Kp),
@ -74,9 +42,8 @@ PIDImpl::PIDImpl( double dt, double max, double min, double Kp, double Kd, doubl
{
}
double PIDImpl::calculate( double setpoint, double pv )
double PID::calculate( double dt, double setpoint, double pv )
{
// Calculate error
double error = setpoint - pv;
@ -84,11 +51,11 @@ double PIDImpl::calculate( double setpoint, double pv )
double Pout = _Kp * error;
// Integral term
_integral += error * _dt;
_integral += error * dt;
double Iout = _Ki * _integral;
// Derivative term
double derivative = (error - _pre_error) / _dt;
double derivative = (error - _pre_error) / dt;
double Dout = _Kd * derivative;
// Calculate total output
@ -106,7 +73,3 @@ double PIDImpl::calculate( double setpoint, double pv )
return output;
}
PIDImpl::~PIDImpl()
{
}

View File

@ -22,7 +22,9 @@
#pragma once
class PIDImpl;
namespace sim::util
{
class PID
{
public:
@ -32,13 +34,23 @@ class PID
// dt - loop interval time
// max - maximum value of manipulated variable
// min - minimum value of manipulated variable
PID( double dt, double max, double min, double Kp, double Kd, double Ki );
PID( double max, double min, double Kp, double Ki, double Kd );
// Returns the manipulated variable given a setpoint and current process value
double calculate( double setpoint, double pv );
~PID();
// Returns the manipulated variable, given
// dt - loop interval time
// sp - current setpoint
// pv - current process value
double calculate( double dt, double sp, double pv );
private:
PIDImpl *pimpl;
double _max;
double _min;
double _Kp;
double _Kd;
double _Ki;
double _pre_error;
double _integral;
};
};