Commit 19273c48 authored by fmg005's avatar fmg005

clean up

parent 0c9599fa
......@@ -10,8 +10,8 @@ m_pumps(pumps), m_action(logger, pumps) {
void HardwareSimulator::update(std::unique_ptr<PhysiologyEngine>& engine) {
/*
* synchronize simulation time to real clock
* i.e., 1s of Simulation = 1s of real-time
*/
* i.e., 1s of Simulation = 1s of real-time
*/
m_start = system_clock::now(); // start
engine->AdvanceModelTime(advance_time, TimeUnit::s);
m_engine_advance_end = system_clock::now();
......@@ -29,11 +29,11 @@ void HardwareSimulator::update(std::unique_ptr<PhysiologyEngine>& engine) {
SetOxygenSat(oxygen_sat);
m_command_end = system_clock::now();
/*publish to dummy monitor topic for algorithm to capture
/* publish data to dummy monitor topic for algorithm to capture
* i.e. systolic/diastolic
*/
m_monitor->publish_patient_data(to_string(systolic_pressure)+"/" \
+to_string(diastolic_pressure));
+to_string(diastolic_pressure));
/* perform infusions whenever the pump rate changes */
m_infusion_start = system_clock::now();
......@@ -44,11 +44,11 @@ void HardwareSimulator::update(std::unique_ptr<PhysiologyEngine>& engine) {
m_next_update_time = system_clock::to_time_t(m_start) + m_next_start_in.count();
/* compute durations */
m_engine_duration_ms = duration_cast<milliseconds>(m_engine_advance_end -\
m_start).count();
m_start).count();
m_command_duration_ms = duration_cast<milliseconds>(m_command_end -\
m_command_start).count();
m_command_start).count();
m_infusion_duration_ms = duration_cast<milliseconds>(m_infusion_end -\
m_infusion_start).count();
m_infusion_start).count();
m_next_update_time_duration_ms = duration_cast<milliseconds>(m_next_start_in).count();
/* total duration */
m_total_duration_ms = duration_cast<milliseconds>(m_infusion_end - m_start).count();
......@@ -63,12 +63,12 @@ void HardwareSimulator::update(std::unique_ptr<PhysiologyEngine>& engine) {
if (m_total_duration_ms > m_next_update_time_duration_ms)
m_logger->error("Simulation too slow");
/* wait until start time of next execution is reached i.e., time:s of real-time */
/* wait out the remaining time before starting the next execution */
while(true) {
m_delta = m_next_update_time - system_clock::to_time_t(system_clock::now());
if (m_delta <= 0)
break;
std::this_thread::sleep_for(milliseconds(m_delta));
m_delta = m_next_update_time - system_clock::to_time_t(system_clock::now());
if (m_delta <= 0)
break;
std::this_thread::sleep_for(milliseconds(m_delta));
}
/* track data */
engine->GetEngineTracker()->TrackData(engine->GetSimulationTime(TimeUnit::s));
......@@ -77,9 +77,10 @@ void HardwareSimulator::update(std::unique_ptr<PhysiologyEngine>& engine) {
void HardwareSimulator::LoadConfig(std::unique_ptr<PhysiologyEngine>& engine) {
/* pulse engine advance time */
advance_time = m_env.pulse_advance_time;
/* advance in realtime the `secs` rate*/
secs = m_env.prosim_advance_time;
m_next_start_in = duration<double>(secs); /* convert to duration seconds */
/* run update method at rate of `secs` in realtime*/
secs = m_env.prosim_real_time;
/* convert to duration seconds */
m_next_start_in = duration<double>(secs);
/*intialize action -> prepare for infusions */
m_action.initialize(engine);
}
......@@ -92,16 +93,16 @@ bool HardwareSimulator::isOpen() {
void HardwareSimulator::SetHeartRate(double rate) {
int heart_rate = static_cast<int>(rate);
if (rate >= 10 && rate < 100) {
SendCommand("NSRA=0"+to_string(heart_rate));
m_logger->info("Heart Rate: "+to_string(heart_rate)+" bpm");
SendCommand("NSRA=0"+to_string(heart_rate));
m_logger->info("Heart Rate: "+to_string(heart_rate)+" bpm");
}
else if (rate > 99 && rate <= 360) {
SendCommand("NSRA="+to_string(heart_rate));
m_logger->info("Heart Rate: "+to_string(heart_rate)+" bpm");
SendCommand("NSRA="+to_string(heart_rate));
m_logger->info("Heart Rate: "+to_string(heart_rate)+" bpm");
}
else {
m_logger->warn("Heart rate value: "+to_string(heart_rate)+" not within \
acceptable range 10-360");
m_logger->warn("Heart Rate value: "+to_string(heart_rate)+" not within \
acceptable range 10-360");
}
}
......@@ -109,16 +110,16 @@ void HardwareSimulator::SetHeartRate(double rate) {
void HardwareSimulator::SetRespRate(double rate) {
int resp_rate = static_cast<int>(rate);
if (rate >= 10 && rate < 100 ) {
SendCommand("RESPRATE=0"+std::to_string(resp_rate));
m_logger->info("Resp Rate: "+to_string(resp_rate)+" bprm");
SendCommand("RESPRATE=0"+std::to_string(resp_rate));
m_logger->info("Resp Rate: "+to_string(resp_rate)+" bprm");
}
else if(rate > 99 && rate <=150) {
SendCommand("RESPRATE="+std::to_string(resp_rate));
m_logger->info("Resp Rate: "+to_string(resp_rate)+" bprm");
SendCommand("RESPRATE="+std::to_string(resp_rate));
m_logger->info("Resp Rate: "+to_string(resp_rate)+" bprm");
}
else {
m_logger->warn("Respiratory rate value: "+to_string(resp_rate)+" not \
within acceptable range 10-150");
m_logger->warn("Respiratory rate value: "+to_string(resp_rate)+" not \
within acceptable range 10-150");
}
}
......@@ -183,19 +184,19 @@ void HardwareSimulator::SetIBP(int channel, double systolic_p, double diastolic_
}
}
else {
m_logger->warn("One of the set IBP values: "+s_syst+"/"+s_diast+" is not \
within acceptable range 0-300");
m_logger->warn("One of the set IBP values: "+s_syst+"/"+s_diast+" is \
not within acceptable range 0-300");
}
}
void HardwareSimulator::SetRemoteMode() {
SendCommand("REMOTE");
m_logger->debug("Device switched to REMOTE mode");
m_logger->debug("ProSim simulator switched to REMOTE mode");
}
void HardwareSimulator::SetLocalMode() {
SendCommand("LOCAL");
m_logger->debug("Device switched to LOCAL mode");
m_logger->debug("ProSim simulator switched to LOCAL mode");
}
string HardwareSimulator::GetCurrentMode() {
......
......@@ -33,7 +33,7 @@
int main() {
CLA::Timer timer; /* Time entire simulation duration*/
CLA::Timer timer; /* time how long the simulation takes to run*/
auto cf = std::make_shared<Config>();
string f_path = prosim_config_dir+"scenario.cfg";
const char* filepath = f_path.c_str();
......@@ -79,7 +79,7 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg)
bool START_DEVICES = false;
CLA::Environment sim_env;
std::shared_ptr<CLA::LOGGER> logger = std::make_shared<CLA::LOGGER>(patient_name,patient_name);
//logger->setLevel("INFO");/*DEFAULT: DEBUG; */
//logger->setLevel("INFO");/*DEFAULT: DEBUG */
Global_LoadConfig(logger, pe, cfg, sim_env);
/*serial instance arguments*/
std::string port = "/dev/ttyACM0"; // arbitrary port
......@@ -91,7 +91,7 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg)
serial::flowcontrol_t flowcontrol = serial::flowcontrol_hardware;
/*end serial instance arguments*/
serial::Serial myserial(port, baudrate, timeout, bytesize, parity, stopbits, flowcontrol);
std::map<std::string, CLA::Pump*> my_pumps; /* container for pumps */
std::map<std::string, CLA::Pump*> my_pumps; /* pumps */
/* Params: drug name, pump topic to subscribe to, type of data published on that topic*/
CLA::Pump bpdrug_pump(logger, "Norepinephrine", "/bpdrug_pumpout", "std_msgs/Float64");
CLA::Pump saline_pump(logger, "Saline", "/saline_pumpout", "std_msgs/Float64");
......@@ -123,44 +123,44 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg)
prosim.SetRemoteMode(); /* switch remote mode */
// stop when the set time is reached
while (sim_env.time_index <= sim_env.simulation_timesteps ) {
currentMAP = pe->GetCardiovascularSystem()->GetMeanArterialPressure(PressureUnit::mmHg);
// event_hemorrage_start
if(sim_env.time_index == sim_env.simulation_injury_start_timestep) {
// Hemorrhage Starts - instantiate a hemorrhage action and have the engine process it
logger->info("Hemorrhage Started");
logger->info("Patient hemorrhaging at a rate of "+ \
to_string((int)hemorrhage_rate)+ " mL/min");
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
//the rate of hemorrhage
hemorrhageLeg.GetRate().SetValue(hemorrhage_rate,VolumePerTimeUnit::mL_Per_min);
pe->ProcessAction(hemorrhageLeg);
STOP_HEMORRHAGE = true;
}
if( currentMAP <= initialMAP && STOP_HEMORRHAGE) {
logger->info("Hemorrhage stopped");
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
hemorrhageLeg.GetRate().SetValue(0,VolumePerTimeUnit::mL_Per_min);
pe->ProcessAction(hemorrhageLeg);
STOP_HEMORRHAGE = false;
START_DEVICES = true; /* Begin real time simulation */
/* update environment variables and switch to realtime */
sim_env.simulation_time = sim_env.simulation_realtime;
sim_env.simulation_timesteps = sim_env.time_index + \
sim_env.simulation_time/sim_env.pulse_advance_time;
logger->info("Realtime simulation started");
logger->info("Devices started");
}
/* We both track data and advance engine inside the update method */
if (START_DEVICES) {
prosim.update(pe);
}
/* This stops running once realtime starts */
if (!START_DEVICES) {
pe->GetEngineTracker()->TrackData(pe->GetSimulationTime(TimeUnit::s));
pe->AdvanceModelTime();
}
sim_env.time_index++;
}// End while looop
currentMAP = pe->GetCardiovascularSystem()->GetMeanArterialPressure(PressureUnit::mmHg);
// event_hemorrage_start
if(sim_env.time_index == sim_env.simulation_injury_start_timestep) {
// Hemorrhage Starts - instantiate a hemorrhage action and have the engine process it
logger->info("Hemorrhage Started");
logger->info("Patient hemorrhaging at a rate of "+ \
to_string((int)hemorrhage_rate)+ " mL/min");
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
//the rate of hemorrhage
hemorrhageLeg.GetRate().SetValue(hemorrhage_rate,VolumePerTimeUnit::mL_Per_min);
pe->ProcessAction(hemorrhageLeg);
STOP_HEMORRHAGE = true;
}
if( currentMAP <= initialMAP && STOP_HEMORRHAGE) {
logger->info("Hemorrhage stopped");
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
hemorrhageLeg.GetRate().SetValue(0,VolumePerTimeUnit::mL_Per_min);
pe->ProcessAction(hemorrhageLeg);
STOP_HEMORRHAGE = false;
START_DEVICES = true; /* Begin real time simulation */
/* update environment variables and switch to realtime */
sim_env.simulation_time = sim_env.simulation_realtime;
sim_env.simulation_timesteps = sim_env.time_index + \
sim_env.simulation_time/sim_env.pulse_advance_time;
logger->info("Realtime simulation started");
logger->info("Devices started");
}
/* We both track data and advance engine inside the update method */
if (START_DEVICES) {
prosim.update(pe);
}
/* This stops running once realtime starts */
if (!START_DEVICES) {
pe->GetEngineTracker()->TrackData(pe->GetSimulationTime(TimeUnit::s));
pe->AdvanceModelTime();
}
sim_env.time_index++;
}// End while looop
}// End Simulation function
void Global_LoadConfig(std::shared_ptr<CLA::LOGGER>& logger, std::unique_ptr<PhysiologyEngine>&
......@@ -169,7 +169,6 @@ engine, const std::shared_ptr<Config>& cf, CLA::Environment& env) {
env.simulation_realtime = cf->lookup("simulation.time.run");
}
catch(const SettingNotFoundException) {
cerr << "Setting Not Found: simulation.time.run" << endl;
logger->error("Setting Not Found: simulation.time.run");
exit(EXIT_FAILURE);
}
......@@ -177,7 +176,6 @@ engine, const std::shared_ptr<Config>& cf, CLA::Environment& env) {
env.simulation_time = cf->lookup("simulation.time.simulation_time_run");
}
catch(const SettingNotFoundException) {
cerr << "Setting Not Found: simulation.time.simulation_time_run" << endl;
logger->error("Setting Not Found: simulation.time.simulation_time_run");
exit(EXIT_FAILURE);
}
......@@ -186,7 +184,6 @@ engine, const std::shared_ptr<Config>& cf, CLA::Environment& env) {
env.simulation_injury_start = cf->lookup("simulation.time.injury_start");
}
catch(const SettingNotFoundException) {
cerr << "Setting Not Found: simulation.time.injury_start" << endl;
logger->error("Setting Not Found: simulation.time.injury_start");
exit(EXIT_FAILURE);
}
......@@ -195,7 +192,6 @@ engine, const std::shared_ptr<Config>& cf, CLA::Environment& env) {
env.simulation_injury_stop = cf->lookup("simulation.time.injury_stop");
}
catch(const SettingNotFoundException) {
cerr << "Setting Not Found: simulation.time.injury_stop" << endl;
logger->error("Setting Not Found: simulation.time.injury_stop");
exit(EXIT_FAILURE);
}
......@@ -203,15 +199,13 @@ engine, const std::shared_ptr<Config>& cf, CLA::Environment& env) {
env.pulse_advance_time = cf->lookup("pulse.advance_time");
}
catch(const SettingNotFoundException) {
cerr << "Setting Not Found: pulse.advance_time in Global_LoadConfig" << endl;
logger->error("Setting Not Found: pulse.advance_time");
exit(EXIT_FAILURE);
}
try {
env.prosim_advance_time = cf->lookup("prosim.real_time");
env.prosim_real_time = cf->lookup("prosim.real_time");
}
catch(const SettingNotFoundException) {
cerr << "Setting Not Found: prosim.real_time" << endl;
logger->error("Setting Not Found: prosim.real_time");
exit(EXIT_FAILURE);
}
......
......@@ -2,13 +2,11 @@
#ifndef ENVIRONMENT_H
#define ENVIRONMENT_H
#include <memory>
namespace CLA {
struct Environment {
double engine_timestep; // engine period
double pulse_advance_time;
double prosim_advance_time; // advance duration in realtime
double prosim_real_time; // advance duration in realtime
double simulation_time; // grab from config file in seconds
double simulation_realtime;
unsigned long simulation_timesteps; // how long should simulation run
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment