Commit 63e4ad58 authored by fmg005's avatar fmg005

Added provision for multiple pumps to be used

parent 09b31aae
......@@ -2,43 +2,47 @@
using namespace CLA;
Action::Action(std::shared_ptr<CLA::LOGGER>& logger) : m_logger(logger) {
Action::Action(std::shared_ptr<CLA::LOGGER>& logger, \
std::map<std::string,CLA::Pump*>& pumps) : m_logger(logger), m_pumps(pumps) {
NorepiConcentration = 16;
bpdrug_prev_rate = 0;
saline_prev_rate = 0;
SalineBagVolume = 500;
}
void Action::initialize(std::unique_ptr<PhysiologyEngine>& engine, const string&
substance) {
if (substance == "Norepinephrine") {
m_bpdrug = engine->GetSubstanceManager().GetSubstance(substance);
void Action::initialize(std::unique_ptr<PhysiologyEngine>& engine) {
if (m_pumps["bpdrug"]->getSubstance() != "") {
m_bpdrug = engine->GetSubstanceManager().GetSubstance(m_pumps["bpdrug"]->getSubstance());
m_bpdrug_infusion = new SESubstanceInfusion(*m_bpdrug);
m_bpdrug_infusion->GetConcentration().SetValue(NorepiConcentration, MassPerVolumeUnit::ug_Per_mL);
}
else if (substance == "Saline") {
m_saline = engine->GetSubstanceManager().GetCompound(substance);
if (m_pumps["saline"]->getSubstance() != "") {
m_saline = engine->GetSubstanceManager().GetCompound(m_pumps["saline"]->getSubstance());
m_infusion = new SESubstanceCompoundInfusion(*m_saline);
m_infusion->GetBagVolume().SetValue(SalineBagVolume, VolumeUnit::mL);
}
}
void Action::infuse_drug(std::unique_ptr<PhysiologyEngine>& engine, const string&
substance, double new_rate) {
if (substance == "Norepinephrine") {
if (bpdrug_prev_rate != new_rate && new_rate > 0) {
m_bpdrug_infusion->GetRate().SetValue(new_rate, VolumePerTimeUnit::mL_Per_hr);
void Action::infuse_drug(std::unique_ptr<PhysiologyEngine>& engine) {
m_bpdrug_rate = m_pumps["bpdrug"]->getCurrentInfusionRate();
m_saline_rate = m_pumps["saline"]->getCurrentInfusionRate();
if (m_pumps["bpdrug"]->getSubstance() != "") {
if (bpdrug_prev_rate != m_bpdrug_rate && m_bpdrug_rate > 0) {
m_bpdrug_infusion->GetRate().SetValue(m_bpdrug_rate, VolumePerTimeUnit::mL_Per_hr);
engine->ProcessAction(*m_bpdrug_infusion);
bpdrug_prev_rate = new_rate;
m_logger->info("currently "+substance+" infusing patient at rate a of "+to_string(int(new_rate))+" mL/hr");
bpdrug_prev_rate = m_bpdrug_rate;
m_logger->info(m_pumps["bpdrug"]->getSubstance()+": infusing patient at "\
+to_string(int(m_bpdrug_rate))+" mL/hr");
}
}
else if (substance == "Saline") {
if (saline_prev_rate != new_rate && new_rate > 0) {
m_infusion->GetRate().SetValue(new_rate, VolumePerTimeUnit::mL_Per_hr);
if (m_pumps["saline"]->getSubstance() != "") {
if (saline_prev_rate != m_saline_rate && m_saline_rate > 0) {
m_infusion->GetRate().SetValue(m_saline_rate, VolumePerTimeUnit::mL_Per_hr);
engine->ProcessAction(*m_infusion);
saline_prev_rate = new_rate;
m_logger->info("currently "+substance+" infusing patient at rate a of "+to_string(int(new_rate))+" mL/hr");
saline_prev_rate = m_saline_rate;
m_logger->info(m_pumps["saline"]->getSubstance()+": infusing patient at "\
+to_string(int(m_saline_rate))+" mL/hr");
}
}
}
......
......@@ -10,7 +10,8 @@ CLA::LOGGER::LOGGER(const string& name): m_patient_name(move(name)){
<<"Level"<<setw(9)<<"Message"<<endl;
m_logstream[1]<<"start time,"<<"engine adv duration(s),"<<"command duration(s),"\
<<"infusion duration(s),"<<"totals duration(s),"<<"next_update time"<<endl;
m_logstream[2]<<"Time,"<<"Systolic,"<<"Diastolic,"<<"MAP,"<<"Pump rate"<<endl;
m_logstream[2]<<"Time,"<<"Systolic,"<<"Diastolic,"<<"MAP,"<<"bpdrug rate,"\
<<"saline rate"<<endl;
/* mapping strings to levels */
logging_levels["DEBUG"] = CLA::LOGGER::DEBUG;
logging_levels["INFO"] = CLA::LOGGER::INFO;
......@@ -29,7 +30,8 @@ m_patient_name(move(name)) {
<<"Level"<<setw(9)<<"Message"<<endl;
m_logstream[1]<<"start time,"<<"engine adv duration(s),"<<"command duration(s),"\
<<"infusion duration(s),"<<"totals duration(s),"<<"next_update time"<<endl;
m_logstream[2]<<"Time,"<<"Systolic,"<<"Diastolic,"<<"MAP,"<<"Pump rate"<<endl;
m_logstream[2]<<"Time,"<<"Systolic,"<<"Diastolic,"<<"MAP,"<<"bpdrug rate,"\
<<"saline rate"<<endl;
/* mapping strings to levels */
logging_levels["DEBUG"] = CLA::LOGGER::DEBUG;
logging_levels["INFO"] = CLA::LOGGER::INFO;
......@@ -111,7 +113,7 @@ next_update_time) {
}
void CLA::LOGGER::log_data(system_clock::time_point _time, double systolic, \
double diastolic, double MAP, double rate) {
double diastolic, double MAP, double bpdrug_rate, double saline_rate) {
ss<< format_chrono_time(_time);
ss<< ",";
ss<< systolic;
......@@ -120,7 +122,9 @@ double diastolic, double MAP, double rate) {
ss<< ",";
ss<<MAP;
ss<<",";
ss<<rate;
ss<<bpdrug_rate;
ss<<",";
ss<<saline_rate;
m_logstream[2] << ss.str() << endl;
ss.str("");
}
......
......@@ -2,8 +2,8 @@
HardwareSimulator::HardwareSimulator( std::shared_ptr<CLA::LOGGER>& logger,
serial::Serial* my_serial, std::map<std::string,CLA::Pump*>& pumps):m_action(logger), m_serial(my_serial),
m_pumps(pumps) {
serial::Serial* my_serial, std::map<std::string,CLA::Pump*>& \
pumps):m_action(logger, pumps), m_serial(my_serial), m_pumps(pumps) {
m_monitor = new CLA::Monitor(logger, "/monitor", "std_msgs/Float64");
}
......@@ -11,7 +11,7 @@ m_pumps(pumps) {
HardwareSimulator::HardwareSimulator( std::shared_ptr<CLA::LOGGER>& logger,
serial::Serial* my_serial,CLA::Environment& env, std::map<std::string, CLA::Pump*>&
pumps) : m_serial(my_serial), m_logger(logger), m_env(env),
m_pumps(pumps), m_action(logger) {
m_pumps(pumps), m_action(logger, pumps) {
m_monitor = new CLA::Monitor(logger, "/monitor", "std_msgs/Float64");
}
......@@ -21,11 +21,11 @@ void HardwareSimulator::update(std::unique_ptr<PhysiologyEngine>& engine) {
* synchronize simulation time to real clock
* i.e., 1s of Simulation = 1s of real-time
*/
m_start = system_clock::now();
m_start = system_clock::now(); // start
engine->AdvanceModelTime(advance_time, TimeUnit::s);
//m_end = system_clock::now();
m_engine_advance_end = system_clock::now();
//m_command_start = system_clock::now();
m_command_start = system_clock::now();
systolic_pressure = engine->GetCardiovascularSystem()->GetSystolicArterialPressure(PressureUnit::mmHg);
diastolic_pressure = engine->GetCardiovascularSystem()->GetDiastolicArterialPressure(PressureUnit::mmHg);
heart_rate = engine->GetCardiovascularSystem()->GetHeartRate(FrequencyUnit::Per_min);
......@@ -33,20 +33,36 @@ void HardwareSimulator::update(std::unique_ptr<PhysiologyEngine>& engine) {
/* send patient data to monitor */
SetIBP(1, systolic_pressure, diastolic_pressure);
SetHeartRate(heart_rate);
m_monitor->send_patient_data(MAP); /*publish to dummy monitor topic*/
m_command_end = system_clock::now();
/*publish to dummy monitor topic for algorithm to capture */
m_monitor->send_patient_data(MAP);
/* perform infusions whenever the pump rate changes */
m_bpdrug_infusion_rate = m_pumps["bpdrug"]->getCurrentInfusionRate();
m_action.infuse_drug(engine, m_pumps["bpdrug"]->getSubstance(), m_bpdrug_infusion_rate);
m_end = system_clock::now();
m_infusion_start = system_clock::now();
m_action.infuse_drug(engine);
m_infusion_end = system_clock::now();
/* start time for the next execution */
m_next_update_time = system_clock::to_time_t(m_start) + m_next_start_in.count();
m_duration_ms = duration_cast<milliseconds>(m_end - m_start).count();
//m_command_duration_ms = duration_cast<milliseconds>(m_command_end - m_command_start).count();
/* compute durations */
m_engine_duration_ms = duration_cast<milliseconds>(m_engine_advance_end -\
m_start).count();
m_command_duration_ms = duration_cast<milliseconds>(m_command_end -\
m_command_start).count();
m_infusion_duration_ms = duration_cast<milliseconds>(m_infusion_end -\
m_infusion_start).count();
m_next_update_time_duration_ms = duration_cast<milliseconds>(m_next_start_in).count();
/* capture durations */
m_logger->log_duration(m_start, m_end, m_duration_ms, m_next_update_time);
/* log data */
m_logger->log_data(m_start, systolic_pressure, diastolic_pressure, MAP, \
m_pumps["bpdrug"]->getCurrentInfusionRate(), \
m_pumps["saline"]->getCurrentInfusionRate());
/* end timing */
m_end = system_clock::now();
m_duration_ms = duration_cast<milliseconds>(m_end - m_start).count();
/* durations */
m_logger->log_duration(m_start,m_engine_duration_ms,m_command_duration_ms, \
m_infusion_duration_ms, m_duration_ms, m_next_update_time);
/* checking if simulation will take longer than allotted 'time' */
if (m_duration_ms > m_next_update_time_duration_ms)
m_logger->error("Simulation too slow");
......@@ -54,7 +70,7 @@ void HardwareSimulator::update(std::unique_ptr<PhysiologyEngine>& engine) {
while(true) {
m_delta = m_next_update_time - system_clock::to_time_t(system_clock::now());
if (m_delta <= 0)
break;
break;
std::this_thread::sleep_for(milliseconds(m_delta));
}
/* track data */
......@@ -66,9 +82,9 @@ void HardwareSimulator::LoadConfig(std::unique_ptr<PhysiologyEngine>& engine) {
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); // seconds
/*intialize action */
m_action.initialize(engine, m_pumps["bpdrug"]->getSubstance());
m_next_start_in = duration<double>(secs); /* cast to duration seconds */
/*intialize action -> prepare for infusions */
m_action.initialize(engine);
}
bool HardwareSimulator::isOpen() {
......@@ -80,15 +96,17 @@ 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->debug("Heart Rate: "+to_string(heart_rate)+" bpm");
m_logger->info("Heart Rate: "+to_string(heart_rate)+" bpm");
}
else if (rate > 99 && rate <= 360) {
SendCommand("NSRA="+to_string(heart_rate));
m_logger->debug("Heart Rate: "+to_string(heart_rate)+" bpm");
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");
cerr << "Heart rate value: "+to_string(heart_rate)+" not within acceptable range 10-360"<<endl;
m_logger->warn("Heart rate value: "+to_string(heart_rate)+" not within \
acceptable range 10-360");
cerr << "Heart rate value: "+to_string(heart_rate)+" not within acceptable \
range 10-360"<<endl;
}
}
......@@ -97,14 +115,15 @@ 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->debug("Resp Rate: "+to_string(resp_rate)+" bprm");
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->debug("Resp Rate: "+to_string(resp_rate)+" bprm");
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");
}
}
......@@ -119,18 +138,19 @@ void HardwareSimulator::SetOxygenSat(double rate) {
if (rate >= 0 && rate < 10) {
SendCommand("SAT=00"+s_rate);
m_logger->debug("O2 Sat: "+ to_string(o2_rate));
m_logger->info("O2 Sat: "+ to_string(o2_rate));
}
else if(rate > 10 && rate < 100 ) {
SendCommand("SAT=0"+s_rate);
m_logger->debug("O2 Sat: "+ to_string(o2_rate));
m_logger->info("O2 Sat: "+ to_string(o2_rate));
}
else if(rate == 100 ) {
SendCommand("SAT="+s_rate);
m_logger->debug("O2 Sat: "+ to_string(o2_rate));
m_logger->info("O2 Sat: "+ to_string(o2_rate));
}
else {
m_logger->warn("Oxygen saturation value: "+to_string(o2_rate)+" not within acceptable range 0-100");
m_logger->warn("Oxygen saturation value: "+to_string(o2_rate)+" not \
within acceptable range 0-100");
}
}
......@@ -148,25 +168,28 @@ void HardwareSimulator::SetIBP(int channel, double systolic_p, double diastolic_
}
else if (syst > 99 && diast > 99 && syst > diast) {
SendCommand("IBPP="+s_channel+","+s_syst+","+s_diast);
m_logger->debug("IBP: "+s_syst+ "/" +s_diast+ " mmHg");
m_logger->info("IBP: "+s_syst+ "/" +s_diast+ " mmHg");
}
else if (syst > 99 && diast < 10) {
m_logger->warn("Invalid range between Systolic and Diastolic Pressure: "+s_syst+"/"+s_diast);
m_logger->warn("Invalid range between Systolic and Diastolic Pressure: \
"+s_syst+"/"+s_diast);
}
else if (syst > 99 && diast > 9 && diast < 100 && syst > diast) {
SendCommand("IBPP="+s_channel+","+s_syst+","+"0"+s_diast);
m_logger->debug("IBP: " +s_syst+ "/" +s_diast +" mmHg");
m_logger->info("IBP: " +s_syst+ "/" +s_diast +" mmHg");
}
else if ( diast > syst) {
m_logger->warn("Systolic can't be less than Diastolic pressure: "+s_syst+"/"+s_diast);
m_logger->warn("Systolic can't be less than Diastolic pressure: \
"+s_syst+"/"+s_diast);
}
else if (syst > diast) {
SendCommand("IBPP="+s_channel+","+"0"+s_syst+","+"0"+s_diast);
m_logger->debug("IBP: " +s_syst+ "/" +s_diast+ " mmHg");
m_logger->info("IBP: " +s_syst+ "/" +s_diast+ " mmHg");
}
}
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");
}
}
......
......@@ -5,17 +5,12 @@ using namespace CLA;
/* wrapper for tcp client */
Pump::Pump(std::shared_ptr<CLA::LOGGER>& logger, const string& substance,
const string& topic, const string& type) : m_logger(logger), m_substance(substance),
m_tcp_pump_client(topic, type, logger) {
m_tcp_pump_client(topic, type, substance, logger) {
m_tcp_pump_client.setup_connection(); /* setup connection to server via a tcp socket */
}
double Pump::getCurrentInfusionRate () {
if (m_substance == "Norepinephrine") {
current_rate = m_tcp_pump_client.getCurrentInfusionRate(m_substance);
}
else if (m_substance == "Saline") {
current_rate = m_tcp_pump_client.getCurrentInfusionRate(m_substance);
}
current_rate = m_tcp_pump_client.getCurrentInfusionRate(m_substance);
return current_rate;
}
......
......@@ -9,10 +9,7 @@
#include "SimulationEngine.h"
#include "Timer.h"
#include "Pump.h"
#include "tcp_pump_client.h"
#include "Monitor.h"
// Include the various types you will be using in your code
// Include the various Pulse types you will be using in your code
#include "scenario/SEDataRequestManager.h"
#include "patient/actions/SEHemorrhage.h"
#include "patient/actions/SESubstanceCompoundInfusion.h"
......@@ -75,17 +72,17 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg)
pe->GetEngineTracker()->GetDataRequestManager().SetResultsFilename(prosim_results_dir+"PulseSimEngine_"+patient_name+".txt");
SEHemorrhage hemorrhageLeg;
double initialMAP = 65.0;
double initialMAP = 70.0;
double currentMAP;
bool START_HEMORRHAGE = true;
bool STOP_HEMORRHAGE = false;
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; */
Global_LoadConfig( logger, pe, cfg, sim_env);
//logger->setLevel("INFO");/*DEFAULT: DEBUG; */
Global_LoadConfig(logger, pe, cfg, sim_env);
/*serial instance arguments*/
std::string port = "/dev/ttyACM1"; // arbitrary port
std::string port = "/dev/ttyACM0"; // arbitrary port
uint32_t baudrate = 115200;
serial::Timeout timeout = serial::Timeout();
serial::bytesize_t bytesize = serial::eightbits;
......@@ -96,18 +93,20 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg)
serial::Serial myserial(port, baudrate, timeout, bytesize, parity, stopbits, flowcontrol);
std::map<std::string, CLA::Pump*> my_pumps; /* container for pumps */
/* Params: drug name, pump topic to subscribe to, type of data published on that topic*/
CLA::Pump bpdrug_pump(logger, "Norepinephrine", "/pumpout", "std_msgs/Float64");
CLA::Pump bpdrug_pump(logger, "Norepinephrine", "/bpdrug_pumpout", "std_msgs/Float64");
CLA::Pump saline_pump(logger, "Saline", "/saline_pumpout", "std_msgs/Float64");
my_pumps["bpdrug"] = &bpdrug_pump;
/* Dummy monitor */
//CLA::Monitor monitor(logger, "/monitor", "std_msgs/Float64");
my_pumps["saline"] = &saline_pump;
/* prosim hardware */
HardwareSimulator prosim(logger, &myserial, sim_env, my_pumps);
prosim.LoadConfig(pe);
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 && START_HEMORRHAGE) {
if(sim_env.time_index == sim_env.simulation_injury_start_timestep && \
START_HEMORRHAGE) {
// Hemorrhage Starts - instantiate a hemorrhage action and have the engine process it
logger->info("Hemorrhage Started");
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
......@@ -116,8 +115,6 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg)
pe->ProcessAction(hemorrhageLeg);
STOP_HEMORRHAGE = true;
}
currentMAP = pe->GetCardiovascularSystem()->GetMeanArterialPressure(PressureUnit::mmHg);
if( currentMAP <= initialMAP && STOP_HEMORRHAGE) {
logger->info("Hemorrhage stopped");
hemorrhageLeg.SetCompartment(pulse::VascularCompartment::RightLeg);
......
......@@ -5,6 +5,7 @@
#include <string>
#include <map>
#include "CLA_Logger.h"
#include "Pump.h"
/* pulse stuff */
#include "PulsePhysiologyEngine.h"
#include "patient/actions/SESubstanceCompoundInfusion.h"
......@@ -25,16 +26,19 @@ namespace CLA {
SESubstanceInfusion* m_bpdrug_infusion;
SESubstanceCompoundInfusion* m_infusion;
SESubstanceCompound* m_saline;
std::map<std::string,CLA::Pump*>& m_pumps;
double bpdrug_prev_rate; /* cache the last received rate */
double saline_prev_rate; /* cache the last received rate */
double NorepiConcentration;
double SalineBagVolume;
double m_bpdrug_rate;
double m_saline_rate;
std::shared_ptr<CLA::LOGGER> m_logger;
public:
Action(std::shared_ptr<CLA::LOGGER>&);
Action(std::shared_ptr<CLA::LOGGER>&, std::map<std::string,CLA::Pump*>&);
~Action();
void infuse_drug(std::unique_ptr<PhysiologyEngine>&, const string&, double);
void initialize(std::unique_ptr<PhysiologyEngine>&, const string&);
void infuse_drug(std::unique_ptr<PhysiologyEngine>&);
void initialize(std::unique_ptr<PhysiologyEngine>&);
void clear();
};
}
......
......@@ -43,7 +43,8 @@ namespace CLA {
~LOGGER();
void log_duration(system_clock::time_point, time_t, time_t, time_t, \
time_t, time_t);
void log_data(system_clock::time_point, double, double, double, double);
void log_data(system_clock::time_point, double, double, double, double, \
double);
void setLevel(const string&);
void setVerbosity(bool);
template <typename T>
......@@ -80,7 +81,7 @@ namespace CLA {
ss.str("");
}
}
template <typename T>
void warn(const T& t) {
if (LOGGER::WARN >= DEFAULT_LEVEL) {
......
......@@ -14,7 +14,6 @@
#include "Action.h"
#include "Pump.h"
#include "Monitor.h"
#include "tcp_monitor_client.h"
/* Pulse header files */
#include "CommonDataModel.h"
......@@ -45,23 +44,26 @@ class HardwareSimulator {
double MAP;
double advance_time;
double secs;
double m_bpdrug_infusion_rate;
string mode;
/* chrono */
system_clock::time_point m_start;
system_clock::time_point m_engine_advance_end;
system_clock::time_point m_command_start;
system_clock::time_point m_command_end;
system_clock::time_point m_infusion_start;
system_clock::time_point m_infusion_end;
system_clock::time_point m_end;
//system_clock::time_point m_command_start;
//system_clock::time_point m_command_end;
time_t m_delta;
time_t m_next_update_time;
time_t m_duration_ms;
//time_t m_command_duration_ms;
time_t m_engine_duration_ms;
time_t m_command_duration_ms;
time_t m_infusion_duration_ms;
time_t m_next_update_time_duration_ms;
time_t m_duration_ms;
duration<double> m_next_start_in;
std::map<std::string, CLA::Pump*> m_pumps;
CLA::Monitor* m_monitor;
public:
HardwareSimulator(std::shared_ptr<CLA::LOGGER>&,serial::Serial*,CLA::Environment&,std::map<std::string,CLA::Pump*>&);
HardwareSimulator(std::shared_ptr<CLA::LOGGER>&,serial::Serial*,std::map<std::string,CLA::Pump*>&);
......
......@@ -5,11 +5,11 @@
#include "configure_prosim_paths.h"
#include <libconfig.h++>
#include <libconfig.h++>
using namespace libconfig;
void Simulation(const std::string, const std::shared_ptr<Config>&);
void Global_LoadConfig( std::shared_ptr<CLA::LOGGER>&, std::unique_ptr<PhysiologyEngine>&,
const std::shared_ptr<Config>&, CLA::Environment&);
void update_env_variables_for_realtime(CLA::Environment& );
void update_env_variables_for_realtime(CLA::Environment&);
#endif
......@@ -30,12 +30,12 @@ namespace CLA {
struct timeval timeout;
string json_message;
double m_current_rate;
double m_bpdrug_last_sent_rate;
double m_substance_last_sent_rate;
double m_new_rate;
double m_saline_last_sent_rate;
double m_last_sent_rate;
string m_topic;
string m_type;
string m_substance;
static const int BUFF_SIZE = 100;
char buff[BUFF_SIZE];
const char* m_host;
......@@ -49,7 +49,8 @@ namespace CLA {
std::shared_ptr<CLA::LOGGER> m_logger;
public:
tcp_pump_client(std::shared_ptr<CLA::LOGGER>&);
tcp_pump_client(const string&, const string&, std::shared_ptr<CLA::LOGGER>&);
tcp_pump_client(const string&, const string&, const string&, \
std::shared_ptr<CLA::LOGGER>&);
~tcp_pump_client();
void setup_connection();
double getCurrentInfusionRate(const string&);
......
......@@ -104,7 +104,7 @@ void tcp_monitor_client::send_data_to_server(double p_data) {
m_logger->error("Can not write MAP to socket");
}
strbuf.Clear();
m_logger->debug("patient data sent to rosbridge server");
m_logger->debug("publishing patient data to "+m_topic+" topic " );
}
void tcp_monitor_client::waiting_to_send() {
......
......@@ -9,23 +9,24 @@ tcp_pump_client::tcp_pump_client(std::shared_ptr<CLA::LOGGER>& logger) : m_logge
json_message = "{\"op\":\"subscribe\", \"topic\":\"/chatter\", \"type\":\"std_msgs/Float64\"}";
m_host = "localhost"; /* rosbridge server runs on localhost */
port = 9090; /* rosbridge server uses this port number */
SOCKET_TIMEOUT_SEC = 1;
SOCKET_TIMEOUT_uSEC = 0;
m_bpdrug_last_sent_rate = 0;
SOCKET_TIMEOUT_SEC = 0;
SOCKET_TIMEOUT_uSEC = 100000;
m_substance_last_sent_rate = 0;
m_last_sent_rate = 0;
m_new_rate = 0;
m_current_rate = 0;
}
tcp_pump_client::tcp_pump_client(const string& topic, const string& type,
std::shared_ptr<CLA::LOGGER>& logger): m_topic(move(topic)), m_type(move(type)),
m_logger(logger) {
tcp_pump_client::tcp_pump_client(const string& topic, const string& type, \
const string& substance, std::shared_ptr<CLA::LOGGER>& logger): \
m_topic(move(topic)), m_type(move(type)), m_logger(logger), \
m_substance(substance) {
json_message = "{\"op\":\"subscribe\", \"topic\": \""+m_topic+"\", \"type\": \""+m_type+"\"}";
m_host = "localhost"; /* rosbridge server runs on localhost */
port = 9090; /* rosbridge server uses this port number */
SOCKET_TIMEOUT_SEC = 1;
SOCKET_TIMEOUT_uSEC = 0;
m_bpdrug_last_sent_rate = 0;
SOCKET_TIMEOUT_SEC = 0;
SOCKET_TIMEOUT_uSEC = 100000;
m_substance_last_sent_rate = 0;
m_last_sent_rate = 0;
m_new_rate = 0;
m_current_rate = 0;
......@@ -34,10 +35,10 @@ m_logger(logger) {
void tcp_pump_client::setup_connection() {
/* create a UDP socket */
fd = socket(AF_INET, SOCK_STREAM, 0);
m_logger->debug("Connecting to rosbridge server for pump data ...");
m_logger->debug("Connecting to rosbridge server for "+m_substance+" pump data ...");
if (fd < 0) {
std::cerr <<"can not create socket"<<std::endl;
m_logger->error("Failed to create socket");
m_logger->error("Failed to create socket for "+m_substance+" pump");
exit(EXIT_FAILURE);
}
/* look up the address of the server given its name */
......@@ -56,84 +57,73 @@ void tcp_pump_client::setup_connection() {
addrlen = sizeof(servaddr);
/* initialize buffer s*/
memset(buff, 0 , sizeof(buff));
if (connect(fd, (struct sockaddr*)&servaddr, addrlen) < 0) {
/*connect to server */
cout << "Connection Failed \n";
m_logger->error("Connection Failed");
exit(EXIT_FAILURE);
if (connect(fd, (struct sockaddr*)&servaddr, addrlen) < 0) {
m_logger->debug("Connection to "+m_substance+" pump failed");
exit(EXIT_FAILURE);
}
m_logger->debug("Connection established");
m_logger->info("Connection to "+m_substance+" pump established");
/* send json message to server */
int n = write(fd, json_message.c_str(), strlen(json_message.c_str()));
if (n < 0) {
cerr <<"Can not write to socket\n";
m_logger->error("Can not write to socket");
m_logger->error("Can not write to "+m_substance+" pump socket");
}
m_logger->debug("subscription to "+ m_topic+" request sent to rosbridge server");
m_logger->debug("subscription to "+m_topic+" request sent to rosbridge server");
}
double tcp_pump_client::getCurrentInfusionRate(const string& substance) {
double tcp_pump_client::getCurrentInfusionRate(const string& substance_name) {
timeout.tv_sec = SOCKET_TIMEOUT_SEC;
timeout.tv_usec = SOCKET_TIMEOUT_uSEC;
FD_ZERO(&set); /* initialize set */
FD_SET(fd, &set);/* add descriptor to the read set */
rec_value = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
if (rec_value == -1 ) {
cerr << "socket error\n";
m_logger->error("socket error");
m_logger->error(m_substance+" connection socket error");
exit(EXIT_FAILURE);
}
else if (rec_value == 0) {
/* on timeout -> keep calling the waiting_for_new_rate function
* to return the last sent value until topic publishes a new rate
*/
m_current_rate = waiting_for_new_rate(substance);
m_current_rate = waiting_for_new_rate(substance_name);
}
else {
m_current_rate = get_new_rate(substance);
m_current_rate = get_new_rate(substance_name);
}
return m_current_rate;
}
double tcp_pump_client::get_new_rate(const string& substance) {
double tcp_pump_client::get_new_rate(const string& substance_name) {
recvlen = read(fd, buff, BUFF_SIZE);
if (recvlen > 0) {
buff[recvlen] = '\0'; /* adding EOL */
document.Parse(buff);
if (substance == "Norepinephrine") {
if (substance_name == m_substance) {
m_new_rate = document["msg"]["data"].GetDouble();
m_bpdrug_last_sent_rate = m_new_rate;
m_substance_last_sent_rate = m_new_rate;
if (m_new_rate)
m_logger->debug("New infusion rate for Norepinephrine received: "+to_string(int(m_new_rate))+" mL/hr");
}
else if (substance == "Saline") {
m_new_rate = document["msg"]["data"].GetDouble();
m_saline_last_sent_rate = m_new_rate;
if (m_new_rate > 0)
m_logger->info("New infusion rate for Saline received "+to_string(int(m_new_rate))+" mL/hr");
m_logger->info("New infusion rate for "+m_substance+" received: "\
+to_string(int(m_new_rate))+" mL/hr");
}
}
else {
m_logger->error("Socket connection to rosbridge server lost");
m_logger->error("Socket connection for "+m_substance+" data to rosbridge \
server lost");
exit(EXIT_FAILURE);
}
return m_new_rate;
}
double tcp_pump_client::waiting_for_new_rate(const string& substance) {
double tcp_pump_client::waiting_for_new_rate(const string& substance_name) {
/* while topic is not publishing, keep
* returning the last sent rate value
*/
if (substance == "Norepinephrine") {
m_logger->debug(substance+": waiting for new infusion rate ...");
m_last_sent_rate = m_bpdrug_last_sent_rate;
}
else if (substance == "Saline") {
m_logger->debug(substance +": waiting for new infusion rate ...");
m_last_sent_rate = m_saline_last_sent_rate;
if (substance_name == m_substance) {
m_logger->debug(m_substance+": waiting for new infusion rate ...");
m_last_sent_rate = m_substance_last_sent_rate;
}
return m_last_sent_rate;
}
......@@ -142,7 +132,7 @@ void tcp_pump_client::clear() {
}
tcp_pump_client::~tcp_pump_client() {
m_logger->debug("Shutting down socket connection");
m_logger->debug("Shutting down "+m_substance+" pump connection");
close(fd);
clear();
delete hp;
......
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