Commit 6a3c335e authored by fmg005's avatar fmg005

cache pump rate requests

parent 63e4ad58
......@@ -3,7 +3,8 @@
using namespace CLA;
Action::Action(std::shared_ptr<CLA::LOGGER>& logger, \
std::map<std::string,CLA::Pump*>& pumps) : m_logger(logger), m_pumps(pumps) {
std::map<std::string,CLA::Pump*>& pumps, CLA::Environment& env) : m_logger(\
logger), m_pumps(pumps), m_env(env) {
NorepiConcentration = 16;
bpdrug_prev_rate = 0;
saline_prev_rate = 0;
......@@ -24,25 +25,25 @@ void Action::initialize(std::unique_ptr<PhysiologyEngine>& engine) {
}
void Action::infuse_drug(std::unique_ptr<PhysiologyEngine>& engine) {
m_bpdrug_rate = m_pumps["bpdrug"]->getCurrentInfusionRate();
m_saline_rate = m_pumps["saline"]->getCurrentInfusionRate();
m_env.bpdrug_rate = m_pumps["bpdrug"]->getCurrentInfusionRate();
m_env.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);
if (bpdrug_prev_rate != m_env.bpdrug_rate && m_env.bpdrug_rate > 0) {
m_bpdrug_infusion->GetRate().SetValue(m_env.bpdrug_rate, VolumePerTimeUnit::mL_Per_hr);
engine->ProcessAction(*m_bpdrug_infusion);
bpdrug_prev_rate = m_bpdrug_rate;
bpdrug_prev_rate = m_env.bpdrug_rate;
m_logger->info(m_pumps["bpdrug"]->getSubstance()+": infusing patient at "\
+to_string(int(m_bpdrug_rate))+" mL/hr");
+to_string(int(m_env.bpdrug_rate))+" mL/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);
if (saline_prev_rate != m_env.saline_rate && m_env.saline_rate > 0) {
m_infusion->GetRate().SetValue(m_env.saline_rate, VolumePerTimeUnit::mL_Per_hr);
engine->ProcessAction(*m_infusion);
saline_prev_rate = m_saline_rate;
saline_prev_rate = m_env.saline_rate;
m_logger->info(m_pumps["saline"]->getSubstance()+": infusing patient at "\
+to_string(int(m_saline_rate))+" mL/hr");
+to_string(int(m_env.saline_rate))+" mL/hr");
}
}
}
......
#include "HardwareSimulator.h"
HardwareSimulator::HardwareSimulator( std::shared_ptr<CLA::LOGGER>& logger,
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");
}
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, pumps) {
m_pumps(pumps), m_action(logger, pumps, env) {
m_monitor = new CLA::Monitor(logger, "/monitor", "std_msgs/Float64");
}
......@@ -53,19 +45,19 @@ void HardwareSimulator::update(std::unique_ptr<PhysiologyEngine>& engine) {
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();
/* total duration */
m_total_duration_ms = duration_cast<milliseconds>(m_infusion_end - m_start).count();
/* 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_env.bpdrug_rate, m_env.saline_rate);
/* log 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);
m_infusion_duration_ms, m_total_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)
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 */
while(true) {
m_delta = m_next_update_time - system_clock::to_time_t(system_clock::now());
......
......@@ -2,11 +2,12 @@
using namespace CLA;
/* wrapper for tcp client */
/* wrapper for pump 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, substance, logger) {
m_tcp_pump_client.setup_connection(); /* setup connection to server via a tcp socket */
/* setup connection to server via a tcp socket */
m_tcp_pump_client.setup_connection();
}
double Pump::getCurrentInfusionRate () {
......
......@@ -6,6 +6,7 @@
#include <map>
#include "CLA_Logger.h"
#include "Pump.h"
#include "Environment.h"
/* pulse stuff */
#include "PulsePhysiologyEngine.h"
#include "patient/actions/SESubstanceCompoundInfusion.h"
......@@ -31,11 +32,11 @@ namespace CLA {
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;
CLA::Environment m_env;
public:
Action(std::shared_ptr<CLA::LOGGER>&, std::map<std::string,CLA::Pump*>&);
Action(std::shared_ptr<CLA::LOGGER>&, std::map<std::string,CLA::Pump*>&, \
CLA::Environment&);
~Action();
void infuse_drug(std::unique_ptr<PhysiologyEngine>&);
void initialize(std::unique_ptr<PhysiologyEngine>&);
......
......@@ -16,6 +16,8 @@ namespace CLA {
double simulation_injury_start;
double simulation_injury_stop;
double sim_currentTime;
double bpdrug_rate;
double saline_rate;
unsigned long simulation_injury_start_timestep;
unsigned long simulation_injury_stop_timestep;
};
......
......@@ -52,21 +52,21 @@ class HardwareSimulator {
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;
time_t m_delta;
time_t m_next_update_time;
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;
time_t m_total_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*>&);
HardwareSimulator(std::shared_ptr<CLA::LOGGER>&,serial::Serial*,\
CLA::Environment&,std::map<std::string,CLA::Pump*>&);
~HardwareSimulator();
bool isOpen();
void SetHeartRate(double);
......
......@@ -48,7 +48,6 @@ namespace CLA {
fd_set set; /* descriptor read set*/
std::shared_ptr<CLA::LOGGER> m_logger;
public:
tcp_pump_client(std::shared_ptr<CLA::LOGGER>&);
tcp_pump_client(const string&, const string&, const string&, \
std::shared_ptr<CLA::LOGGER>&);
~tcp_pump_client();
......
......@@ -2,30 +2,18 @@
using namespace CLA;
tcp_pump_client::tcp_pump_client(std::shared_ptr<CLA::LOGGER>& logger) : m_logger(logger) {
/* we can only talk to rosbrige server using json strings; here we want to
* 'subscribe' to 'chatter topic' publishing messages of type 'std_msgs/String'
*/
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 = 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, \
const string& substance, std::shared_ptr<CLA::LOGGER>& logger): \
m_topic(move(topic)), m_type(move(type)), m_logger(logger), \
m_substance(substance) {
/* we can only talk to rosbrige server using json strings; here we want to
* 'subscribe' to 'chatter topic' publishing messages of type 'std_msgs/String'
*/
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 = 0;
SOCKET_TIMEOUT_uSEC = 100000;
SOCKET_TIMEOUT_uSEC = 100000; /* 100 millisecond*/
m_substance_last_sent_rate = 0;
m_last_sent_rate = 0;
m_new_rate = 0;
......@@ -44,7 +32,7 @@ void tcp_pump_client::setup_connection() {
/* look up the address of the server given its name */
hp = gethostbyname(m_host);
if(!hp) {
std::cerr << "could not obtain address " << m_host << std::endl;
m_logger->error("could not obtain address for "+m_substance+" pump");
exit(EXIT_FAILURE);
}
/* initialize sockaddr_in servaddr*/
......@@ -78,7 +66,7 @@ double tcp_pump_client::getCurrentInfusionRate(const string& substance_name) {
FD_SET(fd, &set);/* add descriptor to the read set */
rec_value = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
if (rec_value == -1 ) {
m_logger->error(m_substance+" connection socket error");
m_logger->error(m_substance+" pump connection socket error");
exit(EXIT_FAILURE);
}
else if (rec_value == 0) {
......@@ -123,7 +111,6 @@ double tcp_pump_client::waiting_for_new_rate(const string& substance_name) {
m_logger->debug(m_substance+": waiting for new infusion rate ...");
m_last_sent_rate = m_substance_last_sent_rate;
}
return m_last_sent_rate;
}
......
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