Commit d39aca98 authored by fmg005's avatar fmg005

Removed HardwareSimulator files

parent 57e9e677
#include "HardwareSimulator.h"
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_monitor = new CLA::Monitor(logger, "/dummy_monitor", "std_msgs/String");
}
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(); // start
engine->AdvanceModelTime(advance_time, TimeUnit::s);
m_engine_advance_end = 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);
resp_rate = engine->GetRespiratorySystem()->GetRespirationRate(FrequencyUnit::Per_min);
oxygen_sat = engine->GetBloodChemistrySystem()->GetOxygenSaturation();
/* send patient data to monitor */
SetIBP(1, systolic_pressure, diastolic_pressure);
SetHeartRate(heart_rate);
SetRespRate(resp_rate);
SetOxygenSat(oxygen_sat);
m_command_end = system_clock::now();
/* 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));
/* perform infusions whenever the pump rate changes */
m_infusion_start = system_clock::now();
m_action.infuse_drug(engine);
m_infusion_end = system_clock::now();// end
/* start time for the next execution */
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_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();
/* total duration */
m_total_duration_ms = duration_cast<milliseconds>(m_infusion_end - m_start).count();
/* log data */
m_logger->log_data(engine->GetSimulationTime(TimeUnit::s), \
m_action.get_bpdrug_rate(), m_action.get_saline_rate());
/* log durations */
m_logger->log_duration(m_start,m_engine_duration_ms,m_command_duration_ms, \
m_infusion_duration_ms, m_total_duration_ms, m_next_update_time);
/* checking if simulation will take longer than allotted 'time' */
if (m_total_duration_ms > m_next_update_time_duration_ms)
m_logger->error("Simulation too slow");
/* 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));
}
/* track data */
engine->GetEngineTracker()->TrackData(engine->GetSimulationTime(TimeUnit::s));
}
void HardwareSimulator::LoadConfig(std::unique_ptr<PhysiologyEngine>& engine) {
/* pulse engine advance time */
advance_time = m_env.pulse_advance_time;
/* 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);
}
bool HardwareSimulator::isOpen() {
return m_serial->isOpen();
}
/* HR range: 10-360 */
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");
}
else if (rate > 99 && rate <= 360) {
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");
}
}
/* RR range: 10-150 */
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");
}
else if(rate > 99 && rate <=150) {
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");
}
}
/* O2 range: 0-100 */
void HardwareSimulator::SetOxygenSat(double rate) {
int o2_rate = static_cast<int>(rate*100);
/* prosim has a bug, monitor displays value which is 1 unit
higher than set value (for oxygen saturation)
*/
int i_rate = o2_rate - 1;
std::string s_rate = std::to_string(i_rate);
if (rate >= 0 && rate < 10) {
SendCommand("SAT=00"+s_rate);
m_logger->info("O2 Sat: "+ to_string(o2_rate));
}
else if(rate > 10 && rate < 100 ) {
SendCommand("SAT=0"+s_rate);
m_logger->info("O2 Sat: "+ to_string(o2_rate));
}
else if(rate == 100 ) {
SendCommand("SAT="+s_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");
}
}
/* IBP range: 0-300 */
void HardwareSimulator::SetIBP(int channel, double systolic_p, double diastolic_p ) {
int syst = static_cast<int>(systolic_p);
int diast = static_cast<int>(diastolic_p);
string s_syst = to_string(syst);
string s_diast = to_string(diast);
string s_channel = to_string(channel);
if (syst >= 0 && syst <= 300 && diast >= 0 && diast <= 300) {
if (syst < 10 && diast < 10) {
m_logger->warn("IBP can't be this low: "+s_syst+"/"+s_diast);
}
else if (syst > 99 && diast > 99 && syst > diast) {
SendCommand("IBPP="+s_channel+","+s_syst+","+s_diast);
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);
}
else if (syst > 99 && diast > 9 && diast < 100 && syst > diast) {
SendCommand("IBPP="+s_channel+","+s_syst+","+"0"+s_diast);
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);
}
else if (syst > diast) {
SendCommand("IBPP="+s_channel+","+"0"+s_syst+","+"0"+s_diast);
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");
}
}
void HardwareSimulator::SetRemoteMode() {
SendCommand("REMOTE");
m_logger->debug("ProSim simulator switched to REMOTE mode");
}
void HardwareSimulator::SetLocalMode() {
SendCommand("LOCAL");
m_logger->debug("ProSim simulator switched to LOCAL mode");
}
string HardwareSimulator::GetCurrentMode() {
SendCommand("QMODE");
string data = GetResponse();
if (data.find("RMAIN") != std::string::npos) {
mode = "REMOTE";
}
else {
mode = "LOCAL";
}
return mode;
}
size_t HardwareSimulator::SendCommand(string command) {
size_t bytes_sent = m_serial->write(command+"\r\n");
std::this_thread::sleep_for(milliseconds(100));
return bytes_sent;
}
string HardwareSimulator::GetResponse() {
size_t bytes_in_buffer = m_serial->available();
std::string response = m_serial->read(bytes_in_buffer);
return response;
}
void HardwareSimulator::Clear() {
m_serial = nullptr;
m_monitor = nullptr;
m_pumps.clear();
}
HardwareSimulator::~HardwareSimulator() {
SetLocalMode();// Switch back device to LOCAL mode when done
Clear();
delete m_serial;
delete m_monitor;
m_logger->info("End of simulation");
}
#ifndef HARDWARESIMULATOR_H
#define HARDWARESIMULATOR_H
#include <string>
#include <iostream>
#include <cstdio>
#include <chrono>
#include <thread>
#include <map>
#include <libconfig.h++>
#include "serial/serial.h"
#include "CLA_Logger.h"
#include "Environment.h"
#include "Action.h"
#include "Pump.h"
#include "Monitor.h"
/* Pulse header files */
#include "CommonDataModel.h"
#include "PulsePhysiologyEngine.h"
#include "properties/SEScalarTime.h"
#include "properties/SEScalarPressure.h"
#include "properties/SEScalarFrequency.h"
#include "engine/SEEngineTracker.h"
#include "system/physiology/SECardiovascularSystem.h"
#include "system/physiology/SEBloodChemistrySystem.h"
#include "system/physiology/SERespiratorySystem.h"
using namespace std;
using namespace libconfig;
using namespace std::chrono;
class HardwareSimulator {
private:
serial::Serial* m_serial;
CLA::Environment m_env;
std::shared_ptr<CLA::LOGGER> m_logger;
CLA::Action m_action;
void Clear();
size_t SendCommand(string);
string GetResponse();
double systolic_pressure;
double diastolic_pressure;
double heart_rate;
double resp_rate;
double oxygen_sat;
double advance_time;
double secs;
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;
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_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();
bool isOpen();
void SetHeartRate(double);
void SetRespRate(double);
void SetOxygenSat(double);
void SetIBP(int, double, double);
void SetRemoteMode();
void SetLocalMode();
string GetCurrentMode();
void update(std::unique_ptr<PhysiologyEngine>&);
void LoadConfig(std::unique_ptr<PhysiologyEngine>&);
};
#endif
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