Commit ad3ff978 authored by djk032's avatar djk032

added timestamp to filenames

parent 8a5bf2b6
......@@ -3,11 +3,15 @@
CLA::LOGGER::LOGGER(const string& name): m_patient_name(move(name)){
/* timestamped logs */
m_logstream[0].open(prosim_logs_dir+"prosim_"+m_patient_name+current_time(false)+".log");
m_logstream[1].open(prosim_results_dir+"duration_"+m_patient_name+current_time(false)+".txt");
m_logstream[0].open(prosim_logs_dir+"prosim_"+m_patient_name+current_time(true)+".log", \
ios::out|ios::binary);
m_logstream[1].open(prosim_results_dir+"duration_"+m_patient_name+current_time(true)+".txt", \
ios::out|ios::binary);
m_logstream[2].open(prosim_results_dir+"duration_sim_monitor_"\
+m_patient_name+current_time(false)+".txt");
m_logstream[3].open(prosim_results_dir+"data_"+m_patient_name+current_time(false)+".txt");
+m_patient_name+current_time(true)+".txt", \
ios::out|ios::binary);
m_logstream[3].open(prosim_results_dir+"data_"+m_patient_name+current_time(true)+".txt",\
ios::out|ios::binary);
m_logstream[0]<< "Date"<<setw(12)<<"Time"<<setw(12)<<"Tz"<<setw(3)<<" "\
<<"Level"<<setw(9)<<"Message"<<endl;
m_logstream[1]<<"start time,"<<"engine adv duration(s),"<<"command duration(s),"\
......@@ -58,14 +62,15 @@ string CLA::LOGGER::current_time(bool display_timezone) {
sprintf(m_sec_buff, "%.3u", m_secs); /*cast unit to string */
if (display_timezone) {
strftime(buffer, 50, "%Y-%m-%d %X,", localtime(&tv.tv_sec));
strftime(buffer, 50, "%Y-%m-%d-%H %M %S,", localtime(&tv.tv_sec));
strftime(tz_buff, 10, " %Z", localtime(&tv.tv_sec));
strcat(buffer, m_sec_buff);
strcat(buffer, tz_buff);
}
else {
strftime(buffer, 50, "%Y-%m-%d %X", localtime(&tv.tv_sec));
strftime(buffer, 50, "%Y-%m-%d-%H %M %S", localtime(&tv.tv_sec));
}
cout << "time for files" << buffer <<endl;
return buffer;
}
......
#include "Prosim.h"
Prosim::Prosim( std::shared_ptr<CLA::LOGGER>& logger,CLA::Environment& env, \
std::map<std::string, CLA::Pump*>& pumps) : m_logger(logger), m_env(env), \
m_pumps(pumps), PatientOut(logger, env, pumps) {
std::string port = m_env.port;
uint32_t baudrate = m_env.baudrate;
serial::Timeout timeout = serial::Timeout();
serial::bytesize_t bytesize = serial::eightbits;
serial::parity_t parity = serial::parity_none;
serial::stopbits_t stopbits = serial::stopbits_one;
serial::flowcontrol_t flowcontrol = serial::flowcontrol_hardware;
m_serial = new serial::Serial(port, baudrate, timeout, bytesize, parity, \
stopbits, flowcontrol);
m_logger->info("Patient data will be sent to the patient monitor");
}
void Prosim::update(std::unique_ptr<PhysiologyEngine>& engine) {
/*
* synchronize pulse with to real-time clock
*/
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();
/* 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));}
bool Prosim::isOpen() {
return m_serial->isOpen();
}
/* HR range: 10-360 */
void Prosim::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 Prosim::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 Prosim::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 Prosim::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 Prosim::SetRemoteMode() {
SendCommand("REMOTE");
m_logger->debug("Prosim simulator switched to REMOTE mode");
}
void Prosim::SetLocalMode() {
SendCommand("LOCAL");
m_logger->debug("Prosim simulator switched to LOCAL mode");
}
string Prosim::GetCurrentMode() {
SendCommand("QMODE");
string data = GetResponse();
if (data.find("RMAIN") != std::string::npos) {
mode = "REMOTE";
}
else {
mode = "LOCAL";
}
return mode;
}
size_t Prosim::SendCommand(string command) {
size_t bytes_sent = m_serial->write(command+"\r\n");
std::this_thread::sleep_for(milliseconds(100));
return bytes_sent;
}
string Prosim::GetResponse() {
size_t bytes_in_buffer = m_serial->available();
std::string response = m_serial->read(bytes_in_buffer);
return response;
}
void Prosim::Clear() {
m_serial = nullptr;
m_pumps.clear();
}
Prosim::~Prosim() {
SetLocalMode();// Switch back device to LOCAL mode when done
Clear();
delete m_serial;
}
No preview for this file type
......@@ -25,6 +25,8 @@
#include "properties/SEScalarVolumePerTime.h"
#include "engine/SEEngineTracker.h"
string current_time(bool display_timezone);
using namespace libconfig;
int main() {
......@@ -32,9 +34,9 @@ int main() {
auto cf = std::make_shared<Config>();
string f_path = prosim_config_dir+"scenario.cfg";
const char* filepath = f_path.c_str();
const string patient_name = "Hassan";
const string patient_name = "ExtremeFemale";
std::vector<std::string> patients{"Hassan", "Gus"};
std::vector<std::string> patients{"Gus", "Hassan", "ExtremeFemale"};
try {
cf->readFile(filepath);
......@@ -48,6 +50,7 @@ int main() {
}
/*start simulation*/
//Simulation(patient_name, cf);
for (auto patient: patients) {
Simulation(patient, cf);
}
......@@ -56,7 +59,7 @@ int main() {
void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg)
{
// Create the engine and load the patient
std::unique_ptr<PhysiologyEngine> pe = CreatePulseEngine(prosim_logs_dir+"SimulationEngine_"+patient_name+".log");
std::unique_ptr<PhysiologyEngine> pe = CreatePulseEngine(prosim_logs_dir+"SimulationEngine_"+patient_name+current_time(true)+".log");
pe->GetLogger()->Info("CLA_Prosim Simulation");
if (!pe->LoadStateFile(pulse_state_dir+patient_name+"@0s.pba")) {
pe->GetLogger()->Error("Could not load state, check the error");
......@@ -69,7 +72,7 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg)
pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("HeartRate", FrequencyUnit::Per_min);
pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("RespirationRate", FrequencyUnit::Per_min);
pe->GetEngineTracker()->GetDataRequestManager().CreatePhysiologyDataRequest("OxygenSaturation");
pe->GetEngineTracker()->GetDataRequestManager().SetResultsFilename(prosim_results_dir+"PulseSimEngine_"+patient_name+".txt");
pe->GetEngineTracker()->GetDataRequestManager().SetResultsFilename(prosim_results_dir+"PulseSimEngine_"+patient_name+current_time(true)+".txt");
SEHemorrhage hemorrhageLeg;
double initialMAP = 70.0;
......@@ -78,7 +81,11 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg)
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);
//std::shared_ptr<CLA::LOGGER> logger = std::make_shared<CLA::LOGGER>(patient_name,patient_name);
// add timestamp to filenames
std::shared_ptr<CLA::LOGGER> logger = std::make_shared<CLA::LOGGER>(patient_name);
//logger->setLevel("INFO");/*DEFAULT: DEBUG */
Global_LoadConfig(logger, pe, cfg, sim_env);
CLA::Control control(logger, "/control", "std_msgs/String");
......@@ -142,8 +149,10 @@ void Simulation(const string patient_name, const std::shared_ptr<Config>& cfg)
}
sim_env.time_index++;
}// End while looop
/* stop executing algorith */
/* stop executing algorithm */
control.publish_control_message("stop");
}// End Simulation function
void Global_LoadConfig(std::shared_ptr<CLA::LOGGER>& logger, std::unique_ptr<PhysiologyEngine>&
......@@ -212,3 +221,26 @@ engine, const std::shared_ptr<Config>& cf, CLA::Environment& env) {
env.simulation_injury_start_timestep = env.simulation_injury_start/env.engine_timestep;
env.simulation_injury_stop_timestep = env.simulation_injury_stop/env.engine_timestep;
}// End Global_LoadConfig
string current_time(bool display_timezone) {
struct timeval tv; /* for seconds and micro seconds */
gettimeofday(&tv , NULL); /* populate `tv` with time since epoch */
char buffer[50];
char m_sec_buff[10];
char tz_buff[10];
/* this is where the milli seconds come from :) */
uint m_secs = static_cast<uint>(tv.tv_usec)/1000;
sprintf(m_sec_buff, "%.3u", m_secs); /*cast unit to string */
if (display_timezone) {
strftime(buffer, 50, "%Y-%m-%d-%H %M %S,", localtime(&tv.tv_sec));
strftime(tz_buff, 10, " %Z", localtime(&tv.tv_sec));
strcat(buffer, m_sec_buff);
strcat(buffer, tz_buff);
}
else {
strftime(buffer, 50, "%Y-%m-%d-%H %M %S", localtime(&tv.tv_sec));
}
cout << "time for files" << buffer <<endl;
return buffer;
}
#!/bin/bash
g++ -c -std=c++11 -W -Wall ./Prosim.cpp -I./include/ -I./3rdParty/serial/include/ -I./3rdParty/include/ -I/home/cmd/projects/physiology/builds/install/include -I/home/cmd/projects/physiology/builds/install/include/cdm
# Configuration file
pulse = {
advance_time = 2.0; # Simulation engine advance time -> seconds
advance_time = 1.0; # Simulation engine advance time -> seconds
}
prosim = {
real_time = 2.0; # Real time -> seconds
real_time = 1.0; # Real time -> seconds
baudrate = 115200;
port = "/dev/ttyACM0";
port = "/dev/ttyUSB0";
}
simulation = {
......@@ -18,4 +18,3 @@ simulation = {
simulation_time_run = 30000.0;
}
}
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