From 83737fae663e73582a581a7abb434deced790d18 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Tue, 6 Feb 2024 17:57:59 +1100 Subject: [PATCH] added PID to primary pump, working on secondary system --- assets/model/pump_switch_1.glb | 3 + assets/model/pump_switch_2.glb | 3 + assets/model/pump_switch_3.glb | 3 + assets/model/pump_switch_click_1.stl | Bin 0 -> 584 bytes assets/model/pump_switch_click_2.stl | Bin 0 -> 584 bytes assets/model/pump_switch_click_3.stl | Bin 0 -> 584 bytes assets/scene-baked.glb | 4 +- assets/unbaked/scene.blend | 4 +- assets/unbaked/scene/labels.png | 4 +- assets/unbaked/scene/labels.xcf | 4 +- src/coolant/condenser_secondary.cpp | 17 ++++ src/coolant/condenser_secondary.hpp | 22 +++++ src/coolant/evaporator.cpp | 20 +++++ src/coolant/evaporator.hpp | 20 +++++ src/coolant/pump.cpp | 63 ++++++------- src/coolant/pump.hpp | 18 ++-- src/coolant/sink.cpp | 14 +++ src/coolant/sink.hpp | 34 +++++++ src/electric/turbine.cpp | 2 +- src/graphics/input/keyboard.cpp | 13 +-- src/graphics/locations.cpp | 19 +++- src/graphics/locations.hpp | 2 +- src/graphics/monitor/core.cpp | 2 +- src/graphics/monitor/core.hpp | 2 +- src/graphics/monitor/primary_loop.cpp | 32 +++---- src/graphics/monitor/primary_loop.hpp | 8 +- src/graphics/monitor/secondary_loop.cpp | 114 ++++++++++++++++++++++++ src/graphics/monitor/secondary_loop.hpp | 33 +++++++ src/graphics/monitor/vessel.cpp | 5 +- src/graphics/monitor/vessel.hpp | 2 +- src/graphics/window.cpp | 11 ++- src/system.cpp | 6 +- src/util/pid.cpp | 47 ++-------- src/util/pid.hpp | 24 +++-- 34 files changed, 413 insertions(+), 142 deletions(-) create mode 100644 assets/model/pump_switch_1.glb create mode 100644 assets/model/pump_switch_2.glb create mode 100644 assets/model/pump_switch_3.glb create mode 100644 assets/model/pump_switch_click_1.stl create mode 100644 assets/model/pump_switch_click_2.stl create mode 100644 assets/model/pump_switch_click_3.stl create mode 100644 src/coolant/condenser_secondary.cpp create mode 100644 src/coolant/condenser_secondary.hpp create mode 100644 src/coolant/evaporator.cpp create mode 100644 src/coolant/evaporator.hpp create mode 100644 src/coolant/sink.cpp create mode 100644 src/coolant/sink.hpp create mode 100644 src/graphics/monitor/secondary_loop.cpp create mode 100644 src/graphics/monitor/secondary_loop.hpp diff --git a/assets/model/pump_switch_1.glb b/assets/model/pump_switch_1.glb new file mode 100644 index 0000000..7349c91 --- /dev/null +++ b/assets/model/pump_switch_1.glb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3173c61c356d50c3dab1428ba5248a89ac3c6ec7625777ab1dc73685efba58ab +size 2288 diff --git a/assets/model/pump_switch_2.glb b/assets/model/pump_switch_2.glb new file mode 100644 index 0000000..fadd4b1 --- /dev/null +++ b/assets/model/pump_switch_2.glb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fbc192f0fada6db983af27c7e0cd1376683dee5636ce2bef1042699fedd27f6e +size 2288 diff --git a/assets/model/pump_switch_3.glb b/assets/model/pump_switch_3.glb new file mode 100644 index 0000000..2a76b7a --- /dev/null +++ b/assets/model/pump_switch_3.glb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5d5dfa5402e80bc83145cf61a2cc734234da560940909db5237a5921d26ad45e +size 2284 diff --git a/assets/model/pump_switch_click_1.stl b/assets/model/pump_switch_click_1.stl new file mode 100644 index 0000000000000000000000000000000000000000..46029f2429341c0c2c5b761d3a528e310e435f33 GIT binary patch literal 584 zcmZ>*D9A4=Nlj5mE6UGRaLP%|OGz!#HPJKBGh!eIaDg>7?1$-1OPl6kY#i;7w^-61 z#y({v17@E+li~nkgVf^I0aFFi4Pt{#f|=d`W+BVK*dTLf&YTI>0kZ>a4~7n8RWLTp z4v6_6eaJE}Hp~u?|6q3D)&Wxm(hUm{xIgy8?10I@RDsMz4maF7kX6ChFn_>xfx`sZ TCFr3G^9Obv$e|0f1EK=}Q6i&i literal 0 HcmV?d00001 diff --git a/assets/model/pump_switch_click_2.stl b/assets/model/pump_switch_click_2.stl new file mode 100644 index 0000000000000000000000000000000000000000..873b920acfb24444ba8081f1736717962cd3c769 GIT binary patch literal 584 zcmbV|F$%&!5Jj!Mfnbpf7$j@&B6teh-K{i{#lkXo33!e4LGL6p;e|m><5nN$zxg}= zc)vFJ7DJJ8b14qzScjN5yK-Ari+|oyp|*#0uJg{FeBG_~=%HGxem#R1E#7$om(ZeR z6Wd!=c7m3@DLs@RLvy|e>q3(;?ibEZ&}7JcWXuz|gr*)DhdCTzQ8jKmumpqTss literal 0 HcmV?d00001 diff --git a/assets/model/pump_switch_click_3.stl b/assets/model/pump_switch_click_3.stl new file mode 100644 index 0000000000000000000000000000000000000000..7d09481ee45864db26eee46c91d0c6132d0e7273 GIT binary patch literal 584 zcmZ>*D9A4=Nlj5mE6UGRaLP%|OGz!#HPJKBGh!eIaDg>7>__O`;9zVV?U1)v(w>0< z!aijr17@E+li~njGce%Rfl#%<0mO!w1Tnt>XbuP<%Rty5bB&ERgLS~{0I9^(fvgI~ zhS>pA&A@;x17pMN0QnDQ2W}lORUqAn5CORf=6hrr7#m_Pa=78rfgZZ(c7W{xhY7Mv W&_fsI4~Q+G@IrS9vJRL@3=9C`4~~ui literal 0 HcmV?d00001 diff --git a/assets/scene-baked.glb b/assets/scene-baked.glb index f179956..e512f24 100644 --- a/assets/scene-baked.glb +++ b/assets/scene-baked.glb @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:28b515def72bcb7c1fb46ca68443b510565e9538032fd9320016953154f902f9 -size 59501212 +oid sha256:748d437c09a212c8f66cb47a61dcc17ccbbb67b4697f52d7af93177f71769fd0 +size 52246948 diff --git a/assets/unbaked/scene.blend b/assets/unbaked/scene.blend index cabd2ba..c93032a 100644 --- a/assets/unbaked/scene.blend +++ b/assets/unbaked/scene.blend @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:617293315153b7993db7beeaba58d2c30e5fc77e75b1d2447907541070357320 -size 12058488 +oid sha256:6d07ecf8050cb85754bd38a6d12225547cdb90f570f3993dacc048fb202d52e1 +size 10498676 diff --git a/assets/unbaked/scene/labels.png b/assets/unbaked/scene/labels.png index 3e2cb14..183f442 100644 --- a/assets/unbaked/scene/labels.png +++ b/assets/unbaked/scene/labels.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9387fc6ea27299ea12fcc2b27240e9443c168989d5a642740567a4a4b5680745 -size 48112 +oid sha256:c544574d9fa0b14faa254f856bdb691a914fc7dae1875def98bcc52ed6f14563 +size 61947 diff --git a/assets/unbaked/scene/labels.xcf b/assets/unbaked/scene/labels.xcf index ca10bf9..ccdbd0d 100644 --- a/assets/unbaked/scene/labels.xcf +++ b/assets/unbaked/scene/labels.xcf @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:faf2f6dc7b152f0322e515b3e859427cd0af4af13dcafdda50c84248cd804984 -size 140228 +oid sha256:1b4993babc6e4d39f41c0dba3d28eb7ecc77ccc4ef0c74721be817b117912b74 +size 186224 diff --git a/src/coolant/condenser_secondary.cpp b/src/coolant/condenser_secondary.cpp new file mode 100644 index 0000000..2a9e56f --- /dev/null +++ b/src/coolant/condenser_secondary.cpp @@ -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); +} + diff --git a/src/coolant/condenser_secondary.hpp b/src/coolant/condenser_secondary.hpp new file mode 100644 index 0000000..f9ba891 --- /dev/null +++ b/src/coolant/condenser_secondary.hpp @@ -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); +}; + +}; + diff --git a/src/coolant/evaporator.cpp b/src/coolant/evaporator.cpp new file mode 100644 index 0000000..f7413a8 --- /dev/null +++ b/src/coolant/evaporator.cpp @@ -0,0 +1,20 @@ + +#include "evaporator.hpp" + +#include + +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; +} + diff --git a/src/coolant/evaporator.hpp b/src/coolant/evaporator.hpp new file mode 100644 index 0000000..da9610a --- /dev/null +++ b/src/coolant/evaporator.hpp @@ -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); +}; + +}; + diff --git a/src/coolant/pump.cpp b/src/coolant/pump.cpp index 8291b94..b959f21 100644 --- a/src/coolant/pump.cpp +++ b/src/coolant/pump.cpp @@ -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; } diff --git a/src/coolant/pump.hpp b/src/coolant/pump.hpp index 30fda18..66fc4f4 100644 --- a/src/coolant/pump.hpp +++ b/src/coolant/pump.hpp @@ -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); diff --git a/src/coolant/sink.cpp b/src/coolant/sink.cpp new file mode 100644 index 0000000..0cf22c5 --- /dev/null +++ b/src/coolant/sink.cpp @@ -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) +{ + +} + diff --git a/src/coolant/sink.hpp b/src/coolant/sink.hpp new file mode 100644 index 0000000..6f2a1dc --- /dev/null +++ b/src/coolant/sink.hpp @@ -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 +}; + +}; + diff --git a/src/electric/turbine.cpp b/src/electric/turbine.cpp index d3ba6f4..1eb5a3d 100644 --- a/src/electric/turbine.cpp +++ b/src/electric/turbine.cpp @@ -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) diff --git a/src/graphics/input/keyboard.cpp b/src/graphics/input/keyboard.cpp index 391999b..98a9ca8 100644 --- a/src/graphics/input/keyboard.cpp +++ b/src/graphics/input/keyboard.cpp @@ -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; } } diff --git a/src/graphics/locations.cpp b/src/graphics/locations.cpp index a6b812a..06b19a3 100644 --- a/src/graphics/locations.cpp +++ b/src/graphics/locations.cpp @@ -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(-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(-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(-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(-90), glm::vec3(1, 0, 0)) * + glm::rotate(glm::mat4(1), glm::radians(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(-90), glm::vec3(1, 0, 0)) * + glm::rotate(glm::mat4(1), glm::radians(90), glm::vec3(0, 1, 0)) * + glm::scale(glm::mat4(1), glm::vec3(1.9, 1.9, 1.9)) ) }; diff --git a/src/graphics/locations.hpp b/src/graphics/locations.hpp index 26b1a7c..8ad8f46 100644 --- a/src/graphics/locations.hpp +++ b/src/graphics/locations.hpp @@ -6,7 +6,7 @@ namespace sim::graphics::locations { -extern const glm::mat4 monitors[4]; +extern const glm::mat4 monitors[7]; }; diff --git a/src/graphics/monitor/core.cpp b/src/graphics/monitor/core.cpp index 1cbe0a1..79e966d 100644 --- a/src/graphics/monitor/core.cpp +++ b/src/graphics/monitor/core.cpp @@ -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; diff --git a/src/graphics/monitor/core.hpp b/src/graphics/monitor/core.hpp index e6981f1..deaafc8 100644 --- a/src/graphics/monitor/core.hpp +++ b/src/graphics/monitor/core.hpp @@ -19,7 +19,7 @@ public: core(); void init(); - void update(); + void update(double dt); void render(); }; diff --git a/src/graphics/monitor/primary_loop.cpp b/src/graphics/monitor/primary_loop.cpp index 961eb8b..c57ee41 100644 --- a/src/graphics/monitor/primary_loop.cpp +++ b/src/graphics/monitor/primary_loop.cpp @@ -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(sys.turbine_bypass_valve.get())); if(m_joystick_turbine_inlet.check_focus()) focus::set(std::make_unique(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(); } diff --git a/src/graphics/monitor/primary_loop.hpp b/src/graphics/monitor/primary_loop.hpp index eb56eab..20d5fbe 100644 --- a/src/graphics/monitor/primary_loop.hpp +++ b/src/graphics/monitor/primary_loop.hpp @@ -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(); }; diff --git a/src/graphics/monitor/secondary_loop.cpp b/src/graphics/monitor/secondary_loop.cpp new file mode 100644 index 0000000..1d3a8c8 --- /dev/null +++ b/src/graphics/monitor/secondary_loop.cpp @@ -0,0 +1,114 @@ + +#include +#include + +#include "helpers.hpp" +#include "secondary_loop.hpp" +#include "../locations.hpp" +#include "../../system.hpp" +#include "../../coolant/valve.hpp" +#include "../input/focus.hpp" + +#include +#include + +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(); +} + + diff --git a/src/graphics/monitor/secondary_loop.hpp b/src/graphics/monitor/secondary_loop.hpp new file mode 100644 index 0000000..647f467 --- /dev/null +++ b/src/graphics/monitor/secondary_loop.hpp @@ -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(); +}; + +}; + diff --git a/src/graphics/monitor/vessel.cpp b/src/graphics/monitor/vessel.cpp index 45daa36..0ed310b 100644 --- a/src/graphics/monitor/vessel.cpp +++ b/src/graphics/monitor/vessel.cpp @@ -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; diff --git a/src/graphics/monitor/vessel.hpp b/src/graphics/monitor/vessel.hpp index 3906568..fce8e4d 100644 --- a/src/graphics/monitor/vessel.hpp +++ b/src/graphics/monitor/vessel.hpp @@ -14,7 +14,7 @@ public: vessel(); void init(); - void update(); + void update(double dt); void render(); }; diff --git a/src/graphics/window.cpp b/src/graphics/window.cpp index dedef36..5725c8f 100644 --- a/src/graphics/window.cpp +++ b/src/graphics/window.cpp @@ -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(); diff --git a/src/system.cpp b/src/system.cpp index 0805a3f..6cc8909 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -35,14 +35,14 @@ system::system() " C C C C " }; - vessel = std::make_unique(sim::coolant::WATER, 8, 10, 6e6, 300); + vessel = std::make_unique(sim::coolant::WATER, 8, 10, 6e6, 500); reactor = std::make_unique(sim::reactor::builder(19, 19, 1.0 / 4.0, 4, reactor::fuel::fuel_rod(0.5), vessel.get(), layout)); - condenser = std::make_unique(sim::coolant::WATER, 8, 6, 3e6, 200); + condenser = std::make_unique(sim::coolant::WATER, 8, 6, 3e6, 0); turbine = std::make_unique(sim::coolant::WATER, condenser.get(), 6, 3, 2e6); turbine_inlet_valve = std::make_unique(vessel.get(), turbine.get(), 0, 1e-2); turbine_bypass_valve = std::make_unique(vessel.get(), condenser.get(), 0, 1e-2); - primary_pump = std::make_unique(condenser.get(), vessel.get(), 1e6, 1, 1e6, 1, 10); + primary_pump = std::make_unique(condenser.get(), vessel.get(), 1e5, 1, 1e4, 0.1, 10); } system::system(system&& o) diff --git a/src/util/pid.cpp b/src/util/pid.cpp index a5b9ca8..1e50328 100644 --- a/src/util/pid.cpp +++ b/src/util/pid.cpp @@ -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() -{ -} - diff --git a/src/util/pid.hpp b/src/util/pid.hpp index c537f20..c85ad20 100644 --- a/src/util/pid.hpp +++ b/src/util/pid.hpp @@ -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; +}; + };